v0

package
v0.0.0-...-1db7b98 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package v0 contains handlers, middleware and response formats for the v0 API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Authentication

func Authentication(publicKey *rsa.PublicKey) fiber.Handler

Authentication wraps Fiber's JWT middleware, parsing the current user ID from the JWT claims and setting it on the request context.

A request ID is expected to be present on the request context.

func ContentTypeValidation

func ContentTypeValidation(c *fiber.Ctx) error

ContentTypeValidation is middleware that validates the request Content-Type header.

func ErrorHandling

func ErrorHandling(c *fiber.Ctx) error

ErrorHandling is middleware that handles errors returned by the v0 API handlers.

func NewBadRequestError

func NewBadRequestError(requestID string, cause error) error

NewBadRequestError should be used in responses to syntactically invalid requests.

func NewInternalServerError

func NewInternalServerError(requestID string, cause error) error

func NewNotFoundError

func NewNotFoundError(requestID string, resource missingResource) error

NewNotFoundError should be used in responses to requests for resources that don't exist.

func NewPreconditionFailedError

func NewPreconditionFailedError(
	requestID string,
	resource string,
	eTag etag.ETag,
	cause error,
) error

func NewUnauthorizedError

func NewUnauthorizedError(requestID string, cause error) error

func NewUnprocessableEntityError

func NewUnprocessableEntityError(requestID string, validationErrs user.ValidationErrors) error

func NewUnsupportedContentTypeError

func NewUnsupportedContentTypeError(requestID string, contentType string, supportedContentTypes []string) error

func UsersErrorHandling

func UsersErrorHandling(c *fiber.Ctx) error

UsersErrorHandling is middleware that handles all users endpoint-related response.

Types

type Claims

type Claims struct {
	jwt.RegisteredClaims
}

Claims is an extensible set of JWT claims that includes all RFC 7519 Registered Claims.

type JSONError

type JSONError struct {
	RequestID string              `json:"id"`
	Status    int                 `json:"status"`
	Message   string              `json:"message"`
	Errors    map[string][]string `json:"errors,omitempty"`
	// contains filtered or unexported fields
}

JSONError maps an application error to its JSON representation.

func (*JSONError) Error

func (e *JSONError) Error() string

func (*JSONError) Is

func (e *JSONError) Is(other error) bool

type JWT

type JWT string

JWT represents a JSON Web Token.

type JWTConfig

type JWTConfig struct {
	RS256PrivateKey *rsa.PrivateKey
	TTL             time.Duration
	Issuer          string
}

JWTConfig is the minimal configuration required by [JWTProvider]s and JWT middleware.

func (JWTConfig) PublicKey

func (cfg JWTConfig) PublicKey() *rsa.PublicKey

type JWTProvider

type JWTProvider interface {
	// TokenFor returns a signed JWT for the given subject.
	//
	// # Errors
	//	- Internal errors related to signing the token.
	TokenFor(subject uuid.UUID) (JWT, error)
}

JWTProvider is a source of signed JSON Web Tokens.

func NewJWTProvider

func NewJWTProvider(privateKey *rsa.PrivateKey, ttl time.Duration, issuer string) JWTProvider

NewJWTProvider returns the default JWT provider.

type UsersHandler

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

UsersHandler holds dependencies for users endpoints.

func NewUsersHandler

func NewUsersHandler(service user.Service, jwtProvider JWTProvider) *UsersHandler

func (*UsersHandler) GetCurrent

func (h *UsersHandler) GetCurrent(c *fiber.Ctx) error

GetCurrent returns the user corresponding to the ID contained in the request JWT.

func (*UsersHandler) Login

func (h *UsersHandler) Login(c *fiber.Ctx) error

Login authenticates a user and returns the user and token if successful.

func (*UsersHandler) Register

func (h *UsersHandler) Register(c *fiber.Ctx) error

Register creates and returns a new user, along with an auth token.

func (*UsersHandler) UpdateCurrent

func (h *UsersHandler) UpdateCurrent(c *fiber.Ctx) error

UpdateCurrent updates the user corresponding to the ID contained in the request JWT.

Jump to

Keyboard shortcuts

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