|
@@ -33,6 +33,7 @@ import (
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
|
"github.com/gorilla/sessions"
|
|
|
|
|
|
|
|
+ "imuslab.com/arozos/mod/auth/authlogger"
|
|
db "imuslab.com/arozos/mod/database"
|
|
db "imuslab.com/arozos/mod/database"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -52,6 +53,9 @@ type AuthAgent struct {
|
|
//Autologin Related
|
|
//Autologin Related
|
|
AllowAutoLogin bool
|
|
AllowAutoLogin bool
|
|
autoLoginTokens []AutoLoginToken
|
|
autoLoginTokens []AutoLoginToken
|
|
|
|
+
|
|
|
|
+ //Logger
|
|
|
|
+ Logger *authlogger.Logger
|
|
}
|
|
}
|
|
|
|
|
|
type AuthEndpoints struct {
|
|
type AuthEndpoints struct {
|
|
@@ -75,6 +79,9 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
|
ticker := time.NewTicker(300 * time.Second)
|
|
ticker := time.NewTicker(300 * time.Second)
|
|
done := make(chan bool)
|
|
done := make(chan bool)
|
|
|
|
|
|
|
|
+ //Create a new logger for logging all login request
|
|
|
|
+ newLogger := authlogger.NewLogger()
|
|
|
|
+
|
|
//Create a new AuthAgent object
|
|
//Create a new AuthAgent object
|
|
newAuthAgent := AuthAgent{
|
|
newAuthAgent := AuthAgent{
|
|
SessionName: sessionName,
|
|
SessionName: sessionName,
|
|
@@ -87,6 +94,7 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
|
mutex: &sync.Mutex{},
|
|
mutex: &sync.Mutex{},
|
|
AllowAutoLogin: false,
|
|
AllowAutoLogin: false,
|
|
autoLoginTokens: []AutoLoginToken{},
|
|
autoLoginTokens: []AutoLoginToken{},
|
|
|
|
+ Logger: newLogger,
|
|
}
|
|
}
|
|
|
|
|
|
//Create a timer to listen to its token storage
|
|
//Create a timer to listen to its token storage
|
|
@@ -132,11 +140,14 @@ func (a *AuthAgent) RegisterPublicAPIs(ep AuthEndpoints) {
|
|
|
|
|
|
//Handle login request, require POST username and password
|
|
//Handle login request, require POST username and password
|
|
func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
+
|
|
//Get username from request using POST mode
|
|
//Get username from request using POST mode
|
|
username, err := mv(r, "username", true)
|
|
username, err := mv(r, "username", true)
|
|
if err != nil {
|
|
if err != nil {
|
|
//Username not defined
|
|
//Username not defined
|
|
log.Println("[System Auth] Someone trying to login with username: " + username)
|
|
log.Println("[System Auth] Someone trying to login with username: " + username)
|
|
|
|
+ //Write to log
|
|
|
|
+ a.Logger.LogAuth(r, false)
|
|
sendErrorResponse(w, "Username not defined or empty.")
|
|
sendErrorResponse(w, "Username not defined or empty.")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -145,6 +156,7 @@ func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
password, err := mv(r, "password", true)
|
|
password, err := mv(r, "password", true)
|
|
if err != nil {
|
|
if err != nil {
|
|
//Password not defined
|
|
//Password not defined
|
|
|
|
+ a.Logger.LogAuth(r, false)
|
|
sendErrorResponse(w, "Password not defined or empty.")
|
|
sendErrorResponse(w, "Password not defined or empty.")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -165,11 +177,13 @@ func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
a.LoginUserByRequest(w, r, username, rememberme)
|
|
a.LoginUserByRequest(w, r, username, rememberme)
|
|
//Print the login message to console
|
|
//Print the login message to console
|
|
log.Println(username + " logged in.")
|
|
log.Println(username + " logged in.")
|
|
|
|
+ a.Logger.LogAuth(r, true)
|
|
sendOK(w)
|
|
sendOK(w)
|
|
} else {
|
|
} else {
|
|
//Password incorrect
|
|
//Password incorrect
|
|
log.Println(username + " has entered an invalid username or password")
|
|
log.Println(username + " has entered an invalid username or password")
|
|
sendErrorResponse(w, "Invalid username or password")
|
|
sendErrorResponse(w, "Invalid username or password")
|
|
|
|
+ a.Logger.LogAuth(r, false)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|