core

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

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

AuthKey kinds.

Variables

View Source
var ReqCtxKey = "forge:req_ctx"

Functions

func CheckPassword

func CheckPassword(hash *string, 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 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, reqCtx ReqCtx) context.Context

NewCtx returns a new Go context with given reqCtx 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 interface {
	Authenticate(ctx context.Context, token string) (*Session, error)
}

Auth implementation is responsible for validating access tokens and restoring user-session from it.

type ConfLoader

type ConfLoader interface {
	Int(key string, defVal int) int
	Bool(key string, defVal bool) bool
	String(key string, defVal string) string
	Strings(key string, defVal []string) []string
	Float64(key string, defVal float64) float64
	Duration(key string, defVal time.Duration) time.Duration
}

ConfLoader is responsible for loading configurations during initial setup.

type M

type M = map[string]any

M is an alias for the generic map provided for convenience.

type Middleware added in v0.2.0

type Middleware func(http.Handler) http.Handler

Middleware is convenience type for http middlewares.

type ReqCtx

type ReqCtx struct {
	Path       string
	Route      string
	Method     string
	Session    *Session
	RequestID  string
	RemoteAddr string
}

ReqCtx represents the context for the current request.

func FromCtx

func FromCtx(ctx context.Context) ReqCtx

FromCtx returns the request context from Go context.

func (ReqCtx) Authenticated

func (rc ReqCtx) Authenticated() bool

Authenticated returns true if rc contains authenticated user.

func (ReqCtx) IsZero added in v0.1.1

func (rc ReqCtx) IsZero() bool

type Session

type Session struct {
	User   User      `json:"user"`
	Token  string    `json:"token"`
	Expiry time.Time `json:"expiry"`
}

Session represents a login-session for the contained user.

type User

type User struct {
	ID          string         `json:"id"`
	Data        UserData       `json:"data"`
	Email       string         `json:"email"`
	PwdHash     *string        `json:"pwd_hash,omitempty"`
	Username    string         `json:"username,omitempty"`
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	VerifiedAt  *time.Time     `json:"verified_at,omitempty"`
	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) 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

type UserKey added in v0.1.1

type UserKey struct {
	AuthKey string `json:"auth_key"`
	Attribs M      `json:"attribs"`
}

UserKey represents login-key of a user with any additional attributes.

type UserRegistry

type UserRegistry interface {
	Get(ctx context.Context, key string) (*User, error)
	Upsert(ctx context.Context, u User) (*User, error)
}

UserRegistry implementation is responsible for maintaining user data.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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