api

package
v0.0.0-...-375b72a Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

The package api is responsible for handling requests and validating user input

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnknown represents an unknown error.
	ErrUnknown = &Error{http.StatusInternalServerError, "UNKNOWN", nil, nil}
	// ErrServiceUnavailable indicates that an external service such as the database is unavailable.
	ErrServiceUnavailable = &Error{http.StatusInternalServerError, "SERVICE_UNAVAILABLE", nil, nil}

	// ErrMissingParameters indicates that the user did not provide identity, password or desired role.
	ErrMissingParameters = &Error{http.StatusBadRequest, "MISSING_PARAMETERS", nil, nil}
	// ErrMissingParameters indicates that the user does not have a password in the database.
	ErrMissingPassword = &Error{http.StatusNotFound, "MISSING_PASSWORD", nil, nil}

	// ErrWrongIdentity indicates that no account was found with the provided parameters.
	ErrWrongIdentity = &Error{http.StatusUnauthorized, "WRONG_IDENTITY", nil, nil}

	// ErrAlreadyLoggedIn indicates that the user is already logged in (JWT token was provided).
	ErrAlreadyLoggedIn = &Error{http.StatusBadRequest, "ALREADY_LOGGED_IN", nil, nil}
	// ErrTokenNotProvided indicates that the user did not provide a token for reset API.
	ErrTokenNotProvided = &Error{http.StatusUnauthorized, "TOKEN_NOT_PROVIDED", nil, nil} // #nosec G101
	// ErrTokenInvalid indicates that the user provided an invalid or expired token.
	ErrTokenInvalid = &Error{http.StatusUnauthorized, "INVALID_TOKEN", nil, nil}

	// ErrInvalidRoute indicates that the user tried to access an invalid route.
	ErrInvalidRoute = &Error{http.StatusNotFound, "INVALID_ROUTE", nil, nil}
)
View Source
var ErrNoSecret = errors.New("$JWT_SECRET must be set")

ErrNoSecret indicates that the JWT_SECRET environment variable is not set.

Functions

func ErrorHandler

func ErrorHandler(e error, c echo.Context)

Custom error handler conformant with shared API rules.

func Login

func Login(c echo.Context, userRepository *database.UserRepository, auth *echojwt.Config) error

Login route handler.

func NewAuthConfig

func NewAuthConfig() (*echojwt.Config, error)

NewAuthConfig creates a new echojwt config from JWT_SECRET.

func NewServer

func NewServer(db *sql.DB) (*echo.Echo, error)

NewServer creates a new Echo server instance for the login REST API.

func PasswordReset

func PasswordReset(c echo.Context, userRepository *database.UserRepository, auth *echojwt.Config) error

Password reset route handler.

Types

type Error

type Error struct {
	// HTTP status code.
	StatusCode int `json:"-"`
	// Error type.
	ErrorType string `json:"error"`
	// Error details.
	Details any `json:"details,omitempty"`
	// Internal wrapped error. Not visible to users.
	Internal error `json:"-"`
}

Represents an error that occurred in the API layer. API errors can be translated into a JSON object following shared API rules.

func (*Error) Error

func (e *Error) Error() string

Describes the API error.

func (*Error) Is

func (e *Error) Is(target error) bool

Service errors are considered equivalent if their error type is equivalent.

func (*Error) Unwrap

func (e *Error) Unwrap() error

If an error has been wrapped in a.Internal, return the error.

func (*Error) WithDetails

func (e *Error) WithDetails(details any) *Error

Attaches detailed user-visible information to an API error. This is intended to give the API consumer more information about where and how it occurred.

func (*Error) Wrap

func (e *Error) Wrap(err error) *Error

Attaches an internal error to an API error.

type Validator

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

Custom validator that uses go-playground/validator.

func NewValidator

func NewValidator() *Validator

NewValidator creates a new instance of service.Validator.

func (*Validator) Validate

func (cv *Validator) Validate(i any) error

Validates user data using go-playground/validator.

Jump to

Keyboard shortcuts

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