core

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrStrategyNotFound       = errors.New("authentication strategy not found")
	ErrSessionManagerNotSet   = errors.New("session manager not configured")
	ErrStorageNotSet          = errors.New("storage not configured")
	ErrUnauthorized           = errors.New("unauthorized")
	ErrInsufficientRole       = errors.New("insufficient role permissions")
	ErrInsufficientPermission = errors.New("insufficient permissions")
)

Common errors

Functions

func ContextWithLoginfy

func ContextWithLoginfy(ctx context.Context, loginfyCtx *Context) context.Context

ContextWithLoginfy stores the Loginfy context in the request context

Types

type Context

type Context struct {
	Request   *http.Request
	Response  http.ResponseWriter
	Loginfy   *Loginfy
	RequestID string // unique identifier for this request
	// contains filtered or unexported fields
}

Context holds the request context for authentication operations

func LoginfyFromContext

func LoginfyFromContext(ctx context.Context) (*Context, bool)

LoginfyFromContext retrieves the Loginfy context from the request context

func (*Context) Get

func (c *Context) Get(key string) (interface{}, bool)

Get retrieves a value from the context data

func (*Context) GetString

func (c *Context) GetString(key string) string

GetString retrieves a string value from the context data

func (*Context) GetUser

func (c *Context) GetUser() (*User, bool)

GetUser retrieves the authenticated user from the context

func (*Context) HasUser

func (c *Context) HasUser() bool

HasUser checks if an authenticated user exists in the context

func (*Context) Set

func (c *Context) Set(key string, value interface{})

Set stores a value in the context data

func (*Context) SetUser

func (c *Context) SetUser(user *User)

SetUser stores the authenticated user in the context

type Hooks

type Hooks struct {
	OnLogin    func(user *User)
	OnRegister func(user *User)
}

type Loginfy

type Loginfy struct {
	// contains filtered or unexported fields
}

Loginfy is the main authentication framework instance

func New

func New() *Loginfy

New creates a new Loginfy instance

func (*Loginfy) Authenticate

func (l *Loginfy) Authenticate(strategyName string, ctx *Context) (*User, error)

Authenticate performs authentication using the specified strategy

func (*Loginfy) GetSessionManager

func (l *Loginfy) GetSessionManager() SessionManager

GetSessionManager returns the session manager

func (*Loginfy) GetStorage

func (l *Loginfy) GetStorage() Storage

GetStorage returns the storage adapter

func (*Loginfy) GetStrategy

func (l *Loginfy) GetStrategy(name string) (Strategy, bool)

GetStrategy retrieves a registered strategy by name

func (*Loginfy) Login

func (l *Loginfy) Login(user *User) (string, error)

Login creates a session for a user after successful authentication

func (*Loginfy) Logout

func (l *Loginfy) Logout(ctx *Context, token string) error

Logout destroys a user's session

func (*Loginfy) Mount

func (l *Loginfy) Mount() func(http.Handler) http.Handler

Mount returns an HTTP middleware that integrates Loginfy with your HTTP framework This middleware creates a Loginfy context for each request

func (*Loginfy) SetHooks

func (l *Loginfy) SetHooks(hooks Hooks)

SetHooks sets the authentication hooks

func (*Loginfy) SetSessionManager

func (l *Loginfy) SetSessionManager(session SessionManager)

SetSessionManager sets the session manager

func (*Loginfy) SetStorage

func (l *Loginfy) SetStorage(storage Storage)

SetStorage sets the storage adapter

func (*Loginfy) Use

func (l *Loginfy) Use(strategy Strategy)

Use registers an authentication strategy

type OAuthConfig

type OAuthConfig struct {
	ClientID     string
	ClientSecret string
	RedirectURL  string
	Scopes       []string
}

OAuthConfig holds common OAuth configuration

type OAuthProvider

type OAuthProvider interface {
	Strategy

	// AuthURL returns the URL to redirect the user to for authentication
	AuthURL(state string) string

	// HandleCallback processes the OAuth callback and returns the authenticated user
	HandleCallback(ctx *Context) (*User, error)

	// ProviderName returns the OAuth provider name (e.g., "google", "github")
	ProviderName() string
}

OAuthProvider extends Strategy with OAuth-specific methods

type SessionManager

type SessionManager interface {
	CreateSession(userId string) (string, error)
	ValidateSession(ctx *Context, token string) (string, error)
	DestroySession(ctx *Context, token string) error
}

type Storage

type Storage interface {
	CreateUser(user *User) error
	GetUserByEmail(email string) (*User, error)
	GetUserById(id string) (*User, error)
	UpdateUser(user *User) error
	DeleteUser(id string) error
}

type Strategy

type Strategy interface {
	Name() string
	Authenticate(ctx *Context) (*User, error)
}

type User

type User struct {
	ID        string                 `json:"id" bson:"_id,omitempty"`
	Email     string                 `json:"email" bson:"email"`
	Password  string                 `json:"-" bson:"password"` // Never serialize password in JSON
	Roles     []string               `json:"roles" bson:"roles"`
	Metadata  map[string]interface{} `json:"metadata,omitempty" bson:"metadata,omitempty"`
	CreatedAt time.Time              `json:"created_at" bson:"created_at"`
	UpdatedAt time.Time              `json:"updated_at" bson:"updated_at"`
}

User represents an authenticated user in the system

func (*User) HasAnyRole

func (u *User) HasAnyRole(roles ...string) bool

HasAnyRole checks if the user has any of the specified roles

func (*User) HasRole

func (u *User) HasRole(role string) bool

HasRole checks if the user has a specific role

Jump to

Keyboard shortcuts

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