chiJwk

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 20 Imported by: 1

Documentation

Index

Constants

View Source
const JwtTokenKey = "jwt-token"

JwtTokenKey is the key for the jwt token in the context.

View Source
const LogCtxKey = "log"

Variables

This section is empty.

Functions

func FromCtx added in v0.8.0

func FromCtx(ctx context.Context) *zap.Logger

func Get added in v0.8.0

func Get() *zap.Logger

func GetClaims

func GetClaims(ctx context.Context, token Token) error

GetClaims extracts the token claims from the context into the provided object.

func TokenHasRequiredRoles added in v0.3.0

func TokenHasRequiredRoles(tokenRoles []string, requiredRoles []string) bool

TokenHasRequiredRoles checks if the token has the required scopes.

func TokenHasRequiredScopes added in v0.3.0

func TokenHasRequiredScopes(tokenScopes []string, requiredScopes []string) bool

TokenHasRequiredScopes checks if the token has the required scopes.

Types

type AuthenticationType added in v0.5.0

type AuthenticationType int

type CookieOptions added in v0.5.0

type CookieOptions struct {
	Name string
}

type DefaultFilter added in v0.4.0

type DefaultFilter struct {
	FilterRoles  []string
	FilterScopes []string
}

func (DefaultFilter) Roles added in v0.4.0

func (f DefaultFilter) Roles() []string

func (DefaultFilter) Scopes added in v0.4.0

func (f DefaultFilter) Scopes() []string

type Filter added in v0.3.0

type Filter interface {
	Roles() []string
	Scopes() []string
}

type JwkAuthOptions

type JwkAuthOptions struct {
	CookieOptions CookieOptions
	JwkSet        jwk.Set
	Issuer        string
	IssuerJwkUrl  string
	Filter        Filter
	CreateToken   func(claims map[string]interface{}) (Token, error)
}

JwkAuthOptions is the struct for the jwk auth middleware.

func NewJwkOptions

func NewJwkOptions(issuer string, jwksUrl string) (*JwkAuthOptions, error)

NewJwkOptions creates a new jwk auth middleware.

func (*JwkAuthOptions) AuthMiddleware

func (options *JwkAuthOptions) AuthMiddleware(filter ...Filter) func(next http.Handler) http.Handler

AuthMiddleware is the middleware for authenticating requests.

func (*JwkAuthOptions) WithCookieOptions added in v0.5.0

func (options *JwkAuthOptions) WithCookieOptions(cookieOptions CookieOptions) *JwkAuthOptions

WithCookieOptions sets the cookie options that determines how the cookie is extracted from the request.

func (*JwkAuthOptions) WithCreateToken added in v0.3.2

func (options *JwkAuthOptions) WithCreateToken(createToken func(claims map[string]interface{}) (Token, error)) *JwkAuthOptions

WithCreateToken sets the create token option that determines how the token is created.

func (*JwkAuthOptions) WithFilter added in v0.3.0

func (options *JwkAuthOptions) WithFilter(filter Filter) *JwkAuthOptions

WithFilter sets the filter option that determines the roles and scopes that are required for the token.

func (*JwkAuthOptions) WithIssuer added in v0.2.0

func (options *JwkAuthOptions) WithIssuer(issuer string) *JwkAuthOptions

WithIssuer sets the issuer option that determines the issuer of the tokens.

func (*JwkAuthOptions) WithIssuerJwkUrl added in v0.2.0

func (options *JwkAuthOptions) WithIssuerJwkUrl(issuerJwkUrl string) *JwkAuthOptions

WithIssuerJwkUrl sets the issuer JWK URL option that determines where the JWK Set should be fetched from.

type TestServer

type TestServer struct {
	JwkSet     jwk.Set
	PrivateKey *rsa.PrivateKey
	Issuer     string
	Server     *http.Server
	Kid        string
}

func NewTestServer

func NewTestServer(addr string) (*TestServer, error)

NewTestServer creates a new test server with a JWK Set and RSA key pair. The server will listen on the specified address. If no address is specified, the server will listen on a random port. The server can be stopped by calling the Stop method.

The JWK Set will contain a single JWK with the following properties: - Key ID: test-kid - Algorithm: RS256 - Key Usage: Signature - Key Type: RSA

The RSA key pair will be used to sign JWTs.

The Issuer property will be set to the address of the server.

The Kid property will be set to the Key ID of the JWK.

The server will have one endpoint: - /keys: Returns the JWK Set

func (*TestServer) IssueToken

func (s *TestServer) IssueToken(claims any) (string, error)

IssueToken issues a JWT with the specified claims. The token will be signed with the RSA key pair.

The token will have the following claims: - Audience: test-audience - Issuer: The address of the test server - Subject: test-subject - Not Before: The current time - Issued At: The current time - Expiration: The current time plus one minute - JWT ID: test-token-id

func (*TestServer) Stop

func (s *TestServer) Stop(ctx context.Context)

Stop stops the test server.

type Token added in v0.3.0

type Token interface {
	Roles() []string
	Scopes() []string
}

func CreateTokenFromClaims added in v0.4.0

func CreateTokenFromClaims[T Token](claims map[string]interface{}) (Token, error)

CreateTokenFromClaims create a token from claims. The token must implement the Token interface and have mapstructure tags.

Jump to

Keyboard shortcuts

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