Documentation
¶
Overview ¶
Package canopy provides Go-native authentication primitives for API servers.
Canopy includes email/password authentication, Google and Apple OAuth sign-in, database-backed opaque sessions, email verification, password reset, provider token refresh, rate-limiting hooks, audit hooks, and net/http handlers. The package is designed as a library, not a hosted service, so applications can use the built-in HTTP routes or call the service API directly.
Index ¶
- Constants
- Variables
- func ContextWithSession(ctx context.Context, data *SessionData) context.Context
- type Account
- type AccountLinkingPolicy
- type AuditEvent
- type AuditLogger
- type Auth
- type Config
- type EmailSender
- type EmailVerificationMessage
- type Environment
- type Hooks
- type OAuthCallbackInput
- type PasswordResetMessage
- type RateLimitRequest
- type RateLimiter
- type RefreshProviderTokenInput
- type RefreshProviderTokenOutput
- type RequestPasswordResetInput
- type ResetPasswordInput
- type SendEmailVerificationInput
- type Service
- func (s *Service) GetSession(ctx context.Context, token string) (*SessionData, error)
- func (s *Service) Middleware(next http.Handler) http.Handler
- func (s *Service) OAuthCallback(ctx context.Context, in OAuthCallbackInput) (*SessionData, string, string, *bool, error)
- func (s *Service) RefreshProviderToken(ctx context.Context, in RefreshProviderTokenInput) (*RefreshProviderTokenOutput, error)
- func (s *Service) RequestPasswordReset(ctx context.Context, in RequestPasswordResetInput) error
- func (s *Service) ResetPassword(ctx context.Context, in ResetPasswordInput) error
- func (s *Service) RevokeUserSessions(ctx context.Context, userID string) error
- func (s *Service) SendEmailVerification(ctx context.Context, in SendEmailVerificationInput) error
- func (s *Service) SignInEmail(ctx context.Context, in SignInEmailInput) (*SessionData, string, error)
- func (s *Service) SignInSocial(ctx context.Context, in SignInSocialInput) (*SignInSocialOutput, string, error)
- func (s *Service) SignOut(ctx context.Context, token string) error
- func (s *Service) SignUpEmail(ctx context.Context, in SignUpEmailInput) (*SessionData, string, error)
- func (s *Service) VerifyEmail(ctx context.Context, in VerifyEmailInput) (*User, error)
- type Session
- type SessionData
- type SignInEmailInput
- type SignInSocialInput
- type SignInSocialOutput
- type SignUpEmailInput
- type Store
- type User
- type Verification
- type VerifyEmailInput
Constants ¶
const ProviderEmailPassword = "email-password"
Variables ¶
var ( ErrInvalidCredentials = errors.New("canopy: invalid credentials") ErrSignupDisabled = errors.New("canopy: signup disabled") ErrUnverifiedEmail = errors.New("canopy: unverified email") ErrInvalidState = errors.New("canopy: invalid state") ErrInvalidToken = errors.New("canopy: invalid token") ErrExpiredToken = errors.New("canopy: expired token") ErrProviderFailure = errors.New("canopy: provider failure") ErrStorageFailure = errors.New("canopy: storage failure") ErrRateLimited = errors.New("canopy: rate limited") ErrAccountLinking = errors.New("canopy: account linking required") ErrNoRefreshToken = errors.New("canopy: no provider refresh token") ErrProviderTokenRefreshFailed = errors.New("canopy: provider token refresh failed") ErrProviderAccountNotFound = errors.New("canopy: provider account not found") ErrNotFound = errors.New("canopy: not found") ErrConflict = errors.New("canopy: conflict") ErrInvalidInput = errors.New("canopy: invalid input") )
Functions ¶
func ContextWithSession ¶
func ContextWithSession(ctx context.Context, data *SessionData) context.Context
Types ¶
type Account ¶
type Account struct {
ID string `json:"id"`
UserID string `json:"userId"`
AccountID string `json:"accountId"`
ProviderID string `json:"providerId"`
AccessToken string `json:"-"`
RefreshToken string `json:"-"`
AccessTokenExpiresAt *time.Time `json:"accessTokenExpiresAt,omitempty"`
RefreshTokenExpiresAt *time.Time `json:"refreshTokenExpiresAt,omitempty"`
Scope string `json:"scope,omitempty"`
IDToken string `json:"-"`
Password string `json:"-"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type AccountLinkingPolicy ¶
type AccountLinkingPolicy string
const (
AccountLinkingRejectExplicit AccountLinkingPolicy = "reject-explicit"
)
type AuditEvent ¶
type AuditLogger ¶
type AuditLogger interface {
LogAuthEvent(ctx context.Context, event AuditEvent)
}
type Config ¶
type Config struct {
Store Store
Secret string
Environment Environment
BasePath string
DisableSignup bool
RequireEmailVerification bool
PasswordMinLength int
PasswordMaxLength int
PasswordHasher password.Hasher
RateLimiter RateLimiter
AuditLogger AuditLogger
EmailSender EmailSender
AccountLinkingPolicy AccountLinkingPolicy
TrustedOrigins []string
DisableOriginCheck bool
Providers []oauth.Provider
OAuthStateTTL time.Duration
OAuthStateCookieName string
EmailVerificationTTL time.Duration
PasswordResetTTL time.Duration
Session sessions.Config
Hooks Hooks
}
type EmailSender ¶
type EmailSender interface {
SendEmailVerification(ctx context.Context, message EmailVerificationMessage) error
SendPasswordReset(ctx context.Context, message PasswordResetMessage) error
}
type Environment ¶
type Environment string
const ( Development Environment = "development" Production Environment = "production" )
type Hooks ¶
type Hooks struct {
BeforeUserCreate func(user *User) error
AfterUserCreate func(user User) error
AfterSignIn func(data SessionData) error
AfterSignOut func(session *Session) error
AfterOAuth func(data SessionData) error
AfterEmailVerified func(user User) error
AfterPasswordReset func(user User) error
}
type OAuthCallbackInput ¶
type PasswordResetMessage ¶
type RateLimitRequest ¶
type RateLimiter ¶
type RateLimiter interface {
Allow(ctx context.Context, request RateLimitRequest) error
Report(ctx context.Context, request RateLimitRequest, success bool)
}
type RefreshProviderTokenOutput ¶
type RefreshProviderTokenOutput struct {
AccountID string `json:"accountId"`
ProviderID string `json:"providerId"`
AccessToken string `json:"accessToken"`
RefreshToken string `json:"-"`
AccessTokenExpiresAt *time.Time `json:"accessTokenExpiresAt,omitempty"`
RefreshTokenExpiresAt *time.Time `json:"refreshTokenExpiresAt,omitempty"`
Scope string `json:"scope,omitempty"`
}
type ResetPasswordInput ¶
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
func (*Service) GetSession ¶
func (*Service) OAuthCallback ¶
func (s *Service) OAuthCallback(ctx context.Context, in OAuthCallbackInput) (*SessionData, string, string, *bool, error)
func (*Service) RefreshProviderToken ¶
func (s *Service) RefreshProviderToken(ctx context.Context, in RefreshProviderTokenInput) (*RefreshProviderTokenOutput, error)
func (*Service) RequestPasswordReset ¶
func (s *Service) RequestPasswordReset(ctx context.Context, in RequestPasswordResetInput) error
func (*Service) ResetPassword ¶
func (s *Service) ResetPassword(ctx context.Context, in ResetPasswordInput) error
func (*Service) RevokeUserSessions ¶
func (*Service) SendEmailVerification ¶
func (s *Service) SendEmailVerification(ctx context.Context, in SendEmailVerificationInput) error
func (*Service) SignInEmail ¶
func (s *Service) SignInEmail(ctx context.Context, in SignInEmailInput) (*SessionData, string, error)
func (*Service) SignInSocial ¶
func (s *Service) SignInSocial(ctx context.Context, in SignInSocialInput) (*SignInSocialOutput, string, error)
func (*Service) SignUpEmail ¶
func (s *Service) SignUpEmail(ctx context.Context, in SignUpEmailInput) (*SessionData, string, error)
func (*Service) VerifyEmail ¶
type Session ¶
type Session struct {
ID string `json:"id"`
UserID string `json:"userId"`
Token string `json:"-"`
ExpiresAt time.Time `json:"expiresAt"`
IPAddress string `json:"ipAddress,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
type SessionData ¶
func SessionFromContext ¶
func SessionFromContext(ctx context.Context) (*SessionData, bool)
type SignInEmailInput ¶
type SignInSocialInput ¶
type SignInSocialOutput ¶
type SignInSocialOutput struct {
URL string `json:"url"`
}
type SignUpEmailInput ¶
type Store ¶
type Store interface {
FindUserByID(ctx context.Context, id string) (*User, error)
FindUserByEmail(ctx context.Context, email string) (*User, error)
CreateUser(ctx context.Context, user *User) error
UpdateUser(ctx context.Context, user *User) error
FindAccount(ctx context.Context, providerID, accountID string) (*Account, error)
FindAccountByUserProvider(ctx context.Context, userID, providerID string) (*Account, error)
CreateAccount(ctx context.Context, account *Account) error
UpdateAccount(ctx context.Context, account *Account) error
CreateSession(ctx context.Context, session *Session) error
FindSessionByToken(ctx context.Context, token string) (*SessionData, error)
UpdateSession(ctx context.Context, session *Session) error
DeleteSessionByToken(ctx context.Context, token string) error
DeleteUserSessions(ctx context.Context, userID string) error
CreateVerification(ctx context.Context, verification *Verification) error
ConsumeVerification(ctx context.Context, identifier, value string, now time.Time) (*Verification, error)
DeleteExpiredVerifications(ctx context.Context, now time.Time) error
}
type Verification ¶
type VerifyEmailInput ¶
type VerifyEmailInput struct {
Token string
}
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
adapters
|
|
|
chi
module
|
|
|
echo
module
|
|
|
gin
module
|
|
|
Package oauth defines provider interfaces and shared OAuth profile types used by Canopy.
|
Package oauth defines provider interfaces and shared OAuth profile types used by Canopy. |
|
Package password provides password hashing interfaces and Canopy's default Argon2id implementation.
|
Package password provides password hashing interfaces and Canopy's default Argon2id implementation. |
|
providers
|
|
|
apple
Package apple provides Canopy's Apple OAuth provider implementation.
|
Package apple provides Canopy's Apple OAuth provider implementation. |
|
google
Package google provides Canopy's Google OAuth provider implementation.
|
Package google provides Canopy's Google OAuth provider implementation. |
|
Package ratelimit provides Canopy's built-in in-memory RateLimiter.
|
Package ratelimit provides Canopy's built-in in-memory RateLimiter. |
|
Package sessions provides session cookie configuration and helpers for Canopy's database-backed opaque sessions.
|
Package sessions provides session cookie configuration and helpers for Canopy's database-backed opaque sessions. |
|
store
|
|
|
postgres
Package postgres provides a Postgres Store implementation and schema migration for Canopy.
|
Package postgres provides a Postgres Store implementation and schema migration for Canopy. |