auth

package
v1.7.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2026 License: GPL-2.0 Imports: 50 Imported by: 1

Documentation

Index

Constants

View Source
const (
	APPVAR_AUTH_EMAIL_LOGIN       = "AUTH_EMAIL_LOGIN"       // type: bool
	APPVAR_REGISTER_AUTH_URLS     = "REGISTER_AUTH_URLS"     // type: bool
	APPVAR_ALLOW_USER_REGISTER    = "ALLOW_USER_REGISTER"    // type: bool
	APPVAR_LOGIN_REDIRECT_URL     = "LOGIN_REDIRECT_URL"     // type: string || func(*http.Request) string
	APPVAR_LOGIN_VIEW_REVERSE_URL = "LOGIN_VIEW_REVERSE_URL" // type: string

	DEFAULT_LOGIN_REDIRECT_URL = "/" // default value for LOGIN_REDIRECT_URL
)
View Source
const SESSION_COOKIE_NAME = "user_authentication"

Variables

View Source
var CHECKER = func(hashedPassword, password string) error {
	if err := bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password)); err != nil {
		return autherrors.ErrPwdHashMismatch
	}
	return nil
}
View Source
var ChrFlagDEFAULT = ChrFlagAll
View Source
var HASHER = func(b string) (string, error) {
	var bytes, err = bcrypt.GenerateFromPassword([]byte(b), bcrypt.DefaultCost)
	return string(bytes), err
}

Functions

func AddUserMiddleware

func AddUserMiddleware() mux.Middleware

Add a user to a request, if one exists in the session.

func CheckPassword

func CheckPassword(u *User, password string) error

func Logout

func Logout(r *http.Request) error

func LogoutView added in v1.6.9

func LogoutView(w http.ResponseWriter, r *http.Request)

func NewAppConfig

func NewAppConfig() django.AppConfig

func PasswordValidators

func PasswordValidators(fn ...func(*Password) error) func(*PasswordField)

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
	Session         *scs.SessionManager
	LoginWithEmail  bool
	LoginReverseURL string
}

The AuthApplication struct is the main struct used for the auth app.

func (*AuthApplication) Check added in v1.7.2

func (app *AuthApplication) Check(ctx context.Context, settings django.Settings) []checks.Message

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 LoginView

func LoginView(baseView *views.BaseView, opts ...func(forms.Form)) *AuthView[*BaseUserForm]

func RegisterView

func RegisterView(baseView *views.BaseView, cfg RegisterFormConfig, opts ...func(forms.Form)) *AuthView[*BaseUserForm]

func (*AuthView[T]) GetContext

func (v *AuthView[T]) GetContext(req *http.Request) (ctx.Context, error)

func (*AuthView[T]) Render

func (v *AuthView[T]) Render(w http.ResponseWriter, req *http.Request, templateName string, context ctx.Context) (err error)

type BaseUserForm

type BaseUserForm struct {
	*forms.BaseForm
	Request  *http.Request
	Instance *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) Save

func (f *BaseUserForm) Save() (*User, error)

func (*BaseUserForm) SetRequest

func (f *BaseUserForm) SetRequest(r *http.Request)

type Password added in v1.7.2

type Password struct {
	Raw string `json:"raw"`

	Hash string `json:"hash"`
	// contains filtered or unexported fields
}

func NewHashedPassword added in v1.7.2

func NewHashedPassword(hash string) *Password

func NewPassword added in v1.7.2

func NewPassword(raw string) *Password

func (*Password) Check added in v1.7.2

func (p *Password) Check(password string) error

func (*Password) DBType added in v1.7.2

func (p *Password) DBType() dbtype.Type

func (*Password) IsZero added in v1.7.2

func (p *Password) IsZero() bool

func (*Password) Scan added in v1.7.2

func (p *Password) Scan(value any) error

func (*Password) String added in v1.7.2

func (p *Password) String() string

func (*Password) Value added in v1.7.2

func (p *Password) Value() (driver.Value, error)

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(*Password) error
}

func NewPasswordField

func NewPasswordField(config PasswordFieldOptions, opts ...func(fields.Field)) *PasswordField

func (*PasswordField) Clean

func (p *PasswordField) Clean(ctx context.Context, value interface{}) (interface{}, error)

func (*PasswordField) ValueToForm added in v1.7.2

func (p *PasswordField) ValueToForm(value interface{}) interface{}

type PasswordFieldOptions added in v1.7.0

type PasswordFieldOptions struct {
	Flags             PasswordCharacterFlag
	IsRegistering     bool
	UseDefaultOptions bool
	Options           []func(fields.Field)
}

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
}

type User added in v1.7.2

type User struct {
	models.Model `table:"auth_users" json:"-"`
	users.Base

	ID        uint64           `json:"id" attrs:"primary;readonly"`
	Email     *drivers.Email   `json:"email"`
	Username  string           `json:"username"`
	Password  *Password        `json:"password"`
	FirstName string           `json:"first_name"`
	LastName  string           `json:"last_name"`
	CreatedAt drivers.DateTime `json:"created_at" attrs:"readonly"`
	UpdatedAt drivers.DateTime `json:"updated_at" attrs:"readonly"`
}

func Login

func Login(r *http.Request, u *User) (*User, error)

func UnAuthenticatedUser

func UnAuthenticatedUser() *User

func UserFromRequest

func UserFromRequest(r *http.Request) *User

Get the user from a request.

func (*User) BeforeCreate added in v1.7.2

func (u *User) BeforeCreate(ctx context.Context) error

func (*User) BeforeSave added in v1.7.2

func (u *User) BeforeSave(ctx context.Context) error

func (*User) DatabaseIndexes added in v1.7.2

func (u *User) DatabaseIndexes(obj attrs.Definer) []migrator.Index

func (*User) FieldDefs added in v1.7.2

func (u *User) FieldDefs() attrs.Definitions

func (*User) Fields added in v1.7.2

func (u *User) Fields() []any

func (*User) SetPassword added in v1.7.2

func (u *User) SetPassword(password string) *User

func (*User) String added in v1.7.2

func (u *User) String() string

func (*User) UniqueTogether added in v1.7.2

func (u *User) UniqueTogether() [][]string

type UserQuerySet added in v1.7.2

type UserQuerySet struct {
	*queries.WrappedQuerySet[*User, *UserQuerySet, *queries.QuerySet[*User]]
}

func GetUserQuerySet added in v1.7.2

func GetUserQuerySet() *UserQuerySet

func (*UserQuerySet) CloneQuerySet added in v1.7.2

func (qs *UserQuerySet) CloneQuerySet(wrapped *queries.WrappedQuerySet[*User, *UserQuerySet, *queries.QuerySet[*User]]) *UserQuerySet

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL