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