auth

package
v0.0.0-...-ca3ac9b Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package auth all things related to authentication and authorization

Index

Constants

This section is empty.

Variables

View Source
var (
	TokenCtxKey = &contextKey{"Token"}
	ErrorCtxKey = &contextKey{"Error"}
)

used context key for parameter given in a std context

View Source
var (
	ErrUnauthorized = errors.New("token is unauthorized")
	ErrExpired      = errors.New("token is expired")
	ErrNBFInvalid   = errors.New("token nbf validation failed")
	ErrIATInvalid   = errors.New("token iat validation failed")
	ErrNoTokenFound = errors.New("no token found")
	ErrAlgoInvalid  = errors.New("algorithm mismatch")
)

defining some common errors

View Source
var JWTConfig = JWTAuthConfig{
	Active: false,
}

JWTConfig for the service

Functions

func Authenticator

func Authenticator(next http.Handler) http.Handler

Authenticator returns a handler for authentication

func NewContext

func NewContext(ctx context.Context, t *JWT, err error) context.Context

NewContext creating a new context for the http functions

func TokenFromCookie

func TokenFromCookie(r *http.Request) string

TokenFromCookie tries to retrieve the token string from a cookie named "jwt".

func TokenFromHeader

func TokenFromHeader(r *http.Request) string

TokenFromHeader tries to retrieve the token string from the "Authorization" request header: "Authorization: BEARER T".

func TokenFromQuery

func TokenFromQuery(r *http.Request) string

TokenFromQuery tries to retrieve the token string from the "jwt" URI query parameter.

To use it, build our own middleware handler, such as:

func Verifier(ja *JWTAuth) func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return Verify(ja, TokenFromQuery, TokenFromHeader, TokenFromCookie)(next)
	}
}

func Verifier

func Verifier(ja *JWTAuth) func(http.Handler) http.Handler

Verifier returns a handler for verification

func Verify

func Verify(ja *JWTAuth, findTokenFns ...func(r *http.Request) string) func(http.Handler) http.Handler

Verify checking a request

Types

type JWT

type JWT struct {
	Token     string
	Header    map[string]any
	Payload   map[string]any
	Signature string
	IsValid   bool
}

JWT struct for the decoded jwt token

func DecodeJWT

func DecodeJWT(token string) (JWT, error)

DecodeJWT simple decode the jwt token string

func FromContext

func FromContext(ctx context.Context) (*JWT, map[string]any, error)

FromContext extract the JWT and a flatten claim structure from a context

func VerifyRequest

func VerifyRequest(ja *JWTAuth, r *http.Request, findTokenFns ...func(r *http.Request) string) (*JWT, error)

VerifyRequest this request -> authorization

func VerifyToken

func VerifyToken(ja *JWTAuth, tokenString string) (*JWT, error)

VerifyToken verify the bearer token

func (*JWT) Validate

func (j *JWT) Validate(_ JWTAuthConfig) error

Validate validation of the token is not implemented

type JWTAuth

type JWTAuth struct {
	Config JWTAuthConfig
}

JWTAuth the jwt authentication struct

func InitJWT

func InitJWT(cnfg JWTAuthConfig) JWTAuth

InitJWT initialise the JWT for this service

type JWTAuthConfig

type JWTAuthConfig struct {
	Active      bool
	Validate    bool
	TenantClaim string
	Strict      bool
	RoleActive  bool
	RoleClaim   string
	RoleMapping map[string]string
}

JWTAuthConfig authentication/Authorisation configuration for JWT authentification

func ParseJWTConfig

func ParseJWTConfig(cfg config.Authentication) (JWTAuthConfig, error)

ParseJWTConfig building up the dynamical configuration for this

type JWTRoleChecker

type JWTRoleChecker struct {
	Config JWTAuthConfig
}

JWTRoleChecker checking a user role against the configuration

func (JWTRoleChecker) CheckRole

func (j JWTRoleChecker) CheckRole(ctx context.Context, allowedRoles []api.Role) bool

CheckRole checking the user role against the given in the REST Api route

type JWTTntChecker

type JWTTntChecker struct {
	Config JWTAuthConfig
}

JWTTntChecker checking a user tenant against the configuration

func (JWTTntChecker) CheckTenant

func (j JWTTntChecker) CheckTenant(ctx context.Context, tenant string) bool

CheckTenant checking the user tenant against the given in the REST Api route

Jump to

Keyboard shortcuts

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