Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Sep 1, 2022
1 parent 80ba3af commit de39f7e
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions server/remote/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/url"
"path"
"path/filepath"
"time"

"code.gitea.io/sdk/gitea"
"golang.org/x/oauth2"
Expand Down Expand Up @@ -76,18 +77,32 @@ func New(opts Opts) (remote.Remote, error) {
}, nil
}

// Name returns the string name of this driver
func (c *Gitea) Name() string {
return "gitea"
}

func (c *Gitea) oauth2Config(ctx context.Context) (*oauth2.Config, context.Context) {
return &oauth2.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf(authorizeTokenURL, c.URL),
TokenURL: fmt.Sprintf(accessTokenURL, c.URL),
},
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.OAuthHost),
},

context.WithValue(ctx, oauth2.HTTPClient, &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: c.SkipVerify},
Proxy: http.ProxyFromEnvironment,
}})
}

// Login authenticates an account with Gitea using basic authentication. The
// Gitea account details are returned when the user is successfully authenticated.
func (c *Gitea) Login(ctx context.Context, w http.ResponseWriter, req *http.Request) (*model.User, error) {
config := &oauth2.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf(authorizeTokenURL, c.URL),
TokenURL: fmt.Sprintf(accessTokenURL, c.URL),
},
RedirectURL: fmt.Sprintf("%s/authorize", server.Config.Server.OAuthHost),
}
config, oauth2Ctx := c.oauth2Config(ctx)

// get the OAuth errors
if err := req.FormValue("error"); err != "" {
Expand All @@ -105,7 +120,7 @@ func (c *Gitea) Login(ctx context.Context, w http.ResponseWriter, req *http.Requ
return nil, nil
}

token, err := config.Exchange(ctx, code)
token, err := config.Exchange(oauth2Ctx, code)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -146,15 +161,14 @@ func (c *Gitea) Auth(ctx context.Context, token, secret string) (string, error)
// Refresh refreshes the Gitea oauth2 access token. If the token is
// refreshed the user is updated and a true value is returned.
func (c *Gitea) Refresh(ctx context.Context, user *model.User) (bool, error) {
config := &oauth2.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: fmt.Sprintf(authorizeTokenURL, c.URL),
TokenURL: fmt.Sprintf(accessTokenURL, c.URL),
},
}
source := config.TokenSource(ctx, &oauth2.Token{RefreshToken: user.Secret})
config, oauth2Ctx := c.oauth2Config(ctx)
config.RedirectURL = ""

source := config.TokenSource(oauth2Ctx, &oauth2.Token{
AccessToken: user.Token,
RefreshToken: user.Secret,
Expiry: time.Unix(user.Expiry, 0),
})

token, err := source.Token()
if err != nil || len(token.AccessToken) == 0 {
Expand Down

0 comments on commit de39f7e

Please sign in to comment.