From 8a926e1234bba3432adfb2b46a14ff40d0d0f96a Mon Sep 17 00:00:00 2001 From: Andy Zhao Date: Mon, 15 Mar 2021 13:45:25 -0700 Subject: [PATCH] authhandler: Rename to CmdAuthorizationHandler and add example_test.go --- ...ault_authhandler.go => cmd_authhandler.go} | 14 +---- authhandler/example_test.go | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+), 11 deletions(-) rename authhandler/{default_authhandler.go => cmd_authhandler.go} (68%) create mode 100644 authhandler/example_test.go diff --git a/authhandler/default_authhandler.go b/authhandler/cmd_authhandler.go similarity index 68% rename from authhandler/default_authhandler.go rename to authhandler/cmd_authhandler.go index 1989f31..450040b 100644 --- a/authhandler/default_authhandler.go +++ b/authhandler/cmd_authhandler.go @@ -8,7 +8,7 @@ import ( "fmt" ) -// DefaultAuthorizationHandler returns a command line auth handler +// 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. // @@ -19,18 +19,10 @@ import ( // 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 { +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: ") + 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 new file mode 100644 index 0000000..99456ef --- /dev/null +++ b/authhandler/example_test.go @@ -0,0 +1,56 @@ +// 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_test + +import ( + "context" + "fmt" + "net/http" + "net/http/httptest" + + "golang.org/x/oauth2" + "golang.org/x/oauth2/authhandler" +) + +func ExampleCmdAuthorizationHandler() { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(`{ + "access_token": "90d64460d14870c08c81352a05dedd3465940a7c", + "scope": "pubsub", + "token_type": "bearer", + "expires_in": 3600 + }`)) + })) + defer ts.Close() + + ctx := context.Background() + conf := &oauth2.Config{ + ClientID: "testClientID", + Scopes: []string{"pubsub"}, + Endpoint: oauth2.Endpoint{ + AuthURL: "testAuthCodeURL", + TokenURL: ts.URL, + }, + } + state := "unique_state" + + token, err := authhandler.TokenSource(ctx, conf, authhandler.CmdAuthorizationHandler(state), state).Token() + + if err != nil { + fmt.Println(err) + } + + fmt.Printf("AccessToken: %s", token.AccessToken) + + // Output: + // Go to the following link in your browser: + // + // testAuthCodeURL?client_id=testClientID&response_type=code&scope=pubsub&state=unique_state + // + // Enter authorization code: + // AccessToken: 90d64460d14870c08c81352a05dedd3465940a7c +}