application

package
v0.0.0-...-32b810b Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RoleUser defines the role every user belong to.
	RoleUser = "user"
	// RoleAdmin defines the role only admin users belong to.
	RoleAdmin = "admin"

	// TokenScopeAPI defines the scope for granting general API access.
	TokenScopeAPI = "api"
	// TokenScopeAuthRefresh defines the scope for granting refresh of
	// authentication.
	TokenScopeAuthRefresh = "auth:refresh"
)
View Source
const (
	// InternalServerError specifies an unknown error.
	InternalServerError = ErrorType("INTERNAL_SERVER")
	// BadRequestError specifies a validation realted error.
	BadRequestError = ErrorType("BAD_REQUEST")
	// UnauthorizedError specifies authentication related errors.
	UnauthorizedError = ErrorType("UNAUTHORIZED")
	// ForbiddenError specifies authorization (permission) related errors.
	ForbiddenError = ErrorType("FORBIDDEN")
	// NotFoundError specifies errors related with non-existent resources.
	NotFoundError = ErrorType("NOT_FOUND")
	// ConflictError specifies errors related with a resource conflict.
	ConflictError = ErrorType("CONFLICT")
	// UnexpectedError specifies errors occurring unexpectedly, caused by technical issues.
	UnexpectedError = ErrorType("UNEXPECTED")
)
View Source
const (
	// TokenClaimSubject defines the token claim holding the token's subject.
	TokenClaimSubject = "sub"
	// TokenClaimRoles defines the token claim holding the user's roles.
	TokenClaimRoles = "roles"
	// TokenClaimScopes defines the token claim holding the token's scopes.
	TokenClaimScopes = "scope"
)

Variables

This section is empty.

Functions

func RemoveErrorType

func RemoveErrorType(err error) error

RemoveErrorType removes associated error type information form the given error.

func SetErrorType

func SetErrorType(err error, typ ErrorType) error

SetErrorType associates the given error type with the given error.

Types

type AccessTokenClaims

type AccessTokenClaims struct {
	jwt.StandardClaims
	Scope string   `json:"scope"`
	Roles []string `json:"roles"`
}

AccessTokenClaims defines all JWT (standard and custom) claims contained in an accesss tokens.

type AuthService

type AuthService interface {
	// AuthenticateUserByCredentials tries to authenticate the user using the
	// given username and password and returns a new access token in case the
	// credentials are valid.
	AuthenticateUserByCredentials(username, password string) (*Token, error)

	// AuthenicateUserByRefreshToken tries to authenticate the user using the
	// given refresh token and returns a new access token in case the
	// provided refresh token is valid.
	AuthenicateUserByRefreshToken(token string) (*Token, error)

	// SignAccessToken signs the given token and returns the access token
	// encoded as a JWT.
	SignAccessToken(token *Token) (string, error)

	// SignRefreshToken signs the given token and returns the refresh token
	// encoded as a JWT.
	SignRefreshToken(token *Token) (string, error)

	// ExtractScopes extracts the token scopes from the given set of claims.
	ExtractScopes(claims jwt.MapClaims) []string

	// ExtractUsername extracts the username from the given set of claims.
	ExtractUsername(claims jwt.MapClaims) *string

	// ExtractRoles extracts the user's roles from the given set of claims.
	ExtractRoles(claims jwt.MapClaims) []string
}

AuthService defines an application service for authentication and authorization use-cases.

func NewAuthService

func NewAuthService(
	config *config.Configuration,
	users domain.Users,
	tokenKeyResolver TokenKeyResolver,
) AuthService

NewAuthService returns an auth service based on the given user repository and configuration.

type DocumentService

type DocumentService interface {
	// GetUserDocuments returns the given user's documents with respect to the
	// given page request.
	GetUserDocuments(username string, pr domain.PageRequest) ([]domain.Document, int64, error)

	// SearchUserDocuments returns all documents matching the given query with respect to the given page request.
	SearchUserDocuments(username, query string, pr domain.PageRequest) ([]domain.DocumentSearchResult, int64, error)

	// GetUserDocumentByDocumentNumber returns the document with the given document number owned by the given user.
	GetUserDocumentByDocumentNumber(username string, documentNumber uint) (*domain.Document, error)

	// CreateNewDocument creates the given new document owned by the user with the given username.
	CreateNewDocument(username string, document *domain.Document) (*domain.Document, error)

	// GetUserDocumentPagesByDocumentNumber returns the document pages for the document with the given document number with respect to the given
	// username and page request.
	GetUserDocumentPagesByDocumentNumber(username string, documentNumber uint, pr domain.PageRequest) ([]domain.DocumentPage, int64, error)

	// GetUserDocumentPageByDocumentNumberAndPageNumber returns the page with the given page number for the document with the given document number,
	// accessible by the user with the given username.
	GetUserDocumentPageByDocumentNumberAndPageNumber(username string, documentNumber uint, pageNumber uint) (*domain.DocumentPage, error)

	// AddPagesToUserDocument adds the given pages to the document with the given ID.
	AddPagesToUserDocument(username string, documentNumber uint, files []*multipart.FileHeader) ([]domain.DocumentPage, error)

	// GetUserDocumentPageContent returns a reader to a document pages content, if present.
	GetUserDocumentPageContent(username string, documentNumber uint, pageNumber uint) (io.ReadCloser, error)
}

DocumentService defines an application service for managing document-related use cases.

func NewDocumentService

func NewDocumentService(
	users domain.Users,
	documents domain.Documents,
	documentArchive domain.DocumentArchive,
	documentIndex domain.DocumentIndex,
	documentRegistry domain.DocumentRegistry,
) DocumentService

NewDocumentService creates a new document service.

type ErrorType

type ErrorType string

ErrorType enumerates all possible application error types.

func GetErrorType

func GetErrorType(err error) ErrorType

GetErrorType retreives an error's associated error type.

func (ErrorType) New

func (typ ErrorType) New(message string) error

New creates a new error for the given message.

func (ErrorType) Newf

func (typ ErrorType) Newf(message string, args ...interface{}) error

Newf creates a new error with the given message format and arguments.

type RefreshTokenClaims

type RefreshTokenClaims struct {
	jwt.StandardClaims
	Scope string `json:"scope"`
}

RefreshTokenClaims defines all JWT claims contained in a refresh token.

type Token

type Token struct {
	Username       string
	Roles          []string
	Expires        time.Time
	RefreshExpires time.Time
}

Token defines a struct for holding authorization information.

func (*Token) GetAccessTokenClaims

func (t *Token) GetAccessTokenClaims(issuer, audience, scope string) AccessTokenClaims

GetAccessTokenClaims returns the JWT accesss token claims for the given Token instance.

func (*Token) GetRefreshTokenClaims

func (t *Token) GetRefreshTokenClaims(issuer, audience, scope string) RefreshTokenClaims

GetRefreshTokenClaims returns the JWT refresh token claims for the given Token instance.

func (*Token) GrantsGroupMembership

func (t *Token) GrantsGroupMembership(group string) bool

GrantsGroupMembership returns a boolean value indicating whether the token instance grants the given role.

type TokenKeyResolver

type TokenKeyResolver = func(token *jwt.Token) (interface{}, error)

TokenKeyResolver defines a function type for a function that obtains a verification key for a given token.

func ConfigTokenKeyResolver

func ConfigTokenKeyResolver(config *config.Configuration) TokenKeyResolver

ConfigTokenKeyResolver returns a token key resolver using the key from the given config.

type UserService

type UserService interface {
	// GetUserByUsername returns the user with the given username or
	// an error in case no such user exists.
	GetUserByUsername(username string) (*domain.User, error)

	// GetUsers finds and returns users with respect to the given page request.
	GetUsers(pr domain.PageRequest) ([]domain.User, int64, error)

	// Creates the given new user with the desired password as clear-text.
	CreateNewUser(user *domain.User, password string) (*domain.User, error)

	// Update user updates all possible field of the given user.
	UpdateUser(user *domain.User, password *string) (*domain.User, error)

	// DeleteUser deletes the user with the given ID.
	DeleteUser(username string) error

	// UpdateUserPassword updates the password of the user with the given ID.
	UpdateUserPassword(username string, currentPassword, newPassword string) error
}

UserService defines an application service for managing users use-cases.

func NewUserService

func NewUserService(users domain.Users) UserService

NewUserService creates a new user service.

Jump to

Keyboard shortcuts

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