forked from remote/oauth2
authhandler: Add support for 3-legged-OAuth
Added authhandler.go, which implements a TokenSource to support "three-legged OAuth 2.0" via a custom AuthorizationHandler. Added default_authhandler.go to provide a command line implementation for AuthorizationHandler.
This commit is contained in:
42
authhandler/default_authhandler.go
Normal file
42
authhandler/default_authhandler.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// Copyright 2021 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package authhandler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// DefaultAuthorizationHandler returns a command line auth handler
|
||||
// that prints the auth URL on the console and prompts the user to
|
||||
// authorize in the browser and paste the auth code back via stdin.
|
||||
//
|
||||
// Per OAuth protocol, a unique "state" string should be sent and verified
|
||||
// before exchanging auth code for OAuth token to prevent CSRF attacks.
|
||||
//
|
||||
// For convenience, this handler returns a pre-configured state
|
||||
// instead of asking the user to additionally paste the state from
|
||||
// the auth response. In order for this to work, the state
|
||||
// configured here must match the state used in authCodeURL.
|
||||
//
|
||||
// Usage example:
|
||||
//
|
||||
// state := uuid.New().String()
|
||||
// tokenSource:= authhandler.TokenSource(ctx, conf
|
||||
// authhandler.DefaultAuthorizationHandler(state), state)
|
||||
// pubsubService, err := pubsub.NewService(ctx,
|
||||
// option.WithTokenSource(tokenSource))
|
||||
func DefaultAuthorizationHandler(state string) AuthorizationHandler {
|
||||
return func(authCodeURL string) (string, string, error) {
|
||||
return defaultAuthorizationHandlerHelper(state, authCodeURL)
|
||||
}
|
||||
}
|
||||
|
||||
func defaultAuthorizationHandlerHelper(state string, authCodeURL string) (string, string, error) {
|
||||
fmt.Printf("Go to the following link in your browser:\n\n %s\n\n", authCodeURL)
|
||||
fmt.Println("Enter authorization code: ")
|
||||
var code string
|
||||
fmt.Scanln(&code)
|
||||
return code, state, nil
|
||||
}
|
||||
Reference in New Issue
Block a user