goth

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 14 Imported by: 2

README

👻 Goth

Go Reference Go Report Card Taylor Swift

A fiber 🚀 middleware to integrate authentication to your application. It uses lightweight adapters and providers interfaces to integrate with multi-providers.

Installation

$ go get github.com/zeiss/fiber-goth

Examples

See examples to understand the provided interfaces

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingProviderName is thrown if the provider cannot be determined.
	ErrMissingProviderName = errors.New("missing provider name in request")
	// ErrMissingSession is thrown if there is no active session.
	ErrMissingSession = errors.New("could not find a matching session for this request")
	// ErrMissingCookie is thrown if the cookie is missing.
	ErrMissingCookie = errors.New("missing session cookie")
)
View Source
var ConfigDefault = Config{
	ErrorHandler:        defaultErrorHandler,
	ResponseFilter:      defaultResponseFilter,
	BeginAuthHandler:    BeginAuthHandler{},
	CompleteAuthHandler: CompleteAuthCompleteHandler{},
	LogoutHandler:       LogoutHandler{},
	SessionHandler:      SessionHandler{},
	IndexHandler:        defaultIndexHandler,
	Encryptor:           EncryptCookie,
	Decryptor:           DecryptCookie,
	Expiry:              "7h",
	CookieName:          "fiber_goth.session",
	Extractor:           TokenFromCookie("fiber_goth.session"),
	CookieSameSite:      fasthttp.CookieSameSiteLaxMode,
	LoginURL:            "/login",
	LogoutURL:           "/logout",
	CallbackURL:         "/auth",
}

ConfigDefault is the default config.

Functions

func ContextWithProvider

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

ContextWithProvider returns a new request context containing the provider.

func DecryptCookie added in v1.1.0

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

DecryptCookie Decrypts a cookie value with specific encryption key

func EncryptCookie added in v1.1.0

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

EncryptCookie Encrypts a cookie value with specific encryption key

func GenerateKey added in v1.1.0

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

NewBeginCompleteAuthHandler creates a new middleware handler to complete authentication.

func NewLogoutHandler

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

NewLogoutHandler returns a new default logout handler.

func NewProtectMiddleware added in v1.1.1

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

NewProtectMiddleware returns a new default protect handler.

nolint:gocyclo

func NewProtectedHandler added in v1.1.1

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

NewProtectedHandler returns a new default protected handler.

func NewSessionHandler added in v1.1.0

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

NewSessionHandler returns a new default session handler.

func ProviderFromContext

func ProviderFromContext(c *fiber.Ctx) string

ProviderFromContext returns the provider from the request context.

func SessionFromContext added in v1.1.1

func SessionFromContext(c *fiber.Ctx) (adapters.Session, error)

Session from the request context.

func TokenFromContext added in v1.1.0

func TokenFromContext(c *fiber.Ctx) string

TokenFromContext returns the token from the request context.

func TokenFromCookie added in v1.1.0

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

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

Types

type BeginAuthHandler

type BeginAuthHandler struct{}

BeginAuthHandler is the default handler to begin the authentication process.

func (BeginAuthHandler) New

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

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

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

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 GothHandler

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

	// LogoutHandler is the handler to logout.
	LogoutHandler GothHandler

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

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

	// ProtectedHandler is the handler to protect the route.
	ProtectedHandler fiber.Handler

	// Response filter that is executed when responses need to returned.
	ResponseFilter 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

	// CookieName is the name of the cookie used to store the session.
	CookieName string

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

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

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

	// 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)
}

Config caputes the configuration for running the goth middleware.

type GothHandler

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

GothHandler 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) Get

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

Get returns the value of a query paramater.

type ProtectMiddleware added in v1.1.1

type ProtectMiddleware struct{}

ProtectMiddleware is the default handler for the protection process.

type ProtectedHandler added in v1.1.1

type ProtectedHandler struct{}

ProtectedHandler is the default handler for the validation process.

type SessionHandler added in v1.1.0

type SessionHandler struct{}

SessionHandler is the default handler for the session.

func (SessionHandler) New added in v1.1.0

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

New creates a new handler to manage the session.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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