ソースを参照

Update OAuth2.go

AY 4 年 前
コミット
308942376c
2 ファイル変更17 行追加2 行削除
  1. 14 2
      mod/auth/oauth2/oauth2.go
  2. 3 0
      mod/auth/oauth2/serviceSelector.go

+ 14 - 2
mod/auth/oauth2/oauth2.go

@@ -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))

+ 3 - 0
mod/auth/oauth2/serviceSelector.go

@@ -7,6 +7,7 @@ import (
 	db "imuslab.com/arozos/mod/database"
 )
 
+//getScope use to select the correct scope
 func getScope(coredb *db.Database) []string {
 	idp := readSingleConfig("idp", coredb)
 	if idp == "Google" {
@@ -19,6 +20,7 @@ func getScope(coredb *db.Database) []string {
 	return []string{}
 }
 
+//getEndpoint use to select the correct endpoint
 func getEndpoint(coredb *db.Database) oauth2.Endpoint {
 	idp := readSingleConfig("idp", coredb)
 	if idp == "Google" {
@@ -31,6 +33,7 @@ func getEndpoint(coredb *db.Database) oauth2.Endpoint {
 	return oauth2.Endpoint{}
 }
 
+//getUserinfo use to select the correct way to retrieve userinfo
 func getUserInfo(accessToken string, coredb *db.Database) (string, error) {
 	idp := readSingleConfig("idp", coredb)
 	if idp == "Google" {