google oauth implmented

This commit is contained in:
0xdcarns
2021-10-22 09:47:29 -04:00
parent 7939e5968f
commit 9c5703e28c
3 changed files with 23 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
package auth package auth
import ( import (
"encoding/base64"
"encoding/json" "encoding/json"
"net/http" "net/http"
@@ -56,6 +57,10 @@ func InitializeAuthProvider() string {
if err != nil { if err != nil {
return "" return ""
} }
var currentFrontendURL = servercfg.GetFrontendURL()
if currentFrontendURL == "" {
return ""
}
var authInfo = servercfg.GetAuthProviderInfo() var authInfo = servercfg.GetAuthProviderInfo()
functions[init_provider].(func(string, string, string))(servercfg.GetAPIConnString()+"/api/oauth/callback", authInfo[1], authInfo[2]) functions[init_provider].(func(string, string, string))(servercfg.GetAPIConnString()+"/api/oauth/callback", authInfo[1], authInfo[2])
return authInfo[0] return authInfo[0]
@@ -118,8 +123,9 @@ func fetchPassValue(newValue string) (string, error) {
type valueHolder struct { type valueHolder struct {
Value string `json:"value" bson:"value"` Value string `json:"value" bson:"value"`
} }
var b64NewValue = base64.StdEncoding.EncodeToString([]byte(newValue))
var newValueHolder = &valueHolder{ var newValueHolder = &valueHolder{
Value: newValue, Value: b64NewValue,
} }
var data, marshalErr = json.Marshal(newValueHolder) var data, marshalErr = json.Marshal(newValueHolder)
if marshalErr != nil { if marshalErr != nil {
@@ -134,5 +140,11 @@ func fetchPassValue(newValue string) (string, error) {
if unmarshErr != nil { if unmarshErr != nil {
return "", unmarshErr 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
} }

View File

@@ -35,6 +35,12 @@ func initGoogle(redirectURL string, clientID string, clientSecret string) {
func handleGoogleLogin(w http.ResponseWriter, r *http.Request) { func handleGoogleLogin(w http.ResponseWriter, r *http.Request) {
oauth_state_string = logic.RandomString(16) 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) var url = auth_provider.AuthCodeURL(oauth_state_string)
http.Redirect(w, r, url, http.StatusTemporaryRedirect) 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 { func verifyGoogleUser(token *oauth2.Token) bool {
if token.Valid() { return token.Valid()
var err error
_, err = http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
return err == nil
}
return false
} }

View File

@@ -47,6 +47,8 @@ func initialize() { // Client Mode Prereq Check
var authProvider = auth.InitializeAuthProvider() var authProvider = auth.InitializeAuthProvider()
if authProvider != "" { if authProvider != "" {
logic.Log("OAuth provider, "+authProvider+", initialized", 0) 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" { if servercfg.IsClientMode() != "off" {