Skip to content

Commit

Permalink
chore: Debugging Bitbucket OAuth 2.0 impl
Browse files Browse the repository at this point in the history
Signed-off-by: Thor Anker Kvisgård Lange <[email protected]>
  • Loading branch information
langecode committed Feb 20, 2024
1 parent 83423bb commit 1c54f69
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
30 changes: 23 additions & 7 deletions server/forge/bitbucketdatacenter/bitbucketdatacenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func (c *client) Login(ctx context.Context, req *forge_types.OAuthRequest) (*mod
if err != nil {
return nil, redirectURL, err
}
log.Info().Any("token", token).Msg("retrieved token")

client := internal.NewClientWithToken(ctx, config.TokenSource(ctx, token), c.url)
userSlug, err := client.FindCurrentUser(ctx)
Expand All @@ -130,23 +131,38 @@ func (c *client) Login(ctx context.Context, req *forge_types.OAuthRequest) (*mod
}

u := convertUser(user, c.url)
u.Token = token.AccessToken
u.Secret = token.RefreshToken
u.Expiry = token.Expiry.Unix()
updateUserCredentials(u, token)
log.Info().Any("user", u).Msg("returning user from Login..")
return u, "", nil
}

// Auth is not supported.
func (c *client) Auth(ctx context.Context, accessToken, refreshToken string) (string, error) {
func (c *client) Auth(ctx context.Context, accessToken, _ string) (string, error) {
config := c.newOAuth2Config()
token := &oauth2.Token{
AccessToken: accessToken,
RefreshToken: refreshToken,
AccessToken: accessToken,
}
client := internal.NewClientWithToken(ctx, config.TokenSource(ctx, token), c.url)
return client.FindCurrentUser(ctx)
}

func (c *client) Refresh(ctx context.Context, u *model.User) (bool, error) {
config := c.newOAuth2Config()
t := &oauth2.Token{
RefreshToken: u.Secret,
}
ts := config.TokenSource(ctx, t)

tok, err := ts.Token()
if err != nil {
return false, fmt.Errorf("unable to refresh OAuth 2.0 token from bitbucket datacenter: %w", err)
}

updateUserCredentials(u, tok)
log.Info().Any("user", u).Msg("updated user after refresh")

return true, nil
}

func (c *client) Repo(ctx context.Context, u *model.User, rID model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
bc, err := c.newClient(ctx, u)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions server/forge/bitbucketdatacenter/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"time"

bb "github.com/neticdk/go-bitbucket/bitbucket"
"github.com/rs/zerolog/log"
"golang.org/x/oauth2"

"go.woodpecker-ci.org/woodpecker/v2/server/model"
)
Expand Down Expand Up @@ -153,3 +155,10 @@ func convertListOptions(p *model.ListOptions) bb.ListOptions {
}
return bb.ListOptions{Limit: uint(p.PerPage), Start: uint((p.Page - 1) * p.PerPage)}
}

func updateUserCredentials(u *model.User, t *oauth2.Token) {
u.Token = t.AccessToken
u.Secret = t.RefreshToken
u.Expiry = t.Expiry.Unix()
log.Info().Any("user", u).Msg("updated user credentials")
}

0 comments on commit 1c54f69

Please sign in to comment.