|
@@ -15,12 +15,17 @@ import (
|
|
|
|
|
|
var google_functions = map[string]interface{}{
|
|
var google_functions = map[string]interface{}{
|
|
init_provider: initGoogle,
|
|
init_provider: initGoogle,
|
|
- get_user_info: getUserInfo,
|
|
|
|
|
|
+ get_user_info: getGoogleUserInfo,
|
|
handle_callback: handleGoogleCallback,
|
|
handle_callback: handleGoogleCallback,
|
|
handle_login: handleGoogleLogin,
|
|
handle_login: handleGoogleLogin,
|
|
verify_user: verifyGoogleUser,
|
|
verify_user: verifyGoogleUser,
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+type googleOauthUser struct {
|
|
|
|
+ Email string `json:"email" bson:"email"`
|
|
|
|
+ AccessToken string `json:"accesstoken" bson:"accesstoken"`
|
|
|
|
+}
|
|
|
|
+
|
|
// == handle google authentication here ==
|
|
// == handle google authentication here ==
|
|
|
|
|
|
func initGoogle(redirectURL string, clientID string, clientSecret string) {
|
|
func initGoogle(redirectURL string, clientID string, clientSecret string) {
|
|
@@ -37,6 +42,7 @@ 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() != "" {
|
|
if auth_provider == nil && servercfg.GetFrontendURL() != "" {
|
|
http.Redirect(w, r, servercfg.GetFrontendURL()+"?oauth=callback-error", http.StatusTemporaryRedirect)
|
|
http.Redirect(w, r, servercfg.GetFrontendURL()+"?oauth=callback-error", http.StatusTemporaryRedirect)
|
|
|
|
+ return
|
|
} else if auth_provider == nil {
|
|
} 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"))
|
|
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
|
|
return
|
|
@@ -47,9 +53,9 @@ func handleGoogleLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
|
|
func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
- var content, err = getUserInfo(r.FormValue("state"), r.FormValue("code"))
|
|
|
|
|
|
+ var content, err = getGoogleUserInfo(r.FormValue("state"), r.FormValue("code"))
|
|
if err != nil {
|
|
if err != nil {
|
|
- fmt.Println(err.Error())
|
|
|
|
|
|
+ logic.Log("error when getting user info from google: "+err.Error(), 1)
|
|
http.Redirect(w, r, servercfg.GetFrontendURL()+"?oauth=callback-error", http.StatusTemporaryRedirect)
|
|
http.Redirect(w, r, servercfg.GetFrontendURL()+"?oauth=callback-error", http.StatusTemporaryRedirect)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -79,7 +85,7 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
|
|
http.Redirect(w, r, servercfg.GetFrontendURL()+"?login="+jwt+"&email="+content.Email, http.StatusPermanentRedirect)
|
|
http.Redirect(w, r, servercfg.GetFrontendURL()+"?login="+jwt+"&email="+content.Email, http.StatusPermanentRedirect)
|
|
}
|
|
}
|
|
|
|
|
|
-func getUserInfo(state string, code string) (*OauthUser, error) {
|
|
|
|
|
|
+func getGoogleUserInfo(state string, code string) (*googleOauthUser, error) {
|
|
if state != oauth_state_string {
|
|
if state != oauth_state_string {
|
|
return nil, fmt.Errorf("invalid oauth state")
|
|
return nil, fmt.Errorf("invalid oauth state")
|
|
}
|
|
}
|
|
@@ -101,7 +107,7 @@ func getUserInfo(state string, code string) (*OauthUser, error) {
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed reading response body: %s", err.Error())
|
|
return nil, fmt.Errorf("failed reading response body: %s", err.Error())
|
|
}
|
|
}
|
|
- var userInfo = &OauthUser{}
|
|
|
|
|
|
+ var userInfo = &googleOauthUser{}
|
|
if err = json.Unmarshal(contents, userInfo); err != nil {
|
|
if err = json.Unmarshal(contents, userInfo); err != nil {
|
|
return nil, fmt.Errorf("failed parsing email from response data: %s", err.Error())
|
|
return nil, fmt.Errorf("failed parsing email from response data: %s", err.Error())
|
|
}
|
|
}
|