auth

package
v0.30.2 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AuthMiddleware

func AuthMiddleware(next http.Handler) http.Handler

AuthMiddleware validates Supabase JWT tokens (uses default SupabaseAuthClient)

func AuthMiddlewareWithClient

func AuthMiddlewareWithClient(authClient AuthClient) func(http.Handler) http.Handler

AuthMiddlewareWithClient validates JWT tokens using the provided AuthClient

func OptionalAuthMiddleware

func OptionalAuthMiddleware(next http.Handler) http.Handler

OptionalAuthMiddleware validates JWT if present but doesn't require it

Types

type AuthClient

type AuthClient interface {
	ValidateToken(ctx context.Context, token string) (*UserClaims, error)
	ExtractTokenFromRequest(r *http.Request) (string, error)
	SetUserInContext(r *http.Request, user *UserClaims) *http.Request
}

AuthClient defines the interface for authentication operations

type Config

type Config struct {
	AuthURL        string
	PublishableKey string
}

Config holds Supabase authentication configuration

func NewConfigFromEnv

func NewConfigFromEnv() (*Config, error)

NewConfigFromEnv creates auth config from environment variables

func (*Config) Validate

func (c *Config) Validate() error

Validate ensures all required configuration is present

type SessionInfo

type SessionInfo struct {
	IsValid       bool   `json:"is_valid"`
	ExpiresAt     int64  `json:"expires_at,omitempty"`
	RefreshNeeded bool   `json:"refresh_needed"`
	UserID        string `json:"user_id,omitempty"`
	Email         string `json:"email,omitempty"`
}

SessionInfo holds session information and token validity

func ValidateSession

func ValidateSession(tokenString string) *SessionInfo

ValidateSession validates a JWT token and returns session information

type SupabaseAuthClient

type SupabaseAuthClient struct{}

SupabaseAuthClient implements AuthClient for Supabase authentication

func NewSupabaseAuthClient

func NewSupabaseAuthClient() *SupabaseAuthClient

NewSupabaseAuthClient creates a new SupabaseAuthClient

func (*SupabaseAuthClient) ExtractTokenFromRequest

func (s *SupabaseAuthClient) ExtractTokenFromRequest(r *http.Request) (string, error)

ExtractTokenFromRequest extracts a JWT token from the Authorization header

func (*SupabaseAuthClient) SetUserInContext

func (s *SupabaseAuthClient) SetUserInContext(r *http.Request, user *UserClaims) *http.Request

SetUserInContext adds user claims to the request context

func (*SupabaseAuthClient) ValidateToken

func (s *SupabaseAuthClient) ValidateToken(ctx context.Context, token string) (*UserClaims, error)

ValidateToken validates a Supabase JWT token

type UserClaims

type UserClaims struct {
	jwt.RegisteredClaims
	UserID       string         `json:"sub"`
	Email        string         `json:"email"`
	AppMetadata  map[string]any `json:"app_metadata"`
	UserMetadata map[string]any `json:"user_metadata"`
	Role         string         `json:"role"`
}

UserClaims represents the Supabase JWT claims

func GetUserFromContext

func GetUserFromContext(ctx context.Context) (*UserClaims, bool)

GetUserFromContext extracts user claims from the request context

type UserContextKey

type UserContextKey string

UserContextKey is the key used to store user claims in the request context

const (
	UserKey UserContextKey = "user"
)

Jump to

Keyboard shortcuts

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