2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"golang.org/x/oauth2"
|
|
|
|
"golang.org/x/oauth2/google"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var googleConfig *oauth2.Config
|
|
|
|
|
|
|
|
func googleOauthConfigure() error {
|
|
|
|
googleConfig = nil
|
|
|
|
if os.Getenv("GOOGLE_KEY") == "" && os.Getenv("GOOGLE_SECRET") == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("GOOGLE_KEY") == "" {
|
2018-06-13 02:52:06 +08:00
|
|
|
logger.Errorf("COMMENTO_GOOGLE_KEY not configured, but COMMENTO_GOOGLE_SECRET is set")
|
2018-05-27 22:40:42 +08:00
|
|
|
return errorOauthMisconfigured
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("GOOGLE_SECRET") == "" {
|
2018-06-13 02:52:06 +08:00
|
|
|
logger.Errorf("COMMENTO_GOOGLE_SECRET not configured, but COMMENTO_GOOGLE_KEY is set")
|
2018-05-27 22:40:42 +08:00
|
|
|
return errorOauthMisconfigured
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Infof("loading Google OAuth config")
|
|
|
|
|
|
|
|
googleConfig = &oauth2.Config{
|
2018-06-07 01:21:59 +08:00
|
|
|
RedirectURL: os.Getenv("ORIGIN") + "/api/oauth/google/callback",
|
2018-05-27 22:40:42 +08:00
|
|
|
ClientID: os.Getenv("GOOGLE_KEY"),
|
|
|
|
ClientSecret: os.Getenv("GOOGLE_SECRET"),
|
|
|
|
Scopes: []string{
|
|
|
|
"https://www.googleapis.com/auth/userinfo.profile",
|
|
|
|
"https://www.googleapis.com/auth/userinfo.email",
|
|
|
|
},
|
|
|
|
Endpoint: google.Endpoint,
|
|
|
|
}
|
|
|
|
|
2019-04-20 07:03:34 +08:00
|
|
|
googleConfigured = true
|
2018-06-11 01:15:56 +08:00
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
return nil
|
|
|
|
}
|