Browse Source

OAuth update

Syntax update
AY 4 years ago
parent
commit
d596102645
2 changed files with 21 additions and 7 deletions
  1. 19 5
      mod/auth/oauth2/oauth2.go
  2. 2 2
      mod/auth/oauth2/syncdb/syncdb.go

+ 19 - 5
mod/auth/oauth2/oauth2.go

@@ -63,9 +63,9 @@ func NewOauthHandler(authAgent *auth.AuthAgent, register *reg.RegisterHandler, c
 //HandleOauthLogin xxx
 func (oh *OauthHandler) HandleLogin(w http.ResponseWriter, r *http.Request) {
 	//add cookies
-	redirect, e := r.URL.Query()["redirect"]
+	redirect, err := r.URL.Query()["redirect"]
 	uuid := ""
-	if !e || len(redirect[0]) < 1 {
+	if !err || len(redirect[0]) < 1 {
 		uuid = oh.syncDb.Store("/")
 	} else {
 		uuid = oh.syncDb.Store(redirect[0])
@@ -85,13 +85,22 @@ func (oh *OauthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request)
 		return
 	}
 
-	state := r.FormValue("state")
+	state, err := mv(r, "state", true)
 	if state != uuid.Value {
 		sendTextResponse(w, "Invalid oauth state.")
 		return
 	}
+	if err != nil {
+		sendTextResponse(w, "Invalid state parameter.")
+		return
+	}
+
+	code, err := mv(r, "code", true)
+	if err != nil {
+		sendTextResponse(w, "Invalid state parameter.")
+		return
+	}
 
-	code := r.FormValue("code")
 	token, err := oh.googleOauthConfig.Exchange(oauth2.NoContext, code)
 	if err != nil {
 		sendTextResponse(w, "Code exchange failed.")
@@ -145,7 +154,12 @@ func (oh *OauthHandler) addCookie(w http.ResponseWriter, name, value string, ttl
 }
 
 func (oh *OauthHandler) ReadConfig(w http.ResponseWriter, r *http.Request) {
-	enabled, _ := strconv.ParseBool(oh.readSingleConfig("enabled"))
+	enabled, err := strconv.ParseBool(oh.readSingleConfig("enabled"))
+	if err != nil {
+		sendTextResponse(w, "Invalid config value [key=enabled].")
+		return
+	}
+
 	idp := oh.readSingleConfig("idp")
 	redirecturl := oh.readSingleConfig("redirecturl")
 	clientid := oh.readSingleConfig("clientid")

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

@@ -23,11 +23,11 @@ func NewSyncDB() *SyncDB {
 	//Put the newly craeted syncmap into the db object
 	newSyncDB := SyncDB{db: &newDB} //!!! USE POINTER HERE INSTEAD OF THE SYNC MAP ITSELF
 	//Return the pointer of the new SyncDB object
-	newSyncDB.AutoClening()
+	newSyncDB.AutoCleaning()
 	return &newSyncDB
 }
 
-func (p SyncDB) AutoClening() {
+func (p SyncDB) AutoCleaning() {
 	//create the routine for auto clean trash
 	go func() {
 		for {