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
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
}

View file

@ -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()
}

View file

@ -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" {