forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f184e9c
commit 7463104
Showing
18 changed files
with
287 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// 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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.