|
@@ -64,12 +64,14 @@ func NewOauthHandler(authAgent *auth.AuthAgent, register *reg.RegisterHandler, c
|
|
|
func (oh *OauthHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
//add cookies
|
|
|
redirect, err := mv(r, "redirect", false)
|
|
|
+ //store the redirect url to the sync map
|
|
|
uuid := ""
|
|
|
if err != nil {
|
|
|
uuid = oh.syncDb.Store("/")
|
|
|
} else {
|
|
|
uuid = oh.syncDb.Store(redirect)
|
|
|
}
|
|
|
+ //store the key to client
|
|
|
oh.addCookie(w, "uuid_login", uuid, 30*time.Minute)
|
|
|
//handle redirect
|
|
|
url := oh.googleOauthConfig.AuthCodeURL(uuid)
|
|
@@ -101,12 +103,14 @@ func (oh *OauthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ //exchange the infromation to get code
|
|
|
token, err := oh.googleOauthConfig.Exchange(oauth2.NoContext, code)
|
|
|
if err != nil {
|
|
|
sendTextResponse(w, "Code exchange failed.")
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ //get user info
|
|
|
username, err := getUserInfo(token.AccessToken, oh.coredb)
|
|
|
if err != nil {
|
|
|
oh.ag.Logger.LogAuth(r, false)
|
|
@@ -114,10 +118,14 @@ func (oh *OauthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ r.Form.Add("username", username) // to address Tobychui's log auth function
|
|
|
+
|
|
|
if !oh.ag.UserExists(username) {
|
|
|
//register user if not already exists
|
|
|
- //random pwd to prevent ppl bypassing the OAuth handler
|
|
|
+ //if registration is closed, return error message.
|
|
|
+ //also makr the login as fail.
|
|
|
if oh.reg.AllowRegistry {
|
|
|
+ oh.ag.Logger.LogAuth(r, false)
|
|
|
http.Redirect(w, r, "/public/register/register.system?user="+username, 302)
|
|
|
} else {
|
|
|
oh.ag.Logger.LogAuth(r, false)
|
|
@@ -137,6 +145,7 @@ func (oh *OauthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//CheckOAuth check if oauth is enabled
|
|
|
func (oh *OauthHandler) CheckOAuth(w http.ResponseWriter, r *http.Request) {
|
|
|
enabled := oh.readSingleConfig("enabled")
|
|
|
if enabled == "" {
|
|
@@ -178,7 +187,10 @@ func (oh *OauthHandler) ReadConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
DefaultUserGroup: defaultusergroup,
|
|
|
})
|
|
|
if err != nil {
|
|
|
- empty, _ := json.Marshal(Config{})
|
|
|
+ empty, err := json.Marshal(Config{})
|
|
|
+ if err != nil {
|
|
|
+ sendErrorResponse(w, "Error while marshalling config")
|
|
|
+ }
|
|
|
sendJSONResponse(w, string(empty))
|
|
|
}
|
|
|
sendJSONResponse(w, string(config))
|