api: update commenter information on new login

This commit is contained in:
Adhityaa Chandrasekar 2019-08-11 15:52:56 -07:00
parent 696361df4a
commit 9538c9036e
6 changed files with 68 additions and 13 deletions

30
api/commenter_update.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
)
func commenterUpdate(commenterHex string, email string, name string, link string, photo string, provider string) error {
if email == "" || name == "" || link == "" || photo == "" || provider == "" {
return errorMissingField
}
// See utils_sanitise.go's documentation on isHttpsUrl. This is not a URL
// validator, just an XSS preventor.
// TODO: reject URLs instead of malforming them.
if link != "undefined" && !isHttpsUrl(link) {
link = "https://" + link
}
statement := `
UPDATE commenters
SET email = $3, name = $4, link = $5, photo = $6
WHERE commenterHex = $1 and provider = $2;
`
_, err := db.Exec(statement, commenterHex, provider, email, name, link, photo)
if err != nil {
logger.Errorf("cannot update commenter: %v", err)
return errorInternal
}
return nil
}

View File

@ -109,7 +109,6 @@ func githubCallbackHandler(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?
if err == errorNoSuchCommenter { if err == errorNoSuchCommenter {
commenterHex, err = commenterNew(email, name, link, photo, "github", "") commenterHex, err = commenterNew(email, name, link, photo, "github", "")
if err != nil { if err != nil {
@ -117,6 +116,11 @@ func githubCallbackHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
} else { } else {
if err = commenterUpdate(c.CommenterHex, email, name, link, photo, "github"); err != nil {
logger.Warningf("cannot update commenter: %s", err)
// not a serious enough to exit with an error
}
commenterHex = c.CommenterHex commenterHex = c.CommenterHex
} }

View File

@ -76,7 +76,6 @@ func gitlabCallbackHandler(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?
if err == errorNoSuchCommenter { if err == errorNoSuchCommenter {
commenterHex, err = commenterNew(email, name, link, photo, "gitlab", "") commenterHex, err = commenterNew(email, name, link, photo, "gitlab", "")
if err != nil { if err != nil {
@ -84,6 +83,11 @@ func gitlabCallbackHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
} else { } else {
if err = commenterUpdate(c.CommenterHex, email, name, link, photo, "gitlab"); err != nil {
logger.Warningf("cannot update commenter: %s", err)
// not a serious enough to exit with an error
}
commenterHex = c.CommenterHex commenterHex = c.CommenterHex
} }

View File

@ -52,23 +52,32 @@ func googleCallbackHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
name := user["name"].(string)
link := "undefined"
if user["link"] != nil {
link = user["link"].(string)
}
photo := "undefined"
if user["picture"] != nil {
photo = user["picture"].(string)
}
var commenterHex string var commenterHex string
// TODO: in case of returning users, update the information we have on record?
if err == errorNoSuchCommenter { if err == errorNoSuchCommenter {
var link string commenterHex, err = commenterNew(email, name, link, photo, "google", "")
if val, ok := user["link"]; ok {
link = val.(string)
} else {
link = "undefined"
}
commenterHex, err = commenterNew(email, user["name"].(string), link, user["picture"].(string), "google", "")
if err != nil { if err != nil {
fmt.Fprintf(w, "Error: %s", err.Error()) fmt.Fprintf(w, "Error: %s", err.Error())
return return
} }
} else { } else {
if err = commenterUpdate(c.CommenterHex, email, name, link, photo, "google"); err != nil {
logger.Warningf("cannot update commenter: %s", err)
// not a serious enough to exit with an error
}
commenterHex = c.CommenterHex commenterHex = c.CommenterHex
} }

View File

@ -96,7 +96,6 @@ func ssoCallbackHandler(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?
if err == errorNoSuchCommenter { if err == errorNoSuchCommenter {
commenterHex, err = commenterNew(payload.Email, payload.Name, payload.Link, payload.Photo, "sso:"+domain, "") commenterHex, err = commenterNew(payload.Email, payload.Name, payload.Link, payload.Photo, "sso:"+domain, "")
if err != nil { if err != nil {
@ -104,6 +103,11 @@ func ssoCallbackHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
} else { } else {
if err = commenterUpdate(c.CommenterHex, payload.Email, payload.Name, payload.Link, payload.Photo, "sso:"+domain); err != nil {
logger.Warningf("cannot update commenter: %s", err)
// not a serious enough to exit with an error
}
commenterHex = c.CommenterHex commenterHex = c.CommenterHex
} }

View File

@ -88,7 +88,6 @@ func twitterCallbackHandler(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?
if err == errorNoSuchCommenter { if err == errorNoSuchCommenter {
commenterHex, err = commenterNew(email, name, link, photo, "twitter", "") commenterHex, err = commenterNew(email, name, link, photo, "twitter", "")
if err != nil { if err != nil {
@ -96,6 +95,11 @@ func twitterCallbackHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
} else { } else {
if err = commenterUpdate(c.CommenterHex, email, name, link, photo, "twitter"); err != nil {
logger.Warningf("cannot update commenter: %s", err)
// not a serious enough to exit with an error
}
commenterHex = c.CommenterHex commenterHex = c.CommenterHex
} }