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 ¶
- func RSAPrivateKeyFromString(str string) (*rsa.PrivateKey, error)
- func RSAPublicKeyFromString(str string) (*rsa.PublicKey, error)
- type Alg
- func ES256(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) Alg
- func ES384(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) Alg
- func ES512(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey) Alg
- func HS256(key string) Alg
- func HS384(key string) Alg
- func HS512(key string) Alg
- func RS256(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) Alg
- func RS384(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) Alg
- func RS512(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) Alg
- type Claims
- type Header
- type JWT
- type RsaKeySet
- type StdClaims
- func (c *StdClaims) GetAudience() string
- func (c *StdClaims) GetExpirationTime() int64
- func (c *StdClaims) GetIssuedAt() int64
- func (c *StdClaims) GetIssuer() string
- func (c *StdClaims) GetJWTID() string
- func (c *StdClaims) GetNotBefore() int64
- func (c *StdClaims) GetSubject() string
- func (c *StdClaims) SetAudience(aud string)
- func (c *StdClaims) SetExpirationTime(exp int64)
- func (c *StdClaims) SetIssuedAt(iat int64)
- func (c *StdClaims) SetIssuer(iss string)
- func (c *StdClaims) SetJWTID(jti string)
- func (c *StdClaims) SetNotBefore(nbf int64)
- func (c *StdClaims) SetSubject(sub string)
- func (c *StdClaims) Valid() error
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
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 RS256 ¶
func RS256(privateKey *rsa.PrivateKey, publicKey *rsa.PublicKey) Alg
RS256 is an crypto algorithm using RSA and SHA-256
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 ¶
type Header struct { Type string `json:"typ,omitempty"` Alg string `json:"alg,omitempty"` KeyID string `json:"kid,omitempty"` }
Header is jwt's header
type JWT ¶
JWT is a Json Web Token object.
func New ¶
New build an JWT entity with default value:
alg: alg Header: NewHeader(alg) Claims: NewStdClaims()
func (*JWT) Parse ¶
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.
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 ¶
NewRsaKeySet NewRsaKeySet load the string key and create a RsaKeySet.
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 ¶
GetAudience GetAudience get audience.
func (*StdClaims) GetExpirationTime ¶
GetExpirationTime GetExpirationTime get expiration time
func (*StdClaims) GetIssuedAt ¶
GetIssuedAt GetIssuedAt get issued at
func (*StdClaims) GetNotBefore ¶
GetNotBefore GetNotBefore get not before
func (*StdClaims) GetSubject ¶
GetSubject GetSubject get subject.
func (*StdClaims) SetAudience ¶
SetAudience SetAudience set audience = aud.
func (*StdClaims) SetExpirationTime ¶
SetExpirationTime SetExpirationTime set expireation time = exp.
func (*StdClaims) SetIssuedAt ¶
SetIssuedAt SetIssuedAt set issued at = iat.
func (*StdClaims) SetNotBefore ¶
SetNotBefore SetNotBefore set not before = nbf.
func (*StdClaims) SetSubject ¶
SetSubject SetSubject set subject = sub.