oauth_google_callback.go: use error to detect auth

This commit is contained in:
Adhityaa 2018-06-07 13:13:02 +05:30
parent 36f281ec44
commit a066062f8b
2 changed files with 12 additions and 16 deletions

View File

@ -12,17 +12,12 @@ func googleCallbackHandler(w http.ResponseWriter, r *http.Request) {
session := r.FormValue("state") session := r.FormValue("state")
code := r.FormValue("code") code := r.FormValue("code")
cs, err := commenterSessionGet(session) _, err := commenterSessionGet(session)
if err != nil { if err != nil && err != errorNoSuchSession {
fmt.Fprintf(w, "Error: %s\n", err.Error()) fmt.Fprintf(w, "Error: %s\n", err.Error())
return return
} }
if cs.Session != "none" {
fmt.Fprintf(w, "Error: %v", errorSessionAlreadyInUse.Error())
return
}
token, err := googleConfig.Exchange(oauth2.NoContext, code) token, err := googleConfig.Exchange(oauth2.NoContext, code)
if err != nil { if err != nil {
fmt.Fprintf(w, "Error: %s", err.Error()) fmt.Fprintf(w, "Error: %s", err.Error())
@ -53,12 +48,12 @@ func googleCallbackHandler(w http.ResponseWriter, r *http.Request) {
var commenterHex string var commenterHex string
// TODO: in case of returning users, update the information we have on record? // TODO: in case of returning users, update the information we have on record?
if !exists { if err == errorNoSuchCommenter {
var email string var email string
if _, ok := user["email"]; ok { if _, ok := user["email"]; ok {
email = user["email"].(string) email = user["email"].(string)
} else { } else {
fmt.Fprintf(w, "error: %s", errorInvalidEmail.Error()) fmt.Fprintf(w, "Error: %s", errorInvalidEmail.Error())
return return
} }

View File

@ -6,16 +6,17 @@ import (
) )
func googleRedirectHandler(w http.ResponseWriter, r *http.Request) { func googleRedirectHandler(w http.ResponseWriter, r *http.Request) {
session := r.FormValue("session") if googleConfig == nil {
logger.Errorf("google oauth access attempt without configuration")
c, err := commenterGetBySession(session) fmt.Fprintf(w, "error: this website has not configured Google OAuth")
if err != nil {
fmt.Fprintf(w, "error: %s\n", err.Error())
return return
} }
if c.CommenterHex != "none" { session := r.FormValue("session")
fmt.Fprintf(w, "error: that session is already in use\n")
_, err := commenterGetBySession(session)
if err != nil && err != errorNoSuchSession {
fmt.Fprintf(w, "error: %s\n", err.Error())
return return
} }