parent
7463104491
commit
5ec7008bbb
@ -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")
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue