Bläddra i källkod

Added working test oauth arch

Toby Chui 6 månader sedan
förälder
incheckning
01b72d8649
2 ändrade filer med 47 tillägg och 12 borttagningar
  1. 30 8
      mod/auth/sso/oauth2.go
  2. 17 4
      mod/auth/sso/users.go

+ 30 - 8
mod/auth/sso/oauth2.go

@@ -17,6 +17,7 @@ import (
 	"github.com/go-oauth2/oauth2/v4/server"
 	"github.com/go-oauth2/oauth2/v4/store"
 	"github.com/go-session/session"
+	"imuslab.com/zoraxy/mod/utils"
 )
 
 type OAuth2Server struct {
@@ -45,7 +46,7 @@ func NewOAuth2Server(config *SSOConfig, parent *SSOHandler) (*OAuth2Server, erro
 	clientStore.Set("alanyeung", &models.Client{
 		ID:     "alanyeung",
 		Secret: "password",
-		Domain: "localhost",
+		Domain: "localhost:8000",
 	})
 	manager.MapClientStorage(clientStore)
 
@@ -72,8 +73,8 @@ func NewOAuth2Server(config *SSOConfig, parent *SSOHandler) (*OAuth2Server, erro
 // Password handler, validate if the given username and password are correct
 func (oas *OAuth2Server) PasswordAuthorizationHandler(ctx context.Context, clientID, username, password string) (userID string, err error) {
 	fmt.Println(username, password)
-	if username == "test" && password == "test" {
-		userID = "test"
+	if username == "alanyeung" && password == "password" {
+		userID = "alanyeung"
 	}
 	return
 }
@@ -94,7 +95,7 @@ func (oas *OAuth2Server) UserAuthorizeHandler(w http.ResponseWriter, r *http.Req
 		store.Set("ReturnUri", r.Form)
 		store.Save()
 
-		w.Header().Set("Location", "/login")
+		w.Header().Set("Location", "/oauth2/login")
 		w.WriteHeader(http.StatusFound)
 		return
 	}
@@ -107,8 +108,8 @@ func (oas *OAuth2Server) UserAuthorizeHandler(w http.ResponseWriter, r *http.Req
 
 /* SSO Web Server Toggle Functions */
 func (oas *OAuth2Server) RegisterOauthEndpoints(primaryMux *http.ServeMux) {
-	primaryMux.HandleFunc("/oauth2/login", loginHandler)
-	primaryMux.HandleFunc("/oauth2/auth", authHandler)
+	primaryMux.HandleFunc("/oauth2/login", oas.loginHandler)
+	primaryMux.HandleFunc("/oauth2/auth", oas.authHandler)
 
 	primaryMux.HandleFunc("/oauth2/authorize", func(w http.ResponseWriter, r *http.Request) {
 		store, err := session.Start(r.Context(), w, r)
@@ -157,7 +158,7 @@ func (oas *OAuth2Server) RegisterOauthEndpoints(primaryMux *http.ServeMux) {
 	})
 }
 
-func loginHandler(w http.ResponseWriter, r *http.Request) {
+func (oas *OAuth2Server) loginHandler(w http.ResponseWriter, r *http.Request) {
 	store, err := session.Start(r.Context(), w, r)
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)
@@ -171,6 +172,27 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
 				return
 			}
 		}
+
+		//Load username and password from form post
+		username, err := utils.PostPara(r, "username")
+		if err != nil {
+			w.Write([]byte("Invalid username or password"))
+			return
+		}
+
+		password, err := utils.PostPara(r, "password")
+		if err != nil {
+			w.Write([]byte("Invalid username or password"))
+			return
+		}
+
+		//Validate the user
+		if !oas.parent.ValidateUsernameAndPassword(username, password) {
+			//Wrong password
+			w.Write([]byte("Invalid username or password"))
+			return
+		}
+
 		store.Set("ZoraxySSO", r.Form.Get("username"))
 		store.Save()
 
@@ -182,7 +204,7 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
 	w.Write(loginHtml)
 }
 
-func authHandler(w http.ResponseWriter, r *http.Request) {
+func (oas *OAuth2Server) authHandler(w http.ResponseWriter, r *http.Request) {
 	store, err := session.Start(context.TODO(), w, r)
 	if err != nil {
 		http.Error(w, err.Error(), http.StatusInternalServerError)

+ 17 - 4
mod/auth/sso/users.go

@@ -36,10 +36,7 @@ func (s *SSOHandler) SSO_UserExists(userid string) bool {
 	//Check if the user exists in the database
 	var userEntry UserEntry
 	err := s.Config.Database.Read("sso_users", userid, &userEntry)
-	if err != nil {
-		return false
-	}
-	return true
+	return err == nil
 }
 
 func (s *SSOHandler) SSO_GetUser(userid string) (UserEntry, error) {
@@ -53,6 +50,22 @@ func (s *SSOHandler) SSO_GetUser(userid string) (UserEntry, error) {
 	return userEntry, nil
 }
 
+// Validate the username and password
+func (s *SSOHandler) ValidateUsernameAndPassword(username string, password string) bool {
+	//Validate the username and password
+	var userEntry UserEntry
+	err := s.Config.Database.Read("sso_users", username, &userEntry)
+	if err != nil {
+		return false
+	}
+
+	//TODO: Remove after testing
+	if (username == "test") && (password == "test") {
+		return true
+	}
+	return userEntry.VerifyPassword(password)
+}
+
 func (s *UserEntry) VerifyPassword(password string) bool {
 	return s.PasswordHash == auth.Hash(password)
 }