auth

package
v0.8.5 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2024 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

JWT token generation and verification logic.

Index

Constants

View Source
const (
	Issuer = "scrape"
)

Variables

This section is empty.

Functions

func ExpiresAt

func ExpiresAt(t time.Time) option

func ExpiresIn

func ExpiresIn(d time.Duration) option

func JWTAuthMiddleware

func JWTAuthMiddleware(
	key HMACBase64Key,
	contextKey any,
	cc ...ClaimsAuthorizer,
) func(http.HandlerFunc) http.HandlerFunc

Checks the Authorization header for a JWT token and verifies it using the provided key. The token is always validated against the HMAC key, the issuer, and the Claims.Validate function.

The ClaimsAuthorizer functions, if any, are called in order. If any of them return an error, the request is rejected with a 401 Unauthorized status and the error message is written to the response body.

If the token is valid, the claims are added to the request context at the key value specified by contextKey.

func WithAudience

func WithAudience(aud string) option

func WithSubject

func WithSubject(sub string) option

Types

type Claims

type Claims struct {
	jwt.RegisteredClaims
}

func NewClaims

func NewClaims(options ...option) (*Claims, error)

func VerifyToken

func VerifyToken(key HMACBase64Key, tokenString string) (*Claims, error)

VerifyToken verifies the token string using the provided key. In the case where the token's signature is invalid, the function will not return any claims.

func (Claims) Sign

func (c Claims) Sign(key HMACBase64Key) (string, error)

func (Claims) String

func (c Claims) String() string

func (Claims) Token

func (c Claims) Token() *jwt.Token

func (Claims) Validate

func (c Claims) Validate() error

Implements the jwt.ClaimsValidator interface In addition to the explicit call when the claims are created, this method is called by ParseWithClaims when the token is parsed.

type ClaimsAuthorizer

type ClaimsAuthorizer func(claims *Claims) error

type HMACBase64Key

type HMACBase64Key []byte

func MustNewHS256SigningKey

func MustNewHS256SigningKey() HMACBase64Key

func NewHS256SigningKey

func NewHS256SigningKey() (HMACBase64Key, error)

func (HMACBase64Key) MarshalText

func (b HMACBase64Key) MarshalText() ([]byte, error)

func (HMACBase64Key) String added in v0.8.5

func (b HMACBase64Key) String() string

func (*HMACBase64Key) UnmarshalText

func (b *HMACBase64Key) UnmarshalText(text []byte) error

Jump to

Keyboard shortcuts

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