|
@@ -66,6 +66,9 @@ type AuthAgent struct {
|
|
WhitelistManager *whitelist.WhiteList
|
|
WhitelistManager *whitelist.WhiteList
|
|
BlacklistManager *blacklist.BlackList
|
|
BlacklistManager *blacklist.BlackList
|
|
|
|
|
|
|
|
+ //Account Switcher
|
|
|
|
+ SwitchableAccountManager *SwitchableAccountPoolManager
|
|
|
|
+
|
|
//Logger
|
|
//Logger
|
|
Logger *authlogger.Logger
|
|
Logger *authlogger.Logger
|
|
}
|
|
}
|
|
@@ -78,7 +81,7 @@ type AuthEndpoints struct {
|
|
Autologin string
|
|
Autologin string
|
|
}
|
|
}
|
|
|
|
|
|
-//Constructor
|
|
|
|
|
|
+// Constructor
|
|
func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database, allowReg bool, loginRedirectionHandler func(http.ResponseWriter, *http.Request)) *AuthAgent {
|
|
func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database, allowReg bool, loginRedirectionHandler func(http.ResponseWriter, *http.Request)) *AuthAgent {
|
|
store := sessions.NewCookieStore(key)
|
|
store := sessions.NewCookieStore(key)
|
|
err := sysdb.NewTable("auth")
|
|
err := sysdb.NewTable("auth")
|
|
@@ -125,9 +128,14 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
|
WhitelistManager: thisWhitelistManager,
|
|
WhitelistManager: thisWhitelistManager,
|
|
BlacklistManager: thisBlacklistManager,
|
|
BlacklistManager: thisBlacklistManager,
|
|
ExpDelayHandler: expLoginHandler,
|
|
ExpDelayHandler: expLoginHandler,
|
|
- Logger: newLogger,
|
|
|
|
|
|
+
|
|
|
|
+ //Switchable Account Pool Manager
|
|
|
|
+ Logger: newLogger,
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ poolManager := NewSwitchableAccountPoolManager(sysdb, &newAuthAgent)
|
|
|
|
+ newAuthAgent.SwitchableAccountManager = poolManager
|
|
|
|
+
|
|
//Create a timer to listen to its token storage
|
|
//Create a timer to listen to its token storage
|
|
go func(listeningAuthAgent *AuthAgent) {
|
|
go func(listeningAuthAgent *AuthAgent) {
|
|
for {
|
|
for {
|
|
@@ -144,7 +152,7 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
|
return &newAuthAgent
|
|
return &newAuthAgent
|
|
}
|
|
}
|
|
|
|
|
|
-//Close the authAgent listener
|
|
|
|
|
|
+// Close the authAgent listener
|
|
func (a *AuthAgent) Close() {
|
|
func (a *AuthAgent) Close() {
|
|
//Stop the token listening
|
|
//Stop the token listening
|
|
a.terminateTokenListener <- true
|
|
a.terminateTokenListener <- true
|
|
@@ -153,7 +161,7 @@ func (a *AuthAgent) Close() {
|
|
a.Logger.Close()
|
|
a.Logger.Close()
|
|
}
|
|
}
|
|
|
|
|
|
-//This function will handle an http request and redirect to the given login address if not logged in
|
|
|
|
|
|
+// This function will handle an http request and redirect to the given login address if not logged in
|
|
func (a *AuthAgent) HandleCheckAuth(w http.ResponseWriter, r *http.Request, handler func(http.ResponseWriter, *http.Request)) {
|
|
func (a *AuthAgent) HandleCheckAuth(w http.ResponseWriter, r *http.Request, handler func(http.ResponseWriter, *http.Request)) {
|
|
if a.CheckAuth(r) {
|
|
if a.CheckAuth(r) {
|
|
//User already logged in
|
|
//User already logged in
|
|
@@ -164,7 +172,7 @@ func (a *AuthAgent) HandleCheckAuth(w http.ResponseWriter, r *http.Request, hand
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//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
|
|
@@ -242,7 +250,7 @@ func (a *AuthAgent) ValidateUsernameAndPassword(username string, password string
|
|
return succ
|
|
return succ
|
|
}
|
|
}
|
|
|
|
|
|
-//validate the username and password, return reasons if the auth failed
|
|
|
|
|
|
+// validate the username and password, return reasons if the auth failed
|
|
func (a *AuthAgent) ValidateUsernameAndPasswordWithReason(username string, password string) (bool, string) {
|
|
func (a *AuthAgent) ValidateUsernameAndPasswordWithReason(username string, password string) (bool, string) {
|
|
hashedPassword := Hash(password)
|
|
hashedPassword := Hash(password)
|
|
var passwordInDB string
|
|
var passwordInDB string
|
|
@@ -260,7 +268,7 @@ func (a *AuthAgent) ValidateUsernameAndPasswordWithReason(username string, passw
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//Validate the user request for login
|
|
|
|
|
|
+// Validate the user request for login, return true if the target request original is not blocked
|
|
func (a *AuthAgent) ValidateLoginRequest(w http.ResponseWriter, r *http.Request) (bool, error) {
|
|
func (a *AuthAgent) ValidateLoginRequest(w http.ResponseWriter, r *http.Request) (bool, error) {
|
|
//Get the ip address of the request
|
|
//Get the ip address of the request
|
|
clientIP, err := network.GetIpFromRequest(r)
|
|
clientIP, err := network.GetIpFromRequest(r)
|
|
@@ -287,7 +295,7 @@ func (a *AuthAgent) ValidateLoginIpAccess(ipv4 string) (bool, error) {
|
|
return true, nil
|
|
return true, nil
|
|
}
|
|
}
|
|
|
|
|
|
-//Login the user by creating a valid session for this user
|
|
|
|
|
|
+// 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) {
|
|
func (a *AuthAgent) LoginUserByRequest(w http.ResponseWriter, r *http.Request, username string, rememberme bool) {
|
|
session, _ := a.SessionStore.Get(r, a.SessionName)
|
|
session, _ := a.SessionStore.Get(r, a.SessionName)
|
|
|
|
|
|
@@ -310,7 +318,7 @@ func (a *AuthAgent) LoginUserByRequest(w http.ResponseWriter, r *http.Request, u
|
|
session.Save(r, w)
|
|
session.Save(r, w)
|
|
}
|
|
}
|
|
|
|
|
|
-//Handle logout, reply OK after logged out. WILL NOT DO REDIRECTION
|
|
|
|
|
|
+// Handle logout, reply OK after logged out. WILL NOT DO REDIRECTION
|
|
func (a *AuthAgent) HandleLogout(w http.ResponseWriter, r *http.Request) {
|
|
func (a *AuthAgent) HandleLogout(w http.ResponseWriter, r *http.Request) {
|
|
username, _ := a.GetUserName(w, r)
|
|
username, _ := a.GetUserName(w, r)
|
|
if username != "" {
|
|
if username != "" {
|
|
@@ -337,7 +345,7 @@ func (a *AuthAgent) Logout(w http.ResponseWriter, r *http.Request) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-//Get the current session username from request
|
|
|
|
|
|
+// Get the current session username from request
|
|
func (a *AuthAgent) GetUserName(w http.ResponseWriter, r *http.Request) (string, error) {
|
|
func (a *AuthAgent) GetUserName(w http.ResponseWriter, r *http.Request) (string, error) {
|
|
if a.CheckAuth(r) {
|
|
if a.CheckAuth(r) {
|
|
//This user has logged in.
|
|
//This user has logged in.
|
|
@@ -349,16 +357,16 @@ func (a *AuthAgent) GetUserName(w http.ResponseWriter, r *http.Request) (string,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//Check if the user has logged in, return true / false in JSON
|
|
|
|
|
|
+// Check if the user has logged in, return true / false in JSON
|
|
func (a *AuthAgent) CheckLogin(w http.ResponseWriter, r *http.Request) {
|
|
func (a *AuthAgent) CheckLogin(w http.ResponseWriter, r *http.Request) {
|
|
- if a.CheckAuth(r) != false {
|
|
|
|
|
|
+ if a.CheckAuth(r) {
|
|
sendJSONResponse(w, "true")
|
|
sendJSONResponse(w, "true")
|
|
} else {
|
|
} else {
|
|
sendJSONResponse(w, "false")
|
|
sendJSONResponse(w, "false")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//Handle new user register. Require POST username, password, group.
|
|
|
|
|
|
+// Handle new user register. Require POST username, password, group.
|
|
func (a *AuthAgent) HandleRegister(w http.ResponseWriter, r *http.Request) {
|
|
func (a *AuthAgent) HandleRegister(w http.ResponseWriter, r *http.Request) {
|
|
userCount := a.GetUserCounts()
|
|
userCount := a.GetUserCounts()
|
|
|
|
|
|
@@ -407,7 +415,7 @@ func (a *AuthAgent) HandleRegister(w http.ResponseWriter, r *http.Request) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
-//Check authentication from request header's session value
|
|
|
|
|
|
+// Check authentication from request header's session value
|
|
func (a *AuthAgent) CheckAuth(r *http.Request) bool {
|
|
func (a *AuthAgent) CheckAuth(r *http.Request) bool {
|
|
session, _ := a.SessionStore.Get(r, a.SessionName)
|
|
session, _ := a.SessionStore.Get(r, a.SessionName)
|
|
// Check if user is authenticated
|
|
// Check if user is authenticated
|
|
@@ -417,11 +425,11 @@ func (a *AuthAgent) CheckAuth(r *http.Request) bool {
|
|
return true
|
|
return true
|
|
}
|
|
}
|
|
|
|
|
|
-//Handle de-register of users. Require POST username.
|
|
|
|
-//THIS FUNCTION WILL NOT CHECK FOR PERMISSION. PLEASE USE WITH PERMISSION HANDLER
|
|
|
|
|
|
+// Handle de-register of users. Require POST username.
|
|
|
|
+// THIS FUNCTION WILL NOT CHECK FOR PERMISSION. PLEASE USE WITH PERMISSION HANDLER
|
|
func (a *AuthAgent) HandleUnregister(w http.ResponseWriter, r *http.Request) {
|
|
func (a *AuthAgent) HandleUnregister(w http.ResponseWriter, r *http.Request) {
|
|
//Check if the user is logged in
|
|
//Check if the user is logged in
|
|
- if a.CheckAuth(r) == false {
|
|
|
|
|
|
+ if !a.CheckAuth(r) {
|
|
//This user has not logged in
|
|
//This user has not logged in
|
|
sendErrorResponse(w, "Login required to remove user from the system.")
|
|
sendErrorResponse(w, "Login required to remove user from the system.")
|
|
return
|
|
return
|
|
@@ -472,7 +480,7 @@ func (a *AuthAgent) UnregisterUser(username string) error {
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-//Get the number of users in the system
|
|
|
|
|
|
+// Get the number of users in the system
|
|
func (a *AuthAgent) GetUserCounts() int {
|
|
func (a *AuthAgent) GetUserCounts() int {
|
|
entries, _ := a.Database.ListTable("auth")
|
|
entries, _ := a.Database.ListTable("auth")
|
|
usercount := 0
|
|
usercount := 0
|
|
@@ -489,7 +497,7 @@ func (a *AuthAgent) GetUserCounts() int {
|
|
return usercount
|
|
return usercount
|
|
}
|
|
}
|
|
|
|
|
|
-//List all username within the system
|
|
|
|
|
|
+// List all username within the system
|
|
func (a *AuthAgent) ListUsers() []string {
|
|
func (a *AuthAgent) ListUsers() []string {
|
|
entries, _ := a.Database.ListTable("auth")
|
|
entries, _ := a.Database.ListTable("auth")
|
|
results := []string{}
|
|
results := []string{}
|
|
@@ -502,7 +510,7 @@ func (a *AuthAgent) ListUsers() []string {
|
|
return results
|
|
return results
|
|
}
|
|
}
|
|
|
|
|
|
-//Check if the given username exists
|
|
|
|
|
|
+// Check if the given username exists
|
|
func (a *AuthAgent) UserExists(username string) bool {
|
|
func (a *AuthAgent) UserExists(username string) bool {
|
|
userpasswordhash := ""
|
|
userpasswordhash := ""
|
|
err := a.Database.Read("auth", "passhash/"+username, &userpasswordhash)
|
|
err := a.Database.Read("auth", "passhash/"+username, &userpasswordhash)
|
|
@@ -512,14 +520,14 @@ func (a *AuthAgent) UserExists(username string) bool {
|
|
return true
|
|
return true
|
|
}
|
|
}
|
|
|
|
|
|
-//Update the session expire time given the request header.
|
|
|
|
|
|
+// Update the session expire time given the request header.
|
|
func (a *AuthAgent) UpdateSessionExpireTime(w http.ResponseWriter, r *http.Request) bool {
|
|
func (a *AuthAgent) UpdateSessionExpireTime(w http.ResponseWriter, r *http.Request) bool {
|
|
session, _ := a.SessionStore.Get(r, a.SessionName)
|
|
session, _ := a.SessionStore.Get(r, a.SessionName)
|
|
- if session.Values["authenticated"].(bool) == true {
|
|
|
|
|
|
+ if session.Values["authenticated"].(bool) {
|
|
//User authenticated. Extend its expire time
|
|
//User authenticated. Extend its expire time
|
|
rememberme := session.Values["rememberMe"].(bool)
|
|
rememberme := session.Values["rememberMe"].(bool)
|
|
//Extend the session expire time
|
|
//Extend the session expire time
|
|
- if rememberme == true {
|
|
|
|
|
|
+ if rememberme {
|
|
session.Options = &sessions.Options{
|
|
session.Options = &sessions.Options{
|
|
MaxAge: 3600 * 24 * 7, //One week
|
|
MaxAge: 3600 * 24 * 7, //One week
|
|
Path: "/",
|
|
Path: "/",
|
|
@@ -537,7 +545,7 @@ func (a *AuthAgent) UpdateSessionExpireTime(w http.ResponseWriter, r *http.Reque
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//Create user account
|
|
|
|
|
|
+// Create user account
|
|
func (a *AuthAgent) CreateUserAccount(newusername string, password string, group []string) error {
|
|
func (a *AuthAgent) CreateUserAccount(newusername string, password string, group []string) error {
|
|
key := newusername
|
|
key := newusername
|
|
hashedPassword := Hash(password)
|
|
hashedPassword := Hash(password)
|
|
@@ -553,7 +561,7 @@ func (a *AuthAgent) CreateUserAccount(newusername string, password string, group
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
-//Hash the given raw string into sha512 hash
|
|
|
|
|
|
+// Hash the given raw string into sha512 hash
|
|
func Hash(raw string) string {
|
|
func Hash(raw string) string {
|
|
h := sha512.New()
|
|
h := sha512.New()
|
|
h.Write([]byte(raw))
|
|
h.Write([]byte(raw))
|