Revert "added token refresh function"

This reverts commit 7463104491.
pull/1/head
Brad Rydzewski 6 years ago
parent 7463104491
commit 5ec7008bbb

@ -93,7 +93,6 @@ type (
PullRequests PullRequestService
Repositories RepositoryService
Reviews ReviewService
Tokens TokenService
Users UserService
Webhooks WebhookService

@ -14,18 +14,10 @@ import (
"strings"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/transport/oauth2"
)
// ClientID string
// ClientSecret string
// Endpoint string
// Source scm.TokenSource
// Client *http.Client
// New returns a new Bitbucket API client.
func New(uri string, opt ...Option) (*scm.Client, error) {
func New(uri string) (*scm.Client, error) {
base, err := url.Parse(uri)
if err != nil {
return nil, err
@ -33,16 +25,6 @@ func New(uri string, opt ...Option) (*scm.Client, error) {
if !strings.HasSuffix(base.Path, "/") {
base.Path = base.Path + "/"
}
opts := new(Options)
for _, o := range opt {
o(opts)
}
refresher := &oauth2.Refresher{
Endpoint: tokenEndpoint,
ClientID: opts.clientID,
ClientSecret: opts.clientSecret,
Client: opts.client,
}
client := &wrapper{new(scm.Client)}
client.BaseURL = base
// initialize services
@ -54,7 +36,6 @@ func New(uri string, opt ...Option) (*scm.Client, error) {
client.PullRequests = &pullService{&issueService{client}}
client.Repositories = &repositoryService{client}
client.Reviews = &reviewService{client}
client.Tokens = &tokenService{refresher}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
return client.Client, nil
@ -62,8 +43,8 @@ func New(uri string, opt ...Option) (*scm.Client, error) {
// NewDefault returns a new Bitbucket API client using the
// default api.bitbucket.org address.
func NewDefault(opt ...Option) *scm.Client {
client, _ := New("https://api.bitbucket.org", opt...)
func NewDefault() *scm.Client {
client, _ := New("https://api.bitbucket.org")
return client
}

@ -1,41 +0,0 @@
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bitbucket
import "net/http"
// Options provides Bitbucket client options.
type Options struct {
clientID string
clientSecret string
client *http.Client
}
// Option provides a Bitbucket client option.
type Option func(*Options)
// WithClient returns an option to set the http.Client
// used to refresh authorization tokens.
func WithClient(client *http.Client) Option {
return func(opts *Options) {
opts.client = client
}
}
// WithClientID returns an option to set the Bitbucket
// oauth2 client identifier.
func WithClientID(clientID string) Option {
return func(opts *Options) {
opts.clientID = clientID
}
}
// WithClientSecret returns an option to set the
// Bitbucket oauth2 client secret.
func WithClientSecret(clientSecret string) Option {
return func(opts *Options) {
opts.clientSecret = clientSecret
}
}

@ -1,29 +0,0 @@
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bitbucket
import (
"context"
"github.com/drone/go-scm/scm"
"github.com/drone/go-scm/scm/transport/oauth2"
)
// bitbucket cloud access_token endpoint.
const tokenEndpoint = "https://bitbucket.org/site/oauth2/access_token"
type tokenService struct {
refresher *oauth2.Refresher
}
func (t *tokenService) Refresh(ctx context.Context, token *scm.Token) (bool, error) {
if oauth2.Expired(token) == false {
return false, nil
}
t1 := token.Token
err := t.refresher.Refresh(token)
t2 := token.Token
return t1 != t2, err
}

@ -1,74 +0,0 @@
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package bitbucket
import (
"context"
"testing"
"time"
"github.com/drone/go-scm/scm"
"github.com/h2non/gock"
)
func TestTokenRefresh(t *testing.T) {
defer gock.Off()
gock.New("https://bitbucket.org").
Post("/site/oauth2/access_token").
MatchHeader("Authorization", "Basic NTU5OTE4YTgwODowMmJiYTUwMTJm").
Reply(200).
BodyString(`
{
"access_token": "9698fa6a8113b3",
"expires_in": 7200,
"refresh_token": "3a2bfce4cb9b0f",
"token_type": "bearer"
}
`)
token := &scm.Token{
Token: "ae215a0a8223a9",
Refresh: "3a2bfce4cb9b0f",
Expires: time.Now().Add(-time.Second),
}
client, _ := New("https://api.bitbucket.org",
WithClientID("559918a808"),
WithClientSecret("02bba5012f"),
)
refreshed, err := client.Tokens.Refresh(context.Background(), token)
if err != nil {
t.Error(err)
}
if !refreshed {
t.Errorf("Expected token refresh")
}
if got, want := token.Token, "9698fa6a8113b3"; got != want {
t.Errorf("Expected refresh token %s, got %s", want, got)
}
}
func TestTokenRefresh_NoRefresh(t *testing.T) {
token := &scm.Token{
Token: "ae215a0a8223a9",
Refresh: "3a2bfce4cb9b0f",
Expires: time.Now().Add(time.Hour),
}
client, _ := New("https://api.bitbucket.org",
WithClientID("559918a808"),
WithClientSecret("02bba5012f"),
)
refreshed, err := client.Tokens.Refresh(context.Background(), token)
if err != nil {
t.Error(err)
}
if refreshed {
t.Errorf("Expected token not refreshed")
}
}

@ -38,7 +38,6 @@ func New(uri string) (*scm.Client, error) {
client.PullRequests = &pullService{client}
client.Repositories = &repositoryService{client}
client.Reviews = &reviewService{client}
client.Tokens = &tokenService{}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
return client.Client, nil

@ -1,20 +0,0 @@
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gitea
import (
"context"
"github.com/drone/go-scm/scm"
)
type tokenService struct {
}
func (t *tokenService) Refresh(context.Context, *scm.Token) (bool, error) {
// this function is a no-op because Gitea
// does not implement refresh tokens.
return false, nil
}

@ -36,7 +36,6 @@ func New(uri string) (*scm.Client, error) {
client.PullRequests = &pullService{&issueService{client}}
client.Repositories = &repositoryService{client}
client.Reviews = &reviewService{client}
client.Tokens = &tokenService{}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
return client.Client, nil

@ -1,20 +0,0 @@
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"github.com/drone/go-scm/scm"
)
type tokenService struct {
}
func (t *tokenService) Refresh(context.Context, *scm.Token) (bool, error) {
// this function is a no-op because GitHub
// does not implement refresh tokens.
return false, nil
}

@ -35,9 +35,7 @@ func New(uri string) (*scm.Client, error) {
client.PullRequests = &pullService{client}
client.Repositories = &repositoryService{client}
client.Reviews = &reviewService{client}
client.Tokens = &tokenService{}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
return client.Client, nil
}

@ -1,20 +0,0 @@
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gitlab
import (
"context"
"github.com/drone/go-scm/scm"
)
type tokenService struct {
}
func (t *tokenService) Refresh(context.Context, *scm.Token) (bool, error) {
// this function is a no-op because GitLab
// does not implement refresh tokens.
return false, nil
}

@ -38,7 +38,6 @@ func New(uri string) (*scm.Client, error) {
client.PullRequests = &pullService{client}
client.Repositories = &repositoryService{client}
client.Reviews = &reviewService{client}
client.Tokens = &tokenService{}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
return client.Client, nil

@ -1,20 +0,0 @@
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gogs
import (
"context"
"github.com/drone/go-scm/scm"
)
type tokenService struct {
}
func (t *tokenService) Refresh(context.Context, *scm.Token) (bool, error) {
// this function is a no-op because Gogs
// does not implement refresh tokens.
return false, nil
}

@ -40,7 +40,6 @@ func New(uri string) (*scm.Client, error) {
client.PullRequests = &pullService{client}
client.Repositories = &repositoryService{client}
client.Reviews = &reviewService{client}
client.Tokens = &tokenService{}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
return client.Client, nil

@ -1,20 +0,0 @@
// Copyright 2018 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stash
import (
"context"
"github.com/drone/go-scm/scm"
)
type tokenService struct {
}
func (t *tokenService) Refresh(context.Context, *scm.Token) (bool, error) {
// this function is a no-op because Bitbucket Server
// does not implement refresh tokens.
return false, nil
}

@ -23,20 +23,12 @@ type (
Token(context.Context) (*Token, error)
}
// TokenService provides token refresh capabilities.
TokenService interface {
// Refresh refreshes the token if necessary. The
// function returns a boolean value indicating
// whether or not the token is refreshed.
Refresh(context.Context, *Token) (bool, error)
}
// TokenKey is the key to use with the context.WithValue
// function to associate an Token value with a context.
TokenKey struct{}
)
// WithToken returns a copy of parent in which the token value is set
func WithToken(parent context.Context, token *Token) context.Context {
// WithContext returns a copy of parent in which the token value is set
func WithContext(parent context.Context, token *Token) context.Context {
return context.WithValue(parent, TokenKey{}, token)
}

@ -42,7 +42,7 @@ func (t *Refresher) Token(ctx context.Context) (*scm.Token, error) {
if err != nil {
return nil, err
}
if !Expired(token) {
if !expired(token) {
return token, nil
}
err = t.Refresh(token)
@ -106,8 +106,8 @@ func (t *Refresher) client() *http.Client {
return http.DefaultClient
}
// Expired reports whether the token is expired.
func Expired(token *scm.Token) bool {
// expired reports whether the token is expired.
func expired(token *scm.Token) bool {
if len(token.Refresh) == 0 {
return false
}

@ -192,7 +192,7 @@ func TestExpired(t *testing.T) {
}
for i, test := range tests {
if got, want := Expired(test.token), test.expired; got != want {
if got, want := expired(test.token), test.expired; got != want {
t.Errorf("Want token expired %v, got %v at index %d", want, got, i)
}
}

Loading…
Cancel
Save