jwt

package
v0.0.0-...-38ffd7d Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package jwt implement JSON Web Tokens.

Example (Claims)

This example shows how to init claims

package main

import (
	"encoding/json"
	"fmt"

	"github.com/mapleque/cell/jwt"
)

// MyClaims extend jwt.StdClaims
// which implement jwt.Claims interface
type MyClaims struct {
	jwt.StdClaims
	OtherField string
}

// This example shows how to init claims
func main() {
	// make a algorithm
	key := "a secret key"
	alg := jwt.HS256(key)

	// normal jwt with default value
	j := jwt.New(alg)

	// set claims
	j.Claims.SetSubject("sub")
	j.Claims.SetAudience("aud")
	// ...
	if ret, err := json.Marshal(j); err == nil {
		fmt.Printf("claims is %s\n", string(ret))
	}

	// diy claims
	c := &MyClaims{}
	c.SetSubject("sub")
	c.OtherField = "value"
	j.Claims = c
	if ret, err := json.Marshal(j); err == nil {
		fmt.Printf("my claims is %s\n", string(ret))
	}

}
Output:

claims is {"Header":{"typ":"JWT","alg":"HS256"},"Claims":{"sub":"sub","aud":"aud"}}
my claims is {"Header":{"typ":"JWT","alg":"HS256"},"Claims":{"sub":"sub","OtherField":"value"}}
Example (Jwt)

This example shows how to use JWT

package main

import (
	"encoding/json"
	"fmt"

	"github.com/mapleque/cell/jwt"
)

func main() {
	// make a algorithm
	key := "a secret key"
	alg := jwt.HS256(key)

	// normal jwt with default value
	j := jwt.New(alg)

	// set claims
	j.Claims.SetSubject("sub")

	// sign
	token, err := j.Sign()
	if err == nil {
		fmt.Printf("sign token is %s\n", token)
	} else {
		fmt.Printf("sign error %v", err)
	}

	// verify
	if err := j.Verify(token); err == nil {
		fmt.Printf("verify pass\n")
	} else {
		fmt.Printf("verify error %v", err)
	}

	if err := j.Parse(token); err == nil {
		ret, _ := json.Marshal(j)
		fmt.Printf("claims is %v\n", string(ret))
	} else {
		fmt.Printf("parse error %v", err)
	}
}
Output:

sign token is eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzdWIifQ.7FKVJPwdyL3lZ_BP3CBC1P-Ghoq7MRNAphUkYUXyUMU
verify pass
claims is {"Header":{"typ":"JWT","alg":"HS256"},"Claims":{"sub":"sub"}}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func RSAPrivateKeyFromString

func RSAPrivateKeyFromString(str string) (*rsa.PrivateKey, error)

RSAPrivateKeyFromString build a rsa.PrivateKey from string

func RSAPublicKeyFromString

func RSAPublicKeyFromString(str string) (*rsa.PublicKey, error)

RSAPublicKeyFromString build a rsa.PublicKey from string.

Types

type Alg

type Alg interface {
	// Name return the alogrithm's name
	Name() string
	// Sign build data signature with alogrithm defined
	Sign(data []byte) ([]byte, error)
	// Verify check data signature
	Verify(data, sign []byte) error
}

Alg is algorithm interface

func ES256

func ES256(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) Alg

ES256 is an crypto algorithm using ECDSA and SHA-256

func ES384

func ES384(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) Alg

ES384 is an crypto algorithm using ECDSA and SHA-384

func ES512

func ES512(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) Alg

ES512 is an crypto algorithm using ECDSA and SHA-512

func HS256

func HS256(key string) Alg

HS256 is an crypto alogorithm using HMAC and SHA256

func HS384

func HS384(key string) Alg

HS384 is an crypto alogorithm using HMAC and SHA384

func HS512

func HS512(key string) Alg

HS512 is an crypto alogorithm using HMAC and SHA512

func RS256

func RS256(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) Alg

RS256 is an crypto algorithm using RSA and SHA-256

func RS384

func RS384(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) Alg

RS384 is an crypto algorithm using RSA and SHA-384

func RS512

func RS512(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) Alg

RS512 is an crypto algorithm using RSA and SHA-512

type Claims

type Claims interface {
	Valid() error
	SetIssuer(iss string)
	SetSubject(sub string)
	SetAudience(aud string)
	SetExpirationTime(exp int64)
	SetNotBefore(nbf int64)
	SetIssuedAt(iat int64)
	SetJWTID(jti string)
	GetIssuer() string
	GetSubject() string
	GetAudience() string
	GetExpirationTime() int64
	GetNotBefore() int64
	GetIssuedAt() int64
	GetJWTID() string
}

Claims is an interface of claims

func NewStdClaims

func NewStdClaims() Claims

NewStdClaims returns a StdClaims entity with default value

type Header struct {
	Type  string `json:"typ,omitempty"`
	Alg   string `json:"alg,omitempty"`
	KeyID string `json:"kid,omitempty"`
}

Header is jwt's header

func NewHeader

func NewHeader(alg Alg) *Header

NewHeader can build a Header entity pointer with algorithm

type JWT

type JWT struct {
	Header *Header
	Claims Claims
	// contains filtered or unexported fields
}

JWT is a Json Web Token object.

func New

func New(alg Alg) *JWT

New build an JWT entity with default value:

alg:    alg
Header: NewHeader(alg)
Claims: NewStdClaims()

func (*JWT) Parse

func (jwt *JWT) Parse(token string) error

Parse read token and decode to JWT entity.

The JWT entity should init header and claims for data witch to bind.

Invalid token returns on error.

func (*JWT) Sign

func (jwt *JWT) Sign() (string, error)

Sign for encode a token from claims by alg.

func (*JWT) Verify

func (jwt *JWT) Verify(token string) error

Verify check the token signature.

type RsaKeySet

type RsaKeySet struct {
	Kty string `json:"kty"`
	Kid string `json:"kid"`
	E   string `json:"e"`
	N   string `json:"n"`
	Use string `json:"use"`
	Alg string `json:"alg"`
	// contains filtered or unexported fields
}

RsaKeySet RsaKeySet is a key set for storage.

func NewRsaKeySet

func NewRsaKeySet(kid, publicKey, privateKey, alg string) (*RsaKeySet, error)

NewRsaKeySet NewRsaKeySet load the string key and create a RsaKeySet.

func (*RsaKeySet) Pair

func (s *RsaKeySet) Pair() (*rsa.PrivateKey, *rsa.PublicKey)

Pair Pair return the private key and public key pair.

type StdClaims

type StdClaims struct {
	Issuer         string `json:"iss,omitempty"`
	Subject        string `json:"sub,omitempty"`
	Audience       string `json:"aud,omitempty"`
	ExpirationTime int64  `json:"exp,omitempty"`
	NotBefore      int64  `json:"nbf,omitempty"`
	IssuedAt       int64  `json:"iat,omitempty"`
	JWTID          string `json:"jti,omitempty"`
}

StdClaims implement Claims interface which include all standard properties.

Extend to add more public value if you need.

func (*StdClaims) GetAudience

func (c *StdClaims) GetAudience() string

GetAudience GetAudience get audience.

func (*StdClaims) GetExpirationTime

func (c *StdClaims) GetExpirationTime() int64

GetExpirationTime GetExpirationTime get expiration time

func (*StdClaims) GetIssuedAt

func (c *StdClaims) GetIssuedAt() int64

GetIssuedAt GetIssuedAt get issued at

func (*StdClaims) GetIssuer

func (c *StdClaims) GetIssuer() string

GetIssuer GetIssuer get issuer.

func (*StdClaims) GetJWTID

func (c *StdClaims) GetJWTID() string

GetJWTID GetJWTID get jwtid

func (*StdClaims) GetNotBefore

func (c *StdClaims) GetNotBefore() int64

GetNotBefore GetNotBefore get not before

func (*StdClaims) GetSubject

func (c *StdClaims) GetSubject() string

GetSubject GetSubject get subject.

func (*StdClaims) SetAudience

func (c *StdClaims) SetAudience(aud string)

SetAudience SetAudience set audience = aud.

func (*StdClaims) SetExpirationTime

func (c *StdClaims) SetExpirationTime(exp int64)

SetExpirationTime SetExpirationTime set expireation time = exp.

func (*StdClaims) SetIssuedAt

func (c *StdClaims) SetIssuedAt(iat int64)

SetIssuedAt SetIssuedAt set issued at = iat.

func (*StdClaims) SetIssuer

func (c *StdClaims) SetIssuer(iss string)

SetIssuer SetIssuer set issuer = iss.

func (*StdClaims) SetJWTID

func (c *StdClaims) SetJWTID(jti string)

SetJWTID SetJWTID set jwtid = jti.

func (*StdClaims) SetNotBefore

func (c *StdClaims) SetNotBefore(nbf int64)

SetNotBefore SetNotBefore set not before = nbf.

func (*StdClaims) SetSubject

func (c *StdClaims) SetSubject(sub string)

SetSubject SetSubject set subject = sub.

func (*StdClaims) Valid

func (c *StdClaims) Valid() error

Valid Valid check the claims is valid.

TODO implement valid method

Jump to

Keyboard shortcuts

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