authhandler: Make authHandler the last parameter

This commit is contained in:
Andy Zhao 2021-03-17 12:29:45 -07:00
parent 8a926e1234
commit ab12fee4d1
3 changed files with 8 additions and 8 deletions

View File

@ -30,7 +30,7 @@ type AuthorizationHandler func(authCodeURL string) (code string, state string, e
// //
// Per the OAuth protocol, a unique "state" string should be sent and verified // Per the OAuth protocol, a unique "state" string should be sent and verified
// before exchanging the auth code for OAuth token to prevent CSRF attacks. // before exchanging the auth code for OAuth token to prevent CSRF attacks.
func TokenSource(ctx context.Context, config *oauth2.Config, authHandler AuthorizationHandler, state string) oauth2.TokenSource { func TokenSource(ctx context.Context, config *oauth2.Config, state string, authHandler AuthorizationHandler) oauth2.TokenSource {
return oauth2.ReuseTokenSource(nil, authHandlerSource{config: config, ctx: ctx, authHandler: authHandler, state: state}) return oauth2.ReuseTokenSource(nil, authHandlerSource{config: config, ctx: ctx, authHandler: authHandler, state: state})
} }
@ -47,8 +47,8 @@ func (source authHandlerSource) Token() (*oauth2.Token, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if state == source.state { if state != source.state {
return nil, errors.New("state mismatch in 3-legged-OAuth flow")
}
return source.config.Exchange(source.ctx, code) return source.config.Exchange(source.ctx, code)
} }
return nil, errors.New("state mismatch in 3-legged-OAuth flow.")
}

View File

@ -45,7 +45,7 @@ func TestTokenExchange_Success(t *testing.T) {
}, },
} }
tok, err := TokenSource(context.Background(), conf, authhandler, "testState").Token() tok, err := TokenSource(context.Background(), conf, "testState", authhandler).Token()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -92,8 +92,8 @@ func TestTokenExchange_StateMismatch(t *testing.T) {
}, },
} }
_, err := TokenSource(context.Background(), conf, authhandler, "testState").Token() _, err := TokenSource(context.Background(), conf, "testState", authhandler).Token()
if want_err := "state mismatch in 3-legged-OAuth flow."; err == nil || err.Error() != want_err { if want_err := "state mismatch in 3-legged-OAuth flow"; err == nil || err.Error() != want_err {
t.Errorf("err = %q; want %q", err, want_err) t.Errorf("err = %q; want %q", err, want_err)
} }
} }

View File

@ -38,7 +38,7 @@ func ExampleCmdAuthorizationHandler() {
} }
state := "unique_state" state := "unique_state"
token, err := authhandler.TokenSource(ctx, conf, authhandler.CmdAuthorizationHandler(state), state).Token() token, err := authhandler.TokenSource(ctx, conf, state, authhandler.CmdAuthorizationHandler(state)).Token()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)