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

@ -61,11 +61,11 @@ type Opts struct {
} }
type Client struct { type Client struct {
client *gitea.Client client *gitea.Client
httpClient *http.Client oauth2HTTPClient *http.Client
APIURL string APIURL string
oauth2ClientID string oauth2ClientID string
oauth2Secret string oauth2Secret string
} }
// fromCommitStatus converts a gitsource commit status to a gitea commit status // fromCommitStatus converts a gitsource commit status to a gitea commit status
@ -113,11 +113,11 @@ func New(opts Opts) (*Client, error) {
client.SetHTTPClient(httpClient) client.SetHTTPClient(httpClient)
return &Client{ return &Client{
client: client, client: client,
httpClient: httpClient, oauth2HTTPClient: httpClient,
APIURL: opts.APIURL, APIURL: opts.APIURL,
oauth2ClientID: opts.Oauth2ClientID, oauth2ClientID: opts.Oauth2ClientID,
oauth2Secret: opts.Oauth2Secret, oauth2Secret: opts.Oauth2Secret,
}, nil }, nil
} }
@ -140,8 +140,11 @@ func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, e
} }
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) { 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) var config = c.oauth2Config(callbackURL)
token, err := config.Exchange(context.TODO(), code) token, err := config.Exchange(ctx, code)
if err != nil { if err != nil {
return nil, errors.Errorf("cannot get oauth2 token: %w", err) 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) { func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config("") var config = c.oauth2Config("")
token := &oauth2.Token{RefreshToken: refreshToken} token := &oauth2.Token{RefreshToken: refreshToken}
ts := config.TokenSource(context.TODO(), token) ts := config.TokenSource(ctx, token)
return ts.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))) 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 { if err != nil {
return "", err return "", err
} }

View File

@ -61,12 +61,12 @@ type Opts struct {
} }
type Client struct { type Client struct {
client *github.Client client *github.Client
httpClient *http.Client oauth2HTTPClient *http.Client
APIURL string APIURL string
WebURL string WebURL string
oauth2ClientID string oauth2ClientID string
oauth2Secret string oauth2Secret string
} }
// fromCommitStatus converts a gitsource commit status to a github commit status // fromCommitStatus converts a gitsource commit status to a github commit status
@ -121,6 +121,7 @@ func New(opts Opts) (*Client, error) {
TLSClientConfig: &tls.Config{InsecureSkipVerify: opts.SkipVerify}, TLSClientConfig: &tls.Config{InsecureSkipVerify: opts.SkipVerify},
} }
httpClient := &http.Client{Transport: &TokenTransport{token: opts.Token, rt: transport}} httpClient := &http.Client{Transport: &TokenTransport{token: opts.Token, rt: transport}}
oauth2HTTPClient := &http.Client{Transport: transport}
isPublicGithub := false isPublicGithub := false
// TODO(sgotti) improve detection of public github url (handle also trailing slash) // TODO(sgotti) improve detection of public github url (handle also trailing slash)
@ -149,12 +150,12 @@ func New(opts Opts) (*Client, error) {
client.BaseURL, _ = url.Parse(opts.APIURL) client.BaseURL, _ = url.Parse(opts.APIURL)
return &Client{ return &Client{
client: client, client: client,
httpClient: httpClient, oauth2HTTPClient: oauth2HTTPClient,
APIURL: opts.APIURL, APIURL: opts.APIURL,
WebURL: opts.WebURL, WebURL: opts.WebURL,
oauth2ClientID: opts.Oauth2ClientID, oauth2ClientID: opts.Oauth2ClientID,
oauth2Secret: opts.Oauth2Secret, oauth2Secret: opts.Oauth2Secret,
}, nil }, nil
} }
@ -177,8 +178,11 @@ func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, e
} }
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) { 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) var config = c.oauth2Config(callbackURL)
token, err := config.Exchange(context.TODO(), code) token, err := config.Exchange(ctx, code)
if err != nil { if err != nil {
return nil, errors.Errorf("cannot get oauth2 token: %w", err) 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) { func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config("") var config = c.oauth2Config("")
token := &oauth2.Token{RefreshToken: refreshToken} token := &oauth2.Token{RefreshToken: refreshToken}
ts := config.TokenSource(context.TODO(), token) ts := config.TokenSource(ctx, token)
return ts.Token() return ts.Token()
} }

View File

@ -51,10 +51,11 @@ type Opts struct {
} }
type Client struct { type Client struct {
client *gitlab.Client client *gitlab.Client
APIURL string oauth2HTTPClient *http.Client
oauth2ClientID string APIURL string
oauth2Secret string oauth2ClientID string
oauth2Secret string
} }
// fromCommitStatus converts a gitsource commit status to a gitlab commit status // fromCommitStatus converts a gitsource commit status to a gitlab commit status
@ -89,16 +90,18 @@ func New(opts Opts) (*Client, error) {
TLSClientConfig: &tls.Config{InsecureSkipVerify: opts.SkipVerify}, TLSClientConfig: &tls.Config{InsecureSkipVerify: opts.SkipVerify},
} }
httpClient := &http.Client{Transport: transport} httpClient := &http.Client{Transport: transport}
client := gitlab.NewOAuthClient(httpClient, opts.Token) client := gitlab.NewOAuthClient(httpClient, opts.Token)
if err := client.SetBaseURL(opts.APIURL); err != nil { if err := client.SetBaseURL(opts.APIURL); err != nil {
return nil, errors.Errorf("failed to set gitlab client base url: %w", err) return nil, errors.Errorf("failed to set gitlab client base url: %w", err)
} }
return &Client{ return &Client{
client: client, client: client,
APIURL: opts.APIURL, oauth2HTTPClient: httpClient,
oauth2ClientID: opts.Oauth2ClientID, APIURL: opts.APIURL,
oauth2Secret: opts.Oauth2Secret, oauth2ClientID: opts.Oauth2ClientID,
oauth2Secret: opts.Oauth2Secret,
}, nil }, nil
} }
@ -121,8 +124,11 @@ func (c *Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, e
} }
func (c *Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) { 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) var config = c.oauth2Config(callbackURL)
token, err := config.Exchange(context.TODO(), code) token, err := config.Exchange(ctx, code)
if err != nil { if err != nil {
return nil, errors.Errorf("cannot get oauth2 token: %w", err) 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) { func (c *Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)
var config = c.oauth2Config("") var config = c.oauth2Config("")
token := &oauth2.Token{RefreshToken: refreshToken} token := &oauth2.Token{RefreshToken: refreshToken}
ts := config.TokenSource(context.TODO(), token) ts := config.TokenSource(ctx, token)
return ts.Token() return ts.Token()
} }