jwt

package module
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2021 License: MIT Imports: 13 Imported by: 0

README

@chantzlarge/go-jwt

Go Doc

TL;DR

TL;DR should include an unordered-list of featured aspects of the README

Getting Started

Installation
go get -d github.com/chantzlarge/go-jwt

Usage

Create and serialize JWT example using HMAC using SHA-256:

key := []byte("your-secret-key")
tok := jwt.New().SetSUB("314159")
if err := tok.Sign(HS256, key); err != nil {
    panic(err)
}
byt := tok.SerializeCompact()

Create and serialize JWT example using ECDSA using P-256 and SHA-256:

key, _ := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
tok := jwt.New().SetSUB("314159")
if err := tok.Sign(ES256, key); err != nil {
    panic(err)
}
byt := tok.SerializeCompact()

Create and serialize JWT example using RSASSA-PSS using SHA-256 and MGF1 with SHA-256:

key, _ := rsa.GenerateKey(rand.Reader, 2048)
tok := jwt.New().SetSUB("314159")
if err := tok.Sign(PS256, key); err != nil {
    panic(err)
}
byt := tok.SerializeCompact()

Deserialize and verify JWT example:

// ...
tok := jwt.DeserializeCompact(byt)
if err := tok.Verify(key); err != nil {
    panic(err)
}

For detailed usage information, see https://pkg.go.dev/github.com/chantzlarge/go-jwt@v0.1.6.

Contributing

Contributing should include sections related to contributor guidelines, standards, development methodologies, and style guides.

Built-with

Author

Versioning

This project uses Semantic Versioning 2.0.0:

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,

  • MINOR version when you add functionality in a backwards compatible manner, and

  • PATCH version when you make backwards compatible bug fixes.

Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.

See CHANGELOG for a summary of released versions and their changes.

License

MIT

References

Documentation

Index

Constants

View Source
const (
	// ECDSA using P-256 and SHA-256
	ES256 = "ES256"
	// ECDSA using P-384 and SHA-384
	ES384 = "ES384"
	// ECDSA using P-521 and SHA-512
	ES512 = "ES512"
	// HMAC using SHA-256
	HS256 = "HS256"
	// HMAC using SHA-384
	HS384 = "HS384"
	// HMAC using SHA-512
	HS512 = "HS512"
	// RSASSA-PSS using SHA-256 and MGF1 with SHA-256
	PS256 = "PS256"
	// RSASSA-PSS using SHA-384 and MGF1 with SHA-384
	PS384 = "PS384"
	// RSASSA-PSS using SHA-512 and MGF1 with SHA-512
	PS512 = "PS512"
	// RSASSA-PKCS1-v15 using SHA-256
	RS256 = "RS256"
	// RSASSA-PKCS1-v15 using SHA-384
	RS384 = "RS384"
	// RSASSA-PKCS1-v15 using SHA-512
	RS512 = "RS512"
	// None
	NONE = "none"
)

Algorithms

View Source
const (
	// ISS represents the "iss" (Issuer) registered claim name. See
	// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1 for more
	// information.
	ISS = "iss"
	// SUB represents the "sub" (Subject) registered claim name. See
	// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2 for more
	// information.
	SUB = "sub"
	// AUD represents the "aud" (Audience) claim. See
	// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3 for more
	// information.
	AUD = "aud"
	// EXP represents the "exp" (Expiration Time) claim. See
	// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4 for more
	// information.
	EXP = "exp"
	// NBF represents the "nbf" (Not Before) claim. See
	// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5 for more
	// information.
	NBF = "nbf"
	// IAT represents the "iat" (Issued At) claim. See
	// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6 for more
	// information.
	IAT = "iat"
	// JTI represents the "jti" (JWT ID) claim. See
	// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7 for more
	// information.
	JTI = "jti"
)

Registered Claim Names

View Source
const (
	// Algorithm
	ALG = "alg"
	// Critical
	CRIT = "crit"
	// Content-Type
	CTY = "cty"
	// JWK URL
	JKU = "jku"
	// JWK
	JWK = "jwk"
	// ...
	KID = "kid"
	// Type
	TYP = "typ"
	// ...
	X5TS256 = "x5t#S256"
	// ...
	X5T = "x5t"
	// ...
	X5U = "x5u"
)

Registered Header Parameter Names

Variables

View Source
var (
	ErrorInvalidALG       = errors.New("invalid claim: 'ALG'")
	ErrorInvalidAUD       = errors.New("invalid claim: 'AUD'")
	ErrorInvalidEXP       = errors.New("invalid claim: 'EXP'")
	ErrorInvalidIAT       = errors.New("invalid claim: 'IAT'")
	ErrorInvalidISS       = errors.New("invalid claim: 'ISS'")
	ErrorInvalidJTI       = errors.New("invalid claim: 'JTI'")
	ErrorInvalidNBF       = errors.New("invalid claim: 'NBF'")
	ErrorInvalidSUB       = errors.New("invalid claim: 'SUB'")
	ErrorInvalidCRIT      = errors.New("invalid claim: 'CRIT'")
	ErrorInvalidCTY       = errors.New("invalid claim: 'CTY'")
	ErrorInvalidJKU       = errors.New("invalid claim: 'JKU'")
	ErrorInvalidJWK       = errors.New("invalid claim: 'JWK'")
	ErrorInvalidKID       = errors.New("invalid claim: 'KID'")
	ErrorInvalidTYP       = errors.New("invalid claim: 'TYP'")
	ErrorInvalidX5TS256   = errors.New("invalid claim: 'X5TS256'")
	ErrorInvalidX5T       = errors.New("invalid claim: 'X5T'")
	ErrorInvalidX5U       = errors.New("invalid claim: 'X5U'")
	ErrorInvalidSignature = errors.New("invalid signature")
	ErrorInvalidToken     = errors.New("invalid token")
	ErrorNotImplemented   = errors.New("not implemented")
)

Errors

Functions

This section is empty.

Types

type JWT

type JWT struct {
	Header    map[string]interface{}
	Payload   map[string]interface{}
	Signature []byte
	// contains filtered or unexported fields
}

JWT represents instance of JSON Web Token (JWT).

func Deserialize added in v0.1.5

func Deserialize(byt []byte) (*JWT, error)

Deserialize ...

func DeserializeCompact

func DeserializeCompact(byt []byte) (*JWT, error)

DeserializeCompact deserializes compact representation of JWT.

func New

func New() *JWT

New creates new instance of JWT.

func (*JWT) GetALG added in v0.1.5

func (tok *JWT) GetALG() string

GetALG returns "alg" (Algorithm) registered header parameter.

func (*JWT) GetAUD

func (tok *JWT) GetAUD() string

GetAUD returns "aud" (Audience) registered claim.

func (*JWT) GetCRIT added in v0.1.5

func (tok *JWT) GetCRIT() []string

GetCRIT ...

func (*JWT) GetCTY added in v0.1.5

func (tok *JWT) GetCTY() string

GetCTY ...

func (*JWT) GetClaim

func (tok *JWT) GetClaim(key string) interface{}

GetClaim ...

func (*JWT) GetEXP added in v0.1.5

func (tok *JWT) GetEXP() *time.Time

GetEXP ...

func (*JWT) GetHeaderParameter

func (tok *JWT) GetHeaderParameter(key string) interface{}

GetHeaderParameter ...

func (*JWT) GetIAT added in v0.1.5

func (tok *JWT) GetIAT() *time.Time

GetIAT ...

func (*JWT) GetISS added in v0.1.5

func (tok *JWT) GetISS() string

GetISS ...

func (*JWT) GetJKU added in v0.1.5

func (tok *JWT) GetJKU() string

GetJKU ...

func (*JWT) GetJTI added in v0.1.5

func (tok *JWT) GetJTI() string

GetJTI ...

func (*JWT) GetJWK added in v0.1.5

func (tok *JWT) GetJWK() string

GetJWK ...

func (*JWT) GetKID added in v0.1.5

func (tok *JWT) GetKID() string

GetKID ...

func (*JWT) GetNBF added in v0.1.5

func (tok *JWT) GetNBF() *time.Time

GetNBF ...

func (*JWT) GetSUB added in v0.1.5

func (tok *JWT) GetSUB() string

GetSUB ...

func (*JWT) GetTYP added in v0.1.5

func (tok *JWT) GetTYP() string

GetTYP ...

func (*JWT) GetX5T added in v0.1.5

func (tok *JWT) GetX5T() string

GetX5T ...

func (*JWT) GetX5TS256 added in v0.1.5

func (tok *JWT) GetX5TS256() string

GetX5TS256 ...

func (*JWT) GetX5U added in v0.1.5

func (tok *JWT) GetX5U() string

GetX5U ...

func (*JWT) Serialize added in v0.1.5

func (tok *JWT) Serialize() ([]byte, error)

Serialize ...

func (*JWT) SerializeCompact

func (tok *JWT) SerializeCompact() ([]byte, error)

SerializeCompact ...

func (*JWT) SetAUD

func (tok *JWT) SetAUD(aud string) *JWT

SetAUD ...

func (*JWT) SetCRIT added in v0.1.5

func (tok *JWT) SetCRIT(crit []string) *JWT

SetCRIT ...

func (*JWT) SetCTY added in v0.1.5

func (tok *JWT) SetCTY(cty string) *JWT

SetCTY ...

func (*JWT) SetClaim

func (tok *JWT) SetClaim(key string, val interface{}) *JWT

SetClaim ...

func (*JWT) SetEXP added in v0.1.5

func (tok *JWT) SetEXP(exp *time.Time) *JWT

SetEXP ...

func (*JWT) SetHeaderParameter added in v0.1.5

func (tok *JWT) SetHeaderParameter(key string, val interface{}) *JWT

SetHeaderParameter ...

func (*JWT) SetIAT added in v0.1.5

func (tok *JWT) SetIAT(iat *time.Time) *JWT

SetIAT ...

func (*JWT) SetISS added in v0.1.5

func (tok *JWT) SetISS(iss string) *JWT

SetISS ...

func (*JWT) SetJKU added in v0.1.5

func (tok *JWT) SetJKU(jku string) *JWT

SetJKU ...

func (*JWT) SetJTI

func (tok *JWT) SetJTI(jti string) *JWT

SetJTI ...

func (*JWT) SetJWK added in v0.1.5

func (tok *JWT) SetJWK(jwk string) *JWT

SetJWK ...

func (*JWT) SetKID added in v0.1.5

func (tok *JWT) SetKID(kid string) *JWT

SetKID ...

func (*JWT) SetNBF added in v0.1.5

func (tok *JWT) SetNBF(nbf *time.Time) *JWT

SetNBF ...

func (*JWT) SetSUB added in v0.1.5

func (tok *JWT) SetSUB(sub string) *JWT

SetSUB ...

func (*JWT) SetTYP added in v0.1.5

func (tok *JWT) SetTYP(typ string) *JWT

SetTYP ...

func (*JWT) SetX5T added in v0.1.5

func (tok *JWT) SetX5T(x5t string) *JWT

SetX5T ...

func (*JWT) SetX5TS256 added in v0.1.5

func (tok *JWT) SetX5TS256(x5fs256 string) *JWT

SetX5TS256 ...

func (*JWT) SetX5U added in v0.1.5

func (tok *JWT) SetX5U(x5u string) *JWT

SetX5U ...

func (*JWT) Sign

func (tok *JWT) Sign(alg string, key interface{}) error

Sign ...

func (*JWT) Verify added in v0.1.5

func (tok *JWT) Verify(key interface{}) error

Verify ...

func (*JWT) VerifyALG added in v0.1.5

func (tok *JWT) VerifyALG() error

VerifyALG ...

func (*JWT) VerifyAUD added in v0.1.5

func (tok *JWT) VerifyAUD() error

VerifyAUD ...

func (*JWT) VerifyCRIT added in v0.1.5

func (tok *JWT) VerifyCRIT() error

VerifyCRIT ...

func (*JWT) VerifyCTY added in v0.1.5

func (tok *JWT) VerifyCTY() error

VerifyCTY ...

func (*JWT) VerifyEXP added in v0.1.5

func (tok *JWT) VerifyEXP() error

VerifyEXP ...

func (*JWT) VerifyIAT added in v0.1.5

func (tok *JWT) VerifyIAT() error

VerifyIAT ...

func (*JWT) VerifyISS added in v0.1.5

func (tok *JWT) VerifyISS() error

VerifyISS ...

func (*JWT) VerifyJKU added in v0.1.5

func (tok *JWT) VerifyJKU() error

VerifyJKU ...

func (*JWT) VerifyJTI added in v0.1.5

func (tok *JWT) VerifyJTI() error

VerifyJTI ...

func (*JWT) VerifyJWK added in v0.1.5

func (tok *JWT) VerifyJWK() error

VerifyJWK ...

func (*JWT) VerifyKID added in v0.1.5

func (tok *JWT) VerifyKID() error

VerifyKID ...

func (*JWT) VerifyNBF added in v0.1.5

func (tok *JWT) VerifyNBF() error

VerifyNBF ...

func (*JWT) VerifySUB added in v0.1.5

func (tok *JWT) VerifySUB() error

VerifySUB ...

func (*JWT) VerifyTYP added in v0.1.5

func (tok *JWT) VerifyTYP() error

VerifyTYP ...

func (*JWT) VerifyX5T added in v0.1.5

func (tok *JWT) VerifyX5T() error

VerifyX5T ...

func (*JWT) VerifyX5TS256 added in v0.1.5

func (tok *JWT) VerifyX5TS256() error

VerifyX5TS256 ...

func (*JWT) VerifyX5U added in v0.1.5

func (tok *JWT) VerifyX5U() error

VerifyX5U ...

Jump to

Keyboard shortcuts

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