Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: handle gitlab error case (#1132)

Cluas903052bceeda
diff --git a/plugin/vcs/github/github.go b/plugin/vcs/github/github.go
index ba90fdc0f0..817ef8cf3b 100644
--- a/plugin/vcs/github/github.go
+++ b/plugin/vcs/github/github.go
@@ -207,6 +207,7 @@ func tokenRefresher(oauthCtx oauthContext, refresher common.TokenRefresher) oaut
if err != nil {
return errors.Wrapf(err, "read body of POST %s", url)
}
+ defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return errors.Errorf("non-200 status code %d with body %q", resp.StatusCode, body)
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index 2522aa2a62..5fa13986f5 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -214,6 +214,34 @@ func (p *Provider) APIURL(instanceURL string) string {
return fmt.Sprintf("%s/%s", instanceURL, apiPath)
}
+// gitlab oauth response
+type oauthResponse struct {
+ AccessToken string `json:"access_token" `
+ RefreshToken string `json:"refresh_token"`
+ ExpiresIn int64 `json:"expires_in"`
+ CreatedAt int64 `json:"created_at"`
+ ExpiresTs int64 `json:"expires_ts"`
+ Error string `json:"error,omitempty"`
+ ErrorDescription string `json:"error_description,omitempty"`
+}
+
+// convert to *vcs.OAuthToken
+func (o oauthResponse) toVCSOAuthToken() *vcs.OAuthToken {
+ oauthToken := &vcs.OAuthToken{
+ AccessToken: o.AccessToken,
+ RefreshToken: o.RefreshToken,
+ ExpiresIn: o.ExpiresIn,
+ CreatedAt: o.CreatedAt,
+ ExpiresTs: o.ExpiresTs,
+ }
+ // For GitLab, as of 13.12, the default config won't expire the access token,
+ // thus this field is 0. See https://gitlab.com/gitlab-org/gitlab/-/issues/21745.
+ if oauthToken.ExpiresIn != 0 {
+ oauthToken.ExpiresTs = oauthToken.CreatedAt + oauthToken.ExpiresIn
+ }
+ return oauthToken
+}
+
// ExchangeOAuthToken exchange oauth content with the provided authentication code.
func (p *Provider) ExchangeOAuthToken(ctx context.Context, instanceURL string, oauthExchange *common.OAuthExchange) (*vcs.OAuthToken, error) {
urlParams := &url.Values{}
@@ -241,18 +269,16 @@ func (p *Provider) ExchangeOAuthToken(ctx context.Context, instanceURL string, o
if err != nil {
return nil, fmt.Errorf("failed to read oauth response body, code %v, error: %v", resp.StatusCode, err)
}
+ defer resp.Body.Close()
- oauthToken := &vcs.OAuthToken{}
- if err := json.Unmarshal(body, oauthToken); err != nil {
+ oauthResp := new(oauthResponse)
+ if err := json.Unmarshal(body, oauthResp); err != nil {
return nil, fmt.Errorf("failed to unmarshal oauth response body, code %v, error: %v", resp.StatusCode, err)
}
-
- // For GitLab, as of 13.12, the default config won't expire the access token,
- // thus this field is 0. See https://gitlab.com/gitlab-org/gitlab/-/issues/21745.
- if oauthToken.ExpiresIn != 0 {
- oauthToken.ExpiresTs = oauthToken.CreatedAt + oauthToken.ExpiresIn
+ if oauthResp.Error != "" {
+ return nil, fmt.Errorf("failed to exchange Oauth Token, error: %v, error_description: %v", oauthResp.Error, oauthResp.ErrorDescription)
}
- return oauthToken, nil
+ return oauthResp.toVCSOAuthToken(), nil
}
// FetchRepositoryList fetched all repositories in which the authenticated user