app

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CodeSealKeyMissing = "E_SEAL_KEY_MISSING"

	CodeSealFailed = "E_SEAL_FAILED"
)
View Source
const FedLoginCookie = "tk_fed_oauth"
View Source
const FedProviderPrefix = "oidc_fed:"
View Source
const FedSecretMask = "***"
View Source
const OAuthStateCookie = "tk_oauth_state"

Variables

View Source
var ErrDuplicate = errors.New("duplicate")
View Source
var ErrNotFound = errors.New("not found")

Functions

func FedProviderKey

func FedProviderKey(workspaceID string) string

func HashPassword

func HashPassword(password string) (string, error)

func VerifyPassword

func VerifyPassword(password, phc string) bool

Types

type AccountConfig

type AccountConfig struct {
	SecretPepper string

	LinkBaseURL string
}

type AccountService

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

func NewAccountService

func NewAccountService(repo Repo, mail EmailSender, cfg AccountConfig, now Clock) *AccountService

func (*AccountService) DeleteAccount

func (s *AccountService) DeleteAccount(ctx context.Context, p *webx.Principal) error

func (*AccountService) DeleteAccountFor

func (s *AccountService) DeleteAccountFor(ctx context.Context, actor *webx.Principal, targetUserID string) error

func (*AccountService) MarkVerified

func (s *AccountService) MarkVerified(ctx context.Context, targetUserID string) error

func (*AccountService) RequestPasswordReset

func (s *AccountService) RequestPasswordReset(ctx context.Context, rawEmail string) error

func (*AccountService) ResendVerificationFor

func (s *AccountService) ResendVerificationFor(ctx context.Context, email string) error

func (*AccountService) ResetPassword

func (s *AccountService) ResetPassword(ctx context.Context, rawEmail, token, newPassword string) error

func (*AccountService) ResetPasswordFor

func (s *AccountService) ResetPasswordFor(ctx context.Context, targetUserID string) error

func (*AccountService) SendVerification

func (s *AccountService) SendVerification(ctx context.Context, p *webx.Principal) error

func (*AccountService) VerifyEmail

func (s *AccountService) VerifyEmail(ctx context.Context, rawEmail, token string) error

func (*AccountService) WithAuditor

func (s *AccountService) WithAuditor(a Auditor) *AccountService

type Auditor

type Auditor interface {
	Record(ctx context.Context, workspaceID *string, p *webx.Principal, action, resourceType, resourceID string, meta map[string]any)
}

type Challenge

type Challenge struct {
	ID         string
	Email      string
	Kind       string
	SecretHash string
	Attempts   int
	ExpiresAt  time.Time
	CreatedAt  time.Time
}

type Clock

type Clock func() time.Time

type EmailSender

type EmailSender interface {
	SendLoginCode(ctx context.Context, to, code string) error
	SendInvite(ctx context.Context, to, workspaceName, invitationID string) error
	SendPasswordReset(ctx context.Context, to, link string) error
	SendEmailVerification(ctx context.Context, to, link string) error
}

type FedOIDCProvider

type FedOIDCProvider interface {
	Name() string

	AuthURL(state, redirectURI, verifier string) string

	Exchange(ctx context.Context, code, redirectURI, verifier string) (OAuthIdentity, error)
}

type FedOption

type FedOption func(*FedService)

func WithSecrets

func WithSecrets(s Secrets) FedOption

type FedProviderRow

type FedProviderRow struct {
	WorkspaceID  string    `json:"workspace_id"`
	IssuerURL    string    `json:"issuer_url"`
	ClientID     string    `json:"client_id"`
	ClientSecret string    `json:"client_secret"`
	Scopes       string    `json:"scopes"`
	UpdatedAt    time.Time `json:"updated_at"`
}

type FedResolver

type FedResolver interface {
	Resolve(ctx context.Context, issuerURL, clientID, clientSecret, scopes string) (FedOIDCProvider, error)
}

type FedService

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

func NewFedService

func NewFedService(repo Repo, slugLookup WorkspaceSlugLookup, resolver FedResolver, now Clock, opts ...FedOption) *FedService

func (*FedService) Callback

func (s *FedService) Callback(ctx context.Context, r *http.Request, code, state string, gate SignupGateCheck) (ThirdPartyLinkResult, error)

func (*FedService) DeleteProvider

func (s *FedService) DeleteProvider(ctx context.Context, workspaceID string) error

func (*FedService) GetProvider

func (s *FedService) GetProvider(ctx context.Context, workspaceID string) (*FedProviderRow, bool, error)

func (*FedService) Start

func (s *FedService) Start(w http.ResponseWriter, r *http.Request, slug string) error

func (*FedService) UpsertProvider

func (s *FedService) UpsertProvider(ctx context.Context, workspaceID, issuerURL, clientID, clientSecret, scopes string) (*FedProviderRow, error)

func (*FedService) WithSecureCookie

func (s *FedService) WithSecureCookie(secure bool) *FedService

type LoginResult

type LoginResult struct {
	Token string    `json:"-"`
	Exp   time.Time `json:"expires_at"`
	User  User      `json:"user"`
}

type NoopEmailSender added in v0.4.1

type NoopEmailSender struct{}

NoopEmailSender satisfies EmailSender with every send as a no-op (orkome F12): products that only use code login embed it and override exactly the sends they need, instead of hand-writing no-op stubs for SendInvite / SendPasswordReset / SendEmailVerification on every adapter.

type DevMailer struct {
    app.NoopEmailSender // invites/resets/verifications silently dropped
}
func (DevMailer) SendLoginCode(ctx context.Context, to, code string) error { ... }

func (NoopEmailSender) SendEmailVerification added in v0.4.1

func (NoopEmailSender) SendEmailVerification(ctx context.Context, to, link string) error

func (NoopEmailSender) SendInvite added in v0.4.1

func (NoopEmailSender) SendInvite(ctx context.Context, to, workspaceName, invitationID string) error

func (NoopEmailSender) SendLoginCode added in v0.4.1

func (NoopEmailSender) SendLoginCode(ctx context.Context, to, code string) error

func (NoopEmailSender) SendPasswordReset added in v0.4.1

func (NoopEmailSender) SendPasswordReset(ctx context.Context, to, link string) error

type OAuthIdentity

type OAuthIdentity struct {
	Provider      string
	Subject       string
	Email         string
	Name          string
	EmailVerified bool
}

type OAuthProvider

type OAuthProvider interface {
	Name() string

	AuthURL(state, redirectURI string) string

	Exchange(ctx context.Context, code, redirectURI string) (OAuthIdentity, error)
}

type OAuthService

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

func NewOAuthService

func NewOAuthService(repo Repo, providers []OAuthProvider, now Clock) *OAuthService

func (*OAuthService) Callback

func (s *OAuthService) Callback(ctx context.Context, r *http.Request, providerName, code, state string, gate SignupGateCheck) (ThirdPartyLinkResult, error)

func (*OAuthService) Names

func (s *OAuthService) Names() []string

func (*OAuthService) Start

func (s *OAuthService) Start(w http.ResponseWriter, r *http.Request, providerName string) error

func (*OAuthService) WithSecureCookie

func (s *OAuthService) WithSecureCookie(secure bool) *OAuthService

type PAT

type PAT struct {
	ID         string     `json:"id"`
	Name       string     `json:"name"`
	Prefix     string     `json:"prefix"`
	ExpiresAt  *time.Time `json:"expires_at,omitempty"`
	LastUsedAt *time.Time `json:"last_used_at,omitempty"`
	CreatedAt  time.Time  `json:"created_at"`

	Scope *webx.CredentialScope `json:"scope,omitempty"`
}

type Repo

type Repo interface {
	UpsertUserByEmail(ctx context.Context, email string, now time.Time) (User, error)
	GetUser(ctx context.Context, id string) (User, error)
	GetUserByEmail(ctx context.Context, email string) (User, bool, error)
	UpdateProfile(ctx context.Context, id, displayName, avatarURL string) (User, error)
	CreateUserWithPassword(ctx context.Context, email, passwordHash, displayName string) (User, error)
	SetPasswordHash(ctx context.Context, userID, passwordHash string, now time.Time) error
	SetEmailVerified(ctx context.Context, userID string, at time.Time) error

	FindOAuthAccount(ctx context.Context, provider, subject string) (userID string, ok bool, err error)

	LinkOAuthAccount(ctx context.Context, provider, subject, userID, emailAtLink string, now time.Time) error

	GetFedProvider(ctx context.Context, workspaceID string) (*FedProviderRow, bool, error)

	UpsertFedProvider(ctx context.Context, row *FedProviderRow, now time.Time) error

	DeleteFedProvider(ctx context.Context, workspaceID string) error

	CreateChallenge(ctx context.Context, email, kind, secretHash string, expiresAt time.Time) error
	LatestPendingChallenge(ctx context.Context, email, kind string) (*Challenge, error)
	IncChallengeAttempts(ctx context.Context, id string) error
	ConsumeChallenge(ctx context.Context, id string) error

	CreateSession(ctx context.Context, in webx.SessionCreate) (token string, exp time.Time, err error)
	VerifySession(ctx context.Context, token string, now time.Time) (*webx.Principal, error)

	// ConfirmSessionPassword stamps password_confirmed_at on a live session
	// (step-up): POST /auth/confirm-password → RequireRecentAuth unblocks.
	ConfirmSessionPassword(ctx context.Context, sessionID string, now time.Time) error

	RenewSession(ctx context.Context, token string, now time.Time) (exp time.Time, renewed bool, err error)
	RevokeSession(ctx context.Context, token string) error
	RevokeAllUserSessions(ctx context.Context, userID string) error
	ListUserSessions(ctx context.Context, userID string) ([]SessionInfo, error)

	RevokeUserSessionByID(ctx context.Context, userID, sessionID string, now time.Time) error

	CreatePAT(ctx context.Context, userID, name, tokenHash, prefix string, expiresAt *time.Time, scope *webx.CredentialScope) (PAT, error)
	ResolvePAT(ctx context.Context, token string, now time.Time) (*webx.Principal, error)
	ListPATs(ctx context.Context, userID string) ([]PAT, error)
	RevokePAT(ctx context.Context, userID, patID string, now time.Time) error

	CountRecentAttempts(ctx context.Context, ipHash, identity string, since time.Time) (int, error)
	RecordAttempt(ctx context.Context, ipHash, identity string, success bool, at time.Time) error
	CleanupAttempts(ctx context.Context, before time.Time) error

	RunInTx(ctx context.Context, fn func(ctx context.Context, tx pgx.Tx) error) error
	CreateUserWithPasswordTx(ctx context.Context, tx pgx.Tx, email, passwordHash, displayName string) (User, error)

	SoftDeleteUserTx(ctx context.Context, tx pgx.Tx, userID string, now time.Time) error
}

type Secrets

type Secrets interface {
	Seal(plain string) (string, error)
	Unseal(stored string) (string, error)
}

type SessionConfig

type SessionConfig struct {
	SecretPepper string

	AllowSignup    bool
	AllowedEmails  []string
	AllowedDomains []string

	DevCode string

	Production bool

	MaxAttemptsPerIP int
	AttemptWindow    time.Duration
}

type SessionInfo

type SessionInfo struct {
	ID         string    `json:"id"`
	IPHash     string    `json:"ip_hash"`
	UserAgent  string    `json:"user_agent"`
	LastSeenAt time.Time `json:"last_seen_at"`
	ExpiresAt  time.Time `json:"expires_at"`
	CreatedAt  time.Time `json:"created_at"`

	Current bool `json:"current"`

	ImpersonatedBy string `json:"impersonated_by,omitempty"`
}

type SessionService

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

func NewSessionService

func NewSessionService(repo Repo, mail EmailSender, cfg SessionConfig, sessionTTL, _ time.Duration, now Clock) *SessionService

func (*SessionService) ChangePassword

func (s *SessionService) ChangePassword(ctx context.Context, p *webx.Principal, oldPassword, newPassword string) (*LoginResult, error)

func (*SessionService) CompleteThirdPartyLogin

func (s *SessionService) CompleteThirdPartyLogin(ctx context.Context, userID string, jitNew bool, ipHash, userAgent string) (*LoginResult, error)

func (*SessionService) ConfirmPassword added in v0.4.0

func (s *SessionService) ConfirmPassword(ctx context.Context, ipHash string, p *webx.Principal, password string) (time.Time, error)

ConfirmPassword re-verifies the caller's password and stamps the current session (step-up). It backs the two-phase protocol of webx.RequireRecentAuth: 403 E_REAUTH_REQUIRED → confirm here → retry the original request. Failed confirmations go through the same attempt limiter as login (a hijacked session must not brute-force the password).

func (*SessionService) FireOnLoginFailed

func (s *SessionService) FireOnLoginFailed(ctx context.Context, email, ipHash string)

func (*SessionService) ListSessions

func (s *SessionService) ListSessions(ctx context.Context, p *webx.Principal) ([]SessionInfo, error)

func (*SessionService) ListSessionsFor

func (s *SessionService) ListSessionsFor(ctx context.Context, targetUserID string) ([]SessionInfo, error)

func (*SessionService) LoginWithPassword

func (s *SessionService) LoginWithPassword(ctx context.Context, ipHash, userAgent, rawEmail, password string) (*LoginResult, error)

func (*SessionService) Logout

func (s *SessionService) Logout(ctx context.Context, token string) error

func (*SessionService) Me

func (*SessionService) Register

func (s *SessionService) Register(ctx context.Context, ipHash, userAgent, rawEmail, password, displayName string) (*LoginResult, error)

func (*SessionService) RevokeAllSessions

func (s *SessionService) RevokeAllSessions(ctx context.Context, p *webx.Principal) error

func (*SessionService) RevokeAllSessionsFor

func (s *SessionService) RevokeAllSessionsFor(ctx context.Context, targetUserID string) error

func (*SessionService) RevokeSessionByID

func (s *SessionService) RevokeSessionByID(ctx context.Context, p *webx.Principal, sessionID string) error

func (*SessionService) RevokeSessionByIDFor

func (s *SessionService) RevokeSessionByIDFor(ctx context.Context, targetUserID, sessionID string) error

func (*SessionService) SendCode

func (s *SessionService) SendCode(ctx context.Context, ipHash, rawEmail string) error

func (*SessionService) SignupBlocked

func (s *SessionService) SignupBlocked(email string) bool

func (*SessionService) UpdateProfile

func (s *SessionService) UpdateProfile(ctx context.Context, p *webx.Principal, displayName, avatarURL string) (User, error)

func (*SessionService) VerifyCode

func (s *SessionService) VerifyCode(ctx context.Context, ipHash, userAgent, rawEmail, code string) (*LoginResult, error)

func (*SessionService) WithHooks

func (*SessionService) WithSignupGate

func (s *SessionService) WithSignupGate(gate SignupGate) *SessionService

type SignupGate

type SignupGate = func() (allowed bool, domains []string)

type SignupGateCheck

type SignupGateCheck = func(email string) bool

type ThirdPartyLinkResult

type ThirdPartyLinkResult struct {
	UserID string
	Email  string
	JITNew bool
}

type TokenService

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

func NewTokenService

func NewTokenService(repo Repo, auditor Auditor, now Clock) *TokenService

func (*TokenService) CreatePAT

func (s *TokenService) CreatePAT(ctx context.Context, p *webx.Principal, name string, ttl time.Duration, scope *webx.CredentialScope) (PAT, string, error)

func (*TokenService) ListPATs

func (s *TokenService) ListPATs(ctx context.Context, p *webx.Principal) ([]PAT, error)

func (*TokenService) ListPATsFor

func (s *TokenService) ListPATsFor(ctx context.Context, targetUserID string) ([]PAT, error)

func (*TokenService) RevokePAT

func (s *TokenService) RevokePAT(ctx context.Context, p *webx.Principal, patID string) error

func (*TokenService) RevokePATFor

func (s *TokenService) RevokePATFor(ctx context.Context, targetUserID, patID string) error

func (*TokenService) WithPATPrefix

func (s *TokenService) WithPATPrefix(prefix string) *TokenService

type User

type User struct {
	ID          string `json:"id"`
	Email       string `json:"email"`
	DisplayName string `json:"display_name"`
	AvatarURL   string `json:"avatar_url,omitempty"`
	Status      string `json:"status"`

	IsPlatformAdmin bool `json:"is_platform_admin"`
	EmailVerified   bool `json:"email_verified"`

	PasswordHash string    `json:"-"`
	CreatedAt    time.Time `json:"created_at"`
}

type WorkspaceSlugLookup

type WorkspaceSlugLookup func(ctx context.Context, slug string) (string, bool, error)

Jump to

Keyboard shortcuts

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