|
@@ -2,14 +2,12 @@ package oauth2
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
- "io/ioutil"
|
|
|
"log"
|
|
|
"net/http"
|
|
|
"strconv"
|
|
|
"time"
|
|
|
|
|
|
"golang.org/x/oauth2"
|
|
|
- "golang.org/x/oauth2/google"
|
|
|
auth "imuslab.com/arozos/mod/auth"
|
|
|
syncdb "imuslab.com/arozos/mod/auth/oauth2/syncdb"
|
|
|
reg "imuslab.com/arozos/mod/auth/register"
|
|
@@ -27,17 +25,6 @@ type OauthHandler struct {
|
|
|
config *Config
|
|
|
}
|
|
|
|
|
|
-type GoogleField struct {
|
|
|
- ID string `json:"id"`
|
|
|
- Email string `json:"email"`
|
|
|
- VerifiedEmail bool `json:"verified_email"`
|
|
|
- Name string `json:"name"`
|
|
|
- GivenName string `json:"given_name"`
|
|
|
- FamilyName string `json:"family_name"`
|
|
|
- Picture string `json:"picture"`
|
|
|
- Locale string `json:"locale"`
|
|
|
-}
|
|
|
-
|
|
|
type Config struct {
|
|
|
Enabled bool `json:"enabled"`
|
|
|
IDP string `json:"idp"`
|
|
@@ -60,9 +47,8 @@ func NewOauthHandler(authAgent *auth.AuthAgent, register *reg.RegisterHandler, c
|
|
|
RedirectURL: readSingleConfig("redirecturl", coreDb) + "/system/auth/oauth/authorize",
|
|
|
ClientID: readSingleConfig("clientid", coreDb),
|
|
|
ClientSecret: readSingleConfig("clientsecret", coreDb),
|
|
|
- Scopes: []string{"https://www.googleapis.com/auth/userinfo.profile",
|
|
|
- "https://www.googleapis.com/auth/userinfo.email"},
|
|
|
- Endpoint: google.Endpoint,
|
|
|
+ Scopes: getScope(coreDb),
|
|
|
+ Endpoint: getEndpoint(coreDb),
|
|
|
},
|
|
|
DefaultUserGroup: readSingleConfig("defaultusergroup", coreDb),
|
|
|
ag: authAgent,
|
|
@@ -111,24 +97,19 @@ func (oh *OauthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- response, err := http.Get("https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + token.AccessToken)
|
|
|
-
|
|
|
- defer response.Body.Close()
|
|
|
- contents, err := ioutil.ReadAll(response.Body)
|
|
|
- var data GoogleField
|
|
|
- json.Unmarshal([]byte(contents), &data)
|
|
|
+ username, err := getUserInfo(token.AccessToken, oh.coredb)
|
|
|
|
|
|
- if !oh.ag.UserExists(data.Email) {
|
|
|
+ if !oh.ag.UserExists(username) {
|
|
|
//register user if not already exists
|
|
|
//random pwd to prevent ppl bypassing the OAuth handler
|
|
|
if oh.reg.AllowRegistry {
|
|
|
- http.Redirect(w, r, "/public/register/register.system?user="+data.Email, 302)
|
|
|
+ http.Redirect(w, r, "/public/register/register.system?user="+username, 302)
|
|
|
} else {
|
|
|
sendTextResponse(w, "You are not allowed to register in this system. <a href=\"/\">Back</a>")
|
|
|
}
|
|
|
} else {
|
|
|
- log.Println(data.Email + " logged in via OAuth.")
|
|
|
- oh.ag.LoginUserByRequest(w, r, data.Email, true)
|
|
|
+ log.Println(username + " logged in via OAuth.")
|
|
|
+ oh.ag.LoginUserByRequest(w, r, username, true)
|
|
|
//clear the cooke
|
|
|
oh.addCookie(w, "uuid_login", "-invaild-", -1)
|
|
|
//read the value from db and delete it from db
|
|
@@ -219,9 +200,8 @@ func (oh *OauthHandler) WriteConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
RedirectURL: oh.readSingleConfig("redirecturl") + "/system/auth/oauth/authorize",
|
|
|
ClientID: oh.readSingleConfig("clientid"),
|
|
|
ClientSecret: oh.readSingleConfig("clientsecret"),
|
|
|
- Scopes: []string{"https://www.googleapis.com/auth/userinfo.profile",
|
|
|
- "https://www.googleapis.com/auth/userinfo.email"},
|
|
|
- Endpoint: google.Endpoint,
|
|
|
+ Scopes: getScope(oh.coredb),
|
|
|
+ Endpoint: getEndpoint(oh.coredb),
|
|
|
}
|
|
|
|
|
|
sendOK(w)
|