Documentation ¶
Index ¶
- Constants
- func GetSecureCookie(req *http.Request, name string, cookieSecret string) (string, bool)
- func PermissionDenied(w http.ResponseWriter, req *http.Request)
- func RandomCookieFriendlyString(length int) string
- func RandomHumanFriendlyString(length int) string
- func RandomString(length int) string
- func SetCookiePath(w http.ResponseWriter, name, value string, age int64, path string)
- func SetHeader(w http.ResponseWriter, hdr, val string, unique bool)
- func SetSecureCookiePath(w http.ResponseWriter, name, val string, age int64, path string, ...)
- func ValidUsernamePassword(username, password string) error
- type Permissions
- func (perm *Permissions) AddAdminPath(prefix string)
- func (perm *Permissions) AddPublicPath(prefix string)
- func (perm *Permissions) AddUserPath(prefix string)
- func (perm *Permissions) Clear()
- func (perm *Permissions) GetDenyFunction() http.HandlerFunc
- func (perm *Permissions) Rejected(w http.ResponseWriter, req *http.Request) bool
- func (perm *Permissions) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc)
- func (perm *Permissions) SetAdminPath(pathPrefixes []string)
- func (perm *Permissions) SetDenyFunction(f http.HandlerFunc)
- func (perm *Permissions) SetPublicPath(pathPrefixes []string)
- func (perm *Permissions) SetUserPath(pathPrefixes []string)
- func (perm *Permissions) UserState() *UserState
- type UserState
- func (state *UserState) AddUnconfirmed(username, confirmationCode string)
- func (state *UserState) AddUser(username, password, email string)
- func (state *UserState) AdminRights(req *http.Request) bool
- func (state *UserState) AlreadyHasConfirmationCode(confirmationCode string) bool
- func (state *UserState) Close()
- func (state *UserState) Confirm(username string)
- func (state *UserState) ConfirmUserByConfirmationCode(confirmationcode string) error
- func (state *UserState) CorrectPassword(username, password string) bool
- func (state *UserState) FindUserByConfirmationCode(confirmationcode string) (string, error)
- func (state *UserState) GenerateUniqueConfirmationCode() (string, error)
- func (state *UserState) GetAllUnconfirmedUsernames() ([]string, error)
- func (state *UserState) GetAllUsernames() ([]string, error)
- func (state *UserState) GetBooleanField(username, fieldname string) bool
- func (state *UserState) GetConfirmationCode(username string) (string, error)
- func (state *UserState) GetCookieTimeout(username string) int64
- func (state *UserState) GetDatabaseIndex() int
- func (state *UserState) GetEmail(username string) (string, error)
- func (state *UserState) GetPasswordAlgo() string
- func (state *UserState) GetPasswordHash(username string) (string, error)
- func (state *UserState) GetPool() *simpleredis.ConnectionPool
- func (state *UserState) GetUsername(req *http.Request) string
- func (state *UserState) GetUsernameCookie(req *http.Request) (string, error)
- func (state *UserState) GetUsers() *simpleredis.HashMap
- func (state *UserState) HasUser(username string) bool
- func (state *UserState) HashPassword(username, password string) string
- func (state *UserState) IsAdmin(username string) bool
- func (state *UserState) IsConfirmed(username string) bool
- func (state *UserState) IsLoggedIn(username string) bool
- func (state *UserState) Login(w http.ResponseWriter, username string)
- func (state *UserState) Logout(username string)
- func (state *UserState) MarkConfirmed(username string)
- func (state *UserState) RemoveAdminStatus(username string)
- func (state *UserState) RemoveUnconfirmed(username string)
- func (state *UserState) RemoveUser(username string)
- func (state *UserState) SetAdminStatus(username string)
- func (state *UserState) SetBooleanField(username, fieldname string, val bool)
- func (state *UserState) SetCookieTimeout(cookieTime int64)
- func (state *UserState) SetLoggedIn(username string)
- func (state *UserState) SetLoggedOut(username string)
- func (state *UserState) SetMinimumConfirmationCodeLength(length int)
- func (state *UserState) SetPasswordAlgo(algo string)
- func (state *UserState) SetUsernameCookie(w http.ResponseWriter, username string) error
- func (state *UserState) UserRights(req *http.Request) bool
Constants ¶
const (
// Version number. Stable API within major version numbers.
Version = 1.0
)
Variables ¶
This section is empty.
Functions ¶
func GetSecureCookie ¶
func PermissionDenied ¶
func PermissionDenied(w http.ResponseWriter, req *http.Request)
The default "permission denied" http handler.
func RandomCookieFriendlyString ¶
Generate a random, but cookie-friendly, string of the given length.
func RandomHumanFriendlyString ¶
Generate a random, but human-friendly, string of the given length. Should be possible to read out loud and send in an email without problems.
func RandomString ¶
Generate a random string of the given length.
func SetCookiePath ¶
func SetCookiePath(w http.ResponseWriter, name, value string, age int64, path string)
Set a cookie with an explicit path. Duration is the cookie time-to-live in seconds (0 = forever).
func SetHeader ¶
func SetHeader(w http.ResponseWriter, hdr, val string, unique bool)
Used for setting cookies
func SetSecureCookiePath ¶
func ValidUsernamePassword ¶
Check that the given username and password are different. Also check if the chosen username only contains letters, numbers and/or underscore.
Types ¶
type Permissions ¶
type Permissions struct {
// contains filtered or unexported fields
}
func New ¶
func New() *Permissions
Initialize a Permissions struct with all the default settings. This will also connect to the redis host at localhost:6379.
func NewPermissions ¶
func NewPermissions(state *UserState) *Permissions
Initialize a Permissions struct with the given UserState and a few default paths for admin/user/public path prefixes.
func NewWithRedisConf ¶
func NewWithRedisConf(dbindex int, hostPort string) *Permissions
Initialize a Permissions struct with Redis DB index and host:port
func (*Permissions) AddAdminPath ¶
func (perm *Permissions) AddAdminPath(prefix string)
Add an url path prefix that is a page for the logged in administrators
func (*Permissions) AddPublicPath ¶
func (perm *Permissions) AddPublicPath(prefix string)
Add an url path prefix that is a public page
func (*Permissions) AddUserPath ¶
func (perm *Permissions) AddUserPath(prefix string)
Add an url path prefix that is a page for the logged in users
func (*Permissions) GetDenyFunction ¶
func (perm *Permissions) GetDenyFunction() http.HandlerFunc
Get the current http.HandlerFunc for when permissions are denied
func (*Permissions) Rejected ¶
func (perm *Permissions) Rejected(w http.ResponseWriter, req *http.Request) bool
Check if a given request should be rejected.
func (*Permissions) ServeHTTP ¶
func (perm *Permissions) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc)
Negroni middleware handler
func (*Permissions) SetAdminPath ¶
func (perm *Permissions) SetAdminPath(pathPrefixes []string)
Set all url path prefixes that are for the logged in administrator pages
func (*Permissions) SetDenyFunction ¶
func (perm *Permissions) SetDenyFunction(f http.HandlerFunc)
Specify the http.HandlerFunc for when the permissions are denied
func (*Permissions) SetPublicPath ¶
func (perm *Permissions) SetPublicPath(pathPrefixes []string)
Set all url path prefixes that are for the public pages
func (*Permissions) SetUserPath ¶
func (perm *Permissions) SetUserPath(pathPrefixes []string)
Set all url path prefixes that are for the logged in user pages
func (*Permissions) UserState ¶
func (perm *Permissions) UserState() *UserState
Retrieve the UserState struct
type UserState ¶
type UserState struct {
// contains filtered or unexported fields
}
func NewUserState ¶
Create a new *UserState that can be used for managing users. dbindex is the Redis database index. If randomseed is true, the random number generator will be seeded after generating the cookie secret. redisHostPort is host:port for the desired Redis server (can be blank for localhost) Also creates a new ConnectionPool.
func NewUserStateSimple ¶
func NewUserStateSimple() *UserState
Create a new *UserState that can be used for managing users. The random number generator will be seeded after generating the cookie secret. A connection pool for the local Redis server (dbindex 0) will be created.
func (*UserState) AddUnconfirmed ¶
Add a user that is registered but not confirmed
func (*UserState) AddUser ¶
Creates a user and hashes the password, does not check for rights. The given data must be valid.
func (*UserState) AdminRights ¶
Check if the current user is logged in as Administrator right now
func (*UserState) AlreadyHasConfirmationCode ¶
Goes through all the confirmationCodes of all the unconfirmed users and checks if this confirmationCode already is in use
func (*UserState) Confirm ¶
Both remove the username from the list of unconfirmed users and mark the user as confirmed
func (*UserState) ConfirmUserByConfirmationCode ¶
Take a confirmation code and mark the corresponding unconfirmed user as confirmed
func (*UserState) CorrectPassword ¶
Check if a password is correct. username is needed because it is part of the hash.
func (*UserState) FindUserByConfirmationCode ¶
Given a unique confirmation code, find the corresponding username
func (*UserState) GenerateUniqueConfirmationCode ¶
Generate a unique confirmation code that can be used for confirming users
func (*UserState) GetAllUnconfirmedUsernames ¶
Get all registered users that are not yet confirmed
func (*UserState) GetAllUsernames ¶
Get a list of all usernames
func (*UserState) GetBooleanField ¶
Return the boolean value for a given username and fieldname. If the user or field is missing, false will be returned. Useful for states where it makes sense that the returned value is not true unless everything is in order.
func (*UserState) GetConfirmationCode ¶
Get the confirmation code for a specific user
func (*UserState) GetCookieTimeout ¶
Get how long a login cookie should last, in seconds
func (*UserState) GetDatabaseIndex ¶
Get the Redis database index
func (*UserState) GetPasswordAlgo ¶
Get current password algorithm
func (*UserState) GetPasswordHash ¶
Get the password hash for the given username
func (*UserState) GetPool ¶
func (state *UserState) GetPool() *simpleredis.ConnectionPool
Get the Redis connection pool
func (*UserState) GetUsername ¶
Convenience function that will return a username (from the browser cookie) or an empty string
func (*UserState) GetUsernameCookie ¶
Retrieve the username that is stored in a cookie in the browser, if available
func (*UserState) GetUsers ¶
func (state *UserState) GetUsers() *simpleredis.HashMap
Get the users HashMap
func (*UserState) HashPassword ¶
Hash the password if using sha256, use the cookieSecret and username as part of the salt if using bcrypt, rely on it for salting
func (*UserState) IsConfirmed ¶
Check if the given username is confirmed
func (*UserState) IsLoggedIn ¶
Checks if the given username is logged in or not
func (*UserState) Login ¶
func (state *UserState) Login(w http.ResponseWriter, username string)
Convenience function for logging the user in and storing the username in a cookie
func (*UserState) MarkConfirmed ¶
Mark a user as confirmed
func (*UserState) RemoveAdminStatus ¶
Mark user as a regular user
func (*UserState) RemoveUnconfirmed ¶
Remove a user that is registered but not confirmed
func (*UserState) RemoveUser ¶
Remove user and login status
func (*UserState) SetAdminStatus ¶
Mark user as an administrator
func (*UserState) SetBooleanField ¶
Attempt to store a boolean value for the given username and fieldname.
func (*UserState) SetCookieTimeout ¶
Set how long a login cookie should last, in seconds
func (*UserState) SetLoggedIn ¶
Mark the user as logged in. Use the Login function instead, unless cookies are not involved.
func (*UserState) SetLoggedOut ¶
Mark the user as logged out
func (*UserState) SetMinimumConfirmationCodeLength ¶
Set the minimum length of the user confirmation code. The default is 20.
func (*UserState) SetPasswordAlgo ¶
Set password algorithm
func (*UserState) SetUsernameCookie ¶
func (state *UserState) SetUsernameCookie(w http.ResponseWriter, username string) error
Store the given username in a cookie in the browser, if possible. The user must exist.