Documentation ¶
Overview ¶
Package pstore provides a way to keep track of users, login states and permissions.
Index ¶
- Constants
- Variables
- func PermissionDenied(w http.ResponseWriter, req *http.Request)
- 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) DenyFunction() 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() pinterface.IUserState
- 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) AllUnconfirmedUsernames() ([]string, error)
- func (state *UserState) AllUsernames() ([]string, error)
- func (state *UserState) AlreadyHasConfirmationCode(confirmationCode string) bool
- func (state *UserState) BooleanField(username, fieldname string) bool
- func (state *UserState) ClearCookie(w http.ResponseWriter)
- func (state *UserState) Close()
- func (state *UserState) Confirm(username string)
- func (state *UserState) ConfirmUserByConfirmationCode(confirmationcode string) error
- func (state *UserState) ConfirmationCode(username string) (string, error)
- func (state *UserState) CookieSecret() string
- func (state *UserState) CookieTimeout(username string) int64
- func (state *UserState) CorrectPassword(username, password string) bool
- func (state *UserState) Creator() pinterface.ICreator
- func (state *UserState) Email(username string) (string, error)
- func (state *UserState) FindUserByConfirmationCode(confirmationcode string) (string, error)
- func (state *UserState) GenerateUniqueConfirmationCode() (string, error)
- func (state *UserState) HasEmail(email string) (string, error)
- func (state *UserState) HasUser(username string) bool
- func (state *UserState) HashPassword(username, password string) string
- func (state *UserState) Host() pinterface.IHost
- 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) error
- func (state *UserState) Logout(username string)
- func (state *UserState) MarkConfirmed(username string)
- func (state *UserState) PasswordAlgo() string
- func (state *UserState) PasswordHash(username string) (string, error)
- 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) SetCookieSecret(cookieSecret string)
- 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) SetPassword(username, password string)
- func (state *UserState) SetPasswordAlgo(algorithm string) error
- func (state *UserState) SetTablePrefix(prefix string)
- func (state *UserState) SetUsernameCookie(w http.ResponseWriter, username string) error
- func (state *UserState) UserRights(req *http.Request) bool
- func (state *UserState) Username(req *http.Request) string
- func (state *UserState) UsernameCookie(req *http.Request) (string, error)
- func (state *UserState) Users() pinterface.IHashMap
Constants ¶
const (
// Version number. Stable API within major version numbers.
Version = 3.2
)
Variables ¶
var ( // ErrNotFound is returned if HasEmail does not find the given e-mail address ErrNotFound = errors.New("Not found") )
Functions ¶
func PermissionDenied ¶
func PermissionDenied(w http.ResponseWriter, req *http.Request)
PermissionDenied is the default "permission denied" http handler.
func ValidUsernamePassword ¶
ValidUsernamePassword only checks if the given username and password are different and if they only contain letters, numbers and/or underscore. For checking if a given password is correct, use the `CorrectPassword` function instead.
Types ¶
type Permissions ¶
type Permissions struct {
// contains filtered or unexported fields
}
Permissions keeps track of the permissions for various path prefixes
func New ¶
func New() (*Permissions, error)
New will initialize a Permissions struct with all the default settings. This will also connect to the database host at port 3306.
func NewPermissions ¶
func NewPermissions(state *UserState) *Permissions
NewPermissions will initialize a Permissions struct with the given UserState and a few default paths for admin/user/public path prefixes.
func NewWithConf ¶
func NewWithConf(connectionString string) (*Permissions, error)
NewWithConf will initialize a Permissions struct with a database connection string.
func NewWithDSN ¶
func NewWithDSN(connectionString string, databaseName string) (*Permissions, error)
NewWithDSN will initialize a Permissions struct with a DSN
func (*Permissions) AddAdminPath ¶
func (perm *Permissions) AddAdminPath(prefix string)
AddAdminPath will add an URL prefix that will enforce that URLs starting with that prefix will only be for logged in administrators
func (*Permissions) AddPublicPath ¶
func (perm *Permissions) AddPublicPath(prefix string)
AddPublicPath will add an URL prefix that will enforce that URLs starting with that prefix will be public. This overrides the other prefixes.
func (*Permissions) AddUserPath ¶
func (perm *Permissions) AddUserPath(prefix string)
AddUserPath will add an URL prefix that will enforce that URLs starting with that prefix will only be for logged in users
func (*Permissions) DenyFunction ¶
func (perm *Permissions) DenyFunction() http.HandlerFunc
DenyFunction can be used to retrieve the currently configured http.HandlerFunc for when permissions are denied
func (*Permissions) Rejected ¶
func (perm *Permissions) Rejected(w http.ResponseWriter, req *http.Request) bool
Rejected will check if a given request should be rejected.
func (*Permissions) ServeHTTP ¶
func (perm *Permissions) ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc)
ServeHTTP is the middleware handler (compatible with Negroni)
func (*Permissions) SetAdminPath ¶
func (perm *Permissions) SetAdminPath(pathPrefixes []string)
SetAdminPath will add URL prefixes that will enforce that URLs starting with those prefixes will only be for logged in administrators
func (*Permissions) SetDenyFunction ¶
func (perm *Permissions) SetDenyFunction(f http.HandlerFunc)
SetDenyFunction can be used to specify a http.HandlerFunc for when the permissions are denied
func (*Permissions) SetPublicPath ¶
func (perm *Permissions) SetPublicPath(pathPrefixes []string)
SetPublicPath will add URL prefixes that will enforce that URLs starting with those prefixes will be public. This overrides the other prefixes.
func (*Permissions) SetUserPath ¶
func (perm *Permissions) SetUserPath(pathPrefixes []string)
SetUserPath will add URL prefixes that will enforce that URLs starting with those prefixes will only be for logged in users
func (*Permissions) UserState ¶
func (perm *Permissions) UserState() pinterface.IUserState
UserState will return the UserState struct
type UserState ¶
type UserState struct {
// contains filtered or unexported fields
}
UserState keeps track of all usernames, passwords and information about users
func NewUserState ¶
Create a new *UserState that can be used for managing users. connectionString may be on the form "username:password@host:port/database". If randomSeed is true, the random number generator will be seeded after generating the cookie secret (true is a good default value).
func NewUserStateSimple ¶
Create a new *UserState that can be used for managing users. The random number generator will be seeded after generating the cookie secret.
func NewUserStateWithDSN ¶
func NewUserStateWithDSN(connectionString string, databaseName string, randomSeed bool) (*UserState, error)
Create a new *UserState that can be used for managing users. connectionString may be on the form "username:password@host:port/database". If randomSeed is true, the random number generator will be seeded after generating the cookie secret (true is a good default value).
func (*UserState) AddUnconfirmed ¶
AddUnconfirmed adds a user to a list of users that are registered, but not confirmed.
func (*UserState) AddUser ¶
AddUser creates a user and hashes the password, does not check for rights. The given data must be valid.
func (*UserState) AdminRights ¶
AdminRights checks if the current user is logged in and has administrator rights.
func (*UserState) AllUnconfirmedUsernames ¶
AllUnconfirmedUsernames returns a list of all registered users that are not yet confirmed.
func (*UserState) AllUsernames ¶
AllUsernames returns a list of all usernames.
func (*UserState) AlreadyHasConfirmationCode ¶
AlreadyHasConfirmationCode goes through all the confirmationCodes of all the unconfirmed users and checks if this confirmationCode already is in use.
func (*UserState) BooleanField ¶
BooleanField returns a boolean value for the 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) ClearCookie ¶
func (state *UserState) ClearCookie(w http.ResponseWriter)
ClearCookie tries to clear the user cookie by setting it to be expired. Some browsers *may* be configured to keep cookies even after this.
func (*UserState) Confirm ¶
Confirm marks a user as confirmed, and removes the username from the list of unconfirmed users.
func (*UserState) ConfirmUserByConfirmationCode ¶
ConfirmUserByConfirmationCode takes a unique confirmation code and marks the corresponding unconfirmed user as confirmed.
func (*UserState) ConfirmationCode ¶
ConfirmationCode returns the stored confirmation code for a specific user.
func (*UserState) CookieSecret ¶
CookieSecret returns the current cookie secret
func (*UserState) CookieTimeout ¶
CookieTimeout returns the current login cookie timeout, in seconds.
func (*UserState) CorrectPassword ¶
CorrectPassword checks if a password is correct. "username" is needed because it may be part of the hash for some password hashing algorithms.
func (*UserState) Creator ¶
func (state *UserState) Creator() pinterface.ICreator
Creator returns a struct for creating data structures.
func (*UserState) FindUserByConfirmationCode ¶
FindUserByConfirmationCode tries to find the corresponding username, given a unique confirmation code.
func (*UserState) GenerateUniqueConfirmationCode ¶
GenerateUniqueConfirmationCode generates a unique confirmation code that can be used for confirming users.
func (*UserState) HasEmail ¶
HasEmail finds the user that has a given e-mail address. Returns the username and nil if found or a blank string and ErrNotFound if not.
func (*UserState) HashPassword ¶
HashPassword takes a password and creates a password hash. It also takes a username, since some algorithms may use it for salt.
func (*UserState) Host ¶
func (state *UserState) Host() pinterface.IHost
Host retrieves the underlying database. It helps fulfill the IHost interface.
func (*UserState) IsConfirmed ¶
IsConfirmed checks if a user is confirmed (can be used for "e-mail confirmation").
func (*UserState) IsLoggedIn ¶
IsLoggedIn checks if a user is logged in.
func (*UserState) Login ¶
func (state *UserState) Login(w http.ResponseWriter, username string) error
Login is a convenience function for logging a user in and storing the username in a cookie. Returns an error if the cookie could not be set.
func (*UserState) MarkConfirmed ¶
MarkConfirmed marks a user as being confirmed.
func (*UserState) PasswordAlgo ¶
PasswordAlgo returns the current password hashing algorithm.
func (*UserState) PasswordHash ¶
PasswordHash returns the password hash for the given username.
func (*UserState) RemoveAdminStatus ¶
RemoveAdminStatus removes the administrator status from a user.
func (*UserState) RemoveUnconfirmed ¶
RemoveUnconfirmed removes a user from a list of users that are registered, but not confirmed.
func (*UserState) RemoveUser ¶
RemoveUser removes a user and the login status for this user.
func (*UserState) SetAdminStatus ¶
SetAdminStatus marks a user as an administrator.
func (*UserState) SetBooleanField ¶
SetBooleanField stores a boolean value given a username and a custom fieldname.
func (*UserState) SetCookieSecret ¶
SetCookieSecret sets the current cookie secret
func (*UserState) SetCookieTimeout ¶
SetCookieTimeout sets how long a login cookie should last, in seconds.
func (*UserState) SetLoggedIn ¶
SetLoggedIn marks a user as logged in. Use the Login function instead, unless cookies are not involved.
func (*UserState) SetLoggedOut ¶
SetLoggedOut marks a user as logged out.
func (*UserState) SetMinimumConfirmationCodeLength ¶
SetMinimumConfirmationCodeLength sets the minimum length of the user confirmation code. The default is 20.
func (*UserState) SetPassword ¶
SetPassword sets/changes the password for a user. Does not take a password hash, will hash the password string.
func (*UserState) SetPasswordAlgo ¶
SetPasswordAlgo determines which password hashing algorithm should be used.
* * The default value is "bcrypt+". * * Possible values are: * bcrypt -> Store and check passwords with the bcrypt hash. * sha256 -> Store and check passwords with the sha256 hash. * bcrypt+ -> Store passwords with bcrypt, but check with both * bcrypt and sha256, for backwards compatibility * with old passwords that has been stored as sha256.
func (*UserState) SetTablePrefix ¶
Set a custom PostgreSQL database table prefix
func (*UserState) SetUsernameCookie ¶
func (state *UserState) SetUsernameCookie(w http.ResponseWriter, username string) error
SetUsernameCookie stores the given username in a cookie in the browser, if possible. Will return an error if the username is empty or the user does not exist.
func (*UserState) UserRights ¶
UserRights checks if the current user is logged in and has user rights.
func (*UserState) Username ¶
Username is a convenience function for returning the current username (from the browser cookie), or an empty string.
func (*UserState) UsernameCookie ¶
UsernameCookie retrieves the username that is stored in a cookie in the browser, if available.
func (*UserState) Users ¶
func (state *UserState) Users() pinterface.IHashMap
Users returns a hash map of all the users.