2020-05-05 19:55:31 -04:00
|
|
|
// Copyright 2020 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 google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-05 01:27:52 -04:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-05-05 19:55:31 -04:00
|
|
|
)
|
|
|
|
|
2020-06-05 01:27:52 -04:00
|
|
|
// RandomAuthorizationState generates a state via UUID generator.
|
|
|
|
func RandomAuthorizationState() string {
|
|
|
|
return uuid.New().String()
|
|
|
|
}
|
2020-05-05 19:55:31 -04:00
|
|
|
|
2020-06-05 01:27:52 -04:00
|
|
|
// DefaultAuthorizationHandler returns a command line auth handler
|
2020-05-05 19:55:31 -04:00
|
|
|
// 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.
|
2020-06-05 01:27:52 -04:00
|
|
|
//
|
|
|
|
// 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 should match the one in the oauth2 AuthTokenURL.
|
|
|
|
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: ")
|
2020-05-05 19:55:31 -04:00
|
|
|
var code string
|
|
|
|
fmt.Scanln(&code)
|
2020-06-05 01:27:52 -04:00
|
|
|
return code, state, nil
|
2020-05-05 19:55:31 -04:00
|
|
|
}
|