Documentation
¶
Overview ¶
Package auth provides a standard auth for your website. Multiple providers can be added.
Index ¶
- Constants
- Variables
- func AddNavigationPoint(name string, fn func([]string, controller.Interface) ([]Navigation, error))
- func AddProtocol(login string, key string, value ...string) error
- func BuildRouteGuard() error
- func ChangePassword(login string, pwUser string) error
- func ChangePasswordTokenValid(login string, token string) error
- func ConfigureProvider(provider string, options map[string]interface{}) error
- func DeleteUserToken(login string, rt string) error
- func JWTGenerateCallback(w http.ResponseWriter, r *http.Request, c jwt.Claimer, refreshToken string) error
- func JWTRefreshCallback(w http.ResponseWriter, r *http.Request, c jwt.Claimer) error
- func RandomPassword(length int) string
- func Register(name string, provider providerFn) error
- type Base
- type Claim
- type Interface
- type Navigation
- type Option
- type Protocol
- type Rbac
- type RefreshToken
- type Role
- type Schema
- type User
- func (u *User) ComparePassword(hash string, pw string) error
- func (u *User) IncreaseFailedLogin() error
- func (u *User) IsInactive() bool
- func (u *User) IsLocked() bool
- func (u *User) Option(key string) (*Option, error)
- func (u User) OptionsToMap() map[string]string
- func (u *User) SetSecureConfig() error
Constants ¶
const ( ParamLogin = "login" ParamPassword = "password" ParamToken = "token" ParamProvider = "provider" KeyClaim = "claim" KeyLanguages = "languages" )
predefined http parameter and return keys.
const ( LOGIN = "Login" RefreshedToken = "RefreshToken" RefreshedTokenInvalid = "RefreshTokenInvalid" LOGOUT = "Logout" ResetPasswordToken = "ResetPasswordToken" LOCKED = "Locked" INACTIVE = "Inactive" WrongPassword = "WrongPassword" ChangedPassword = "ChangedPassword" )
Pre-defined Protocol keys.
Variables ¶
var ( ErrUserOption = "auth: option %s was not found" ErrUserLocked = errors.New("auth: your user is locked because of too many login attempts") ErrUserInactive = errors.New("auth: your user is inactive") )
Error messages.
var (
ErrProvider = "auth: provider %s is not registered or configured"
)
Error messages.
var ErrRefreshTokenNotValid = errors.New("auth: refresh token is not valid")
Functions ¶
func AddNavigationPoint ¶
func AddNavigationPoint(name string, fn func([]string, controller.Interface) ([]Navigation, error))
AddNavigationPoint to the database navigation. Navigations points can be added to any level. To access a child navigation point use a dot notation. Example: Settings.Accounts
func AddProtocol ¶
AddProtocol is a helper to log a key, value(optional) for the given user id.
func BuildRouteGuard ¶
func BuildRouteGuard() error
BuildRouteGuard is creating a map[PATTERN][HTTPMethod][]roles. The map is used in the RBAC Allowed method.
func ChangePassword ¶
ChangePassword will change the password and delete the pw token.
func ChangePasswordTokenValid ¶
ChangePasswordTokenValid will check if the token was signed the last 15 minutes and if the user is still valid.
func ConfigureProvider ¶
ConfigureProvider will config the provider an add it to a local cache. Error will return if the provider is not allowed by server configuration or it was not registered.
func DeleteUserToken ¶
func JWTGenerateCallback ¶
func JWTGenerateCallback(w http.ResponseWriter, r *http.Request, c jwt.Claimer, refreshToken string) error
JWTGenerateCallback will generate the user claim for the frontend.
func JWTRefreshCallback ¶
JWTRefreshCallback will check if the refresh token is existing and still valid. If so, it will delete the refresh token and generate a new one incl. jwt token. TODO dont delete the rf token each time.
func RandomPassword ¶
RandomPassword generates a random password with the given length.
Types ¶
type Base ¶
Base model is a helper for the default cache and builder.
func (Base) DefaultBuilder ¶
DefaultBuilder of the models.
type Claim ¶
type Claim struct { jwt.Claim UID int Name string Surname string Login string Roles []string Options map[string]string }
Claim will hold the user information.
type Interface ¶
type Interface interface { Login(p controller.Interface) (Schema, error) Logout(p controller.Interface) error ForgotPassword(p controller.Interface) error ChangePassword(p controller.Interface) error RegisterAccount(p controller.Interface) error }
Interface for the providers.
type Navigation ¶
type Navigation struct {}
Navigation struct
func (*Navigation) EndpointsByRoles ¶
func (n *Navigation) EndpointsByRoles(roles []string, controller controller.Interface) ([]Navigation, error)
EndpointsByRoles will return all nav endpoints which are allowed for the given roles. The nav-points are fetched from the navigation database table. Additional navigation points can be added manually - see AddNavigationPoint function. The manually added navigation points have to be added on an early stage (before server.Start()).
type Protocol ¶
type Protocol struct { Base UserID int Key string Value query.NullString }
Protocol struct to log user actions.
func (Protocol) DefaultTableName ¶
DefaultTableName of the protocol model.
type RefreshToken ¶
func (*RefreshToken) DeleteExpired ¶
func (r *RefreshToken) DeleteExpired() error
DeleteExpired refresh tokens of the user account.
type Role ¶
type Role struct { Base Name string `json:",omitempty"` Description query.NullString `json:",omitempty"` Children []Role `json:",omitempty"` Backend []server.Route `orm:"relation:m2m;poly:Route;poly_value:Backend;join_table:role_routes;join_fk:role_id" json:",omitempty"` Frontend []server.Route `orm:"relation:m2m;poly:Route;poly_value:Frontend;join_table:role_routes;join_fk:role_id" json:",omitempty"` }
Role struct is holding the permission for frontend and backend routes. Roles are self referenced.
type Schema ¶
type Schema struct { Provider string UID string Login string Name string Surname string Salutation string Options []Option }
Schema should be used as a return value for the providers. Login will be mandatory and should be the E-Mail address of the user. Additional Options can be added which (will be saved as user options in the database - not implemented yet).
type User ¶
type User struct { Base Login string `json:",omitempty"` Salutation string `json:",omitempty"` Name query.NullString `json:",omitempty"` Surname query.NullString `json:",omitempty"` Email string `json:",omitempty"` State string `json:",omitempty"` LastLogin query.NullTime `json:",omitempty"` FailedLogins query.NullInt `json:",omitempty"` LastFailedLogin query.NullTime `json:",omitempty"` RefreshTokens []RefreshToken `json:",omitempty"` Roles []Role `orm:"relation:m2m" json:",omitempty" validate:"min=1"` Options []Option `json:",omitempty"` // contains filtered or unexported fields }
User model
func UserByLogin ¶
UserByLogin will return the user. Error will return if the user does not exist.
func (*User) ComparePassword ¶
ComparePassword checks the given password with the hashed password.
func (*User) IncreaseFailedLogin ¶
IncreaseFailedLogin will increase the failed logins counter and set the last failed login timestamp.
func (*User) IsInactive ¶
IsInactive is a helper to check if a user is inactive because the duration of the last login is too big.
func (*User) IsLocked ¶
IsLocked is a helper to check if the user is locked because of too many login attempts.
func (*User) Option ¶
Option will return the option by key. Error will return if the option does not exist.
func (User) OptionsToMap ¶
OptionsToMap is a helper to export all user options which are not hidden. This is used for the user claim.
func (*User) SetSecureConfig ¶
SetSecureConfig is adding the lock/inactivity and allowed failed logins.