2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
func googleCallbackHandler(w http.ResponseWriter, r *http.Request) {
|
2018-06-20 11:29:55 +08:00
|
|
|
commenterToken := r.FormValue("state")
|
2018-05-27 22:40:42 +08:00
|
|
|
code := r.FormValue("code")
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
_, err := commenterGetByCommenterToken(commenterToken)
|
|
|
|
if err != nil && err != errorNoSuchToken {
|
2018-05-27 22:40:42 +08:00
|
|
|
fmt.Fprintf(w, "Error: %s\n", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := googleConfig.Exchange(oauth2.NoContext, code)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "Error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
contents, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "Error: %s", errorCannotReadResponse.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := make(map[string]interface{})
|
|
|
|
if err := json.Unmarshal(contents, &user); err != nil {
|
|
|
|
fmt.Fprintf(w, "Error: %s", errorInternal.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-31 10:25:09 +08:00
|
|
|
if user["email"] == nil {
|
|
|
|
fmt.Fprintf(w, "Error: no email address returned by Github")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
email := user["email"].(string)
|
|
|
|
|
|
|
|
c, err := commenterGetByEmail("google", email)
|
2018-06-07 15:44:19 +08:00
|
|
|
if err != nil && err != errorNoSuchCommenter {
|
2018-05-27 22:40:42 +08:00
|
|
|
fmt.Fprintf(w, "Error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-08-12 06:52:56 +08:00
|
|
|
name := user["name"].(string)
|
|
|
|
|
|
|
|
link := "undefined"
|
|
|
|
if user["link"] != nil {
|
|
|
|
link = user["link"].(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
photo := "undefined"
|
|
|
|
if user["picture"] != nil {
|
|
|
|
photo = user["picture"].(string)
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
var commenterHex string
|
|
|
|
|
2018-06-07 15:43:02 +08:00
|
|
|
if err == errorNoSuchCommenter {
|
2019-08-12 06:52:56 +08:00
|
|
|
commenterHex, err = commenterNew(email, name, link, photo, "google", "")
|
2018-05-27 22:40:42 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(w, "Error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
2018-06-07 15:44:19 +08:00
|
|
|
} else {
|
2019-08-12 06:52:56 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-07 15:44:19 +08:00
|
|
|
commenterHex = c.CommenterHex
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
if err := commenterSessionUpdate(commenterToken, commenterHex); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
fmt.Fprintf(w, "Error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "<html><script>window.parent.close()</script></html>")
|
|
|
|
}
|