mirror of
https://github.com/appleboy/drone-jenkins.git
synced 2026-06-04 10:15:02 +08:00
feat: refactor authentication logic and broaden test coverage (#50)
* feat: refactor authentication logic and broaden test coverage - Improve authentication checks to only require username and token when both are provided - Update validation logic to allow either (username and token) or remote-token for authentication - Enhance test coverage for various authentication scenarios - Refine error messages to indicate a generic authentication requirement instead of specifying missing username or token Signed-off-by: appleboy <appleboy.tw@gmail.com> * style: streamline authentication error handling in config validation - Simplify authentication error message in config validation Signed-off-by: appleboy <appleboy.tw@gmail.com> --------- Signed-off-by: appleboy <appleboy.tw@gmail.com>
This commit is contained in:
+9
-5
@@ -224,7 +224,7 @@ func (jenkins *Jenkins) sendRequest(
|
||||
req *http.Request,
|
||||
crumb *CrumbResponse,
|
||||
) (*http.Response, error) {
|
||||
if jenkins.Auth != nil {
|
||||
if jenkins.Auth != nil && jenkins.Auth.Username != "" && jenkins.Auth.Token != "" {
|
||||
req.SetBasicAuth(jenkins.Auth.Username, jenkins.Auth.Token)
|
||||
}
|
||||
|
||||
@@ -277,10 +277,14 @@ func (jenkins *Jenkins) postAndGetLocation(
|
||||
path string,
|
||||
params url.Values,
|
||||
) (int, error) {
|
||||
// Fetch CSRF crumb before POST request
|
||||
crumb, err := jenkins.getCrumb(ctx)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to get crumb: %w", err)
|
||||
// Fetch CSRF crumb before POST request (only if authenticated)
|
||||
var crumb *CrumbResponse
|
||||
if jenkins.Auth != nil && jenkins.Auth.Username != "" && jenkins.Auth.Token != "" {
|
||||
var err error
|
||||
crumb, err = jenkins.getCrumb(ctx)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to get crumb: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
requestURL := jenkins.buildURL(path, params)
|
||||
|
||||
Reference in New Issue
Block a user