|
@@ -14,10 +14,10 @@ import (
|
|
"strings"
|
|
"strings"
|
|
|
|
|
|
"encoding/hex"
|
|
"encoding/hex"
|
|
- "log"
|
|
|
|
|
|
|
|
"github.com/gorilla/sessions"
|
|
"github.com/gorilla/sessions"
|
|
db "imuslab.com/zoraxy/mod/database"
|
|
db "imuslab.com/zoraxy/mod/database"
|
|
|
|
+ "imuslab.com/zoraxy/mod/info/logger"
|
|
"imuslab.com/zoraxy/mod/utils"
|
|
"imuslab.com/zoraxy/mod/utils"
|
|
)
|
|
)
|
|
|
|
|
|
@@ -27,6 +27,7 @@ type AuthAgent struct {
|
|
SessionStore *sessions.CookieStore
|
|
SessionStore *sessions.CookieStore
|
|
Database *db.Database
|
|
Database *db.Database
|
|
LoginRedirectionHandler func(http.ResponseWriter, *http.Request)
|
|
LoginRedirectionHandler func(http.ResponseWriter, *http.Request)
|
|
|
|
+ Logger *logger.Logger
|
|
}
|
|
}
|
|
|
|
|
|
type AuthEndpoints struct {
|
|
type AuthEndpoints struct {
|
|
@@ -37,12 +38,12 @@ type AuthEndpoints struct {
|
|
Autologin string
|
|
Autologin string
|
|
}
|
|
}
|
|
|
|
|
|
-//Constructor
|
|
|
|
-func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database, allowReg bool, loginRedirectionHandler func(http.ResponseWriter, *http.Request)) *AuthAgent {
|
|
|
|
|
|
+// Constructor
|
|
|
|
+func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database, allowReg bool, logger *logger.Logger, loginRedirectionHandler func(http.ResponseWriter, *http.Request)) *AuthAgent {
|
|
store := sessions.NewCookieStore(key)
|
|
store := sessions.NewCookieStore(key)
|
|
err := sysdb.NewTable("auth")
|
|
err := sysdb.NewTable("auth")
|
|
if err != nil {
|
|
if err != nil {
|
|
- log.Println("Failed to create auth database. Terminating.")
|
|
|
|
|
|
+ logger.Println("Failed to create auth database. Terminating.")
|
|
panic(err)
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
|
|
@@ -58,7 +59,7 @@ func NewAuthenticationAgent(sessionName string, key []byte, sysdb *db.Database,
|
|
return &newAuthAgent
|
|
return &newAuthAgent
|
|
}
|
|
}
|
|
|
|
|
|
-func GetSessionKey(sysdb *db.Database) (string, error) {
|
|
|
|
|
|
+func GetSessionKey(sysdb *db.Database, logger *logger.Logger) (string, error) {
|
|
sysdb.NewTable("auth")
|
|
sysdb.NewTable("auth")
|
|
sessionKey := ""
|
|
sessionKey := ""
|
|
if !sysdb.KeyExists("auth", "sessionkey") {
|
|
if !sysdb.KeyExists("auth", "sessionkey") {
|
|
@@ -66,9 +67,9 @@ func GetSessionKey(sysdb *db.Database) (string, error) {
|
|
rand.Read(key)
|
|
rand.Read(key)
|
|
sessionKey = string(key)
|
|
sessionKey = string(key)
|
|
sysdb.Write("auth", "sessionkey", sessionKey)
|
|
sysdb.Write("auth", "sessionkey", sessionKey)
|
|
- log.Println("[Auth] New authentication session key generated")
|
|
|
|
|
|
+ logger.PrintAndLog("auth", "New authentication session key generated", nil)
|
|
} else {
|
|
} else {
|
|
- log.Println("[Auth] Authentication session key loaded from database")
|
|
|
|
|
|
+ logger.PrintAndLog("auth", "Authentication session key loaded from database", nil)
|
|
err := sysdb.Read("auth", "sessionkey", &sessionKey)
|
|
err := sysdb.Read("auth", "sessionkey", &sessionKey)
|
|
if err != nil {
|
|
if err != nil {
|
|
return "", errors.New("database read error. Is the database file corrupted?")
|
|
return "", errors.New("database read error. Is the database file corrupted?")
|
|
@@ -77,7 +78,7 @@ func GetSessionKey(sysdb *db.Database) (string, error) {
|
|
return sessionKey, nil
|
|
return sessionKey, nil
|
|
}
|
|
}
|
|
|
|
|
|
-//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
|
|
@@ -88,14 +89,14 @@ 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
|
|
username, err := utils.PostPara(r, "username")
|
|
username, err := utils.PostPara(r, "username")
|
|
if err != nil {
|
|
if err != nil {
|
|
//Username not defined
|
|
//Username not defined
|
|
- log.Println("[Auth] " + r.RemoteAddr + " trying to login with username: " + username)
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", r.RemoteAddr+" trying to login with username: "+username, nil)
|
|
utils.SendErrorResponse(w, "Username not defined or empty.")
|
|
utils.SendErrorResponse(w, "Username not defined or empty.")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
@@ -124,11 +125,11 @@ 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.")
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", username+" logged in.", nil)
|
|
utils.SendOK(w)
|
|
utils.SendOK(w)
|
|
} else {
|
|
} else {
|
|
//Password incorrect
|
|
//Password incorrect
|
|
- log.Println(username + " login request rejected: " + rejectionReason)
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", username+" login request rejected: "+rejectionReason, nil)
|
|
|
|
|
|
utils.SendErrorResponse(w, rejectionReason)
|
|
utils.SendErrorResponse(w, rejectionReason)
|
|
return
|
|
return
|
|
@@ -140,14 +141,14 @@ 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
|
|
err := a.Database.Read("auth", "passhash/"+username, &passwordInDB)
|
|
err := a.Database.Read("auth", "passhash/"+username, &passwordInDB)
|
|
if err != nil {
|
|
if err != nil {
|
|
//User not found or db exception
|
|
//User not found or db exception
|
|
- log.Println("[Auth] " + username + " login with incorrect password")
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", username+" login with incorrect password", nil)
|
|
return false, "Invalid username or password"
|
|
return false, "Invalid username or password"
|
|
}
|
|
}
|
|
|
|
|
|
@@ -158,7 +159,7 @@ func (a *AuthAgent) ValidateUsernameAndPasswordWithReason(username string, passw
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//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)
|
|
|
|
|
|
@@ -181,11 +182,15 @@ 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, err := a.GetUserName(w, r)
|
|
username, err := a.GetUserName(w, r)
|
|
|
|
+ if err != nil {
|
|
|
|
+ utils.SendErrorResponse(w, "user not logged in")
|
|
|
|
+ return
|
|
|
|
+ }
|
|
if username != "" {
|
|
if username != "" {
|
|
- log.Println(username + " logged out.")
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", username+" logged out", nil)
|
|
}
|
|
}
|
|
// Revoke users authentication
|
|
// Revoke users authentication
|
|
err = a.Logout(w, r)
|
|
err = a.Logout(w, r)
|
|
@@ -194,7 +199,7 @@ func (a *AuthAgent) HandleLogout(w http.ResponseWriter, r *http.Request) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- w.Write([]byte("OK"))
|
|
|
|
|
|
+ utils.SendOK(w)
|
|
}
|
|
}
|
|
|
|
|
|
func (a *AuthAgent) Logout(w http.ResponseWriter, r *http.Request) error {
|
|
func (a *AuthAgent) Logout(w http.ResponseWriter, r *http.Request) error {
|
|
@@ -208,7 +213,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.
|
|
@@ -220,7 +225,7 @@ func (a *AuthAgent) GetUserName(w http.ResponseWriter, r *http.Request) (string,
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//Get the current session user email from request
|
|
|
|
|
|
+// Get the current session user email from request
|
|
func (a *AuthAgent) GetUserEmail(w http.ResponseWriter, r *http.Request) (string, error) {
|
|
func (a *AuthAgent) GetUserEmail(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.
|
|
@@ -239,7 +244,7 @@ func (a *AuthAgent) GetUserEmail(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) {
|
|
if a.CheckAuth(r) {
|
|
utils.SendJSONResponse(w, "true")
|
|
utils.SendJSONResponse(w, "true")
|
|
@@ -248,7 +253,7 @@ func (a *AuthAgent) CheckLogin(w http.ResponseWriter, r *http.Request) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-//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, callback func(string, string)) {
|
|
func (a *AuthAgent) HandleRegister(w http.ResponseWriter, r *http.Request, callback func(string, string)) {
|
|
//Get username from request
|
|
//Get username from request
|
|
newusername, err := utils.PostPara(r, "username")
|
|
newusername, err := utils.PostPara(r, "username")
|
|
@@ -291,10 +296,10 @@ func (a *AuthAgent) HandleRegister(w http.ResponseWriter, r *http.Request, callb
|
|
|
|
|
|
//Return to the client with OK
|
|
//Return to the client with OK
|
|
utils.SendOK(w)
|
|
utils.SendOK(w)
|
|
- log.Println("[Auth] New user " + newusername + " added to system.")
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", "New user "+newusername+" added to system.", nil)
|
|
}
|
|
}
|
|
|
|
|
|
-//Handle new user register without confirmation email. Require POST username, password, group.
|
|
|
|
|
|
+// Handle new user register without confirmation email. Require POST username, password, group.
|
|
func (a *AuthAgent) HandleRegisterWithoutEmail(w http.ResponseWriter, r *http.Request, callback func(string, string)) {
|
|
func (a *AuthAgent) HandleRegisterWithoutEmail(w http.ResponseWriter, r *http.Request, callback func(string, string)) {
|
|
//Get username from request
|
|
//Get username from request
|
|
newusername, err := utils.PostPara(r, "username")
|
|
newusername, err := utils.PostPara(r, "username")
|
|
@@ -324,10 +329,10 @@ func (a *AuthAgent) HandleRegisterWithoutEmail(w http.ResponseWriter, r *http.Re
|
|
|
|
|
|
//Return to the client with OK
|
|
//Return to the client with OK
|
|
utils.SendOK(w)
|
|
utils.SendOK(w)
|
|
- log.Println("[Auth] Admin account created: " + newusername)
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", "Admin account created: "+newusername, nil)
|
|
}
|
|
}
|
|
|
|
|
|
-//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, err := a.SessionStore.Get(r, a.SessionName)
|
|
session, err := a.SessionStore.Get(r, a.SessionName)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -340,8 +345,8 @@ 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) {
|
|
if !a.CheckAuth(r) {
|
|
@@ -365,7 +370,7 @@ func (a *AuthAgent) HandleUnregister(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
//Return to the client with OK
|
|
//Return to the client with OK
|
|
utils.SendOK(w)
|
|
utils.SendOK(w)
|
|
- log.Println("[Auth] User " + username + " has been removed from the system.")
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", "User "+username+" has been removed from the system", nil)
|
|
}
|
|
}
|
|
|
|
|
|
func (a *AuthAgent) UnregisterUser(username string) error {
|
|
func (a *AuthAgent) UnregisterUser(username string) error {
|
|
@@ -381,7 +386,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
|
|
@@ -393,12 +398,12 @@ func (a *AuthAgent) GetUserCounts() int {
|
|
}
|
|
}
|
|
|
|
|
|
if usercount == 0 {
|
|
if usercount == 0 {
|
|
- log.Println("There are no user in the database.")
|
|
|
|
|
|
+ a.Logger.PrintAndLog("auth", "There are no user in the database", nil)
|
|
}
|
|
}
|
|
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{}
|
|
@@ -411,7 +416,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)
|
|
@@ -421,7 +426,7 @@ 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) {
|
|
if session.Values["authenticated"].(bool) {
|
|
@@ -446,7 +451,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, email string) error {
|
|
func (a *AuthAgent) CreateUserAccount(newusername string, password string, email string) error {
|
|
//Check user already exists
|
|
//Check user already exists
|
|
if a.UserExists(newusername) {
|
|
if a.UserExists(newusername) {
|
|
@@ -470,7 +475,7 @@ func (a *AuthAgent) CreateUserAccount(newusername string, password string, email
|
|
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))
|