Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for passing in an existing oauth2.Token in the App config #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions reddit/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package reddit

import "fmt"
import (
"fmt"

"golang.org/x/oauth2"
)

var (
errMissingOauthCredentials = fmt.Errorf("missing oauth credentials")
Expand All @@ -24,17 +28,24 @@ type App struct {

// tokenURL is the url of the token request location for OAuth2.
tokenURL string

// If token is specified, username/password authentication is skipped
Token *oauth2.Token
}

func (a App) unauthenticated() bool {
return a.ID == "" || a.Secret == ""
return a.Token == nil && (a.ID == "" || a.Secret == "")
}

func (a App) validateAuth() error {
if a.unauthenticated() {
return errMissingOauthCredentials
}

if a.Token != nil {
return nil
}

if a.Password != "" && a.Username == "" {
return errMissingUsername
}
Expand Down
35 changes: 22 additions & 13 deletions reddit/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@ package reddit

import (
"testing"

"golang.org/x/oauth2"
)

func TestAppUnauthenticated(t *testing.T) {
for i, test := range []struct {
input App
output bool
}{
{App{"y", "", "", "", ""}, true},
{App{"", "y", "", "", ""}, true},
{App{"y", "y", "", "", ""}, false},
{App{"y", "y", "y", "", ""}, false},
{App{"y", "y", "", "y", ""}, false},
{App{"y", "y", "y", "y", ""}, false},
{App{"y", "", "", "", "", nil}, true},
{App{"", "y", "", "", "", nil}, true},
{App{"y", "", "", "", "", &oauth2.Token{}}, false},
{App{"", "y", "", "", "", &oauth2.Token{}}, false},
{App{"y", "y", "", "", "", nil}, false},
{App{"y", "y", "y", "", "", nil}, false},
{App{"y", "y", "", "y", "", nil}, false},
{App{"y", "y", "y", "y", "", nil}, false},
} {
if actual := test.input.unauthenticated(); actual != test.output {
t.Errorf("wrong on %d; wanted %v", i, test.output)
Expand All @@ -27,13 +31,18 @@ func TestAppValidateAuth(t *testing.T) {
input App
output error
}{
{App{"", "", "", "", ""}, errMissingOauthCredentials},
{App{"y", "", "", "", ""}, errMissingOauthCredentials},
{App{"", "y", "", "", ""}, errMissingOauthCredentials},
{App{"y", "y", "y", "", ""}, errMissingPassword},
{App{"y", "y", "", "y", ""}, errMissingUsername},
{App{"y", "y", "", "", ""}, nil},
{App{"y", "y", "y", "y", ""}, nil},
{App{"", "", "", "", "", nil}, errMissingOauthCredentials},
{App{"y", "", "", "", "", nil}, errMissingOauthCredentials},
{App{"", "y", "", "", "", nil}, errMissingOauthCredentials},
{App{"y", "y", "y", "", "", nil}, errMissingPassword},
{App{"y", "y", "", "y", "", nil}, errMissingUsername},
{App{"", "", "", "", "", &oauth2.Token{}}, nil},
{App{"y", "", "", "", "", &oauth2.Token{}}, nil},
{App{"", "y", "", "", "", &oauth2.Token{}}, nil},
{App{"y", "y", "y", "", "", &oauth2.Token{}}, nil},
{App{"y", "y", "", "y", "", &oauth2.Token{}}, nil},
{App{"y", "y", "", "", "", nil}, nil},
{App{"y", "y", "y", "y", "", nil}, nil},
} {
if actual := test.input.validateAuth(); actual != test.output {
t.Errorf("wrong on %d; wanted %v", i, test.output)
Expand Down
21 changes: 15 additions & 6 deletions reddit/appclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (a *appClient) Do(req *http.Request) ([]byte, error) {
func (a *appClient) authorize() error {
ctx := context.WithValue(oauth2.NoContext, oauth2.HTTPClient, a.cli)

if a.cfg.app.Username == "" || a.cfg.app.Password == "" {
if a.cfg.app.unauthenticated() {
a.baseClient.cli = a.clientCredentialsClient(ctx)
return nil
}
Expand All @@ -41,13 +41,22 @@ func (a *appClient) authorize() error {
Scopes: oauthScopes,
}

token, err := cfg.PasswordCredentialsToken(
ctx,
a.cfg.app.Username,
a.cfg.app.Password,
)
var token *oauth2.Token
var err error

if a.cfg.app.Token != nil {
token = a.cfg.app.Token
err = nil
} else {
token, err = cfg.PasswordCredentialsToken(
ctx,
a.cfg.app.Username,
a.cfg.app.Password,
)
}

a.baseClient.cli = cfg.Client(ctx, token)

return err
}

Expand Down