jwt

package
v0.0.0-...-3bec3de Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2016 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package jwt implements JSON Web Tokens as described in https://tools.ietf.org/html/rfc7519

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrInvalidValue = errors.New("invalid value for key")

ErrInvalidValue is returned when an invalid type is passed to a known claim (i.e. those defined in EssentialClaims

Functions

This section is empty.

Types

type ClaimSet

type ClaimSet struct {
	*EssentialClaims `json:"-"`
	PrivateClaims    map[string]interface{} `json:"-"`
}

ClaimSet holds an arbitrary claim set

Example
c := NewClaimSet()
c.Set("sub", "123456789")
c.Set("aud", "foo")
c.Set("https://github.com/lestrrat", "me")

buf, err := json.MarshalIndent(c, "", "  ")
if err != nil {
	log.Printf("failed to generate JSON: %s", err)
	return
}

log.Printf("%s", buf)
log.Printf("sub     -> '%s'", c.Get("sub").(string))
log.Printf("aud     -> '%v'", c.Get("aud").([]string))
log.Printf("private -> '%s'", c.Get("https://github.com/lestrrat").(string))
Output:

func NewClaimSet

func NewClaimSet() *ClaimSet

NewClaimSet creates a new ClaimSet

func (*ClaimSet) Get

func (c *ClaimSet) Get(key string) interface{}

Get retuns the value registered in this ClaimSet with the matching key name

func (*ClaimSet) MarshalJSON

func (c *ClaimSet) MarshalJSON() ([]byte, error)

MarshalJSON generates JSON representation of this claim set

func (*ClaimSet) Set

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

Set takes a key and a value, and sets the appropriate values in the `ClaimSet` for you. If the key is a known ("Essential") claim, it is set in `c.EssentialClaim` struct, which means that some amoutn of type safety is asserted. Otherwise it is assumed to be a private claim as is.

Set returns an error if a known essential claim name is used and its type does not match with the type given in `value`. If you want to rely on compile time check for types, you should be assigning values directly to the struct.

func (*ClaimSet) UnmarshalJSON

func (c *ClaimSet) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON representation and initializes this ClaimSet

type EssentialClaims

type EssentialClaims struct {
	Audience   []string     `json:"aud,omitempty"` // https://tools.ietf.org/html/rfc7519#section-4.1.3
	Expiration int64        `json:"exp,omitempty"` // https://tools.ietf.org/html/rfc7519#section-4.1.4
	IssuedAt   int64        `json:"iat,omitempty"` // https://tools.ietf.org/html/rfc7519#section-4.1.6
	Issuer     string       `json:"iss,omitempty"` // https://tools.ietf.org/html/rfc7519#section-4.1.1
	JwtID      string       `json:"jti,omitempty"` // https://tools.ietf.org/html/rfc7519#section-4.1.7
	NotBefore  *NumericDate `json:"nbf,omitempty"` // https://tools.ietf.org/html/rfc7519#section-4.1.5
	Subject    string       `json:"sub,omitempty"` // https://tools.ietf.org/html/rfc7519#section-4.1.2
}

EssentialClaims contains the set of known set of claims in JWT spec.

func (*EssentialClaims) Construct

func (c *EssentialClaims) Construct(m map[string]interface{}) error

Construct takes a map and initializes the essential claims with its values

type NumericDate

type NumericDate struct {
	time.Time
}

NumericDate represents the date format used in the 'nbf' claim

func (NumericDate) MarshalJSON

func (n NumericDate) MarshalJSON() ([]byte, error)

MarshalJSON generates JSON representation of this instant

func (*NumericDate) UnmarshalJSON

func (n *NumericDate) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the JSON representation and initializes this NumericDate

Jump to

Keyboard shortcuts

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