jwt: github.com/robbert229/jwt Index | Files | Directories

package jwt

import "github.com/robbert229/jwt"

Package jwt is a barebones JWT implementation that supports just the bare necessities.


Package Files

algorithms.go claims.go jwt.go

type Algorithm Uses

type Algorithm struct {
    // contains filtered or unexported fields

Algorithm is used to sign and validate a token.

func HmacSha256 Uses

func HmacSha256(key string) Algorithm

HmacSha256 returns the SingingMethod for HMAC with SHA256

func HmacSha384 Uses

func HmacSha384(key string) Algorithm

HmacSha384 returns the SigningMethod for HMAC with SHA384

func HmacSha512 Uses

func HmacSha512(key string) Algorithm

HmacSha512 returns the SigningMethod for HMAC with SHA512

func (*Algorithm) Decode Uses

func (a *Algorithm) Decode(encoded string) (*Claims, error)

Decode returns a map representing the token's claims. DOESN'T validate the claims though.

func (*Algorithm) DecodeAndValidate Uses

func (a *Algorithm) DecodeAndValidate(encoded string) (claims *Claims, err error)

DecodeAndValidate returns a map representing the token's claims, and it's valid.

func (*Algorithm) Encode Uses

func (a *Algorithm) Encode(payload *Claims) (string, error)

Encode returns an encoded JWT token from a header, payload, and secret

func (*Algorithm) NewHeader Uses

func (a *Algorithm) NewHeader() *Header

NewHeader returns a new Header object.

func (*Algorithm) Sign Uses

func (a *Algorithm) Sign(unsignedToken string) ([]byte, error)

Sign signs the token with the given hash, and key

func (*Algorithm) Validate Uses

func (a *Algorithm) Validate(encoded string) error

Validate verifies a tokens validity. It returns nil if it is valid, and an error if invalid.

type Claims Uses

type Claims struct {
    // contains filtered or unexported fields

Claims contains the claims of a jwt.

func NewClaim Uses

func NewClaim() *Claims

NewClaim returns a new map representing the claims with the default values. The schema is detailed below.

claim["iis"] Issuer - string - identifies principal that issued the JWT;
claim["sub"] Subject - string - identifies the subject of the JWT;
claim["aud"] Audience - string - The "aud" (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT MUST identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the aud claim when this claim is present, then the JWT MUST be rejected.
claim["exp"] Expiration time - time - The "exp" (expiration time) claim identifies the expiration time on or after which the JWT MUST NOT be accepted for processing.
claim["nbf"] Not before - time - Similarly, the not-before time claim identifies the time on which the JWT will start to be accepted for processing.
claim["iat"] Issued at - time - The "iat" (issued at) claim identifies the time at which the JWT was issued.
claim["jti"] JWT ID - string - case sensitive unique identifier of the token even among different issuers.

func (Claims) Get Uses

func (c Claims) Get(key string) (interface{}, error)

Get returns the claim in string form and returns an error if the specified claim doesn't exist.

func (*Claims) GetTime Uses

func (c *Claims) GetTime(key string) (time.Time, error)

GetTime attempts to return a claim as a time.

func (*Claims) HasClaim Uses

func (c *Claims) HasClaim(key string) bool

HasClaim returns if the claims map has the specified key.

func (*Claims) Set Uses

func (c *Claims) Set(key string, value interface{})

Set sets the claim in string form.

func (*Claims) SetTime Uses

func (c *Claims) SetTime(key string, value time.Time)

SetTime sets the claim given to the specified time.

type Header struct {
    Typ string `json:"typ"` // Token Type
    Alg string `json:"alg"` // Message Authentication Code Algorithm - The issuer can freely set an algorithm to verify the signature on the token. However, some asymmetrical algorithms pose security concerns
    Cty string `json:"cty"` // Content Type - This claim should always be JWT

Header contains important information for encrypting / decrypting



Package jwt imports 10 packages (graph) and is imported by 4 packages. Updated 2018-11-17. Refresh now. Tools for package owners.