This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 8262cb3b8 fix: unauthorized return 400 instead of 401 (#5066)
8262cb3b8 is described below

commit 8262cb3b830fa3e1a141d5408f8e85fabf6d1603
Author: mappjzc <[email protected]>
AuthorDate: Fri Apr 28 19:52:17 2023 +0800

    fix: unauthorized return 400 instead of 401 (#5066)
    
    If test connection return status code http.StatusUnauthorized
    Changed it to be 400
    
    Nddtfjiang <[email protected]>
---
 backend/plugins/ae/api/connection.go         |  2 +-
 backend/plugins/azure/api/connection.go      |  6 +++++-
 backend/plugins/bamboo/models/connection.go  |  7 ++++++-
 backend/plugins/bitbucket/api/connection.go  |  7 ++++++-
 backend/plugins/feishu/models/connection.go  |  6 ++++++
 backend/plugins/gitee/api/connection.go      |  7 ++++++-
 backend/plugins/github/api/connection.go     |  5 +++++
 backend/plugins/gitlab/api/connection.go     |  4 ++++
 backend/plugins/gitlab/models/connection.go  | 11 ++++++++---
 backend/plugins/jenkins/api/connection.go    |  7 ++++++-
 backend/plugins/jira/api/connection.go       |  7 ++++---
 backend/plugins/pagerduty/api/connection.go  |  5 +++++
 backend/plugins/sonarqube/api/connection.go  |  5 +++--
 backend/plugins/tapd/api/connection.go       |  5 +++--
 backend/plugins/teambition/api/connection.go | 11 +++++++++--
 backend/plugins/trello/api/connection.go     |  8 +++++++-
 backend/plugins/zentao/models/connection.go  |  6 ++++++
 17 files changed, 90 insertions(+), 19 deletions(-)

diff --git a/backend/plugins/ae/api/connection.go 
b/backend/plugins/ae/api/connection.go
index ae1ba0a28..7cc80df0f 100644
--- a/backend/plugins/ae/api/connection.go
+++ b/backend/plugins/ae/api/connection.go
@@ -60,7 +60,7 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
        case 200: // right StatusCode
                return &plugin.ApiResourceOutput{Body: true, Status: 200}, nil
        case 401: // error secretKey or nonceStr
-               return &plugin.ApiResourceOutput{Body: false, Status: 
res.StatusCode}, nil
+               return &plugin.ApiResourceOutput{Body: false, Status: 
http.StatusBadRequest}, nil
        default: // unknow what happen , back to user
                return &plugin.ApiResourceOutput{Body: res.Body, Status: 
res.StatusCode}, nil
        }
diff --git a/backend/plugins/azure/api/connection.go 
b/backend/plugins/azure/api/connection.go
index 5662dc65b..285bcc84a 100644
--- a/backend/plugins/azure/api/connection.go
+++ b/backend/plugins/azure/api/connection.go
@@ -19,9 +19,10 @@ package api
 
 import (
        "context"
-       "github.com/apache/incubator-devlake/server/api/shared"
        "net/http"
 
+       "github.com/apache/incubator-devlake/server/api/shared"
+
        "github.com/apache/incubator-devlake/core/errors"
        plugin "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -58,6 +59,9 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, err
        }
 
+       if res.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while 
testing connection")
+       }
        if res.StatusCode != http.StatusOK {
                return nil, errors.HttpStatus(res.StatusCode).New("unexpected 
status code while testing connection")
        }
diff --git a/backend/plugins/bamboo/models/connection.go 
b/backend/plugins/bamboo/models/connection.go
index 69694a522..fa0fceae1 100644
--- a/backend/plugins/bamboo/models/connection.go
+++ b/backend/plugins/bamboo/models/connection.go
@@ -19,10 +19,11 @@ package models
 
 import (
        "fmt"
-       "github.com/apache/incubator-devlake/core/plugin"
        "net/http"
        "time"
 
+       "github.com/apache/incubator-devlake/core/plugin"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        
"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
@@ -54,6 +55,10 @@ func (conn *BambooConn) PrepareApiClient(apiClient 
apihelperabstract.ApiClientAb
        }
        repo := &ApiBambooServerInfo{}
 
+       if res.StatusCode == http.StatusUnauthorized {
+               return 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error")
+       }
+
        if res.StatusCode != http.StatusOK {
                return 
errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code: %d", 
res.StatusCode))
        }
diff --git a/backend/plugins/bitbucket/api/connection.go 
b/backend/plugins/bitbucket/api/connection.go
index b699c133e..64e5d7c31 100644
--- a/backend/plugins/bitbucket/api/connection.go
+++ b/backend/plugins/bitbucket/api/connection.go
@@ -19,9 +19,10 @@ package api
 
 import (
        "context"
-       "github.com/apache/incubator-devlake/server/api/shared"
        "net/http"
 
+       "github.com/apache/incubator-devlake/server/api/shared"
+
        "github.com/apache/incubator-devlake/core/errors"
        plugin "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -58,6 +59,10 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, err
        }
 
+       if res.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error when 
testing connection")
+       }
+
        if res.StatusCode != http.StatusOK {
                return nil, errors.HttpStatus(res.StatusCode).New("unexpected 
status code when testing connection")
        }
diff --git a/backend/plugins/feishu/models/connection.go 
b/backend/plugins/feishu/models/connection.go
index 3069968d5..2ad8988bc 100644
--- a/backend/plugins/feishu/models/connection.go
+++ b/backend/plugins/feishu/models/connection.go
@@ -19,6 +19,7 @@ package models
 
 import (
        "fmt"
+       "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -42,6 +43,11 @@ func (conn *FeishuConn) PrepareApiClient(apiClient 
apihelperabstract.ApiClientAb
        if err != nil {
                return err
        }
+
+       if tokenRes.StatusCode == http.StatusUnauthorized {
+               return 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error when get 
tenant_access_token")
+       }
+
        tokenResBody := &apimodels.ApiAccessTokenResponse{}
        err = helper.UnmarshalResponse(tokenRes, tokenResBody)
        if err != nil {
diff --git a/backend/plugins/gitee/api/connection.go 
b/backend/plugins/gitee/api/connection.go
index 3d9fe0b72..8fac8a660 100644
--- a/backend/plugins/gitee/api/connection.go
+++ b/backend/plugins/gitee/api/connection.go
@@ -19,9 +19,10 @@ package api
 
 import (
        "context"
-       "github.com/apache/incubator-devlake/server/api/shared"
        "net/http"
 
+       "github.com/apache/incubator-devlake/server/api/shared"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -62,6 +63,10 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, err
        }
 
+       if res.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error when 
testing connection")
+       }
+
        if res.StatusCode != http.StatusOK {
                return nil, errors.HttpStatus(res.StatusCode).New("unexpected 
status code when testing connection")
        }
diff --git a/backend/plugins/github/api/connection.go 
b/backend/plugins/github/api/connection.go
index b7c636125..158eac572 100644
--- a/backend/plugins/github/api/connection.go
+++ b/backend/plugins/github/api/connection.go
@@ -60,6 +60,11 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
        if err != nil {
                return nil, errors.BadInput.Wrap(err, "verify token failed")
        }
+
+       if res.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error when 
testing connection")
+       }
+
        if res.StatusCode != http.StatusOK {
                return nil, errors.HttpStatus(res.StatusCode).New("unexpected 
status code while testing connection")
        }
diff --git a/backend/plugins/gitlab/api/connection.go 
b/backend/plugins/gitlab/api/connection.go
index e9cc6265c..48685b4f5 100644
--- a/backend/plugins/gitlab/api/connection.go
+++ b/backend/plugins/gitlab/api/connection.go
@@ -65,6 +65,10 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, err
        }
 
+       if res.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error when 
testing api or read_api permissions")
+       }
+
        if res.StatusCode == http.StatusForbidden {
                return nil, errors.BadInput.New("token need api or read_api 
permissions scope")
        }
diff --git a/backend/plugins/gitlab/models/connection.go 
b/backend/plugins/gitlab/models/connection.go
index 0b8f6fa91..ec3fcd0eb 100644
--- a/backend/plugins/gitlab/models/connection.go
+++ b/backend/plugins/gitlab/models/connection.go
@@ -19,11 +19,12 @@ package models
 
 import (
        "fmt"
+       "net/http"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        
"github.com/apache/incubator-devlake/helpers/pluginhelper/api/apihelperabstract"
-       "net/http"
 )
 
 // GitlabConn holds the essential information to connect to the Gitlab API
@@ -55,7 +56,9 @@ func (conn *GitlabConn) PrepareApiClient(apiClient 
apihelperabstract.ApiClientAb
                if err != nil {
                        return errors.Convert(err)
                }
-
+               if res.StatusCode == http.StatusUnauthorized {
+                       return 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while 
testing connection")
+               }
                if res.StatusCode != http.StatusOK {
                        return 
errors.HttpStatus(res.StatusCode).New("unexpected status code while testing 
connection")
                }
@@ -73,7 +76,9 @@ func (conn *GitlabConn) PrepareApiClient(apiClient 
apihelperabstract.ApiClientAb
                if err != nil {
                        return errors.Convert(err)
                }
-
+               if res.StatusCode == http.StatusUnauthorized {
+                       return 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while 
testing connection[PrivateToken]")
+               }
                if res.StatusCode != http.StatusOK {
                        return 
errors.HttpStatus(res.StatusCode).New("unexpected status code while testing 
connection[PrivateToken]")
                }
diff --git a/backend/plugins/jenkins/api/connection.go 
b/backend/plugins/jenkins/api/connection.go
index f3035ce46..4f76ef5e0 100644
--- a/backend/plugins/jenkins/api/connection.go
+++ b/backend/plugins/jenkins/api/connection.go
@@ -19,9 +19,10 @@ package api
 
 import (
        "context"
-       "github.com/apache/incubator-devlake/server/api/shared"
        "net/http"
 
+       "github.com/apache/incubator-devlake/server/api/shared"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -59,6 +60,10 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, err
        }
 
+       if res.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while 
testing connection")
+       }
+
        if res.StatusCode != http.StatusOK {
                return nil, errors.HttpStatus(res.StatusCode).New("unexpected 
status code when testing connection")
        }
diff --git a/backend/plugins/jira/api/connection.go 
b/backend/plugins/jira/api/connection.go
index 97a938923..c0513685f 100644
--- a/backend/plugins/jira/api/connection.go
+++ b/backend/plugins/jira/api/connection.go
@@ -20,11 +20,12 @@ package api
 import (
        "context"
        "fmt"
-       "github.com/apache/incubator-devlake/server/api/shared"
        "net/http"
        "net/url"
        "strings"
 
+       "github.com/apache/incubator-devlake/server/api/shared"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -82,7 +83,7 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, errors.NotFound.New(fmt.Sprintf("Seems like an 
invalid Endpoint URL, please try %s", restUrl.String()))
        }
        if res.StatusCode == http.StatusUnauthorized {
-               return nil, errors.HttpStatus(res.StatusCode).New("Error 
username/password")
+               return nil, errors.HttpStatus(http.StatusBadRequest).New("Error 
username/password")
        }
 
        resBody := &models.JiraServerInfo{}
@@ -111,7 +112,7 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
 
        errMsg := ""
        if res.StatusCode == http.StatusUnauthorized {
-               return nil, errors.HttpStatus(res.StatusCode).New("it might you 
use the right token(password) but with the wrong username.please check your 
username/password")
+               return nil, errors.HttpStatus(http.StatusBadRequest).New("it 
might you use the right token(password) but with the wrong username.please 
check your username/password")
        }
 
        if res.StatusCode != http.StatusOK {
diff --git a/backend/plugins/pagerduty/api/connection.go 
b/backend/plugins/pagerduty/api/connection.go
index 569596127..932e585e9 100644
--- a/backend/plugins/pagerduty/api/connection.go
+++ b/backend/plugins/pagerduty/api/connection.go
@@ -49,6 +49,11 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
        if err != nil {
                return nil, err
        }
+
+       if response.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while 
testing connection")
+       }
+
        if response.StatusCode == http.StatusOK {
                return &plugin.ApiResourceOutput{Body: nil, Status: 
http.StatusOK}, nil
        }
diff --git a/backend/plugins/sonarqube/api/connection.go 
b/backend/plugins/sonarqube/api/connection.go
index ec81653ac..8ca67b18e 100644
--- a/backend/plugins/sonarqube/api/connection.go
+++ b/backend/plugins/sonarqube/api/connection.go
@@ -19,12 +19,13 @@ package api
 
 import (
        "context"
+       "net/http"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        "github.com/apache/incubator-devlake/plugins/sonarqube/models"
        "github.com/apache/incubator-devlake/server/api/shared"
-       "net/http"
 )
 
 type validation struct {
@@ -75,7 +76,7 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                }
                return &plugin.ApiResourceOutput{Body: body, Status: 200}, nil
        case 401: // error secretKey or nonceStr
-               return &plugin.ApiResourceOutput{Body: false, Status: 
res.StatusCode}, nil
+               return &plugin.ApiResourceOutput{Body: false, Status: 
http.StatusBadRequest}, nil
        default: // unknow what happen , back to user
                return &plugin.ApiResourceOutput{Body: res.Body, Status: 
res.StatusCode}, nil
        }
diff --git a/backend/plugins/tapd/api/connection.go 
b/backend/plugins/tapd/api/connection.go
index 321b77144..3bca819bc 100644
--- a/backend/plugins/tapd/api/connection.go
+++ b/backend/plugins/tapd/api/connection.go
@@ -20,9 +20,10 @@ package api
 import (
        "context"
        "fmt"
-       "github.com/apache/incubator-devlake/server/api/shared"
        "net/http"
 
+       "github.com/apache/incubator-devlake/server/api/shared"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -60,7 +61,7 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, err
        }
        if res.StatusCode == http.StatusUnauthorized {
-               return nil, errors.Unauthorized.New(fmt.Sprintf("verify token 
failed for %s", connection.Username))
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New(fmt.Sprintf("verify token failed 
for %s", connection.Username))
        }
        if res.StatusCode != http.StatusOK {
                return nil, 
errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code: %d", 
res.StatusCode))
diff --git a/backend/plugins/teambition/api/connection.go 
b/backend/plugins/teambition/api/connection.go
index c228c6b9d..a163848d2 100644
--- a/backend/plugins/teambition/api/connection.go
+++ b/backend/plugins/teambition/api/connection.go
@@ -20,13 +20,14 @@ package api
 import (
        "context"
        "fmt"
+       "net/http"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
        "github.com/apache/incubator-devlake/plugins/teambition/models"
        "github.com/apache/incubator-devlake/plugins/teambition/tasks"
        "github.com/apache/incubator-devlake/server/api/shared"
-       "net/http"
 )
 
 type TeambitionTestConnResponse struct {
@@ -61,6 +62,9 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, err
        }
 
+       if res.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while 
testing connection")
+       }
        if res.StatusCode != http.StatusOK {
                return nil, 
errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code: %d", 
res.StatusCode))
        }
@@ -70,7 +74,10 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
                return nil, err
        }
        if resBody.Code != http.StatusOK {
-               return nil, 
errors.HttpStatus(res.StatusCode).New(fmt.Sprintf("unexpected status code: %d", 
res.StatusCode))
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized on body while 
testing connection")
+       }
+       if resBody.Code != http.StatusOK {
+               return nil, 
errors.HttpStatus(resBody.Code).New(fmt.Sprintf("unexpected body status code: 
%d", resBody.Code))
        }
 
        body := TeambitionTestConnResponse{}
diff --git a/backend/plugins/trello/api/connection.go 
b/backend/plugins/trello/api/connection.go
index 639f1017a..be7378f7d 100644
--- a/backend/plugins/trello/api/connection.go
+++ b/backend/plugins/trello/api/connection.go
@@ -19,9 +19,10 @@ package api
 
 import (
        "context"
-       "github.com/apache/incubator-devlake/server/api/shared"
        "net/http"
 
+       "github.com/apache/incubator-devlake/server/api/shared"
+
        "github.com/apache/incubator-devlake/core/errors"
        "github.com/apache/incubator-devlake/core/plugin"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -57,6 +58,11 @@ func TestConnection(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput,
        if err != nil {
                return nil, errors.BadInput.Wrap(err, "verify token failed")
        }
+
+       if res.StatusCode == http.StatusUnauthorized {
+               return nil, 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while 
testing connection")
+       }
+
        if res.StatusCode != http.StatusOK {
                return nil, errors.HttpStatus(res.StatusCode).New("unexpected 
status code while testing connection")
        }
diff --git a/backend/plugins/zentao/models/connection.go 
b/backend/plugins/zentao/models/connection.go
index a1e85bfd8..23a1b58c9 100644
--- a/backend/plugins/zentao/models/connection.go
+++ b/backend/plugins/zentao/models/connection.go
@@ -19,6 +19,7 @@ package models
 
 import (
        "fmt"
+       "net/http"
 
        "github.com/apache/incubator-devlake/core/errors"
        helper "github.com/apache/incubator-devlake/helpers/pluginhelper/api"
@@ -36,6 +37,11 @@ func (connection ZentaoConn) PrepareApiClient(apiClient 
apihelperabstract.ApiCli
        if err != nil {
                return err
        }
+
+       if tokenRes.StatusCode == http.StatusUnauthorized {
+               return 
errors.HttpStatus(http.StatusBadRequest).New("StatusUnauthorized error while to 
request access token")
+       }
+
        tokenResBody := &ApiAccessTokenResponse{}
        err = helper.UnmarshalResponse(tokenRes, tokenResBody)
        if err != nil {

Reply via email to