From fcaf0780fa2407dbf4f36e9b9fe090049f56d401 Mon Sep 17 00:00:00 2001 From: Andy Zhao Date: Thu, 18 Mar 2021 09:14:52 -0700 Subject: [PATCH] authhandler: Remove CmdAuthorizationHandler and use it as example instead --- authhandler/cmd_authhandler.go | 30 ------------------------------ authhandler/example_test.go | 25 +++++++++++++++++++++++-- 2 files changed, 23 insertions(+), 32 deletions(-) delete mode 100644 authhandler/cmd_authhandler.go diff --git a/authhandler/cmd_authhandler.go b/authhandler/cmd_authhandler.go deleted file mode 100644 index 450040b..0000000 --- a/authhandler/cmd_authhandler.go +++ /dev/null @@ -1,30 +0,0 @@ -// 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" -) - -// CmdAuthorizationHandler 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. -func CmdAuthorizationHandler(state string) AuthorizationHandler { - return func(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 - } -} diff --git a/authhandler/example_test.go b/authhandler/example_test.go index ef4cf31..f450590 100644 --- a/authhandler/example_test.go +++ b/authhandler/example_test.go @@ -14,7 +14,28 @@ import ( "golang.org/x/oauth2/authhandler" ) -func ExampleCmdAuthorizationHandler() { +// CmdAuthorizationHandler returns a command line auth handler that prints +// the auth URL to the console and prompts the user to authorize in the +// browser and paste the auth code back via stdin. +// +// Per the 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. +func CmdAuthorizationHandler(state string) authhandler.AuthorizationHandler { + return func(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 + } +} + +func Example() { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.ParseForm() w.Header().Set("Content-Type", "application/json") @@ -38,7 +59,7 @@ func ExampleCmdAuthorizationHandler() { } state := "unique_state" - token, err := authhandler.TokenSource(ctx, conf, state, authhandler.CmdAuthorizationHandler(state)).Token() + token, err := authhandler.TokenSource(ctx, conf, state, CmdAuthorizationHandler(state)).Token() if err != nil { fmt.Println(err)