gousujwt

package module
v2.0.0-...-b942c1c Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CustomClaims

type CustomClaims struct {
	jwt.RegisteredClaims
	UserID int64    `json:"user_id"`
	Groups []string `json:"groups"`
}

CustomClaims definies the format of the JWT payload

func (*CustomClaims) GetAudiences

func (j *CustomClaims) GetAudiences() []string

GetAudiences returns the audiences

func (*CustomClaims) GetGroups

func (j *CustomClaims) GetGroups() []string

GetGroups returns all Groups

func (*CustomClaims) GetUserID

func (j *CustomClaims) GetUserID() int64

GetUserID returns the UserID

type ICustomClaims

type ICustomClaims interface {
	jwt.Claims
	GetUserID() int64
	GetGroups() []string
	GetAudiences() []string
}

ICustomClaims definies the generic interface for custom jwt claims

type IVerifier

type IVerifier interface {
	Verify(w http.ResponseWriter, r *http.Request, groups []string) (*CustomClaims, error)
	VerifyWithCustomClaims(w http.ResponseWriter, r *http.Request, groups []string, claims ICustomClaims) (ICustomClaims, error)
	VerifyTokenWithCustomClaims(r *http.Request, authToken string, groups []string, claims ICustomClaims) (ICustomClaims, error)
}

IJWTVerifier defines the interface of JWTVerifier

type MockVerifier

type MockVerifier struct {
	VerifyFunc                            func(w http.ResponseWriter, r *http.Request, groups []string) (*CustomClaims, error)
	VerifyFuncCalled                      int
	VerifyWithCustomClaimsFunc            func(w http.ResponseWriter, r *http.Request, groups []string, claims ICustomClaims) (ICustomClaims, error)
	VerifyWithCustomClaimsFuncCalled      int
	VerifyTokenWithCustomClaimsFunc       func(r *http.Request, authToken string, groups []string, claims ICustomClaims) (ICustomClaims, error)
	VerifyTokenWithCustomClaimsFuncCalled int
}

MockVerifier provides a simple mock for JWTVerifier

func NewMockVerifier

func NewMockVerifier() *MockVerifier

NewMockVerifier creates a new initialized instance of MockVerifier

func (*MockVerifier) Verify

func (m *MockVerifier) Verify(w http.ResponseWriter, r *http.Request, groups []string) (*CustomClaims, error)

Verify calls VerifyFunc and increases VerifyFuncCalled

func (*MockVerifier) VerifyTokenWithCustomClaims

func (m *MockVerifier) VerifyTokenWithCustomClaims(r *http.Request, authToken string, groups []string, claims ICustomClaims) (ICustomClaims, error)

VerifyWithCustomClaims calls VerifyTokenWithCustomClaimsFunc and increases VerifyTokenWithCustomClaimsFuncCalled

func (*MockVerifier) VerifyWithCustomClaims

func (m *MockVerifier) VerifyWithCustomClaims(w http.ResponseWriter, r *http.Request, groups []string, claims ICustomClaims) (ICustomClaims, error)

VerifyWithCustomClaims calls VerifyWithCustomClaimsFunc and increases VerifyWithCustomClaimsFuncCalled

type Verifier

type Verifier struct {
	// contains filtered or unexported fields
}

JWTVerifier provides a simple JWT verification utility

Used flags:

  • jwt_publickey Filename of JWT-Public-Key-File ()

func NewVerifier

func NewVerifier() (*Verifier, error)

NewVerifier creates a new initilized instance of JWTVerifier and loads the public key from the file specified by the flag 'jwt_publickey'

func (*Verifier) Verify

func (j *Verifier) Verify(w http.ResponseWriter, r *http.Request, groups []string) (*CustomClaims, error)

Verify validates the JWT from the authorization header and checks if the required groups are fullfiled

If the authorization fails it sends a 403 and returns

func (*Verifier) VerifyTokenWithCustomClaims

func (j *Verifier) VerifyTokenWithCustomClaims(r *http.Request, authToken string, groups []string, claims ICustomClaims) (ICustomClaims, error)

VerifyTokenWithCustomClaims validates a JWT and checks if the required groups are fullfiled

If the authorization fails it sends a 403 and returns

func (*Verifier) VerifyWithCustomClaims

func (j *Verifier) VerifyWithCustomClaims(w http.ResponseWriter, r *http.Request, groups []string, claims ICustomClaims) (ICustomClaims, error)

VerifyWithCustomClaims validates the JWT from the authorization header and checks if the required groups are fullfiled

If the authorization fails it sends a 403 and returns

Jump to

Keyboard shortcuts

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