auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	KeyKindID       = "id"
	KeyKindEmail    = "email"
	KeyKindUsername = "username"
)

AuthKey kinds.

Variables

This section is empty.

Functions

func HashPassword

func HashPassword(pwd string) (string, error)

HashPassword hashes and returns the PwdHash value.

func NewAuthKey

func NewAuthKey(kind, value string) string

NewAuthKey returns a new formatted user login-key.

func NewCtx

func NewCtx(ctx context.Context, session *Session) context.Context

NewCtx returns a new Go context with auth session injected.

func SplitAuthKey

func SplitAuthKey(key string) (kind, value string)

SplitAuthKey splits the given key-id into its kind and actual value.

func ValidateAuthKey

func ValidateAuthKey(key string) error

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 Init

func Init(conn *pgx.Conn, baseURL string, cfg Config) (*Auth, error)

Init initialises auth module and returns.

func (*Auth) Authenticate

func (auth *Auth) Authenticate() func(http.Handler) http.Handler

Authenticate returns a middleware that can authenticate incoming requests and inject the user into context.

func (*Auth) CreateSession

func (auth *Auth) CreateSession(_ context.Context, u User) (*Session, error)

CreateSession creates a new session for the given user and returns.

func (*Auth) GetUser

func (auth *Auth) GetUser(ctx context.Context, authKey string) (*User, error)

GetUser finds a user by given key.

func (*Auth) RegisterUser

func (auth *Auth) RegisterUser(ctx context.Context, u User, loginKeys []Key) (*User, error)

func (*Auth) RestoreSession

func (auth *Auth) RestoreSession(_ context.Context, token string) (*Session, error)

RestoreSession verifies the given token, restores the session and returns. If token is not valid, errors.MissingAuth will be returned.

func (*Auth) Routes

func (auth *Auth) Routes(r chi.Router)

Routes installs auth module routes onto the given router.

func (*Auth) SetPassword

func (auth *Auth) SetPassword(ctx context.Context, id, password string) error

func (*Auth) SetUserData

func (auth *Auth) SetUserData(ctx context.Context, id string, data UserData) error

func (*Auth) VerifyUser

func (auth *Auth) VerifyUser(ctx context.Context, userID, token string) (*User, error)

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 Key

type Key struct {
	Key     string         `json:"key"`
	Attribs map[string]any `json:"attribs"`
}

Key represents additional auth key for a user.

type OAuthConf

type OAuthConf struct {
	Scopes       []string `mapstructure:"scopes"`
	ClientID     string   `mapstructure:"client_id"`
	ClientSecret string   `mapstructure:"client_secret"`
}

type Session

type Session struct {
	ID        string
	Token     string
	UserID    string
	UserKind  string
	ExpiresAt time.Time
	RequestID string
}

func CurSession

func CurSession(ctx context.Context) *Session

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 NewUser

func NewUser(kind, username, email string) User

NewUser returns a new user value with sensible defaults set.

func (*User) CheckPassword

func (u *User) CheckPassword(pwd string) bool

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.

func (*User) Clone

func (u *User) Clone(safe bool) User

Clone returns a deep-clone of the user.

func (*User) Validate

func (u *User) Validate() error

Validate validates the user object and returns error if invalid.

type UserData

type UserData map[string]any

UserData represents the standard user profile data. Refer https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims

Jump to

Keyboard shortcuts

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