goth

package module
v3.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SessionScope      = "session"
	CodeVerifierScope = "code_verifier"
)

Variables

View Source
var (
	// ErrMissingProviderName is thrown if the provider cannot be determined.
	ErrMissingProviderName = NewError(http.StatusBadRequest, "missing provider name in request")
	// ErrMissingSession is thrown if there is no active session.
	ErrMissingSession = NewError(http.StatusBadRequest, "could not find a matching session for this request")
	// ErrBadSession is thrown if the session is invalid.
	ErrBadSession = NewError(http.StatusBadRequest, "session is invalid")
	// ErrMissingUser is thrown if the user is missing.
	ErrMissingUser = NewError(http.StatusBadRequest, "missing user")
	// ErrMissingCookie is thrown if the cookie is missing.
	ErrMissingCookie = NewError(http.StatusBadRequest, "missing session cookie")
	// ErrBadRequest is thrown if the request is invalid.
	ErrBadRequest = NewError(http.StatusBadRequest, "bad request")
)
View Source
var ConfigDefault = Config{
	ErrorHandler:        defaultErrorHandler,
	BeginAuthHandler:    BeginAuthHandler{},
	CompleteAuthHandler: CompleteAuthCompleteHandler{},
	LogoutHandler:       LogoutHandler{},
	SessionHandler:      SessionHandler{},
	IndexHandler:        defaultIndexHandler,
	Encryptor:           EncryptCookie,
	Decryptor:           DecryptCookie,
	Expiry:              "7h",
	Extractor:           TokenFromCookie("fiber_goth.session"),
	CookieSameSite:      "lax",
	CompletionURL:       "/",
	LoginURL:            "/login",
	LogoutURL:           "/logout",
	CallbackURL:         "/auth",
	CookiePrefix:        "fiber_goth",
	CookieSecure:        false,
	Environment:         Development,
}

ConfigDefault is the default config.

Functions

func CodeVerifierFromCookie

func CodeVerifierFromCookie(c fiber.Ctx, cfg Config) (string, error)

CodeVerifierFromCookie returns the code verifier from the cookie.

func ContextWithProvider

func ContextWithProvider(ctx fiber.Ctx, provider string) fiber.Ctx

ContextWithProvider returns a new request context containing the provider.

func DecryptCookie

func DecryptCookie(value, key string) (string, error)

DecryptCookie Decrypts a cookie value with specific encryption key.

func EncryptCookie

func EncryptCookie(value, key string) (string, error)

EncryptCookie Encrypts a cookie value with specific encryption key.

func GenerateKey

func GenerateKey() string

GenerateKey Generates an encryption key.

func GetStateFromContext

func GetStateFromContext(ctx fiber.Ctx) string

GetStateFromContext return the state that is returned during the callback.

func NewBeginAuthHandler

func NewBeginAuthHandler(config ...Config) fiber.Handler

NewBeginAuthHandler creates a new middleware handler to start authentication.

func NewCompleteAuthHandler

func NewCompleteAuthHandler(config ...Config) fiber.Handler

NewCompleteAuthHandler creates a new middleware handler to complete authentication.

func NewLogoutHandler

func NewLogoutHandler(config ...Config) fiber.Handler

NewLogoutHandler returns a new default logout handler.

func NewSessionHandler

func NewSessionHandler(config ...Config) fiber.Handler

NewSessionHandler returns a new default session handler.

func NewStateCookie

func NewStateCookie(name, value string, secure bool) *fasthttp.Cookie

NewStateCookie creates a new state cookie.

func Protect

func Protect(config ...Config) fiber.Handler

Protect is a middleware that protects routes by checking for a valid session.

func ProtectedHandler

func ProtectedHandler(handler fiber.Handler, config ...Config) fiber.Handler

ProtectedHandler returns a new default protected handler.

func ProviderFromContext

func ProviderFromContext(c fiber.Ctx) string

ProviderFromContext returns the provider from the request context.

func Session

func Session(config ...Config) fiber.Handler

Session is the default handler to attach the session to the context.

func SessionFromContext

func SessionFromContext(c fiber.Ctx) (adapters.GothSession, error)

SessionFromContext returns the session from the request context.

func TokenFromContext

func TokenFromContext(c fiber.Ctx) string

TokenFromContext returns the token from the request context.

func TokenFromCookie

func TokenFromCookie(param string) func(c fiber.Ctx) (string, error)

TokenFromCookie returns a function that extracts token from the cookie header.

func ValidSession

func ValidSession(c fiber.Ctx) bool

ValidSession returns true if the session is valid.

Types

type BeginAuthHandler

type BeginAuthHandler struct{}

BeginAuthHandler is the default handler to begin the authentication process.

func (BeginAuthHandler) New

New creates a new handler to begin authentication.

type CompleteAuthCompleteHandler

type CompleteAuthCompleteHandler struct{}

CompleteAuthComplete is the default handler to complete the authentication process.

func (CompleteAuthCompleteHandler) New

New creates a new handler to complete authentication.

type Config

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	Next func(c fiber.Ctx) bool

	// BeginAuthHandler is the handler to start authentication.
	BeginAuthHandler Handler

	// CompleteAuthHandler is the handler to complete the authentication.
	CompleteAuthHandler Handler

	// LogoutHandler is the handler to logout.
	LogoutHandler Handler

	// SessionHandler is the handler to manage the session.
	SessionHandler Handler

	// IndexHandler is the handler to display the index.
	IndexHandler fiber.Handler

	// CompletionFilter that is executed when responses need to returned.
	CompletionFilter func(c fiber.Ctx) error

	// Secret is the secret used to sign the session.
	Secret string

	// Expiry is the duration that the session is valid for.
	Expiry string

	// CookiePrefix is the prefix of the cookies used to store session data.
	CookiePrefix string

	// CookieSameSite is the SameSite attribute of the cookie.
	CookieSameSite string

	// CookiePath is the path of the cookie.
	CookiePath string

	// CookieDomain is the domain of the cookie.
	CookieDomain string

	// CookieHTTPOnly is the HTTPOnly attribute of the cookie.
	CookieHTTPOnly bool

	// CookieSecure is the Secure attribute of the cookie.
	CookieSecure bool

	// Encryptor is the function used to encrypt the session.
	Encryptor func(decryptedString, key string) (string, error)

	// Decryptor is the function used to decrypt the session.
	Decryptor func(encryptedString, key string) (string, error)

	// Adapter is the adapter used to store the session.
	// Adapter adapters.Adapter
	Adapter adapters.Adapter

	// LoginURL is the URL to redirect to when the user is not authenticated.
	LoginURL string

	// LogoutURL is the URL to redirect to when the user logs out.
	LogoutURL string

	// CallbackURL is the URL to redirect to when the user logs out.
	CallbackURL string

	// CompletionURL is the default url after completion
	CompletionURL string

	// ErrorHandler is executed when an error is returned from fiber.Handler.
	//
	// Optional. Default: DefaultErrorHandler
	ErrorHandler fiber.ErrorHandler

	// Extractor is the function used to extract the token from the request.
	Extractor func(c fiber.Ctx) (string, error)

	// Environment is the environment the application is running in.
	Environment Environment
}

Config caputes the configuration for running the goth middleware.

func (*Config) CodeVerifierCookieName

func (cfg *Config) CodeVerifierCookieName() string

CodeVerifierCookieName returns the code verifier cookie name with the prefix.

func (*Config) CookieName

func (cfg *Config) CookieName(scope string) string

CookieName returns the cookie name with the prefix.

func (*Config) SessionCookieName

func (cfg *Config) SessionCookieName() string

SessionCookieName returns the session cookie name with the prefix.

type Environment

type Environment string

Environment represents the environment the application is running in.

const (
	// Noop is a no-op function.
	Noop Environment = "noop"
	// Development environment.
	Development Environment = "development"
	// Testing environment.
	Testing Environment = "testing"
	// Staging environment.
	Staging Environment = "staging"
	// Production environment.
	Production Environment = "production"
)

type Error

type Error struct {
	Code    int
	Message string
}

Error is the default error type for the goth middleware.

func NewError

func NewError(code int, message ...string) *Error

NewError creates a new Error instance with an optional message.

func (*Error) Error

func (e *Error) Error() string

Error makes it compatible with the `error` interface.

type Handler

type Handler interface {
	New(cfg Config) fiber.Handler
}

Handler is the interface for defining handlers for the middleware.

type LogoutHandler

type LogoutHandler struct{}

LogoutHandler is the default handler for the logout process.

func (LogoutHandler) New

func (LogoutHandler) New(cfg Config) fiber.Handler

New creates a new handler to logout.

type Params

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

Params maps the parameters of the Fiber context to the gothic context.

func (*Params) CodeVerifier

func (p *Params) CodeVerifier() string

CodeVerifier returns the code verifier for PKCE, if applicable.

func (*Params) Get

func (p *Params) Get(key string) string

Get returns the value of a query paramater.

type SessionHandler

type SessionHandler struct{}

SessionHandler is the default handler for the session.

func (SessionHandler) New

New creates a new handler to manage the session.

type StateCtx

type StateCtx struct {
	Nounce      string `json:"nounce"`
	RedirectURL string `json:"redirect_url"`
}

StateCtx holds the state of the authentication process.

Directories

Path Synopsis
dex

Jump to

Keyboard shortcuts

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