jwsgo

package module
v0.0.0-...-814193f Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2017 License: MIT Imports: 11 Imported by: 0

README

jsonrpc

Build Status Coverage Status License GoDoc

Installation

go get github.com/mushroomsir/jwsgo

Feature

  • HMAC signatures with HS256, HS384 and HS512.
  • Support custom algorithm for encrypt data.
  • Support custom Header
  • Easy to understand and use.

API

Basic usage

NewSha256 create the jws instance with HMAC-SHA256.

// or NewSha512, NewSha384
jws:=jwsgo.NewSha256("Secret-key")

// create the payload
payload := &Payload{
    Iss: "http://google.com/",
    Exp: 3610,
    Iat: 10,
}

// You can also add some extra fileds
payload.Set("userid", "123456")

// encode this payload and get token
token,err := jws.Encode(payload)

// decode token
playload,err := jws.Decode(token)

Custom Header

you can even make own Header with custom value.

header := &Header{
    Algorithm: "HS1",
}
header.Set("id", "mushroom")
token, err := jws.EncodeWith(header, payload)
Custom algorithm

use custom hash func for encrypt data.

hasher := func(data string) []byte {
    h := hmac.New(sha1.New, []byte("xx"))
    h.Write([]byte(data))
    return h.Sum(nil)
}
jws := New("HS256",hasher)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Header struct {
	Algorithm string `json:"alg"`
	// Represents the token type.
	Typ string `json:"typ"`
	// The optional hint of which key is being used.
	KeyID string `json:"kid,omitempty"`
	// contains filtered or unexported fields
}

Header is header of jwt

func (*Header) Encode

func (h *Header) Encode() (string, error)

Encode the current header of jwt

func (*Header) Get

func (h *Header) Get(key string) interface{}

Get ...

func (*Header) Set

func (h *Header) Set(key string, value interface{})

Set ...

type JWS

type JWS struct {
	// contains filtered or unexported fields
}

JWS provides a golang implementation of JSON Web Signature encoding and decoding. See RFC 7515.

func New

func New(alg string, hash hasher) *JWS

New returns the jws instance with custome sign method

func NewSha256

func NewSha256(key string) *JWS

NewSha256 returns the jws instance with HMAC-SHA256

func NewSha384

func NewSha384(key string) *JWS

NewSha384 returns the jws instance with HMAC-SHA256

func NewSha512

func NewSha512(key string) *JWS

NewSha512 returns the jws instance with HMAC-SHA256

func (*JWS) Decode

func (jws *JWS) Decode(token string) (payload *Payload, err error)

Decode ...

func (*JWS) Encode

func (jws *JWS) Encode(payload *Payload) (token string, err error)

Encode return token by the payload

func (*JWS) EncodeWith

func (jws *JWS) EncodeWith(header *Header, payload *Payload) (token string, err error)

EncodeWith return token by the jws header and payload

func (*JWS) Verify

func (jws *JWS) Verify(token string) (err error)

Verify Returns true or false for whether a signature matches a secret or key.

type Payload

type Payload struct {
	Iss   string `json:"iss"`             // email address of the client_id of the application making the access token request
	Scope string `json:"scope,omitempty"` // space-delimited list of the permissions the application requests
	Aud   string `json:"aud,omitempty"`   // descriptor of the intended target of the assertion (Optional).
	Exp   int64  `json:"exp"`             // the expiration time of the assertion (seconds since Unix epoch)
	Iat   int64  `json:"iat"`             // the time the assertion was issued (seconds since Unix epoch)
	Typ   string `json:"typ,omitempty"`   // token type (Optional).
	// Email for which the application is requesting delegated access (Optional).
	Sub string `json:"sub,omitempty"`
	// contains filtered or unexported fields
}

Payload contains information about the JWT signature including the permissions being requested (scopes), the target of the token, the issuer, the time the token was issued, and the lifetime of the token.

func (*Payload) Encode

func (c *Payload) Encode() (string, error)

Encode the current payload of jwt

func (*Payload) Get

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

Get ...

func (*Payload) Set

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

Set ...

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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