Documentation
¶
Index ¶
- func Check2FA(user string, pass2FA string) bool
- func CheckAuthCookie(r *http.Request) error
- func CheckLogin(user string, password string) (bool, int)
- func CheckLoginDelayed(user string, password string, delay int) (bool, int)
- func DeleteUser(user string) error
- func GetAuthMiddleware(authLevel int, notLoggedURL string, forbiddenURL string) func(http.Handler) http.Handler
- func GetUserAuthLevel(token string) int
- func GetUsersCount() (int, error)
- func Init(database *sql.DB, secretKey string, smtpConf SmtpConfig) error
- func IsBlocked(user string, remoteAddress string) bool
- func LogOut(w http.ResponseWriter, r *http.Request) error
- func New2FA(user string, password string, duration int64) error
- func NewSession(user string, duration int, authLevel int, w http.ResponseWriter) error
- func NewUser(user string, password string, email string, authLevel int) error
- func RegBadLogin(user string, remoteAddress string)
- func SetBanDuration(minutes int)
- func SetMaxAttemps(attemps int)
- func UpdateUserEmail(user string, newEmail string) error
- func UpdateUserPass(user string, newPassword string) error
- type SmtpConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckAuthCookie ¶
CheckAuthCookie returns error if not exists a valid session cookie in the request
func CheckLogin ¶
CheckLogin checks user password
Returns (true, authLevel) if login is successful, else returns (false, 0).
func CheckLoginDelayed ¶
CheckLogin checks user password and returns result after [delay] seconds. This is a help against brute force attacks.
Returns (true, authLevel) if login is successful, else returns (false, 0).
func DeleteUser ¶
DeleteUser deletes user register from database.
func GetAuthMiddleware ¶
func GetAuthMiddleware(authLevel int, notLoggedURL string, forbiddenURL string) func(http.Handler) http.Handler
GetAuthMiddleware returns a middleware function to use in the server router.
Returned middleware redirects the user to notLoggedURL if auth cookie is not valid, or redirects to forbiddenURL if user auth level is lower than required. These two URLs may be an empty string, in which case only will be returned a 403 status code.
func GetUserAuthLevel ¶
Gets authorization level from a session token
func GetUsersCount ¶ added in v1.0.1
GetUsersCount gets the current number of users registered
func Init ¶
func Init(database *sql.DB, secretKey string, smtpConf SmtpConfig) error
Init initializes all necesary objects to use this package funcions
database: here a table "Users" is stored
secretKey: Random word used for cryptographic purposes
smtpConf: can be an empty struct, in that case smtp server won't be initialized
func IsBlocked ¶
IsBlocked returns "true" if the user-ip combination is temporarily banned for excessive login attempts (default 5). This function must be used in conjunction with function "RegBadLogin".
remoteAddress is obtained from request -> http.Request.RemoteAddr
func LogOut ¶
func LogOut(w http.ResponseWriter, r *http.Request) error
LogOut deletes current session and user cookie
func New2FA ¶
New2FA checks user password and sends a verification code to user email
The verification code is valid for [duration] seconds and is deleted after use
Returns an error if verification code is not sent
func NewSession ¶
NewSession creates and saves in users database and sessionStore a new session.
Session expires in [duration] seconds.
authLevel should be used to filter user access privileges.
func NewUser ¶
NewUser saves a new user in the database
user: name of the user. Must be unique.
password: will be used for future logins. The password is hashed before save it.
email: can be an empty stryng (""). Is used for two factor validation.
authLevel: this number should be used to filter user access privileges.
func RegBadLogin ¶
RegBadLogin registers the failed login attemp. This function allows, together with "IsBlocked", to block during certain period of time (default 15 mins.) those user-ip combinations that have exceeded a certain number of attempts (default 5).
remoteAddress is obtained from request -> http.Request.RemoteAddr
func SetBanDuration ¶
func SetBanDuration(minutes int)
SetBanDuration sets the duration of ban to combination user-ip for excessive login attemps.
func SetMaxAttemps ¶
func SetMaxAttemps(attemps int)
SetMaxAttemps sets the max number of login attemps before ban temporally a combination user-ip.
func UpdateUserEmail ¶
UpdateUserEmail updates user email
func UpdateUserPass ¶
UpdateUserPass updates user password