Documentation
¶
Overview ¶
Package cryden is an embeddable, framework-agnostic authentication engine. Import this package only — internal packages (auth, token, store, security, session, logger) are implementation detail.
Index ¶
- Variables
- func ChangePassword(ctx context.Context, e *Engine, userID, currentPassword, newPassword string) error
- func ConfirmEmailChange(ctx context.Context, e *Engine, rawToken string) error
- func DeleteAccount(ctx context.Context, e *Engine, userID, currentPassword string) error
- func GetUser(ctx context.Context, e *Engine, email string) (store.User, error)
- func LinkOAuthIdentity(ctx context.Context, e *Engine, ...) error
- func ListPublicSessions(ctx context.Context, e *Engine, userID string) ([]store.PublicSession, error)
- func ListSessions(ctx context.Context, e *Engine, userID string) ([]store.Session, error)
- func Logout(ctx context.Context, e *Engine, sessionID, userID string) error
- func LogoutAll(ctx context.Context, e *Engine, userID string) error
- func RequestEmailChange(ctx context.Context, e *Engine, userID, newEmail string) error
- func RevokeSession(ctx context.Context, e *Engine, sessionID, userID string) error
- func SignUp(ctx context.Context, e *Engine, email, password, callerIP string) (store.User, error)
- func VerifyToken(e *Engine, accessToken string) (string, error)
- type Config
- type Engine
- type Tokens
Constants ¶
This section is empty.
Variables ¶
var ( ErrMissingJWTSecret = errors.New("cryden: JWTSecret is required") ErrMissingUserStore = errors.New("cryden: Config.Users is required") ErrMissingSessionStore = errors.New("cryden: Config.Sessions is required") ErrMissingAuditStore = errors.New("cryden: Config.Audit is required") )
var ErrEmailChangeNotConfigured = errors.New("cryden: email change requires Config.Verifications and Config.EmailSender to be set")
ErrEmailChangeNotConfigured is returned by RequestEmailChange if the Engine was built without Config.Verifications and Config.EmailSender set.
var ErrOAuthNotConfigured = errors.New("cryden: oauth login requires Config.OAuth to be set")
ErrOAuthNotConfigured is returned by LoginWithOAuth if the Engine was built without Config.OAuth set.
Functions ¶
func ChangePassword ¶
func ChangePassword(ctx context.Context, e *Engine, userID, currentPassword, newPassword string) error
ChangePassword requires the caller's current password as re-confirmation, and revokes all sessions on success.
func ConfirmEmailChange ¶
ConfirmEmailChange completes an email change using the token from the verification link.
func DeleteAccount ¶
DeleteAccount requires the caller's current password as re-confirmation before this irreversible action.
func GetUser ¶ added in v2.1.0
GetUser looks up a user by email. Read-only, no side effects — safe to expose as a public facade function, unlike ChangePassword/ DeleteAccount which require self-authentication. Added because admin tooling had no way to do this except reaching past the public facade into the store layer directly.
func LinkOAuthIdentity ¶ added in v2.1.0
func LinkOAuthIdentity(ctx context.Context, e *Engine, userID, provider, externalID, email, callerIP string) error
LinkOAuthIdentity attaches a confirmed external identity to an already-authenticated user. userID must come from a verified session/access token — this is the resolution path api should use after a *auth.ErrOAuthEmailConflict, once the caller has logged in with their password to prove ownership of the account.
func ListPublicSessions ¶ added in v2.1.0
func ListPublicSessions(ctx context.Context, e *Engine, userID string) ([]store.PublicSession, error)
ListPublicSessions is a redacted alternative to ListSessions, returning store.PublicSession (no TokenHash/FamilyID) instead of the full store.Session. Added alongside ListSessions, not as a replacement for it — existing callers of ListSessions are unaffected. Consumers building an HTTP-facing endpoint should prefer this over ListSessions plus their own hand-rolled DTO.
func ListSessions ¶
ListSessions returns all active sessions for a user.
func RequestEmailChange ¶
RequestEmailChange starts an email change — sends a verification link to newEmail. The email is not actually changed until ConfirmEmailChange is called with the resulting token.
func RevokeSession ¶
RevokeSession revokes a specific session. Verifies ownership before revoking.
Types ¶
type Config ¶
type Config struct {
// Required — no default exists for any of these.
JWTSecret string
Users store.UserStore
Sessions store.SessionStore
Audit store.AuditStore
// Optional — only needed if you use RequestEmailChange /
// ConfirmEmailChange. Leave nil if you don't need that flow;
// calling it without these configured returns a clear error
// rather than a nil-pointer panic.
Verifications store.VerificationStore
EmailSender notify.EmailSender
// OAuth is optional — only required if LoginWithOAuth is used.
// Left unset, LoginWithOAuth returns ErrOAuthNotConfigured.
OAuth store.OAuthStore
// Optional — sensible defaults applied in New() if zero-valued.
// These are tuning knobs, not security-critical secrets, so
// defaulting them (unlike JWTSecret) is safe.
AccessTokenTTL time.Duration // default: 15 minutes
BcryptCost int // default: bcrypt.DefaultCost (10)
RefreshTokenByteLength int // default: 32
RateLimitAttempts int // default: 10
RateLimitWindow time.Duration // default: 1 minute
LockoutThreshold int // default: 5 failed attempts
LockoutDuration time.Duration // default: 15 minutes
Logger logger.Logger // default: ConsoleJSONLogger
}
Config wires an Engine. Stores are injected directly, not constructed internally — the engine never hardcodes a storage backend. To run against Postgres, construct store/postgres.PostgresUserStore etc. and assign them here; for tests, use store/memory equivalents.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine holds every wired-up dependency needed by the public facade functions (SignUp, Login, etc. in cryden.go). Consumers never construct this directly — always via New(cfg).
type Tokens ¶
Tokens is the access/refresh token pair returned by Login and RefreshToken.
func Login ¶
func Login(ctx context.Context, e *Engine, email, password, callerIP, userAgent string) (Tokens, error)
Login authenticates a user and issues a new session. callerIP and userAgent are required, caller-supplied.
func LoginWithOAuth ¶ added in v2.1.0
func LoginWithOAuth(ctx context.Context, e *Engine, provider, externalID, email, callerIP, userAgent string) (Tokens, error)
LoginWithOAuth is called after api has already completed the provider's redirect/callback flow and confirmed the person's identity — the engine itself never talks to Google/GitHub or performs an HTTP redirect. Returns *auth.ErrOAuthEmailConflict (retrievable via errors.As) if externalID's email matches an existing password-based account that isn't linked yet; the engine deliberately does not auto-link in that case.
func RefreshToken ¶
RefreshToken rotates a refresh token, issuing a new access/refresh pair. Returns auth.ErrTokenReused (wrapping token.ErrTokenReused) if reuse of an already-rotated token is detected — the entire session family has already been revoked by the time this returns.