|
@@ -23,6 +23,7 @@ import (
|
|
"crypto/sha512"
|
|
"crypto/sha512"
|
|
"errors"
|
|
"errors"
|
|
"net/http"
|
|
"net/http"
|
|
|
|
+ "strconv"
|
|
"strings"
|
|
"strings"
|
|
"sync"
|
|
"sync"
|
|
|
|
|
|
@@ -35,6 +36,7 @@ import (
|
|
"imuslab.com/arozos/mod/auth/accesscontrol/blacklist"
|
|
"imuslab.com/arozos/mod/auth/accesscontrol/blacklist"
|
|
"imuslab.com/arozos/mod/auth/accesscontrol/whitelist"
|
|
"imuslab.com/arozos/mod/auth/accesscontrol/whitelist"
|
|
"imuslab.com/arozos/mod/auth/authlogger"
|
|
"imuslab.com/arozos/mod/auth/authlogger"
|
|
|
|
+ "imuslab.com/arozos/mod/auth/explogin"
|
|
db "imuslab.com/arozos/mod/database"
|
|
db "imuslab.com/arozos/mod/database"
|
|
"imuslab.com/arozos/mod/network"
|
|
"imuslab.com/arozos/mod/network"
|
|
)
|
|
)
|
|
@@ -56,6 +58,9 @@ type AuthAgent struct {
|
|
AllowAutoLogin bool
|
|
AllowAutoLogin bool
|
|
autoLoginTokens []*AutoLoginToken
|
|
autoLoginTokens []*AutoLoginToken
|
|
|
|
|
|
|
|
+ //Exponential Delay Retry Handler
|
|
|
|
+ ExpDelayHandler *explogin.ExpLoginHandler
|
|
|
|
+
|
|
//IPLists manager
|
|
//IPLists manager
|
|
WhitelistManager *whitelist.WhiteList
|
|
WhitelistManager *whitelist.WhiteList
|
|
BlacklistManager *blacklist.BlackList
|
|
BlacklistManager *blacklist.BlackList
|
|
@@ -85,6 +90,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 exponential login delay handler
|
|
|
|
+ expLoginHandler := explogin.NewExponentialLoginHandler(2, 10800)
|
|
|
|
+
|
|
//Create a new whitelist manager
|
|
//Create a new whitelist manager
|
|
thisWhitelistManager := whitelist.NewWhitelistManager(sysdb)
|
|
thisWhitelistManager := whitelist.NewWhitelistManager(sysdb)
|
|
|
|
|
|
@@ -115,6 +123,7 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
|
//Blacklist management
|
|
//Blacklist management
|
|
WhitelistManager: thisWhitelistManager,
|
|
WhitelistManager: thisWhitelistManager,
|
|
BlacklistManager: thisBlacklistManager,
|
|
BlacklistManager: thisBlacklistManager,
|
|
|
|
+ ExpDelayHandler: expLoginHandler,
|
|
Logger: newLogger,
|
|
Logger: newLogger,
|
|
}
|
|
}
|
|
|
|
|
|
@@ -184,6 +193,15 @@ func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
rememberme = true
|
|
rememberme = true
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //Check Exponential Login Handler
|
|
|
|
+ ok, nextRetryIn := a.ExpDelayHandler.AllowImmediateAccess(username, r)
|
|
|
|
+ if !ok {
|
|
|
|
+ //Too many request! (maybe the account is under brute force attack?)
|
|
|
|
+ a.ExpDelayHandler.AddUserRetrycount(username, r)
|
|
|
|
+ sendErrorResponse(w, "Too many request! Next retry in "+strconv.Itoa(int(nextRetryIn))+" seconds")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+
|
|
//Check the database and see if this user is in the database
|
|
//Check the database and see if this user is in the database
|
|
passwordCorrect, rejectionReason := a.ValidateUsernameAndPasswordWithReason(username, password)
|
|
passwordCorrect, rejectionReason := a.ValidateUsernameAndPasswordWithReason(username, password)
|
|
//The database contain this user information. Check its password if it is correct
|
|
//The database contain this user information. Check its password if it is correct
|
|
@@ -195,8 +213,13 @@ func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
sendErrorResponse(w, reasons.Error())
|
|
sendErrorResponse(w, reasons.Error())
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
// Set user as authenticated
|
|
// Set user as authenticated
|
|
a.LoginUserByRequest(w, r, username, rememberme)
|
|
a.LoginUserByRequest(w, r, username, rememberme)
|
|
|
|
+
|
|
|
|
+ //Reset user retry count if any
|
|
|
|
+ a.ExpDelayHandler.ResetUserRetryCount(username, r)
|
|
|
|
+
|
|
//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)
|
|
a.Logger.LogAuth(r, true)
|
|
@@ -204,6 +227,9 @@ func (a *AuthAgent) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
|
} else {
|
|
} else {
|
|
//Password incorrect
|
|
//Password incorrect
|
|
log.Println(username + " login request rejected: " + rejectionReason)
|
|
log.Println(username + " login request rejected: " + rejectionReason)
|
|
|
|
+
|
|
|
|
+ //Add to retry count
|
|
|
|
+ a.ExpDelayHandler.AddUserRetrycount(username, r)
|
|
sendErrorResponse(w, rejectionReason)
|
|
sendErrorResponse(w, rejectionReason)
|
|
a.Logger.LogAuth(r, false)
|
|
a.Logger.LogAuth(r, false)
|
|
return
|
|
return
|