Documentation ¶
Overview ¶
Package security implements authentication / authorization by means of JWT tokens
Index ¶
Constants ¶
const (
// UserKey defines an authenticated user object stored in the context
UserKey = "context_user"
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Claim ¶
type Claim struct { // Name of the application Name string // URL of the application URL string // Roles possible roles Roles []string }
Claim defines the authorization requirements
type Claims ¶
type Claims struct { Type string `json:"Type"` DisplayName string `json:"DisplayName"` Email string `json:"Email"` UserID string `json:"UserId"` UserName string `json:"UserName"` GivenName string `json:"GivenName"` Surname string `json:"Surname"` Claims []string `json:"Claims"` }
Claims defines custom JWT claims for the token
type JwtMiddleware ¶ added in v1.0.3
type JwtMiddleware struct {
// contains filtered or unexported fields
}
JwtMiddleware is used to authenticate a user based on a token the token is either retrieved by the well known Authorization header or fetched from a cookie
func NewJwtMiddleware ¶ added in v1.0.3
func NewJwtMiddleware(options JwtOptions, settings cookies.Settings) *JwtMiddleware
NewJwtMiddleware creates a new instance using the provided options
func (*JwtMiddleware) JwtContext ¶ added in v1.0.3
func (j *JwtMiddleware) JwtContext(next http.Handler) http.Handler
JwtContext performs the middleware action
type JwtOptions ¶ added in v1.0.3
type JwtOptions struct { // JwtSecret is the jwt signing key JwtSecret string // JwtIssuer specifies identifies the principal that issued the token JwtIssuer string // CookieName specifies the HTTP cookie holding the token CookieName string // RequiredClaim to access the application RequiredClaim Claim // RedirectURL forwards the request to an external authentication service RedirectURL string // CacheDuration defines the duration to cache the JWT token result CacheDuration string // ErrorPath is used if html errors are returned to the client ErrorPath string }
JwtOptions defines presets for the Authentication handler by the default the JWT token is fetched from the Authentication header as a fallback it is possible to fetch the token from a specific cookie
type JwtTokenPayload ¶
type JwtTokenPayload struct { Type string UserName string Email string Claims []string UserID string `json:"UserId"` DisplayName string Surname string GivenName string jwt.StandardClaims }
JwtTokenPayload is the parsed contents of the given token
func ParseJwtToken ¶
func ParseJwtToken(token, tokenSecret, issuer string) (JwtTokenPayload, error)
ParseJwtToken parses, validates and extracts data from a jwt token
type MemoryCache ¶ added in v1.0.2
MemoryCache implements a simple cache
func NewMemCache ¶ added in v1.0.2
func NewMemCache(duration time.Duration) *MemoryCache
NewMemCache create a cache with the given TTL
func (*MemoryCache) Get ¶ added in v1.0.2
func (s *MemoryCache) Get(key string) *User
Get returns an Userobject by the given key
func (*MemoryCache) Set ¶ added in v1.0.2
func (s *MemoryCache) Set(key string, user *User)
Set puts an Userobject into the cache