jwt

package
v6.7.2 Latest Latest
Warning

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

Go to latest
Published: May 2, 2020 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// PublicKeyContextKey is a context key. It can be used in HTTP / GRPC
	// handlers with context.WithValue to access the server that
	// started the handler. The associated value will be of type *rsa.PublicKey.
	PublicKeyContextKey = &contextKey{"public-key"}

	// PrivateKeyContextKey is a context key. It can be used in HTTP / GRPC
	// handlers with context.WithValue to access the server that
	// started the handler. The associated value will be of type *rsa.PublicKey.
	PrivateKeyContextKey = &contextKey{"private-key"}

	// TokenContextKey is a context key. It can be used in HTTP / GRPC
	// handlers with context.WithValue to access the server that
	// started the handler. The associated value will be of type string.
	TokenContextKey = &contextKey{"token-key"}
)
View Source
var (
	ErrExpiredToken           = errors.New("token has expired")
	ErrInvalidAudience        = errors.New("token has invalid audience")
	ErrInvalidIdentifier      = errors.New("token has invalid identifier")
	ErrPrivateKeyNotAvailable = errors.New("private key not available in context")
	ErrPublicKeyNotAvailable  = errors.New("public key not available in context")
)

Functions

func Audience

func Audience(token string) string

Audience The "aud" (audience) claim identifies the audience that the JWT is intended for.

func BearerAuth

func BearerAuth(r *http.Request) (token string, ok bool)

BearerAuth returns the token provided in the request's Authorization header, if the request uses HTTP Bearer Authentication.

func BearerTokenStreamServerInterceptor added in v6.2.0

func BearerTokenStreamServerInterceptor() grpc.StreamClientInterceptor

BearerTokenStreamServerInterceptor makes bearer token available in grpc context

func BearerTokenUnaryServerInterceptor added in v6.2.0

func BearerTokenUnaryServerInterceptor() grpc.UnaryServerInterceptor

BearerTokenUnaryServerInterceptor makes bearer token available in grpc context

func Identifier

func Identifier(token string) string

Identifier the "iss" (issuer) claim identifies the principal that issued the JWT.

func Jti

func Jti(token string) string

Jti The "jti" (JWT ID) claim provides a unique identifier for the JWT.

func LoadPrivateKey

func LoadPrivateKey(path string) (*rsa.PrivateKey, error)

LoadPrivateKey loads a private key from PEM encoded data.

func LoadPublicKey

func LoadPublicKey(path string) (*rsa.PublicKey, error)

LoadPublicKey loads a public key from PEM encoded data.

func Middleware

func Middleware(a *rsa.PublicKey, b *rsa.PrivateKey) router.Middleware

Middleware adds *rsa.PublicKey and *rsa.PrivateKey to the context.

func NewToken

func NewToken(ctx context.Context, cs *ClaimSet) (string, error)

NewToken returns a JWT token signed with the given RSA private key.

func NewTokenWithPrivateKey added in v6.7.0

func NewTokenWithPrivateKey(cs *ClaimSet, key *rsa.PrivateKey) (string, error)

NewTokenWithPrivateKey returns a JWT token signed with the given RSA private key.

func Principal

func Principal(token string) string

Principal The "prn" (principal) claim identifies the subject of the JWT.

func PrivateKeyFromContext

func PrivateKeyFromContext(ctx context.Context) (p *rsa.PrivateKey, ok bool)

PrivateKeyFromContext returns private key pointer from a context

func PublicKeyFromContext

func PublicKeyFromContext(ctx context.Context) (p *rsa.PublicKey, ok bool)

PublicKeyFromContext retuns public key pointer from a context if it exists.

func RsaUnaryServerInterceptor

func RsaUnaryServerInterceptor(a *rsa.PublicKey, b *rsa.PrivateKey) grpc.UnaryServerInterceptor

RsaUnaryServerInterceptor makes rsa public and private keys available in grpc context

func Scope

func Scope(token string) string

Scope space-delimited list of the permissions the application requests.

func TokenFromContext added in v6.2.0

func TokenFromContext(ctx context.Context) (p string, ok bool)

TokenFromContext retuns token from a context if it exists.

func Verify

func Verify(ctx context.Context, token string) error

Verify tests whether the provided JWT token's signature was produced by the private key associated with the supplied public key. Also verifies if Token as expired

func VerifyWithPublicKey added in v6.7.0

func VerifyWithPublicKey(token string, key *rsa.PublicKey) error

VerifyWithPublicKey tests whether the provided JWT token's signature was produced by the private key associated with the supplied public key. Also verifies if Token as expired

func WrapBearerToken added in v6.2.0

func WrapBearerToken(h router.HandlerFunc) router.HandlerFunc

WrapBearerToken adds token to the context.

func WrapBearerTokenErr added in v6.2.1

func WrapBearerTokenErr(h router.WrapErr) router.WrapErr

WrapBearerTokenErr adds token to the context.

Types

type ClaimSet

type ClaimSet struct {
	Identifier, Audience, Scope, Jti, Principal string
	Expiration                                  int64
}

Jump to

Keyboard shortcuts

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