auth

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2023 License: MIT Imports: 23 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidToken = errors.New("invalid token")
View Source
var ErrTokenExpired = errors.New("token expired")
View Source
var ErrUnknownUser = errors.New("unknown user")

Functions

func Has2FA added in v2.1.0

func Has2FA(user User) bool

Types

type AuthConfig

type AuthConfig struct {
	AuthKey           string                        `json:"key"                 arg:"key"`
	TTL               int                           `json:"ttl"                 arg:"ttl"`
	Issuer            string                        `json:"issuer"              arg:"issuer"`
	Cookie            string                        `json:"cookie"              arg:"cookie"`
	Header            string                        `json:"header"              arg:"header"`
	EmailSender       string                        `json:"email_sender"        arg:"email-sender"`
	ResetTTL          int                           `json:"reset_ttl"           arg:"reset-ttl"`
	ResetTemplate     TemplateConfig                `json:"reset_template"      arg:"reset-template"`
	TwoFactorTemplate TemplateConfig                `json:"two_factor_template" arg:"two-factor-template"`
	SocialLogin       map[string]*SocialLoginConfig `json:"social"`
}

func (*AuthConfig) Init added in v2.1.0

func (cfg *AuthConfig) Init(serverRoot string) error

type AuthUser

type AuthUser interface {
	User
	GetAuth() (authenticator.Authenticator, error)
	SetAuth(authenticator.Authenticator) error
}

type Authenticator

type Authenticator struct {
	UserSource            UserSource
	EmailClient           EmailClient
	SMSClient             SMSClient
	Domain                string
	JWT                   *JWT
	SocialConfig          map[string]*SocialLoginConfig
	EmailSender           string
	ResetTTL              time.Duration
	ResetTextTemplate     Template
	ResetHTMLTemplate     Template
	ResetSMSTemplate      Template
	TwoFactorTextTemplate Template
	TwoFactorHTMLTemplate Template
	TwoFactorSMSTemplate  Template
}

func NewAuthenticator

func NewAuthenticator(cfg AuthConfig, src UserSource) (*Authenticator, error)

func (*Authenticator) LoginAPI

func (a *Authenticator) LoginAPI(router H.Router)

func (*Authenticator) MakeChangePasswordHandler

func (a *Authenticator) MakeChangePasswordHandler() H.HandlerFunc

func (*Authenticator) MakeComplete2FAHandler

func (a *Authenticator) MakeComplete2FAHandler() H.HandlerFunc

func (*Authenticator) MakeInit2FAHandler

func (a *Authenticator) MakeInit2FAHandler() H.HandlerFunc

func (*Authenticator) MakeLogin2FAHandler

func (a *Authenticator) MakeLogin2FAHandler() H.HandlerFunc

func (*Authenticator) MakeLoginHandler

func (a *Authenticator) MakeLoginHandler() H.HandlerFunc

func (*Authenticator) MakeLogoutHandler

func (a *Authenticator) MakeLogoutHandler() H.HandlerFunc

func (*Authenticator) MakeMiddleware

func (a *Authenticator) MakeMiddleware() H.Middleware

func (*Authenticator) MakeResetPasswordHandler

func (a *Authenticator) MakeResetPasswordHandler() H.HandlerFunc

func (*Authenticator) MakeSend2FACodeHandler

func (a *Authenticator) MakeSend2FACodeHandler() H.HandlerFunc

func (*Authenticator) MakeSocialLoginHandlers

func (a *Authenticator) MakeSocialLoginHandlers(router H.Router)

type AvatarUser

type AvatarUser interface {
	User
	GetAvatar() string
}

type Claims

type Claims interface {
	jwt.Claims
	User
	Extend()
	SetUser(user User)
	SetProvider(string)
	GetProvider() string
	SetTwoFactor(bool)
	GetTwoFactor() bool
}

type EmailClient

type EmailClient interface {
	Send(from, to, subject, textContent string, htmlContent *string) error
}

type EmailUser

type EmailUser interface {
	User
	GetEmailAddress() string
}

type FirstLastNameUser

type FirstLastNameUser interface {
	User
	GetFirstName() string
	GetLastName() string
}

type FullNameUser

type FullNameUser interface {
	User
	GetFullName() string
}

type Init2FAResponse

type Init2FAResponse struct {
	URI          string   `json:"uri"`
	QRCode       string   `json:"qr_code"`
	RecoveryKeys []string `json:"recovery_keys"`
}

type IntIDUser

type IntIDUser interface {
	User
	GetUserID() int64
}

type JWT

type JWT struct {
	// contains filtered or unexported fields
}

func NewJWT

func NewJWT(key []byte, ttl time.Duration, issuer, cookieName, headerName string) *JWT

func (*JWT) GetClaims

func (j *JWT) GetClaims(token string) (*StandardClaims, error)

func (*JWT) GetClaimsFromCookie

func (j *JWT) GetClaimsFromCookie(r *http.Request, name string) (*StandardClaims, error)

func (*JWT) GetClaimsFromHeader

func (j *JWT) GetClaimsFromHeader(r *http.Request, name string) (*StandardClaims, error)

func (*JWT) GetClaimsFromRequest

func (j *JWT) GetClaimsFromRequest(r *http.Request) (*StandardClaims, error)

func (*JWT) MakeToken

func (j *JWT) MakeToken(claims *StandardClaims) (string, error)

func (*JWT) NewClaims

func (j *JWT) NewClaims() *StandardClaims

func (*JWT) SetCookie

func (j *JWT) SetCookie(w http.ResponseWriter, claims *StandardClaims) error

type LocaleUser

type LocaleUser interface {
	User
	GetLocale() string
}

type LoginParams

type LoginParams struct {
	Username    *string `json:"username"`
	Password    *string `json:"password"`
	TwoFactor   *string `json:"two_factor_code"`
	ResetCode   *string `json:"reset_code"`
	NewPassword *string `json:"new_password"`
}

type LoginResponse

type LoginResponse struct {
	Username string          `json:"username"`
	Claims   *StandardClaims `json:"claims"`
	Token    string          `json:"token"`
	Needs2FA bool            `json:"needs_two_factor,omitempty"`
}

type PasswordAuth added in v2.1.0

type PasswordAuth interface {
	authenticator.Authenticator
	SetPassword(password string, inputs ...string) error
	ResetPassword(dur time.Duration) (string, error)
	CheckResetCode(code string) error
	IsDirty() bool
}

type PasswordResetData

type PasswordResetData struct {
	Scheme   string
	Hostname string
	Code     string
	Username string
	Expires  time.Time
}

type PhoneUser

type PhoneUser interface {
	User
	GetPhoneNumber() string
}

type SMSClient

type SMSClient interface {
	Send(phoneNumber, content string) error
}

type SocialLoginConfig

type SocialLoginConfig struct {
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
}

type SocialUser

type SocialUser interface {
	AuthUser
	SetSocialID(driver, id string) error
}

type SocialUserSource

type SocialUserSource interface {
	UserSource
	GetSocialUser(driver, id, username string) (AuthUser, error)
}

type StandardClaims

type StandardClaims struct {
	jwt.StandardClaims
	AuthTime  int64                  `json:"auth_time,omitempty"`
	Provider  string                 `json:"x-provider,omitempty"`
	TTL       int64                  `json:"x-ttl,omitempty"`
	TwoFactor bool                   `json:"x-2fa,omitempty"`
	UserID    int64                  `json:"x-userid,omitempty"`
	UserUUID  uuid.UUID              `json:"x-useruuid,omitempty"`
	FirstName string                 `json:"given_name,omitempty"`
	LastName  string                 `json:"family_name,omitempty"`
	FullName  string                 `json:"name,omitempty"`
	Username  string                 `json:"preferred_username,omitempty"`
	Email     string                 `json:"email,omitempty"`
	Phone     string                 `json:"phone_number,omitempty"`
	TimeZone  string                 `json:"zoneinfo,omitempty"`
	Locale    string                 `json:"locale,omitempty"`
	Avatar    string                 `json:"picture,omitempty"`
	Extra     map[string]interface{} `json:"x-extra,omitempty"`
}

func NewStandardClaims

func NewStandardClaims(issuer string, dur time.Duration) *StandardClaims

func (*StandardClaims) Extend

func (c *StandardClaims) Extend()

func (*StandardClaims) GetAvatar

func (c *StandardClaims) GetAvatar() string

func (*StandardClaims) GetEmailAddress

func (c *StandardClaims) GetEmailAddress() string

func (*StandardClaims) GetFirstName

func (c *StandardClaims) GetFirstName() string

func (*StandardClaims) GetFullName

func (c *StandardClaims) GetFullName() string

func (*StandardClaims) GetLastName

func (c *StandardClaims) GetLastName() string

func (*StandardClaims) GetLocale

func (c *StandardClaims) GetLocale() string

func (*StandardClaims) GetPhoneNumber

func (c *StandardClaims) GetPhoneNumber() string

func (*StandardClaims) GetTimeZone

func (c *StandardClaims) GetTimeZone() string

func (*StandardClaims) GetUUID

func (c *StandardClaims) GetUUID() uuid.UUID

func (*StandardClaims) GetUserID

func (c *StandardClaims) GetUserID() int64

func (*StandardClaims) GetUsername

func (c *StandardClaims) GetUsername() string

func (*StandardClaims) SetFullName

func (c *StandardClaims) SetFullName(name string)

func (*StandardClaims) SetUser

func (c *StandardClaims) SetUser(user User)

func (*StandardClaims) Valid

func (c *StandardClaims) Valid() error

type Template

type Template interface {
	Execute(io.Writer, interface{}) error
}

type TemplateConfig

type TemplateConfig struct {
	Text string `json:"text" arg:"text"`
	HTML string `json:"html" arg:"html"`
	SMS  string `json:"sms"  arg:"sms"`
}

func (*TemplateConfig) GetTemplates

func (cfg *TemplateConfig) GetTemplates() (text, html, sms Template, err error)

func (*TemplateConfig) Init added in v2.1.0

func (cfg *TemplateConfig) Init(serverRoot string) error

type TimeZoneUser

type TimeZoneUser interface {
	User
	GetTimeZone() string
}

type TwoFactorAuth added in v2.1.0

type TwoFactorAuth interface {
	authenticator.Authenticator
	GenCode() string
	Configure() (*authenticator.TwoFactorConfig, error)
	IsDirty() bool
}

type TwoFactorData

type TwoFactorData struct {
	Scheme   string
	Hostname string
	Code     string
	Username string
}

type TwoFactorUser added in v2.1.0

type TwoFactorUser interface {
	AuthUser
	GetTwoFactorAuth() (authenticator.Authenticator, error)
	SetTwoFactorAuth(authenticator.Authenticator) error
	InitTwoFactorAuth() (authenticator.Authenticator, error)
	CompleteTwoFactorAuth(code string) error
}

type UUIDUser

type UUIDUser interface {
	User
	GetUUID() uuid.UUID
}

type User

type User interface {
	GetUsername() string
}

func UserFromContext

func UserFromContext(ctx context.Context) User

func UserFromRequest

func UserFromRequest(r *http.Request) User

type UserSource

type UserSource interface {
	GetUser(username string) (AuthUser, error)
	GetUserByEmail(email string) (AuthUser, error)
}

Jump to

Keyboard shortcuts

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