diff --git a/internal/gitsources/gitlab/gitlab.go b/internal/gitsources/gitlab/gitlab.go index a44e24e..3c492b0 100644 --- a/internal/gitsources/gitlab/gitlab.go +++ b/internal/gitsources/gitlab/gitlab.go @@ -84,9 +84,8 @@ func New(opts Opts) (*Client, error) { }, nil } -func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, error) { - - var config = &oauth2.Config{ +func (c *Client) oauth2Config(callbackURL string) *oauth2.Config { + return &oauth2.Config{ ClientID: c.oauth2ClientID, ClientSecret: c.oauth2Secret, Scopes: GitlabOauth2Scopes, @@ -96,23 +95,15 @@ func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, e }, RedirectURL: callbackURL, } +} +func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, error) { + var config = c.oauth2Config(callbackURL) return config.AuthCodeURL(state), nil } func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) { - - var config = &oauth2.Config{ - ClientID: c.oauth2ClientID, - ClientSecret: c.oauth2Secret, - Scopes: GitlabOauth2Scopes, - Endpoint: oauth2.Endpoint{ - AuthURL: fmt.Sprintf("%s/oauth/authorize", c.URL), - TokenURL: fmt.Sprintf("%s/oauth/token", c.URL), - }, - RedirectURL: callbackURL, - } - + var config = c.oauth2Config(callbackURL) token, err := config.Exchange(context.TODO(), code) if err != nil { return nil, errors.Wrapf(err, "cannot get oauth2 token") @@ -120,6 +111,13 @@ func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, er return token, nil } +func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) { + var config = c.oauth2Config("") + token := &oauth2.Token{RefreshToken: refreshToken} + ts := config.TokenSource(context.TODO(), token) + return ts.Token() +} + func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) { repo, _, err := c.client.Projects.GetProject(repopath) if err != nil { diff --git a/internal/gitsources/gitsource.go b/internal/gitsources/gitsource.go index d11a770..aaff4f6 100644 --- a/internal/gitsources/gitsource.go +++ b/internal/gitsources/gitsource.go @@ -51,11 +51,13 @@ type PasswordSource interface { type Oauth2Source interface { UserSource - // Oauth2AuthorizationRequest return the authorization request URL to the + // GetOauth2AuthorizationURL return the authorization request URL to the // authorization server GetOauth2AuthorizationURL(callbackURL, state string) (redirectURL string, err error) - // OauthTokenRequest requests the oauth2 access token to the authorization server + // RequestOauth2Token requests the oauth2 token to the authorization server RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) + // RefreshOauth2Token refreshed the oauth2 token + RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) } type RepoInfo struct {