|
@@ -33,6 +33,7 @@ import (
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
|
|
"imuslab.com/arozos/mod/auth/authlogger"
|
|
|
+ "imuslab.com/arozos/mod/auth/blacklist"
|
|
|
db "imuslab.com/arozos/mod/database"
|
|
|
)
|
|
|
|
|
@@ -53,6 +54,9 @@ type AuthAgent struct {
|
|
|
AllowAutoLogin bool
|
|
|
autoLoginTokens []*AutoLoginToken
|
|
|
|
|
|
+ //IP Blacklist manager
|
|
|
+ BlacklistManager *blacklist.BlackList
|
|
|
+
|
|
|
//Logger
|
|
|
Logger *authlogger.Logger
|
|
|
}
|
|
@@ -78,6 +82,9 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
|
|
ticker := time.NewTicker(300 * time.Second)
|
|
|
done := make(chan bool)
|
|
|
|
|
|
+ //Create a new blacklist manager
|
|
|
+ thisBlacklistManager := blacklist.NewBlacklistManager(sysdb)
|
|
|
+
|
|
|
//Create a new logger for logging all login request
|
|
|
newLogger, err := authlogger.NewLogger()
|
|
|
if err != nil {
|
|
@@ -94,9 +101,14 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
|
|
ExpireTime: 120,
|
|
|
terminateTokenListener: done,
|
|
|
mutex: &sync.Mutex{},
|
|
|
- AllowAutoLogin: false,
|
|
|
- autoLoginTokens: []*AutoLoginToken{},
|
|
|
- Logger: newLogger,
|
|
|
+
|
|
|
+ //Auto login management
|
|
|
+ AllowAutoLogin: false,
|
|
|
+ autoLoginTokens: []*AutoLoginToken{},
|
|
|
+
|
|
|
+ //Blacklist management
|
|
|
+ BlacklistManager: thisBlacklistManager,
|
|
|
+ Logger: newLogger,
|
|
|
}
|
|
|
|
|
|
//Create a timer to listen to its token storage
|
|
@@ -135,15 +147,6 @@ func (a *AuthAgent) HandleCheckAuth(w http.ResponseWriter, r *http.Request, hand
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-//Register APIs that requires public access
|
|
|
-func (a *AuthAgent) RegisterPublicAPIs(ep AuthEndpoints) {
|
|
|
- http.HandleFunc(ep.Login, a.HandleLogin)
|
|
|
- http.HandleFunc(ep.Logout, a.HandleLogout)
|
|
|
- http.HandleFunc(ep.Register, a.HandleRegister)
|
|
|
- http.HandleFunc(ep.CheckLoggedIn, a.CheckLogin)
|
|
|
- http.HandleFunc(ep.Autologin, a.HandleAutologinTokenLogin)
|
|
|
-}
|
|
|
-
|
|
|
//Handle login request, require POST username and password
|
|
|
func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
@@ -179,6 +182,12 @@ func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
|
//The database contain this user information. Check its password if it is correct
|
|
|
if passwordCorrect {
|
|
|
//Password correct
|
|
|
+ //Check if this request origin is allowed to access
|
|
|
+ ok, reasons := a.ValidateLoginRequest(w, r)
|
|
|
+ if !ok {
|
|
|
+ sendErrorResponse(w, reasons.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
// Set user as authenticated
|
|
|
a.LoginUserByRequest(w, r, username, rememberme)
|
|
|
//Print the login message to console
|
|
@@ -217,6 +226,17 @@ func (a *AuthAgent) ValidateUsernameAndPasswordWithReason(username string, passw
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//Validate the user request for login
|
|
|
+func (a *AuthAgent) ValidateLoginRequest(w http.ResponseWriter, r *http.Request) (bool, error) {
|
|
|
+ //Check if the account is banned
|
|
|
+ if a.BlacklistManager.CheckIsBannedByRequest(r) {
|
|
|
+ //This user is banned
|
|
|
+ return false, errors.New("This IP is banned")
|
|
|
+ }
|
|
|
+ return true, nil
|
|
|
+}
|
|
|
+
|
|
|
+//Login the user by creating a valid session for this user
|
|
|
func (a *AuthAgent) LoginUserByRequest(w http.ResponseWriter, r *http.Request, username string, rememberme bool) {
|
|
|
session, _ := a.SessionStore.Get(r, a.SessionName)
|
|
|
|