From 9c5703e28c207130d9cbc5704a1aa620661e39b7 Mon Sep 17 00:00:00 2001 From: 0xdcarns Date: Fri, 22 Oct 2021 09:47:29 -0400 Subject: [PATCH] google oauth implmented --- auth/auth.go | 16 ++++++++++++++-- auth/google.go | 13 +++++++------ main.go | 2 ++ 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/auth/auth.go b/auth/auth.go index 41adbab0..eb16e73f 100644 --- a/auth/auth.go +++ b/auth/auth.go @@ -1,6 +1,7 @@ package auth import ( + "encoding/base64" "encoding/json" "net/http" @@ -56,6 +57,10 @@ func InitializeAuthProvider() string { if err != nil { return "" } + var currentFrontendURL = servercfg.GetFrontendURL() + if currentFrontendURL == "" { + return "" + } var authInfo = servercfg.GetAuthProviderInfo() functions[init_provider].(func(string, string, string))(servercfg.GetAPIConnString()+"/api/oauth/callback", authInfo[1], authInfo[2]) return authInfo[0] @@ -118,8 +123,9 @@ func fetchPassValue(newValue string) (string, error) { type valueHolder struct { Value string `json:"value" bson:"value"` } + var b64NewValue = base64.StdEncoding.EncodeToString([]byte(newValue)) var newValueHolder = &valueHolder{ - Value: newValue, + Value: b64NewValue, } var data, marshalErr = json.Marshal(newValueHolder) if marshalErr != nil { @@ -134,5 +140,11 @@ func fetchPassValue(newValue string) (string, error) { if unmarshErr != nil { return "", unmarshErr } - return newValueHolder.Value, nil + + var b64CurrentValue, b64Err = base64.StdEncoding.DecodeString(newValueHolder.Value) + if b64Err != nil { + logic.Log("could not decode pass", 0) + return "", nil + } + return string(b64CurrentValue), nil } diff --git a/auth/google.go b/auth/google.go index cb76f9fc..3c47bb3c 100644 --- a/auth/google.go +++ b/auth/google.go @@ -35,6 +35,12 @@ func initGoogle(redirectURL string, clientID string, clientSecret string) { func handleGoogleLogin(w http.ResponseWriter, r *http.Request) { oauth_state_string = logic.RandomString(16) + if auth_provider == nil && servercfg.GetFrontendURL() != "" { + http.Redirect(w, r, servercfg.GetFrontendURL()+"?oauth=callback-error", http.StatusTemporaryRedirect) + } else if auth_provider == nil { + fmt.Fprintf(w, "%s", []byte("no frontend URL was provided and an OAuth login was attempted\nplease reconfigure server to use OAuth or use basic credentials")) + return + } var url = auth_provider.AuthCodeURL(oauth_state_string) http.Redirect(w, r, url, http.StatusTemporaryRedirect) } @@ -104,10 +110,5 @@ func getUserInfo(state string, code string) (*OauthUser, error) { } func verifyGoogleUser(token *oauth2.Token) bool { - if token.Valid() { - var err error - _, err = http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken) - return err == nil - } - return false + return token.Valid() } diff --git a/main.go b/main.go index 8abced07..999adacf 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,8 @@ func initialize() { // Client Mode Prereq Check var authProvider = auth.InitializeAuthProvider() if authProvider != "" { logic.Log("OAuth provider, "+authProvider+", initialized", 0) + } else { + logic.Log("no OAuth provider found or not configured, continuing without OAuth", 0) } if servercfg.IsClientMode() != "off" {