Created
November 23, 2019 07:49
-
-
Save BNPrashanth/703c70cb7d72bf04b8e2f77bc3823452 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package services | |
import ( | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
"github.com/BNPrashanth/poc-go-oauth2/internal/logger" | |
"github.com/spf13/viper" | |
"golang.org/x/oauth2" | |
"golang.org/x/oauth2/google" | |
) | |
var ( | |
oauthConfGl = &oauth2.Config{ | |
ClientID: "", | |
ClientSecret: "", | |
RedirectURL: "http://localhost:9090/callback-gl", | |
Scopes: []string{"https://www.googleapis.com/auth/userinfo.email"}, | |
Endpoint: google.Endpoint, | |
} | |
oauthStateStringGl = "" | |
) | |
/* | |
InitializeOAuthGoogle Function | |
*/ | |
func InitializeOAuthGoogle() { | |
oauthConfGl.ClientID = viper.GetString("google.clientID") | |
oauthConfGl.ClientSecret = viper.GetString("google.clientSecret") | |
oauthStateStringGl = viper.GetString("oauthStateString") | |
} | |
/* | |
HandleGoogleLogin Function | |
*/ | |
func HandleGoogleLogin(w http.ResponseWriter, r *http.Request) { | |
HandleLogin(w, r, oauthConfGl, oauthStateStringGl) | |
} | |
/* | |
CallBackFromGoogle Function | |
*/ | |
func CallBackFromGoogle(w http.ResponseWriter, r *http.Request) { | |
logger.Log.Info("Callback-gl..") | |
state := r.FormValue("state") | |
logger.Log.Info(state) | |
if state != oauthStateStringGl { | |
logger.Log.Info("invalid oauth state, expected " + oauthStateStringGl + ", got " + state + "\n") | |
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | |
return | |
} | |
code := r.FormValue("code") | |
logger.Log.Info(code) | |
if code == "" { | |
logger.Log.Warn("Code not found..") | |
w.Write([]byte("Code Not Found to provide AccessToken..\n")) | |
reason := r.FormValue("error_reason") | |
if reason == "user_denied" { | |
w.Write([]byte("User has denied Permission..")) | |
} | |
// User has denied access.. | |
// http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | |
} else { | |
token, err := oauthConfGl.Exchange(oauth2.NoContext, code) | |
if err != nil { | |
logger.Log.Error("oauthConfGl.Exchange() failed with " + err.Error() + "\n") | |
return | |
} | |
logger.Log.Info("TOKEN>> AccessToken>> " + token.AccessToken) | |
logger.Log.Info("TOKEN>> Expiration Time>> " + token.Expiry.String()) | |
logger.Log.Info("TOKEN>> RefreshToken>> " + token.RefreshToken) | |
resp, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + url.QueryEscape(token.AccessToken)) | |
if err != nil { | |
logger.Log.Error("Get: " + err.Error() + "\n") | |
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | |
return | |
} | |
defer resp.Body.Close() | |
response, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
logger.Log.Error("ReadAll: " + err.Error() + "\n") | |
http.Redirect(w, r, "/", http.StatusTemporaryRedirect) | |
return | |
} | |
logger.Log.Info("parseResponseBody: " + string(response) + "\n") | |
w.Write([]byte("Hello, I'm protected\n")) | |
w.Write([]byte(string(response))) | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment