Skip to content

Commit

Permalink
respect WOODPECKER_GITEA_SKIP_VERIFY fix woodpecker-ci#605
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Sep 1, 2022
1 parent efdad4a commit d3412a4
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions server/remote/gitea/gitea.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"path"
"path/filepath"
"strings"
"time"

"code.gitea.io/sdk/gitea"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -84,22 +85,27 @@ func (c *Gitea) Name() string {
return "gitea"
}

func (c *Gitea) oauth2Config() *oauth2.Config {
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),
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),
},
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 := c.oauth2Config()
config, oauth2Ctx := c.oauth2Config(ctx)

// get the OAuth errors
if err := req.FormValue("error"); err != "" {
Expand All @@ -117,7 +123,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 @@ -158,10 +164,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 := c.oauth2Config()
config, oauth2Ctx := c.oauth2Config(ctx)
config.RedirectURL = ""

source := config.TokenSource(ctx, &oauth2.Token{RefreshToken: user.Secret})
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 d3412a4

Please sign in to comment.