From 2ae8725104ca95f89f8407712bccea812b28999c Mon Sep 17 00:00:00 2001 From: Maina Wycliffe Date: Thu, 5 Dec 2019 19:56:28 +0300 Subject: [PATCH] feat: add method to login without sharing --- oauth/oauth.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/oauth/oauth.go b/oauth/oauth.go index 79505bd..3ef6db6 100644 --- a/oauth/oauth.go +++ b/oauth/oauth.go @@ -1,6 +1,7 @@ package oauth import ( + "bufio" "context" "encoding/base64" "encoding/json" @@ -144,3 +145,25 @@ func LoginWithLocalhost() { log.Fatal(err) } } + +// LoginWithoutLocalhost login without localhost localhost server, suitable in +// environments without a GUI. The user enters the authorization code manually +func LoginWithoutLocalhost() { + googleOauthConfig := getGoogleOAuthConfig("") + oauthStateTracker := generateOauthStateTracker() + u := googleOauthConfig.AuthCodeURL(oauthStateTracker) + fmt.Printf("Visit this URL on any device to log in:\n\n%s\n\nWaiting for authentication...", u) + // open browser now + _ = browser.OpenURL(u) + reader := bufio.NewReader(os.Stdin) + fmt.Print("\n\nEnter the authorization code here: ") + code, _ := reader.ReadString('\n') + data, err := getUserDataFromGoogle(googleOauthConfig, code) + if err != nil { + log.Fatalln(err.Error()) + return + } + // todo: save credentials + // todo: return success message to the user + fmt.Fprintf(os.Stdout, "\n\nSuccess! Logged in as %s\n\n", data.Email) +}