Merge pull request #208 from sgotti/gitsource_oauth2_skipverify

gitsources: handle skipverify also in oauth2 requests
This commit is contained in:
Simone Gotti 2020-02-11 22:28:15 +01:00 committed by GitHub
commit 109d488aba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 37 deletions

View File

@ -62,7 +62,7 @@ type Opts struct {
type Client struct {
client *gitea.Client
httpClient *http.Client
oauth2HTTPClient *http.Client
APIURL string
oauth2ClientID string
oauth2Secret string
@ -114,7 +114,7 @@ func New(opts Opts) (*Client, error) {
return &Client{
client: client,
httpClient: httpClient,
oauth2HTTPClient: httpClient,
APIURL: opts.APIURL,
oauth2ClientID: opts.Oauth2ClientID,
oauth2Secret: opts.Oauth2Secret,
@ -140,8 +140,11 @@ func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, e
}
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config(callbackURL)
token, err := config.Exchange(context.TODO(), code)
token, err := config.Exchange(ctx, code)
if err != nil {
return nil, errors.Errorf("cannot get oauth2 token: %w", err)
}
@ -149,9 +152,12 @@ func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, er
}
func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config("")
token := &oauth2.Token{RefreshToken: refreshToken}
ts := config.TokenSource(context.TODO(), token)
ts := config.TokenSource(ctx, token)
return ts.Token()
}
@ -168,7 +174,7 @@ func (c *Client) LoginPassword(username, password, tokenName string) (string, er
}
req.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(username+":"+password)))
resp, err := c.httpClient.Do(req)
resp, err := c.oauth2HTTPClient.Do(req)
if err != nil {
return "", err
}

View File

@ -62,7 +62,7 @@ type Opts struct {
type Client struct {
client *github.Client
httpClient *http.Client
oauth2HTTPClient *http.Client
APIURL string
WebURL string
oauth2ClientID string
@ -121,6 +121,7 @@ func New(opts Opts) (*Client, error) {
TLSClientConfig: &tls.Config{InsecureSkipVerify: opts.SkipVerify},
}
httpClient := &http.Client{Transport: &TokenTransport{token: opts.Token, rt: transport}}
oauth2HTTPClient := &http.Client{Transport: transport}
isPublicGithub := false
// TODO(sgotti) improve detection of public github url (handle also trailing slash)
@ -150,7 +151,7 @@ func New(opts Opts) (*Client, error) {
return &Client{
client: client,
httpClient: httpClient,
oauth2HTTPClient: oauth2HTTPClient,
APIURL: opts.APIURL,
WebURL: opts.WebURL,
oauth2ClientID: opts.Oauth2ClientID,
@ -177,8 +178,11 @@ func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, e
}
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config(callbackURL)
token, err := config.Exchange(context.TODO(), code)
token, err := config.Exchange(ctx, code)
if err != nil {
return nil, errors.Errorf("cannot get oauth2 token: %w", err)
}
@ -186,9 +190,12 @@ func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, er
}
func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config("")
token := &oauth2.Token{RefreshToken: refreshToken}
ts := config.TokenSource(context.TODO(), token)
ts := config.TokenSource(ctx, token)
return ts.Token()
}

View File

@ -52,6 +52,7 @@ type Opts struct {
type Client struct {
client *gitlab.Client
oauth2HTTPClient *http.Client
APIURL string
oauth2ClientID string
oauth2Secret string
@ -89,6 +90,7 @@ func New(opts Opts) (*Client, error) {
TLSClientConfig: &tls.Config{InsecureSkipVerify: opts.SkipVerify},
}
httpClient := &http.Client{Transport: transport}
client := gitlab.NewOAuthClient(httpClient, opts.Token)
if err := client.SetBaseURL(opts.APIURL); err != nil {
return nil, errors.Errorf("failed to set gitlab client base url: %w", err)
@ -96,6 +98,7 @@ func New(opts Opts) (*Client, error) {
return &Client{
client: client,
oauth2HTTPClient: httpClient,
APIURL: opts.APIURL,
oauth2ClientID: opts.Oauth2ClientID,
oauth2Secret: opts.Oauth2Secret,
@ -121,8 +124,11 @@ func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, e
}
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config(callbackURL)
token, err := config.Exchange(context.TODO(), code)
token, err := config.Exchange(ctx, code)
if err != nil {
return nil, errors.Errorf("cannot get oauth2 token: %w", err)
}
@ -130,9 +136,12 @@ func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, er
}
func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config("")
token := &oauth2.Token{RefreshToken: refreshToken}
ts := config.TokenSource(context.TODO(), token)
ts := config.TokenSource(ctx, token)
return ts.Token()
}