Documentation
¶
Index ¶
- Constants
- Variables
- func AddUserMiddleware() mux.Middleware
- func CheckPassword(u *models.User, password string) error
- func Login(r *http.Request, u *models.User) (*models.User, error)
- func Logout(r *http.Request) error
- func LogoutView(w http.ResponseWriter, r *http.Request)
- func NewAppConfig() django.AppConfig
- func PasswordValidators(fn ...func(PasswordString) error) func(*PasswordField)
- func SetPassword(u *models.User, password string) error
- func UnAuthenticatedUser() *models.User
- func UserFromRequest(r *http.Request) *models.User
- func UserFromRequestPure(r *http.Request) authentication.User
- func ValidateCharacters(isRegister bool, flags PasswordCharacterFlag) func(fields.Field)
- type AuthApplication
- type AuthView
- type BaseUserForm
- type PasswordCharValidator
- type PasswordCharacterFlag
- type PasswordField
- type PasswordFieldOptions
- type PasswordString
- type RegisterFormConfig
Constants ¶
const ( APP_AUTH_EMAIL_LOGIN = "AUTH_EMAIL_LOGIN" // type: bool APP_REGISTER_AUTH_URLS = "REGISTER_AUTH_URLS" // type: bool APP_LOGIN_REDIRECT_URL = "LOGIN_REDIRECT_URL" // type: string || func(*http.Request) string )
const SESSION_COOKIE_NAME = "user_authentication"
Variables ¶
var CHECKER = func(hashedPassword, password string) error { if err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)); err != nil { return autherrors.ErrPwdHashMismatch } return nil }
var ChrFlagDEFAULT = ChrFlagAll
var HASHER = func(b string) (string, error) { var bytes, err = bcrypt.GenerateFromPassword([]byte(b), bcrypt.DefaultCost) return string(bytes), err }
var IS_HASHED = func(hashedPassword string) bool { return isBcryptHash(string(hashedPassword)) }
This function likely cannot 100% guarantee that the password is hashed. It might be susceptible to false positives or meticulously crafted user input.
Functions ¶
func AddUserMiddleware ¶
func AddUserMiddleware() mux.Middleware
Add a user to a request, if one exists in the session.
func LogoutView ¶ added in v1.6.9
func LogoutView(w http.ResponseWriter, r *http.Request)
func NewAppConfig ¶
func PasswordValidators ¶
func PasswordValidators(fn ...func(PasswordString) error) func(*PasswordField)
func UnAuthenticatedUser ¶
func UserFromRequest ¶
Get the user from a request.
func UserFromRequestPure ¶
func UserFromRequestPure(r *http.Request) authentication.User
func ValidateCharacters ¶
func ValidateCharacters(isRegister bool, flags PasswordCharacterFlag) func(fields.Field)
Checks if: - password is at least minlen characters long - password is at most maxlen characters long - password contains at least one special character if specified - password contains at least one uppercase letter - password contains at least one lowercase letter - password contains at least one digit - password contains at least one non-digit - password does not contain any whitespace
Types ¶
type AuthApplication ¶
type AuthApplication struct { *apps.AppConfig Queries models.DBQuerier Session *scs.SessionManager LoginWithEmail bool }
The AuthApplication struct is the main struct used for the auth app.
var Auth *AuthApplication = &AuthApplication{}
type AuthView ¶
type AuthView[T forms.Form] struct { *views.BaseView OnSuccess func(w http.ResponseWriter, req *http.Request, form T) error // contains filtered or unexported fields }
func RegisterView ¶
func RegisterView(baseView *views.BaseView, cfg RegisterFormConfig, opts ...func(forms.Form)) *AuthView[*BaseUserForm]
func (*AuthView[T]) GetContext ¶
type BaseUserForm ¶
type BaseUserForm struct { *forms.BaseForm Request *http.Request Instance *models.User // contains filtered or unexported fields }
func UserLoginForm ¶
func UserLoginForm(r *http.Request, formOpts ...func(forms.Form)) *BaseUserForm
func UserRegisterForm ¶
func UserRegisterForm(r *http.Request, registerConfig RegisterFormConfig, formOpts ...func(forms.Form)) *BaseUserForm
func (*BaseUserForm) Login ¶
func (f *BaseUserForm) Login() error
func (*BaseUserForm) SetRequest ¶
func (f *BaseUserForm) SetRequest(r *http.Request)
type PasswordCharValidator ¶
type PasswordCharValidator struct { GenericError error Flags PasswordCharacterFlag }
func (*PasswordCharValidator) Validate ¶
func (p *PasswordCharValidator) Validate(password string) error
type PasswordCharacterFlag ¶
type PasswordCharacterFlag uint8
const ( ChrFlagSpecial PasswordCharacterFlag = 1 << iota ChrFlagDigit ChrFlagLower ChrFlagUpper ChrFlagAll = ChrFlagSpecial | ChrFlagDigit | ChrFlagLower | ChrFlagUpper )
type PasswordField ¶
type PasswordField struct { *fields.BaseField Validators []func(PasswordString) error }
func NewPasswordField ¶
func NewPasswordField(config PasswordFieldOptions, opts ...func(fields.Field)) *PasswordField
func (*PasswordField) Clean ¶
func (p *PasswordField) Clean(value interface{}) (interface{}, error)
type PasswordFieldOptions ¶ added in v1.7.0
type PasswordFieldOptions struct { Flags PasswordCharacterFlag IsRegistering bool UseDefaultOptions bool Options []func(fields.Field) }
type PasswordString ¶
type PasswordString string
type RegisterFormConfig ¶
type RegisterFormConfig struct { // Include both email and username fields in the registration form. // // If this is false - only the field specified by `LoginWithEmail` will be // included in the form. AlwaysAllLoginFields bool // Automatically login the user after registration. // // This requires a non-nil http request to be passed to the form. AutoLogin bool // Ask for the user's first and last name. AskForNames bool // Create an inactive user account. // // This is useful for when the user needs to verify their email address // before they can login. IsInactive bool }