Documentation
¶
Index ¶
- Constants
- Variables
- func RequiredEmail(fID string, data map[string]interface{}) error
- func RequiredPassword(fID string, data map[string]interface{}) error
- func RequiredText(fID string, data map[string]interface{}) error
- type AccessTokenProvider
- type Auth
- type Claims
- type DefaultAccessTokenProvider
- type DefaultRefreshTokenProvider
- type GAuth
- func (ga *GAuth) AuthMiddleware(next http.Handler) http.Handler
- func (ga *GAuth) Authorized(r *http.Request) (*Auth, error)
- func (ga *GAuth) CreateAccessToken(ctx context.Context, sub string, grants interface{}, expiry time.Time) (string, error)
- func (ga *GAuth) CreateRefreshToken(ctx context.Context, uid, cid string, expiry time.Time) (string, error)
- func (ga *GAuth) MustInit(debug bool) *GAuth
- func (ga *GAuth) ServeHTTP(w http.ResponseWriter, r *http.Request)
- type Identity
- type IdentityProvider
- type RateLimit
- type RefreshTokenProvider
- type Timeout
- type ValidationError
Constants ¶
const ( // AuthKey used to store value of *Auth in context AuthKey ctxKey = "authKey" // RequestKey for accessing request inside context RequestKey ctxKey = "requestKey" )
const ( FieldActiveID = "active" FieldCodeID = "code" FieldTOTPSecretID = "totpsecret" FieldRecoveryCodesID = "recoverycodes" FieldRememberID = "remember" FieldTermsID = "terms" )
Variables ¶
var ( ErrNoToken = errors.New("no token") ErrInvalidAccessToken = errors.New("invalid access token") )
var ( ErrIdentityNotFound = errors.New("identity not found") // Return this error in IdentityLoad to provide Re-Send Activation link flow ErrIdentityNotActive = errors.New("identity not active") // Return in Token Providers to return 401 instead of 500 ErrTokenDenied = errors.New("token denied") )
Functions ¶
func RequiredEmail ¶
func RequiredPassword ¶
func RequiredText ¶
Types ¶
type AccessTokenProvider ¶
type AccessTokenProvider interface { // Optionally implement this to add additional claims under "grants" // and add more role and access information for your token, this token is what's checked against // your middleware. CreateAccessToken(ctx context.Context, uid string, cid string) (interface{}, error) }
type Auth ¶
type Auth struct { UID string `json:"sub"` Grants json.RawMessage `json:"grants"` }
func AuthFromContext ¶
type DefaultAccessTokenProvider ¶
type DefaultAccessTokenProvider struct {
// contains filtered or unexported fields
}
func (*DefaultAccessTokenProvider) CreateAccessToken ¶
func (da *DefaultAccessTokenProvider) CreateAccessToken(ctx context.Context, uid string, cid string) (interface{}, error)
Default behaviour of access token is check cid against client and current pw hash and "access" grants
type DefaultRefreshTokenProvider ¶
type DefaultRefreshTokenProvider struct {
// contains filtered or unexported fields
}
func (*DefaultRefreshTokenProvider) CreateRefreshToken ¶
func (dr *DefaultRefreshTokenProvider) CreateRefreshToken(ctx context.Context, uid string) (cid string, err error)
Default behaviour of refresh token is using cid -> IP + UserAgent + PWHash
func (*DefaultRefreshTokenProvider) DeleteRefreshToken ¶
func (dr *DefaultRefreshTokenProvider) DeleteRefreshToken(ctx context.Context, uid, cid string) error
Default behaviour of logout is in memory black list of cid that only keeps the last 500
type GAuth ¶
type GAuth struct { // IdentityProvider must be implemented for saving your user and notifications IdentityProvider IdentityProvider // Fields for login/register/settings page fields Fields []*form.Field // Field for email verifications EmailFieldID string // Identity field is the field for logging in IdentityFieldID string // Leave blank to use email link for login PasswordFieldID string // Path for login, register, etc // defaults to /login /register /account /refresh Path form.Path Logger *log.Logger // By default this uses embedded alpineJS AlpineJSURL string // Provide a secret to activate recaptcha in register RecaptchaSiteKey string RecaptchaSecret string // JwtKey used for registration and token login JwtKey []byte BCryptCost int // RefreshTokenCookieName defaults to rtoken with NewDefault(), set to blank to not set a cookie RefreshTokenCookieName string // AccessTokenCookieName default is blank, enable to set access token on / AccessTokenCookieName string // Page branding Brand form.Brand RateLimit RateLimit Timeout Timeout // defaults to "gauth" StructTag string // contains filtered or unexported fields }
GAuth is an HTTPServer which handles login, registration, settings, 2fa, etc.
func NewDefault ¶
func NewDefault(appName string, appURL string, ip IdentityProvider) *GAuth
NewDefault returns a sane default for GAuth, you can override properties
func NewPasswordless ¶
func NewPasswordless(appName string, appURL string, ap IdentityProvider) *GAuth
NewPasswordless returns a passwordless login settings
func (*GAuth) CreateAccessToken ¶
func (ga *GAuth) CreateAccessToken(ctx context.Context, sub string, grants interface{}, expiry time.Time) (string, error)
CreateAccessToken returns an access token
type Identity ¶
type Identity interface { // IdentitySave is called to safely save an account, fields provided with "gauth" tag will // automatically be updated with it's corresponding values based on registration/login/account // forms. Return the unique identifier of this account once saved. IdentitySave(ctx context.Context) (uid string, err error) }
type IdentityProvider ¶
type IdentityProvider interface { // IdentityUID should return a unique identifier from your Identifier field(email/username) // this will be use as the subject in your refresh and access token, you should return // ErrIdentityNotFound if it doesn't exists or ErrIdentityNotActive if they are not allowed to login while inactive. IdentityUID(ctx context.Context, id string) (uid string, err error) // IdentityLoad must return a struct that implements Identity interface, provide "gauth" tag // to map gauth.Fields ID to your struct properties. If the account does not exists you must // return an zero/default struct Identity that will be populated for a new registration. IdentityLoad(ctx context.Context, uid string) (identity Identity, err error) }
IdentityProvider must be implemented to login, register, update your user/account.
type RefreshTokenProvider ¶
type RefreshTokenProvider interface { CreateRefreshToken(ctx context.Context, uid string) (cid string, err error) // Called on logout DeleteRefreshToken(ctx context.Context, uid, cid string) error }
Optionally implement this interface to customize your refresh token with a specific client ID or anything that can be identified that is linked to the UID so you can easily revoke it somewhere.
type ValidationError ¶
func (ValidationError) Error ¶
func (ve ValidationError) Error() string