auth

package
v0.0.0-...-def9258 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2019 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// RoleAdmin defines the constant name of the admin role.
	RoleAdmin = "ADMIN"
)

Variables

This section is empty.

Functions

func Middleware

func Middleware(filter Filter) echo.MiddlewareFunc

Middleware defines a middleware which checks for a valid authentication token and uses to custom function to authorize the token.

func RegisterRoutes

func RegisterRoutes(r *echo.Group)

RegisterRoutes registers all related routes for managing users.

func RequireAdminRole

func RequireAdminRole() echo.MiddlewareFunc

RequireAdminRole returns a middleware handler function for protecting end-points needing user authentication. Only users having with 'ADMIN' role are allowed to access the end-point.

func RequireAuthorization

func RequireAuthorization() echo.MiddlewareFunc

RequireAuthorization returns a middleware handler function for protecting end-points needing user authentication. Any valid user ID claim will pass the middleware.

func RequireRoles

func RequireRoles(requiredRoles ...string) echo.MiddlewareFunc

RequireRoles returns a middleware handler function for protecting end-points needing user authentication. The authenticated user needs also the given set of roles to get access granted.

func SignAccessToken

func SignAccessToken(token Token) (string, error)

SignAccessToken signs the given token and returns the access token encoded as a JWT.

func SignRefreshToken

func SignRefreshToken(token Token) (string, error)

SignRefreshToken signs the given token and returns the refresh token encoded as a JWT.

Types

type AccessTokenClaims

type AccessTokenClaims struct {
	jwt.StandardClaims
	UserID uint     `json:"user_id"`
	Roles  []string `json:"roles"`
}

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

type AccessTokenResponse

type AccessTokenResponse struct {
	TokenType    string `json:"token_type"`
	AccessToken  string `json:"access_token"`
	ExpiresIn    int64  `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
}

AccessTokenResponse defines the access token projection returned by API methods.

type AccessTokenSerializer

type AccessTokenSerializer struct {
	C echo.Context
	Token
	AccessToken  string
	RefreshToken string
}

AccessTokenSerializer defines functionality for serializing access tokens.

func (*AccessTokenSerializer) Response

Response returns the API response for a given access token.

type AuthenticationRequestValidator

type AuthenticationRequestValidator struct {
	GrantType string `form:"grant_type" query:"grant_type" validate:"required"`
}

AuthenticationRequestValidator defines the validation rules for a general authentication request.

func NewAuthenticationRequestValidator

func NewAuthenticationRequestValidator() AuthenticationRequestValidator

NewAuthenticationRequestValidator returns a new instance of the respective validator.

func (*AuthenticationRequestValidator) Bind

Bind binds the API request to the request model.

type Filter

type Filter = func(userID uint, username string, roles []string) bool

Filter defines a function type for defining a custom authorization condition.

type PasswordAuthenticationRequestValidator

type PasswordAuthenticationRequestValidator struct {
	Username string `form:"username" validate:"required"`
	Password string `form:"password" validate:"required"`
}

PasswordAuthenticationRequestValidator defines the validation rules for an authentication request with grant type 'password'.

func NewPasswordAuthenticationRequestValidator

func NewPasswordAuthenticationRequestValidator() PasswordAuthenticationRequestValidator

NewPasswordAuthenticationRequestValidator returns a new instance of the respective validator.

func (*PasswordAuthenticationRequestValidator) Bind

Bind binds the API request to the request model.

type RefreshTokenAuthenticationRequestValidator

type RefreshTokenAuthenticationRequestValidator struct {
	Request struct {
		RefreshToken string `form:"refresh_token" validate:"required"`
	}
}

RefreshTokenAuthenticationRequestValidator defines the validation rules for an authentication request with grant type 'refresh_token'.

func NewRefreshTokenAuthenticationRequestValidator

func NewRefreshTokenAuthenticationRequestValidator() RefreshTokenAuthenticationRequestValidator

NewRefreshTokenAuthenticationRequestValidator returns a new instance of the respective validator.

func (*RefreshTokenAuthenticationRequestValidator) Bind

Bind binds the API request to the request model.

type RefreshTokenClaims

type RefreshTokenClaims struct {
	jwt.StandardClaims
	UserID uint `json:"user_id"`
}

RefreshTokenClaims defines all JWT claims contained in a refresh token.

type Token

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

Token defines a struct for holding authorization information.

func AuthenticateUserByID

func AuthenticateUserByID(userID uint) (Token, error)

AuthenticateUserByID returns a new access token for the given user ID if it exists. Note: This method does not verify anything and grants authorization for the given user ID

so be careful when using the result this method.

func AuthenticateUserWithCredentials

func AuthenticateUserWithCredentials(username, password string) (Token, error)

AuthenticateUserWithCredentials tries to authenticate the user with the given username and password and returns a new access token in case the credentials are valid.

func (Token) GetAccessTokenClaims

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

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

func (Token) GetRefreshTokenClaims

func (t Token) GetRefreshTokenClaims(userID uint, issuer, audience string) RefreshTokenClaims

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

func (Token) GrantsRole

func (t Token) GrantsRole(role string) bool

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

Jump to

Keyboard shortcuts

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