Documentation
¶
Index ¶
- Constants
- func HashPassword(pwd string) (string, error)
- func NewAuthKey(kind, value string) string
- func NewCtx(ctx context.Context, session *Session) context.Context
- func SplitAuthKey(key string) (kind, value string)
- func ValidateAuthKey(key string) error
- type Auth
- func (auth *Auth) Authenticate() func(http.Handler) http.Handler
- func (auth *Auth) CreateSession(_ context.Context, u User) (*Session, error)
- func (auth *Auth) GetUser(ctx context.Context, authKey string) (*User, error)
- func (auth *Auth) RegisterUser(ctx context.Context, u User, loginKeys []Key) (*User, error)
- func (auth *Auth) RestoreSession(_ context.Context, token string) (*Session, error)
- func (auth *Auth) Routes(r chi.Router)
- func (auth *Auth) SetPassword(ctx context.Context, id, password string) error
- func (auth *Auth) SetUserData(ctx context.Context, id string, data UserData) error
- func (auth *Auth) VerifyUser(ctx context.Context, userID, token string) (*User, error)
- type Config
- type Key
- type OAuthConf
- type Session
- type User
- type UserData
Constants ¶
const ( KeyKindID = "id" KeyKindEmail = "email" KeyKindUsername = "username" )
AuthKey kinds.
Variables ¶
This section is empty.
Functions ¶
func HashPassword ¶
HashPassword hashes and returns the PwdHash value.
func NewAuthKey ¶
NewAuthKey returns a new formatted user login-key.
func SplitAuthKey ¶
SplitAuthKey splits the given key-id into its kind and actual value.
func ValidateAuthKey ¶
ValidateAuthKey checks the validity of the login-key.
Types ¶
type Auth ¶
type Auth struct {
// contains filtered or unexported fields
}
Auth represents the auth module and implements user management and authentication facilities.
func (*Auth) Authenticate ¶
Authenticate returns a middleware that can authenticate incoming requests and inject the user into context.
func (*Auth) CreateSession ¶
CreateSession creates a new session for the given user and returns.
func (*Auth) RegisterUser ¶
func (*Auth) RestoreSession ¶
RestoreSession verifies the given token, restores the session and returns. If token is not valid, errors.MissingAuth will be returned.
func (*Auth) SetPassword ¶
func (*Auth) SetUserData ¶
type Config ¶
type Config struct {
SessionTTL time.Duration `mapstructure:"session_ttl"`
SessionCookie string `mapstructure:"session_cookie"`
SigningSecret string `mapstructure:"signing_secret"`
EnabledKinds []string `mapstructure:"enabled_kinds"`
LoginPageRoute string `mapstructure:"login_page_route"`
RegisterPageRoute string `mapstructure:"register_page_route"`
Google OAuthConf `mapstructure:"google"`
Github OAuthConf `mapstructure:"github"`
}
type Session ¶
type Session struct {
ID string
Token string
UserID string
UserKind string
ExpiresAt time.Time
RequestID string
}
func CurSession ¶
CurSession returns the current auth session from the go context. Returns guest session if no value found.
type User ¶
type User struct {
ID string `json:"id"`
Kind string `json:"kind"`
Data UserData `json:"data"`
Email string `json:"email"`
PwdHash *string `json:"pwd_hash,omitempty"`
Username string `json:"username"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
VerifiedAt *time.Time `json:"verified_at"`
VerifyToken *string `json:"verify_token,omitempty"`
Attributes map[string]any `json:"-"`
}
User represents a registered user in the system.
func (*User) CheckPassword ¶
CheckPassword returns true if the given password matches the hashed value of the password in the user object. Returns false if mismatch or no password is set for user.
type UserData ¶
UserData represents the standard user profile data. Refer https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims