jwt

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2021 License: MIT Imports: 12 Imported by: 4

README

jwt - human friendly json web token library

Build Status Coverage Status Go Report Card Documentation GitHub issues Release

import "github.com/contiamo/jwt"

Usage

type Claims
type Claims map[string]interface{}

Claims is a map of string->something containing the meta infos associated with a token

func CreateToken
func CreateToken(claims Claims, key interface{}) (string, error)

CreateToken takes some claims and a key (either private rsa, private ec or hmac key) and returns a signed json web token

func GetTokenFromRequest
func GetTokenFromRequest(r *http.Request) (string, string, error)

GetTokenFromRequest takes the first Authorization header and extracts the bearer json web token

func LoadPrivateKey
func LoadPrivateKey(keyFile string) (interface{}, error)

LoadPrivateKey loads a PEM encoded private key (either rsa or ec)

func LoadPublicKey
func LoadPublicKey(keyFile string) (interface{}, error)

LoadPublicKey loads a PEM encoded public key (either rsa or ec)

func ParsePrivateKey
func ParsePrivateKey(data []byte) (interface{}, error)

ParsePrivateKey parses a pem encoded private key (rsa or ecdsa based)

func ParsePublicKey
func ParsePublicKey(data []byte) (interface{}, error)

ParsePublicKey parses a pem encoded public key (rsa or ecdsa based)

func GetClaimsFromRequest
func GetClaimsFromRequest(r *http.Request, key interface{}) (string, Claims, error)

GetClaimsFromRequest extracts the token from a request, returning the claims

func GetClaimsFromRequestWithValidation
func GetClaimsFromRequestWithValidation(r *http.Request, key interface{}) (string, Claims, error)

GetClaimsFromRequestWithValidation extracts and validates the token from a request, returning the claims

func ValidateToken
func ValidateToken(tokenString string, key interface{}) (Claims, error)

ValidateToken checks the signature of the token with a given public key and returns the associated claims

Documentation

Index

Constants

View Source
const (
	// AuthorizationHeader is the constant string used to get the Authorization
	// headers
	AuthorizationHeader = "Authorization"
)

Variables

This section is empty.

Functions

func ClaimsToContext added in v0.2.1

func ClaimsToContext(ctx context.Context, claims Claims) context.Context

ClaimsToContext stores claims in a context

func ClaimsToContextMiddleware added in v0.2.1

func ClaimsToContextMiddleware(handler http.Handler, header string, idpKey interface{}) http.Handler

ClaimsToContextMiddleware is a http middleware which parses and validates a jwt from the authorization header and stores the claims in the requests context before calling the next handler.

func CreateToken

func CreateToken(claims Claims, key interface{}) (string, error)

CreateToken takes some claims and a private key (either rsa or ec) and returns a signed json web token

func GetTokenFromRequest

func GetTokenFromRequest(r *http.Request, header string) (prefix string, token string, err error)

GetTokenFromRequest takes the first Authorization header or `token` GET pararm , then extract the token prefix and json web token

func LoadPrivateKey

func LoadPrivateKey(keyFile string) (interface{}, error)

LoadPrivateKey loads a PEM encoded private key (either rsa or ec)

func LoadPublicKey

func LoadPublicKey(keyFile string) (interface{}, error)

LoadPublicKey loads a PEM encoded public key (either rsa or ec)

func ParsePrivateKey

func ParsePrivateKey(data []byte) (interface{}, error)

ParsePrivateKey parses a pem encoded private key (rsa or ecdsa based)

func ParsePublicKey

func ParsePublicKey(data []byte) (interface{}, error)

ParsePublicKey parses a pem encoded public key (rsa or ecdsa based)

func RequireClaim added in v0.2.1

func RequireClaim(handler http.Handler, claimKey, expectedClaimValue string) http.Handler

RequireClaim checks if the requests claims contain a specific value for a specific key

Types

type Claims

type Claims map[string]interface{}

Claims is a map of string->something containing the meta infos associated with a token

func ClaimsFromContext added in v0.2.1

func ClaimsFromContext(ctx context.Context) Claims

ClaimsFromContext retrieves the requests claims from a context

func GetClaimsFromRequest

func GetClaimsFromRequest(r *http.Request, header string) (prefix string, claims Claims, err error)

GetClaimsFromRequest extracts the token from a request, returning the claims without validating the token. This should only be used in situations where you can already trust or if you are simply logging the claim information.

func GetClaimsFromRequestWithValidation added in v0.2.0

func GetClaimsFromRequestWithValidation(r *http.Request, header string, key interface{}) (prefix string, claims Claims, err error)

GetClaimsFromRequestWithValidation extracts and validates the token from a request, returning the claims

func GetUnvalidatedClaims added in v0.2.0

func GetUnvalidatedClaims(tokenString string) (claims Claims, err error)

GetUnvalidatedClaims extracts the token claims without validating the token

func ValidateToken

func ValidateToken(tokenString string, key interface{}) (Claims, error)

ValidateToken checks the signature of the token with a given public key and returns the associated claims

Jump to

Keyboard shortcuts

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