gitsource: add RefreshOauth2Token method to Oauth2Source

This commit is contained in:
Simone Gotti 2019-04-29 15:39:59 +02:00
parent 95e73e66a0
commit 9393830207
2 changed files with 17 additions and 17 deletions

View File

@ -84,9 +84,8 @@ func New(opts Opts) (*Client, error) {
}, nil }, nil
} }
func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, error) { func (c *Client) oauth2Config(callbackURL string) *oauth2.Config {
return &oauth2.Config{
var config = &oauth2.Config{
ClientID: c.oauth2ClientID, ClientID: c.oauth2ClientID,
ClientSecret: c.oauth2Secret, ClientSecret: c.oauth2Secret,
Scopes: GitlabOauth2Scopes, Scopes: GitlabOauth2Scopes,
@ -96,23 +95,15 @@ func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, e
}, },
RedirectURL: callbackURL, RedirectURL: callbackURL,
} }
}
func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, error) {
var config = c.oauth2Config(callbackURL)
return config.AuthCodeURL(state), nil return config.AuthCodeURL(state), nil
} }
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) { func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) {
var config = c.oauth2Config(callbackURL)
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,
}
token, err := config.Exchange(context.TODO(), code) token, err := config.Exchange(context.TODO(), code)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "cannot get oauth2 token") 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 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) { func (c *Client) GetRepoInfo(repopath string) (*gitsource.RepoInfo, error) {
repo, _, err := c.client.Projects.GetProject(repopath) repo, _, err := c.client.Projects.GetProject(repopath)
if err != nil { if err != nil {

View File

@ -51,11 +51,13 @@ type PasswordSource interface {
type Oauth2Source interface { type Oauth2Source interface {
UserSource UserSource
// Oauth2AuthorizationRequest return the authorization request URL to the // GetOauth2AuthorizationURL return the authorization request URL to the
// authorization server // authorization server
GetOauth2AuthorizationURL(callbackURL, state string) (redirectURL string, err error) 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) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error)
// RefreshOauth2Token refreshed the oauth2 token
RefreshOauth2Token(refreshToken string) (*oauth2.Token, error)
} }
type RepoInfo struct { type RepoInfo struct {