canopy

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: May 24, 2026 License: MIT Imports: 17 Imported by: 4 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Canopy

Canopy is a Go authentication library for API servers. It provides email/password auth, Google and Apple OAuth sign-in, database-backed opaque sessions, audit hooks, and a Postgres storage adapter.

Canopy is inspired by Better Auth: the core entity names, route names, and product direction intentionally follow the same mental model. Canopy is not a Better Auth port and it is not a hosted service. It is a Go-native library built around net/http, explicit interfaces, typed errors, and server-side session storage.

The long-term goal is to support the full Better Auth feature surface in Go over time, while keeping the v1 API small, explicit, and production-oriented.

Project docs:

Status

Implemented today:

  • Email/password sign-up and sign-in.
  • Email verification.
  • Password reset.
  • Google OAuth sign-in.
  • Apple OAuth sign-in.
  • OIDC ID token verification for Google and Apple.
  • PKCE, nonce validation, signed OAuth state, state cookie binding, and replay prevention.
  • Server-side opaque sessions.
  • Session cookies and session middleware.
  • Sign-out and session revocation.
  • Provider access-token refresh API.
  • Audit logging interface.
  • Postgres store and schema migration.
  • Typed public errors for common auth failures.

Not implemented yet:

  • Magic link, passkeys, two-factor auth, organization/team features, admin APIs, and plugins.
  • First-party adapters for Gin, Echo, Chi, Gorilla, etc. net/http works with most routers directly.
  • Built-in rate limiting. Applications should apply rate limiting in HTTP middleware, gateways, or other client-owned request controls.
  • Automatic provider token refresh during session lookup. This is intentionally separate.
  • Explicit account-linking confirmation flow. v1 rejects unsafe implicit linking.

Install

go get github.com/ssnxd/canopy
go get github.com/ssnxd/canopy/store/postgres
go get github.com/ssnxd/canopy/providers/google
go get github.com/ssnxd/canopy/providers/apple
go get github.com/lib/pq

Quick Start

package main

import (
	"context"
	"database/sql"
	"encoding/json"
	"log"
	"net/http"
	"os"

	"github.com/ssnxd/canopy"
	"github.com/ssnxd/canopy/oauth"
	"github.com/ssnxd/canopy/providers/google"
	"github.com/ssnxd/canopy/store/postgres"
	_ "github.com/lib/pq"
)

func main() {
	db, err := sql.Open("postgres", os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal(err)
	}

	store := postgres.New(db)
	if err := store.Migrate(context.Background()); err != nil {
		log.Fatal(err)
	}

	auth, err := canopy.New(canopy.Config{
		Store:       store,
		Secret:      os.Getenv("CANOPY_SECRET"),
		Environment: canopy.Production,
		TrustedOrigins: []string{
			"https://app.example.com",
		},
		Providers: []oauth.Provider{
			google.Provider{
				ClientID:     os.Getenv("GOOGLE_CLIENT_ID"),
				ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
				RedirectURL:  "https://api.example.com/callback/google",
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}

	mux := http.NewServeMux()
	mux.Handle("/auth/", http.StripPrefix("/auth", auth.Handler()))
	mux.Handle("/api/me", auth.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		session, ok := canopy.SessionFromContext(r.Context())
		if !ok {
			http.Error(w, "unauthorized", http.StatusUnauthorized)
			return
		}
		_ = json.NewEncoder(w).Encode(session.User)
	})))

	log.Fatal(http.ListenAndServe(":8080", mux))
}

database/sql drivers are registered by side-effect imports in the application binary. If you use sql.Open("postgres", ...), import github.com/lib/pq as shown above. If you prefer pgx, import github.com/jackc/pgx/v5/stdlib and open the database with sql.Open("pgx", ...).

Database

The Postgres schema follows Better Auth-compatible core entities:

  • user
  • session
  • account
  • verification

Run the migration:

store := postgres.New(db)
err := store.Migrate(ctx)

Important constraints and indexes:

  • Unique normalized user email: lower(email).
  • Unique session token.
  • Unique OAuth account pair: (provider_id, account_id).
  • Unique user-provider pair: (user_id, provider_id).
  • Foreign keys from session.user_id and account.user_id to user.id.
  • Expiry indexes for session and verification cleanup.

Configuration

type Config struct {
	Store       canopy.Store
	Secret      string
	Environment canopy.Environment
	BasePath    string

	DisableSignup            bool
	RequireEmailVerification bool
	PasswordMinLength        int
	PasswordMaxLength        int
	PasswordHasher           password.Hasher

	AuditLogger          canopy.AuditLogger
	EmailSender          canopy.EmailSender
	AccountLinkingPolicy canopy.AccountLinkingPolicy

	TrustedOrigins     []string
	DisableOriginCheck bool

	Providers            []oauth.Provider
	OAuthStateTTL        time.Duration
	OAuthStateCookieName string
	EmailVerificationTTL time.Duration
	PasswordResetTTL     time.Duration

	Session sessions.Config
	Hooks   canopy.Hooks
}

Defaults:

  • Environment: development.
  • Password length: 8 to 128 characters.
  • Password hashing: Argon2id.
  • Session expiry: 7 days.
  • Session refresh/update age: 1 day.
  • Session cookie: canopy.session_token.
  • OAuth state TTL: 10 minutes.
  • Email verification TTL: 24 hours.
  • Password reset TTL: 1 hour.
  • Account linking policy: reject implicit linking.

Production requirements:

  • Secret must be at least 32 bytes in production.
  • Cookies are Secure in production.
  • Set TrustedOrigins for browser-facing deployments.
  • Apply rate limiting before Canopy handlers in browser-facing deployments.

HTTP API

Mount the handler:

mux.Handle("/auth/", http.StripPrefix("/auth", auth.Handler()))

The examples below assume the handler is mounted at /auth.

POST /auth/sign-up/email

Creates a user with an email/password account and starts a Canopy session.

Request:

{
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "password": "correct-password",
  "image": "https://example.com/avatar.png",
  "rememberMe": true
}

Response:

{
  "user": {
    "id": "usr_...",
    "name": "Ada Lovelace",
    "email": "ada@example.com",
    "emailVerified": true,
    "image": "https://example.com/avatar.png",
    "createdAt": "...",
    "updatedAt": "..."
  },
  "session": {
    "id": "ses_...",
    "userId": "usr_...",
    "expiresAt": "...",
    "createdAt": "...",
    "updatedAt": "..."
  }
}

Also sets the canopy.session_token cookie.

When RequireEmailVerification is enabled, sign-up creates the user with emailVerified=false, creates a one-time verification token, and calls EmailSender.SendEmailVerification. Email/password sign-in returns UNVERIFIED_EMAIL until the token is verified.

POST /auth/send-verification-email

Creates a fresh one-time email verification token and sends it through EmailSender.

Request:

{
  "email": "ada@example.com",
  "callbackURL": "https://app.example.com/verify-email"
}

Response:

{ "success": true }

This endpoint returns success even when the email does not exist. That avoids account enumeration.

POST /auth/verify-email

Consumes a one-time email verification token and marks the user as verified.

Request:

{
  "token": "..."
}

Response:

{
  "id": "usr_...",
  "name": "Ada Lovelace",
  "email": "ada@example.com",
  "emailVerified": true,
  "createdAt": "...",
  "updatedAt": "..."
}
GET /auth/verify-email?token=...

Same behavior as POST /auth/verify-email. This route is convenient for email links.

POST /auth/request-password-reset

Creates a one-time password reset token and sends it through EmailSender.

Request:

{
  "email": "ada@example.com",
  "callbackURL": "https://app.example.com/reset-password"
}

Response:

{ "success": true }

This endpoint returns success even when the email does not exist or the account has no password credential. That avoids account enumeration.

POST /auth/reset-password

Consumes a one-time password reset token, updates the email/password account hash, and revokes all existing sessions for that user.

Request:

{
  "token": "...",
  "newPassword": "new-correct-password"
}

Response:

{ "success": true }
POST /auth/sign-in/email

Verifies an email/password account and starts a new Canopy session.

Request:

{
  "email": "ada@example.com",
  "password": "correct-password",
  "rememberMe": true
}

Every successful sign-in creates a fresh session token. Canopy does not reuse any previous unauthenticated or authenticated session token.

POST /auth/sign-in/social

Starts a Google or Apple OAuth flow.

Request:

{
  "provider": "google",
  "callbackURL": "https://app.example.com/dashboard",
  "rememberMe": true
}

Response:

{
  "url": "https://accounts.google.com/o/oauth2/v2/auth?..."
}

The response also sets an HttpOnly OAuth state binding cookie. Redirect the user to url.

GET /auth/callback/{provider}

Completes OAuth after the provider redirects back to Canopy.

Example:

GET /auth/callback/google?code=...&state=...

Canopy verifies:

  • Signed state.
  • State binding cookie.
  • Server-side one-time state record.
  • PKCE verifier.
  • OIDC ID token issuer, audience, expiry, and nonce.
  • Provider account identity.

On success, Canopy creates or updates the provider account, creates a Canopy session, sets the session cookie, and redirects to callbackURL when one was supplied.

POST /auth/callback/{provider}

Same behavior as GET /auth/callback/{provider}. This is useful for clients or intermediaries that prefer form POST callbacks.

GET /auth/get-session

Reads the current Canopy session from the session cookie or Authorization: Bearer <token>.

Response when authenticated:

{
  "user": { "...": "..." },
  "session": { "...": "..." }
}

Response when unauthenticated:

null

GET /get-session is the normal cookie-backed session lookup.

POST /auth/get-session

Same behavior as GET /auth/get-session. This exists for clients and infrastructure that avoid credentialed GET requests. It does not refresh provider access tokens.

POST /auth/sign-out

Revokes the current session and clears the session cookie.

Canopy reads the session token from the cookie or Authorization: Bearer <token>.

Response:

{ "success": true }
POST /auth/refresh-provider-token

Refreshes a Google or Apple provider access token for the currently authenticated Canopy user.

Request:

{
  "providerId": "google"
}

Response:

{
  "accountId": "google-subject",
  "providerId": "google",
  "accessToken": "...",
  "accessTokenExpiresAt": "...",
  "scope": "openid email profile"
}

This endpoint is intentionally separate from get-session. A Canopy session answers, “is this user authenticated with my app?” Provider token refresh answers, “can my app still call Google or Apple APIs for this user?” Those are different lifecycles with different failure modes.

Go Service API

The handler is optional. You can use Canopy as a pure service library.

Constructor
auth, err := canopy.New(canopy.Config{...})
Accessors
api := auth.API()
handler := auth.Handler()
middleware := auth.Middleware(next)
Email/password
data, token, err := auth.API().SignUpEmail(ctx, canopy.SignUpEmailInput{
	Name:     "Ada Lovelace",
	Email:    "ada@example.com",
	Password: "correct-password",
	Image:    "https://example.com/avatar.png",
})
data, token, err := auth.API().SignInEmail(ctx, canopy.SignInEmailInput{
	Email:    "ada@example.com",
	Password: "correct-password",
})
Email Verification
err := auth.API().SendEmailVerification(ctx, canopy.SendEmailVerificationInput{
	Email:       "ada@example.com",
	CallbackURL: "https://app.example.com/verify-email",
})
user, err := auth.API().VerifyEmail(ctx, canopy.VerifyEmailInput{
	Token: tokenFromEmail,
})
Password Reset
err := auth.API().RequestPasswordReset(ctx, canopy.RequestPasswordResetInput{
	Email:       "ada@example.com",
	CallbackURL: "https://app.example.com/reset-password",
})
err := auth.API().ResetPassword(ctx, canopy.ResetPasswordInput{
	Token:       tokenFromEmail,
	NewPassword: "new-correct-password",
})
OAuth
start, stateCookieValue, err := auth.API().SignInSocial(ctx, canopy.SignInSocialInput{
	Provider:    "google",
	CallbackURL: "https://app.example.com/dashboard",
})

start.URL is the provider authorization URL. stateCookieValue is the value that must be placed in the OAuth state binding cookie when using the service API directly.

data, sessionToken, callbackURL, rememberMe, err := auth.API().OAuthCallback(ctx, canopy.OAuthCallbackInput{
	Provider:     "google",
	Code:         code,
	State:        state,
	StateBinding: stateCookieValue,
	IPAddress:    ip,
	UserAgent:    userAgent,
})
Sessions
data, err := auth.API().GetSession(ctx, token)
err := auth.API().SignOut(ctx, token)
err := auth.API().RevokeUserSessions(ctx, userID)
data, ok := canopy.SessionFromContext(r.Context())
Provider Token Refresh
token, err := auth.API().RefreshProviderToken(ctx, canopy.RefreshProviderTokenInput{
	UserID:     userID,
	ProviderID: "google",
})

ErrNoRefreshToken means the user must re-authorize the provider before Canopy can refresh access.

Google OAuth

import (
	"github.com/ssnxd/canopy/oauth"
	"github.com/ssnxd/canopy/providers/google"
)

auth, err := canopy.New(canopy.Config{
	Store:  store,
	Secret: os.Getenv("CANOPY_SECRET"),
	Providers: []oauth.Provider{
		google.Provider{
			ClientID:     os.Getenv("GOOGLE_CLIENT_ID"),
			ClientSecret: os.Getenv("GOOGLE_CLIENT_SECRET"),
			RedirectURL:  "https://api.example.com/auth/callback/google",
		},
	},
})

Default scopes:

openid email profile

Google sometimes does not return a refresh token on later authorizations if the user already granted access. If refresh is required, your app may need to force a re-consent flow.

Apple OAuth

Apple requires an ES256-signed client-secret JWT. Canopy supports either a custom ClientSecretSource or the built-in JWT helper.

secretSource, err := apple.NewJWTClientSecretSource(
	os.Getenv("APPLE_TEAM_ID"),
	os.Getenv("APPLE_CLIENT_ID"),
	os.Getenv("APPLE_KEY_ID"),
	[]byte(os.Getenv("APPLE_PRIVATE_KEY_PEM")),
)
if err != nil {
	return err
}

auth, err := canopy.New(canopy.Config{
	Store:  store,
	Secret: os.Getenv("CANOPY_SECRET"),
	Providers: []oauth.Provider{
		apple.Provider{
			ClientID:     os.Getenv("APPLE_CLIENT_ID"),
			RedirectURL:  "https://api.example.com/auth/callback/apple",
			SecretSource: secretSource,
		},
	},
})

Default scopes:

openid email name

Apple caveats:

  • Apple may only provide name on the first authorization.
  • Apple may not provide a refresh token in some flows.
  • Canopy verifies Apple ID tokens through OIDC and handles string or boolean email_verified claims.

Rate Limiting

Canopy does not include built-in rate limiting. Apply rate limits in your application before requests reach Canopy's HTTP handler or service calls. This keeps brute-force protection close to your deployment topology, whether that is a reverse proxy, API gateway, shared Redis-backed middleware, or framework-specific middleware.

func rateLimit(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if !allowRequest(r) {
			http.Error(w, "too many requests", http.StatusTooManyRequests)
			return
		}
		next.ServeHTTP(w, r)
	})
}

mux := http.NewServeMux()
mux.Handle("/auth/", rateLimit(http.StripPrefix("/auth", auth.Handler())))

For email/password sign-in, limit by IP address and normalized email where practical. In multi-instance deployments, use shared state at the gateway or middleware layer so attempts are counted consistently across instances.

Email Delivery

Canopy generates email verification and password reset tokens, stores their hashes in verification, and calls EmailSender. Your application owns the actual email delivery provider and templates.

type EmailSender interface {
	SendEmailVerification(ctx context.Context, message canopy.EmailVerificationMessage) error
	SendPasswordReset(ctx context.Context, message canopy.PasswordResetMessage) error
}

Each message includes:

  • User
  • Email
  • Token
  • URL
  • CallbackURL
  • ExpiresAt

URL is built by appending ?token=... or &token=... to CallbackURL. If you prefer to build links yourself, use Token directly.

type Mailer struct{}

func (Mailer) SendEmailVerification(ctx context.Context, msg canopy.EmailVerificationMessage) error {
	return sendEmail(msg.Email, "Verify your email", msg.URL)
}

func (Mailer) SendPasswordReset(ctx context.Context, msg canopy.PasswordResetMessage) error {
	return sendEmail(msg.Email, "Reset your password", msg.URL)
}

Audit Logging

type AuditLogger interface {
	LogAuthEvent(ctx context.Context, event canopy.AuditEvent)
}

Canopy emits events for:

  • Email sign-in success.
  • Email sign-in failure.
  • Email verification requested.
  • Email verification success or failure.
  • Password reset requested.
  • Password reset success or failure.
  • Rate-limited sign-in attempts.
  • OAuth callback success.
  • OAuth callback failure.
  • Provider token refresh success.
  • Provider token refresh failure.
  • Session revocation.

Use this to send security events to structured logs, metrics, or a SIEM.

Hooks

type Hooks struct {
	BeforeUserCreate func(user *canopy.User) error
	AfterUserCreate  func(user canopy.User) error
	AfterSignIn      func(data canopy.SessionData) error
	AfterSignOut     func(session *canopy.Session) error
	AfterOAuth       func(data canopy.SessionData) error
	AfterEmailVerified func(user canopy.User) error
	AfterPasswordReset func(user canopy.User) error
}

Hooks are app-level callbacks. Audit logging is separate and should be used for security telemetry.

Store Interface

Applications can implement custom storage by satisfying canopy.Store.

type Store interface {
	FindUserByID(ctx context.Context, id string) (*canopy.User, error)
	FindUserByEmail(ctx context.Context, email string) (*canopy.User, error)
	CreateUser(ctx context.Context, user *canopy.User) error
	UpdateUser(ctx context.Context, user *canopy.User) error

	FindAccount(ctx context.Context, providerID, accountID string) (*canopy.Account, error)
	FindAccountByUserProvider(ctx context.Context, userID, providerID string) (*canopy.Account, error)
	CreateAccount(ctx context.Context, account *canopy.Account) error
	UpdateAccount(ctx context.Context, account *canopy.Account) error

	CreateSession(ctx context.Context, session *canopy.Session) error
	FindSessionByToken(ctx context.Context, token string) (*canopy.SessionData, error)
	UpdateSession(ctx context.Context, session *canopy.Session) error
	DeleteSessionByToken(ctx context.Context, token string) error
	DeleteUserSessions(ctx context.Context, userID string) error

	CreateVerification(ctx context.Context, verification *canopy.Verification) error
	ConsumeVerification(ctx context.Context, identifier, value string, now time.Time) (*canopy.Verification, error)
	DeleteExpiredVerifications(ctx context.Context, now time.Time) error
}

Custom stores should return Canopy typed errors where possible, especially ErrNotFound and ErrConflict.

Data Model

User
type User struct {
	ID            string
	Name          string
	Email         string
	EmailVerified bool
	Image         string
	CreatedAt     time.Time
	UpdatedAt     time.Time
}
Session
type Session struct {
	ID        string
	UserID    string
	Token     string
	ExpiresAt time.Time
	IPAddress string
	UserAgent string
	CreatedAt time.Time
	UpdatedAt time.Time
}

Token is intentionally omitted from JSON responses.

Account
type Account struct {
	ID                    string
	UserID                string
	AccountID             string
	ProviderID            string
	AccessToken           string
	RefreshToken          string
	AccessTokenExpiresAt  *time.Time
	RefreshTokenExpiresAt *time.Time
	Scope                 string
	IDToken               string
	Password              string
	CreatedAt             time.Time
	UpdatedAt             time.Time
}

Email/password credentials are stored as an account with provider ID email-password.

Verification
type Verification struct {
	ID         string
	Identifier string
	Value      string
	ExpiresAt  time.Time
	CreatedAt  time.Time
	UpdatedAt  time.Time
}

OAuth state replay prevention, email verification, and password reset use the verification store in v1. Stored values are hashes of one-time signed tokens.

Public Errors

Canopy exposes typed sentinel errors:

  • ErrInvalidCredentials
  • ErrSignupDisabled
  • ErrUnverifiedEmail
  • ErrInvalidState
  • ErrInvalidToken
  • ErrExpiredToken
  • ErrProviderFailure
  • ErrStorageFailure
  • ErrAccountLinking
  • ErrNoRefreshToken
  • ErrProviderTokenRefreshFailed
  • ErrProviderAccountNotFound
  • ErrNotFound
  • ErrConflict
  • ErrInvalidInput
  • ErrUnauthorized

Use errors.Is(err, canopy.ErrInvalidCredentials) rather than comparing error strings.

Security Defaults

  • Argon2id password hashing.
  • Opaque random session tokens generated with crypto/rand.
  • Server-side session storage.
  • Fresh session token after every successful authentication.
  • HttpOnly session cookies.
  • SameSite=Lax by default.
  • Secure cookies in production.
  • Production secret length validation.
  • Conservative email normalization.
  • OAuth PKCE.
  • OAuth nonce validation.
  • Signed state.
  • State cookie binding.
  • Server-side state replay prevention.
  • Signed one-time email verification tokens.
  • Signed one-time password reset tokens.
  • Password reset revokes existing sessions.
  • Typed errors mapped to stable JSON codes.

Important Caveats

Account linking:

Canopy does not silently link accounts by email. If a user signs up with email/password and later signs in with Google using the same email, Canopy returns ErrAccountLinking. This avoids the common security footgun of treating “same email” as proof of same identity across providers. Build an explicit confirmation flow for linking.

Transactions:

The current Store interface performs user, account, and session writes as separate calls. The Postgres adapter relies on constraints, but a future hardening pass should add store-level transactional creation APIs so user/account/session creation is atomic.

Rate limiting:

Canopy does not enforce request limits internally. Add rate limiting in middleware, a gateway, or another application-owned layer before auth requests reach Canopy.

Provider token refresh:

GET /get-session never refreshes Google or Apple access tokens. Use RefreshProviderToken when your application is about to call provider APIs and needs a fresh access token.

Apple profile data:

Apple may only provide some user fields once. Store what you need on first sign-in.

Email delivery:

Canopy calls EmailSender, but it does not send SMTP or provider email itself. Applications must connect this to their email provider.

Roadmap

The intent is to grow Canopy toward Better Auth feature parity while keeping Go-native APIs:

  • Magic link.
  • Passkeys/WebAuthn.
  • Two-factor authentication.
  • Organization/team support.
  • Admin user management APIs.
  • Account linking confirmation flows.
  • More OAuth providers.
  • MySQL and SQLite stores.
  • Transactional store APIs.
  • Session listing APIs.
  • Device/session management.
  • Hooks and plugin system.
  • First-party router adapters where net/http is not ergonomic enough.
  • Optional stateless or cookie-cache session strategies.

Design Principles

  • Library first, not hosted service.
  • net/http first, no framework lock-in.
  • Opaque database-backed sessions by default.
  • Provider tokens are separate from Canopy sessions.
  • Explicit security failures over silent magic.
  • Typed errors over string matching.
  • Narrow interfaces for custom storage and enterprise integration.

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, 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

View Source
const ProviderEmailPassword = "email-password"

Variables

View Source
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")
	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")
	ErrUnauthorized               = errors.New("canopy: unauthorized")
)

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 AuditEvent struct {
	Type       string
	UserID     string
	Email      string
	ProviderID string
	IPAddress  string
	UserAgent  string
	Success    bool
	Error      string
	At         time.Time
}

type AuditLogger

type AuditLogger interface {
	LogAuthEvent(ctx context.Context, event AuditEvent)
}

type Auth

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

func New

func New(config Config) (*Auth, error)

func (*Auth) API

func (a *Auth) API() *Service

func (*Auth) Handler

func (a *Auth) Handler() http.Handler

func (*Auth) Middleware

func (a *Auth) Middleware(next http.Handler) http.Handler

type Config

type Config struct {
	Store       Store
	Secret      string
	Environment Environment
	BasePath    string

	DisableSignup            bool
	RequireEmailVerification bool
	PasswordMinLength        int
	PasswordMaxLength        int
	PasswordHasher           password.Hasher
	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
}

func (Config) CheckOrigin

func (c Config) CheckOrigin(r *http.Request) bool

type EmailSender

type EmailSender interface {
	SendEmailVerification(ctx context.Context, message EmailVerificationMessage) error
	SendPasswordReset(ctx context.Context, message PasswordResetMessage) error
}

type EmailVerificationMessage

type EmailVerificationMessage struct {
	User        User
	Email       string
	Token       string
	URL         string
	CallbackURL string
	ExpiresAt   time.Time
}

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 OAuthCallbackInput struct {
	Provider     string
	Code         string
	State        string
	StateBinding string
	IPAddress    string
	UserAgent    string
}

type PasswordResetMessage

type PasswordResetMessage struct {
	User        User
	Email       string
	Token       string
	URL         string
	CallbackURL string
	ExpiresAt   time.Time
}

type RefreshProviderTokenInput

type RefreshProviderTokenInput struct {
	UserID     string
	ProviderID string
}

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 RequestPasswordResetInput

type RequestPasswordResetInput struct {
	Email       string
	CallbackURL string
}

type ResetPasswordInput

type ResetPasswordInput struct {
	Token       string
	NewPassword string
}

type SendEmailVerificationInput

type SendEmailVerificationInput struct {
	Email       string
	CallbackURL string
}

type Service

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

func (*Service) GetSession

func (s *Service) GetSession(ctx context.Context, token string) (*SessionData, error)

func (*Service) Middleware

func (s *Service) Middleware(next http.Handler) http.Handler

func (*Service) OAuthCallback

func (s *Service) OAuthCallback(ctx context.Context, in OAuthCallbackInput) (*SessionData, string, string, *bool, error)

func (*Service) RefreshProviderToken

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 (s *Service) RevokeUserSessions(ctx context.Context, userID string) error

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) SignOut

func (s *Service) SignOut(ctx context.Context, token string) error

func (*Service) SignUpEmail

func (s *Service) SignUpEmail(ctx context.Context, in SignUpEmailInput) (*SessionData, string, error)

func (*Service) VerifyEmail

func (s *Service) VerifyEmail(ctx context.Context, in VerifyEmailInput) (*User, error)

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

type SessionData struct {
	User    User    `json:"user"`
	Session Session `json:"session"`
}

func SessionFromContext

func SessionFromContext(ctx context.Context) (*SessionData, bool)

type SignInEmailInput

type SignInEmailInput struct {
	Email      string
	Password   string
	RememberMe *bool
	IPAddress  string
	UserAgent  string
}

type SignInSocialInput

type SignInSocialInput struct {
	Provider    string
	CallbackURL string
	RememberMe  *bool
}

type SignInSocialOutput

type SignInSocialOutput struct {
	URL string `json:"url"`
}

type SignUpEmailInput

type SignUpEmailInput struct {
	Name        string
	Email       string
	Password    string
	Image       string
	CallbackURL string
	RememberMe  *bool
	IPAddress   string
	UserAgent   string
}

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 User

type User struct {
	ID            string    `json:"id"`
	Name          string    `json:"name"`
	Email         string    `json:"email"`
	EmailVerified bool      `json:"emailVerified"`
	Image         string    `json:"image,omitempty"`
	CreatedAt     time.Time `json:"createdAt"`
	UpdatedAt     time.Time `json:"updatedAt"`
}

type Verification

type Verification struct {
	ID         string    `json:"id"`
	Identifier string    `json:"identifier"`
	Value      string    `json:"-"`
	ExpiresAt  time.Time `json:"expiresAt"`
	CreatedAt  time.Time `json:"createdAt"`
	UpdatedAt  time.Time `json:"updatedAt"`
}

type VerifyEmailInput

type VerifyEmailInput struct {
	Token string
}

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 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.

Jump to

Keyboard shortcuts

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