auth

package
v1.6.8 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: GPL-2.0 Imports: 48 Imported by: 0

Documentation

Index

Constants

View Source
const SESSION_COOKIE_NAME = "user_authentication"

Variables

View Source
var (
	SIGNAL_BEFORE_USER_CREATE = user_signal_pool.Get("user.before_create") // -> Send(auth.User) (Returned error unused!)
	SIGNAL_AFTER_USER_CREATE  = user_signal_pool.Get("user.after_create")  // -> Send(auth.User) (Returned error unused!)

	SIGNAL_BEFORE_USER_UPDATE = user_signal_pool.Get("user.before_update") // -> Send(auth.User) (Returned error unused!)
	SIGNAL_AFTER_USER_UPDATE  = user_signal_pool.Get("user.after_update")  // -> Send(auth.User) (Returned error unused!)

	SIGNAL_BEFORE_USER_DELETE = id_signal_pool.Get("user.before_delete") // -> Send(int64) (Returned error unused!)
	SIGNAL_AFTER_USER_DELETE  = id_signal_pool.Get("user.after_delete")  // -> Send(int64) (Returned error unused!)

	SIGNAL_USER_LOGGED_IN  = user_req_pool.Get("auth.logged_in")  // -> Send(auth.User)		  (Returned error unused!)
	SIGNAL_USER_LOGGED_OUT = user_req_pool.Get("auth.logged_out") // -> Send(auth.User(nil))		  (Returned error unused!)
	SIGNAL_LOGIN_FAILED    = signal_pool.Get("auth.login_failed") // -> Send(auth.User, error) (Returned error unused!)
)

import "github.com/Nigel2392/go-signals"

/* Example usage:

auth.SIGNAL_BEFORE_USER_SAVE.Connect(signals.NewRecv(func(s signals.Signal, user ...any) error {
	return nil
}))

*/

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
}
View Source
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 CheckPassword

func CheckPassword(u *models.User, password string) error

func Login

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

func Logout

func Logout(r *http.Request) error

func NewAppConfig

func NewAppConfig() django.AppConfig

func PasswordValidators

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

func SetPassword

func SetPassword(u *models.User, password string) error

func UnAuthenticatedUser

func UnAuthenticatedUser() *models.User

func UserFromRequest

func UserFromRequest(r *http.Request) *models.User

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
	PermQueries    auth_permissions.DBQuerier
	Session        *scs.SessionManager
	LoginWithEmail bool
}

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 *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) Save

func (f *BaseUserForm) Save() (*models.User, 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(flags PasswordCharacterFlag, isRegistering bool, opts ...func(fields.Field)) *PasswordField

func (*PasswordField) Clean

func (p *PasswordField) Clean(value interface{}) (interface{}, error)

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
}

type UserWithRequest added in v1.6.6

type UserWithRequest struct {
	User *models.User
	Req  *http.Request
}

Jump to

Keyboard shortcuts

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