Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gitsources: split oauth2Client from gitsource #403

Merged
merged 1 commit into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 85 additions & 67 deletions internal/gitsources/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,21 @@ var (
pullRequestRefFmt = "refs/pull/%s/head"
)

type httpOpts struct {
SkipVerify bool
}

type Opts struct {
APIURL string
UserName string
Password string
Token string
SkipVerify bool
Oauth2ClientID string
Oauth2Secret string
APIURL string
SkipVerify bool
UserName string
Password string
Token string
}

type Client struct {
client *gitea.Client
oauth2HTTPClient *http.Client
APIURL string
oauth2ClientID string
oauth2Secret string
client *gitea.Client
APIURL string
}

// fromCommitStatus converts a gitsource commit status to a gitea commit status
Expand All @@ -85,7 +84,7 @@ func parseRepoPath(repopath string) (string, string, error) {
return parts[0], parts[1], nil
}

func newHTTPClient(opts Opts) *http.Client {
func newHTTPClient(opts httpOpts) *http.Client {
// copied from net/http until it has a clone function: https://github.com/golang/go/issues/26013
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Expand All @@ -105,81 +104,33 @@ func newHTTPClient(opts Opts) *http.Client {
}

func New(opts Opts) (*Client, error) {
httpClient := newHTTPClient(opts)
httpClient := newHTTPClient(httpOpts{SkipVerify: opts.SkipVerify})

client, err := gitea.NewClient(opts.APIURL, gitea.SetToken(opts.Token), gitea.SetHTTPClient(httpClient))
if err != nil {
return nil, errors.Wrapf(err, "failed to create gitea client")
}

return &Client{
client: client,
oauth2HTTPClient: httpClient,
APIURL: opts.APIURL,
oauth2ClientID: opts.Oauth2ClientID,
oauth2Secret: opts.Oauth2Secret,
client: client,
APIURL: opts.APIURL,
}, nil
}

func NewWithBasicAuth(opts Opts) (*Client, error) {
httpClient := newHTTPClient(opts)
httpClient := newHTTPClient(httpOpts{SkipVerify: opts.SkipVerify})

client, err := gitea.NewClient(opts.APIURL, gitea.SetBasicAuth(opts.UserName, opts.Password), gitea.SetHTTPClient(httpClient))
if err != nil {
return nil, errors.Wrapf(err, "failed to create gitea client")
}

return &Client{
client: client,
oauth2HTTPClient: httpClient,
APIURL: opts.APIURL,
oauth2ClientID: opts.Oauth2ClientID,
oauth2Secret: opts.Oauth2Secret,
client: client,
APIURL: opts.APIURL,
}, nil
}

func (c *Client) oauth2Config(callbackURL string) *oauth2.Config {
return &oauth2.Config{
ClientID: c.oauth2ClientID,
ClientSecret: c.oauth2Secret,
Scopes: GiteaOauth2Scopes,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s/login/oauth/authorize", c.APIURL),
TokenURL: fmt.Sprintf("%s/login/oauth/access_token", c.APIURL),
},
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) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.oauth2HTTPClient)

var config = c.oauth2Config(callbackURL)
token, err := config.Exchange(ctx, code)
if err != nil {
return nil, errors.Wrapf(err, "cannot get oauth2 token")
}
return token, nil
}

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(ctx, token)
ntoken, err := ts.Token()

return ntoken, errors.WithStack(err)
}

func (c *Client) CreateAccessToken(tokenName string) (string, error) {
// try to get agola access token if it already exists
var hasAccessToken bool
Expand Down Expand Up @@ -529,3 +480,70 @@ func (c *Client) TagLink(repoInfo *gitsource.RepoInfo, tag string) string {
func (c *Client) PullRequestLink(repoInfo *gitsource.RepoInfo, prID string) string {
return fmt.Sprintf("%s/pulls/%s", repoInfo.HTMLURL, prID)
}

type Oauth2Opts struct {
APIURL string
SkipVerify bool
Oauth2ClientID string
Oauth2Secret string
}

type Oauth2Client struct {
httpClient *http.Client
APIURL string
oauth2ClientID string
oauth2Secret string
}

func NewOauth2Client(opts Oauth2Opts) (*Oauth2Client, error) {
httpClient := newHTTPClient(httpOpts{SkipVerify: opts.SkipVerify})

return &Oauth2Client{
httpClient: httpClient,
APIURL: opts.APIURL,
oauth2ClientID: opts.Oauth2ClientID,
oauth2Secret: opts.Oauth2Secret,
}, nil
}

func (c *Oauth2Client) oauth2Config(callbackURL string) *oauth2.Config {
return &oauth2.Config{
ClientID: c.oauth2ClientID,
ClientSecret: c.oauth2Secret,
Scopes: GiteaOauth2Scopes,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf("%s/login/oauth/authorize", c.APIURL),
TokenURL: fmt.Sprintf("%s/login/oauth/access_token", c.APIURL),
},
RedirectURL: callbackURL,
}
}

func (c *Oauth2Client) GetOauth2AuthorizationURL(callbackURL, state string) (string, error) {
var config = c.oauth2Config(callbackURL)
return config.AuthCodeURL(state), nil
}

func (c *Oauth2Client) RequestOauth2Token(callbackURL, code string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.httpClient)

var config = c.oauth2Config(callbackURL)
token, err := config.Exchange(ctx, code)
if err != nil {
return nil, errors.Wrapf(err, "cannot get oauth2 token")
}
return token, nil
}

func (c *Oauth2Client) RefreshOauth2Token(refreshToken string) (*oauth2.Token, error) {
ctx := context.TODO()
ctx = context.WithValue(ctx, oauth2.HTTPClient, c.httpClient)

var config = c.oauth2Config("")
token := &oauth2.Token{RefreshToken: refreshToken}
ts := config.TokenSource(ctx, token)
ntoken, err := ts.Token()

return ntoken, errors.WithStack(err)
}
Loading