Documentation
¶
Index ¶
- Constants
- Variables
- func AttachUser() router.MiddlewareFunc
- func GenerateToken(claims jwt.Claims) (string, error)
- func ParseOf[T jwt.Claims](token string) (T, error)
- func Register[T User](ctx context.Context)
- func RegisterRoutes(r *router.Router, controller AuthController)
- func SetAppKey(key []byte)
- type AuthController
- type AuthOption
- type BasicAuthController
- func (o *BasicAuthController[T]) ChangePassword() http.Handler
- func (o *BasicAuthController[T]) ForgotPassword() http.Handler
- func (o *BasicAuthController[T]) Login() http.Handler
- func (o *BasicAuthController[T]) Refresh() http.Handler
- func (o *BasicAuthController[T]) ResetPassword() http.Handler
- func (o *BasicAuthController[T]) RunChangePassword(r *ChangePasswordRequest[T]) (*ChangePasswordResponse[T], error)
- func (o *BasicAuthController[T]) RunForgotPassword(r *ForgotPasswordRequest) (*ForgotPasswordResponse, error)
- func (o *BasicAuthController[T]) RunLogin(r *LoginRequest) (*LoginResponse, error)
- func (o *BasicAuthController[T]) RunRefresh(r *RefreshRequest[T]) (*LoginResponse, error)
- func (o *BasicAuthController[T]) RunResetPassword(r *ResetPasswordRequest) (*ResetPasswordResponse[T], error)
- func (o *BasicAuthController[T]) RunUserCreate(u T, r *UserCreateRequest) (*UserCreateResponse[T], error)
- func (o *BasicAuthController[T]) RunVerifyEmail(r *VerifyEmailRequest) (http.Handler, error)
- func (o *BasicAuthController[T]) UserCreate() http.Handler
- func (o *BasicAuthController[T]) VerifyEmail() http.Handler
- type ChangePasswordRequest
- type ChangePasswordResponse
- type Claims
- func (c *Claims) WithAudience(aud []string) *Claims
- func (c *Claims) WithExpirationTime(exp time.Time) *Claims
- func (c *Claims) WithIssuedAtTime(iat time.Time) *Claims
- func (c *Claims) WithIssuer(iss string) *Claims
- func (c *Claims) WithJWTID(jti string) *Claims
- func (c *Claims) WithLifetime(duration time.Duration) *Claims
- func (c *Claims) WithNotBeforeTime(nbf time.Time) *Claims
- func (c *Claims) WithScopes(scope ...string) *Claims
- func (c *Claims) WithSubject(sub string) *Claims
- type CreateUserFunc
- type EmailVerified
- type EmailVerifiedUser
- func (v *EmailVerifiedUser) GetEmail() string
- func (u *EmailVerifiedUser) GetID() string
- func (u *EmailVerifiedUser) GetPasswordHash() []byte
- func (u *EmailVerifiedUser) GetUsername() string
- func (v *EmailVerifiedUser) IsVerified() bool
- func (u *EmailVerifiedUser) LookupTokenColumn() string
- func (u *EmailVerifiedUser) PasswordColumn() string
- func (u *EmailVerifiedUser) SaltedPassword(password string) []byte
- func (v *EmailVerifiedUser) SetLookupToken(t string)
- func (u *EmailVerifiedUser) SetPasswordHash(b []byte)
- func (v *EmailVerifiedUser) SetVerified(verified bool)
- func (u *EmailVerifiedUser) UsernameColumns() []string
- type EmailVerifiedUserCreateRequest
- type ForgotPasswordRequest
- type ForgotPasswordResponse
- type HasClaimMiddleware
- type LoggedInMiddleware
- type LoginRequest
- type LoginResponse
- type RefreshRequest
- type ResetPasswordRequest
- type ResetPasswordResponse
- type ScopeStrings
- type User
- type UserCreateRequest
- type UserCreateResponse
- type UsernameUser
- func (u *UsernameUser) GetID() string
- func (u *UsernameUser) GetPasswordHash() []byte
- func (u *UsernameUser) GetUsername() string
- func (u *UsernameUser) PasswordColumn() string
- func (u *UsernameUser) SaltedPassword(password string) []byte
- func (u *UsernameUser) SetPasswordHash(b []byte)
- func (u *UsernameUser) UsernameColumns() []string
- type UsernameUserCreateRequest
- type VerifyEmailRequest
Constants ¶
const ( ScopeRefresh = "refresh" ScopeAccess = "access" )
Variables ¶
var ( ErrMissingAuthorizationHeader = fmt.Errorf("missing Authorization header") ErrInvalidAuthorizationHeader = fmt.Errorf("invalid Authorization header") ErrUnexpectedAlgorithm = fmt.Errorf("unexpected algorithm") ErrNoAccessScope = fmt.Errorf("no access scope") )
var ( ErrInvalidUserPass = errors.New("invalid username or password") ErrTokenNotFound = errors.New("token not found") ErrNonEmailVerifiedUser = errors.New("non email verified user") )
var (
)
var ErrInvalidToken = fmt.Errorf("invalid token")
Functions ¶
func AttachUser ¶
func AttachUser() router.MiddlewareFunc
func RegisterRoutes ¶
func RegisterRoutes(r *router.Router, controller AuthController)
Types ¶
type AuthController ¶
type AuthOption ¶
type AuthOption func(a *basicAuthController) *basicAuthController
func AccessTokenOptions ¶
func AccessTokenOptions[T User](cb func(u T, claims *Claims) jwt.Claims) AuthOption
func CreateUser ¶
func CreateUser[T User, TRequest any](cb func(r *TRequest, c *BasicAuthController[T]) (*UserCreateResponse[T], error)) AuthOption
func RefreshTokenOptions ¶
func RefreshTokenOptions[T User](cb func(u T, claims *Claims) jwt.Claims) AuthOption
func ResetPasswordName ¶
func ResetPasswordName(name string) AuthOption
type BasicAuthController ¶
type BasicAuthController[T User] struct { // contains filtered or unexported fields }
func NewBasicAuthController ¶
func NewBasicAuthController[T User](options ...AuthOption) *BasicAuthController[T]
func (*BasicAuthController[T]) ChangePassword ¶
func (o *BasicAuthController[T]) ChangePassword() http.Handler
func (*BasicAuthController[T]) ForgotPassword ¶
func (o *BasicAuthController[T]) ForgotPassword() http.Handler
func (*BasicAuthController[T]) Login ¶
func (o *BasicAuthController[T]) Login() http.Handler
func (*BasicAuthController[T]) Refresh ¶
func (o *BasicAuthController[T]) Refresh() http.Handler
func (*BasicAuthController[T]) ResetPassword ¶
func (o *BasicAuthController[T]) ResetPassword() http.Handler
func (*BasicAuthController[T]) RunChangePassword ¶
func (o *BasicAuthController[T]) RunChangePassword(r *ChangePasswordRequest[T]) (*ChangePasswordResponse[T], error)
func (*BasicAuthController[T]) RunForgotPassword ¶
func (o *BasicAuthController[T]) RunForgotPassword(r *ForgotPasswordRequest) (*ForgotPasswordResponse, error)
func (*BasicAuthController[T]) RunLogin ¶
func (o *BasicAuthController[T]) RunLogin(r *LoginRequest) (*LoginResponse, error)
func (*BasicAuthController[T]) RunRefresh ¶
func (o *BasicAuthController[T]) RunRefresh(r *RefreshRequest[T]) (*LoginResponse, error)
func (*BasicAuthController[T]) RunResetPassword ¶
func (o *BasicAuthController[T]) RunResetPassword(r *ResetPasswordRequest) (*ResetPasswordResponse[T], error)
func (*BasicAuthController[T]) RunUserCreate ¶
func (o *BasicAuthController[T]) RunUserCreate(u T, r *UserCreateRequest) (*UserCreateResponse[T], error)
func (*BasicAuthController[T]) RunVerifyEmail ¶
func (o *BasicAuthController[T]) RunVerifyEmail(r *VerifyEmailRequest) (http.Handler, error)
func (*BasicAuthController[T]) UserCreate ¶
func (o *BasicAuthController[T]) UserCreate() http.Handler
func (*BasicAuthController[T]) VerifyEmail ¶
func (o *BasicAuthController[T]) VerifyEmail() http.Handler
type ChangePasswordRequest ¶
type ChangePasswordResponse ¶
type ChangePasswordResponse[T User] struct { User T `json:"user"` }
type Claims ¶
type Claims struct {
jwt.RegisteredClaims
// The `scope` (Scope) claim. See https://www.rfc-editor.org/rfc/rfc8693.html#name-scope-scopes-claim
Scope ScopeStrings `json:"scope,omitempty"`
}
func (*Claims) WithAudience ¶
The "aud" (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the "aud" claim when this claim is present, then the JWT MUST be rejected. In the general case, the "aud" value is an array of case- sensitive strings, each containing a StringOrURI value. In the special case when the JWT has one audience, the "aud" value MAY be a single case-sensitive string containing a StringOrURI value. The interpretation of audience values is generally application specific. Use of this claim is OPTIONAL.
func (*Claims) WithExpirationTime ¶
The "exp" (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing. The processing of the "exp" claim requires that the current date/time MUST be before the expiration date/time listed in the "exp" claim. Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a NumericDate value. Use of this claim is OPTIONAL.
func (*Claims) WithIssuedAtTime ¶
The "iat" (issued at) claim identifies the time at which the JWT was issued. This claim can be used to determine the age of the JWT. Its value MUST be a number containing a NumericDate value. Use of this claim is OPTIONAL.
func (*Claims) WithIssuer ¶
The "iss" (issuer) claim identifies the principal that issued the JWT. The processing of this claim is generally application specific. The "iss" value is a case-sensitive string containing a StringOrURI value. Use of this claim is OPTIONAL.
func (*Claims) WithJWTID ¶
The "jti" (JWT ID) claim provides a unique identifier for the JWT. The identifier value MUST be assigned in a manner that ensures that there is a negligible probability that the same value will be accidentally assigned to a different data object; if the application uses multiple issuers, collisions MUST be prevented among values produced by different issuers as well. The "jti" claim can be used to prevent the JWT from being replayed. The "jti" value is a case- sensitive string. Use of this claim is OPTIONAL.
func (*Claims) WithNotBeforeTime ¶
The "nbf" (not before) claim identifies the time before which the JWT MUST NOT be accepted for processing. The processing of the "nbf" claim requires that the current date/time MUST be after or equal to the not-before date/time listed in the "nbf" claim. Implementers MAY provide for some small leeway, usually no more than a few minutes, to account for clock skew. Its value MUST be a number containing a NumericDate value. Use of this claim is OPTIONAL.
func (*Claims) WithScopes ¶
The value of the scope claim is a JSON string containing a space-separated list of scopes associated with the token, in the format described in Section 3.3 of [RFC6749].
https://www.rfc-editor.org/rfc/rfc8693.html#name-scope-scopes-claim
func (*Claims) WithSubject ¶
The "sub" (subject) claim identifies the principal that is the subject of the JWT. The claims in a JWT are normally statements about the subject. The subject value MUST either be scoped to be locally unique in the context of the issuer or be globally unique. The processing of this claim is generally application specific. The "sub" value is a case-sensitive string containing a StringOrURI value. Use of this claim is OPTIONAL.
type CreateUserFunc ¶
type CreateUserFunc[T User] func(user T, req *UserCreateRequest) (*UserCreateResponse[T], error)
type EmailVerified ¶
type EmailVerifiedUser ¶
type EmailVerifiedUser struct {
model.BaseModel
ID uuid.UUID `json:"id" db:"id,primary"`
Email string `json:"email" db:"email,unique"`
PasswordHash []byte `json:"-" db:"password"`
Verified bool `json:"-" db:"validated"`
LookupToken string `json:"-" db:"lookup_token"`
}
func (*EmailVerifiedUser) GetEmail ¶
func (v *EmailVerifiedUser) GetEmail() string
func (*EmailVerifiedUser) GetID ¶
func (u *EmailVerifiedUser) GetID() string
func (*EmailVerifiedUser) GetPasswordHash ¶
func (u *EmailVerifiedUser) GetPasswordHash() []byte
func (*EmailVerifiedUser) GetUsername ¶
func (u *EmailVerifiedUser) GetUsername() string
func (*EmailVerifiedUser) IsVerified ¶
func (v *EmailVerifiedUser) IsVerified() bool
func (*EmailVerifiedUser) LookupTokenColumn ¶
func (u *EmailVerifiedUser) LookupTokenColumn() string
func (*EmailVerifiedUser) PasswordColumn ¶
func (u *EmailVerifiedUser) PasswordColumn() string
func (*EmailVerifiedUser) SaltedPassword ¶
func (u *EmailVerifiedUser) SaltedPassword(password string) []byte
func (*EmailVerifiedUser) SetLookupToken ¶
func (v *EmailVerifiedUser) SetLookupToken(t string)
func (*EmailVerifiedUser) SetPasswordHash ¶
func (u *EmailVerifiedUser) SetPasswordHash(b []byte)
func (*EmailVerifiedUser) SetVerified ¶
func (v *EmailVerifiedUser) SetVerified(verified bool)
func (*EmailVerifiedUser) UsernameColumns ¶
func (u *EmailVerifiedUser) UsernameColumns() []string
type EmailVerifiedUserCreateRequest ¶
type EmailVerifiedUserCreateRequest struct {
UserCreateRequest
Email string `json:"username" validate:"required|email"`
}
type ForgotPasswordRequest ¶
type ForgotPasswordRequest struct {
Email string `json:"email" validate:"required|email"`
Update database.Update `inject:""`
Ctx context.Context `inject:""`
Mailer email.Mailer `inject:""`
Logger *slog.Logger `inject:""`
URL router.URLResolver `inject:""`
Template *view.ViewTemplate `inject:",optional"`
}
type ForgotPasswordResponse ¶
type ForgotPasswordResponse struct {
}
type HasClaimMiddleware ¶
type HasClaimMiddleware struct {
// contains filtered or unexported fields
}
func HasClaim ¶
func HasClaim(validate func(c *Claims) bool) *HasClaimMiddleware
func (*HasClaimMiddleware) Middleware ¶
func (m *HasClaimMiddleware) Middleware(next http.Handler) http.Handler
type LoggedInMiddleware ¶
type LoggedInMiddleware struct {
// contains filtered or unexported fields
}
func LoggedIn ¶
func LoggedIn() *LoggedInMiddleware
func (*LoggedInMiddleware) Middleware ¶
func (m *LoggedInMiddleware) Middleware(next http.Handler) http.Handler
func (*LoggedInMiddleware) OperationMiddleware ¶
func (m *LoggedInMiddleware) OperationMiddleware(s *spec.Operation) *spec.Operation
func (*LoggedInMiddleware) SecurityDefinition ¶
func (m *LoggedInMiddleware) SecurityDefinition(name string) *LoggedInMiddleware
type LoginRequest ¶
type LoginResponse ¶
type RefreshRequest ¶
type ResetPasswordRequest ¶
type ResetPasswordResponse ¶
type ResetPasswordResponse[T User] struct { User T `json:"user"` }
type ScopeStrings ¶
type ScopeStrings []string
func (ScopeStrings) MarshalJSON ¶
func (s ScopeStrings) MarshalJSON() ([]byte, error)
func (*ScopeStrings) UnmarshalJSON ¶
func (s *ScopeStrings) UnmarshalJSON(b []byte) error
type UserCreateRequest ¶
type UserCreateRequest struct {
Password string `json:"password" validate:"required"`
Mailer email.Mailer `inject:""`
Update database.Update `inject:""`
Ctx context.Context `inject:""`
Logger *slog.Logger `inject:""`
URL router.URLResolver `inject:""`
Template *view.ViewTemplate `inject:",optional"`
}
type UserCreateResponse ¶
type UserCreateResponse[T User] struct { User T `json:"user"` }
func NewEmailVerifiedUser ¶
func NewEmailVerifiedUser(request *EmailVerifiedUserCreateRequest, c *BasicAuthController[*EmailVerifiedUser]) (*UserCreateResponse[*EmailVerifiedUser], error)
func NewUsernameUser ¶
func NewUsernameUser(request *UsernameUserCreateRequest, c *BasicAuthController[*UsernameUser]) (*UserCreateResponse[*UsernameUser], error)
type UsernameUser ¶
type UsernameUser struct {
model.BaseModel
ID uuid.UUID `json:"id" db:"id,primary"`
Username string `json:"username" db:"username,unique"`
PasswordHash []byte `json:"-" db:"password"`
}
func (*UsernameUser) GetID ¶
func (u *UsernameUser) GetID() string
func (*UsernameUser) GetPasswordHash ¶
func (u *UsernameUser) GetPasswordHash() []byte
func (*UsernameUser) GetUsername ¶
func (u *UsernameUser) GetUsername() string
func (*UsernameUser) PasswordColumn ¶
func (u *UsernameUser) PasswordColumn() string
func (*UsernameUser) SaltedPassword ¶
func (u *UsernameUser) SaltedPassword(password string) []byte
func (*UsernameUser) SetPasswordHash ¶
func (u *UsernameUser) SetPasswordHash(b []byte)
func (*UsernameUser) UsernameColumns ¶
func (u *UsernameUser) UsernameColumns() []string
type UsernameUserCreateRequest ¶
type UsernameUserCreateRequest struct {
UserCreateRequest
Username string `json:"username" validate:"required"`
}