auth

package
v0.0.0-...-0fb68f2 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const ClaimsContextKey = "claims"

Variables

View Source
var (
	ErrNoClaimsInContext = errors.New("no claims in context")
	ErrUnauthorized      = errors.New("unauthorized")
	ErrInvalidSession    = errors.New("invalid or expired session")
)
View Source
var (
	ErrProviderNoConfig           = errors.New("missing provider configuration")
	ErrProviderInvalidConfig      = errors.New("invalid provider configuration")
	ErrProviderValidateToken      = errors.New("provider token validation failed")
	ErrProviderCodeExchange       = errors.New("provider code exchange failed")
	ErrProviderTokenRevoke        = errors.New("provider token revoke failed")
	ErrProviderAuthInfo           = errors.New("provider auth info failed")
	ErrProviderKeyMissing         = errors.New("missing provider key")
	ErrProviderUnsupported        = errors.New("unsupported provider")
	ErrProviderUnexpectedResponse = errors.New("provider returned unexpected response")
)

Functions

func AllowUnauthorized

func AllowUnauthorized() func(*MiddlewareConfig)

func NewOAuthMiddleware

func NewOAuthMiddleware(svc Service, opts ...func(*MiddlewareConfig)) fiber.Handler

func WithAuthorized

func WithAuthorized(fn func(*fiber.Ctx, Claims)) func(*MiddlewareConfig)

func WithConfig

func WithConfig(cfg config.Oidc) func(*MiddlewareConfig)

Types

type Claims

type Claims struct {
	ID    string
	Email string
}

func ClaimsFromCtx

func ClaimsFromCtx(ctx *fiber.Ctx) (Claims, error)

func NewClaims

func NewClaims(id, email string) Claims

type FakeProvider

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

func NewFakeProvider

func NewFakeProvider(opts ...ProviderOption) *FakeProvider

func (*FakeProvider) ExchangeCode

func (p *FakeProvider) ExchangeCode(ctx context.Context, authCode string) (Claims, *JWT, error)

func (*FakeProvider) GenerateState

func (p *FakeProvider) GenerateState() State

func (*FakeProvider) GetAuthURL

func (p *FakeProvider) GetAuthURL(state string) string

func (*FakeProvider) GetName

func (p *FakeProvider) GetName() string

func (*FakeProvider) RevokeToken

func (p *FakeProvider) RevokeToken(ctx context.Context, token *JWT) error

func (*FakeProvider) Token

func (p *FakeProvider) Token(userID string) *JWT

Token Returns a valid token for the given user ID.

func (*FakeProvider) ValidateToken

func (p *FakeProvider) ValidateToken(ctx context.Context, token *JWT) (Claims, error)

type JWT

type JWT struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	IDToken      string `json:"id_token"`
	Scope        string `json:"scope"`
	Type         string `json:"token_type"`
	Provider     string `json:"provider"`
	ExpiresIn    int64  `json:"expires_in"`
}

func DecodeSession

func DecodeSession(value string) (*JWT, error)

DecodeSession decode given base64 url encoded JSONWebToken.

func (*JWT) Encode

func (t *JWT) Encode() (string, error)

type MiddlewareConfig

type MiddlewareConfig struct {
	// Extractor defines how the token claims are extracted from the request
	Extractor func(*fiber.Ctx, string) (Claims, error)
	// Authorized runs after valid claims are found
	Authorized func(*fiber.Ctx, Claims)
	// Key name of the session cookie
	Key string
	// AllowEmptyCookie allows unauthenticated access if true
	AllowEmptyCookie bool
}

type Provider

type Provider interface {
	GetName() string
	GetAuthURL(state string) string
	ExchangeCode(ctx context.Context, authCode string) (Claims, *JWT, error)
	ValidateToken(ctx context.Context, token *JWT) (Claims, error)
	RevokeToken(ctx context.Context, token *JWT) error
	GenerateState() State
}

func TestProvider

func TestProvider(cfg config.Provider, client *http.Client) Provider

type ProviderOption

type ProviderOption func(*FakeProvider)

func WithClaims

func WithClaims(c Claims) ProviderOption

func WithExpire

func WithExpire(timestamp int64) ProviderOption

func WithStateID

func WithStateID(stateID string) ProviderOption

type Providers

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

func FromConfiguration

func FromConfiguration(cfg config.Oidc) (Providers, error)

func NewProviders

func NewProviders(provider ...Provider) Providers

func (Providers) Find

func (pp Providers) Find(key string) (Provider, error)

type RedirectURL

type RedirectURL struct {
	URL   string
	State string
}

func NewRedirectURL

func NewRedirectURL(p Provider, state string) (RedirectURL, error)

type Service

type Service interface {
	AuthInfo(ctx context.Context, provider string, token *JWT) (Claims, error)
	AuthURL(provider string) (RedirectURL, error)
	Authenticate(ctx context.Context, provider string, code string) (Claims, *JWT, error)
	Logout(ctx context.Context, token *JWT) error
}

func New

func New(cfg config.Oidc, providers Providers) Service

type State

type State struct {
	ID string `json:"id"`
}

type TimeService

type TimeService interface {
	Now() time.Time
}

func NewFakeTimeService

func NewFakeTimeService(time time.Time) TimeService

func NewTimeService

func NewTimeService() TimeService

Jump to

Keyboard shortcuts

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