jwt

package module
v0.0.0-...-da5b79c Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2015 License: MIT Imports: 7 Imported by: 45

README

JWT - Helpers for Go Circle CI GoDoc

This package implements helpers for handling HMAC SHA-256 signed JSON Web Tokens (RFC 7519) in Go.

Usage

Producing a Token
package main

import (
  "fmt"
  "log"

  "github.com/nubo/jwt"
)

func main() {
  claims := jwt.ClaimSet{
    jwt.Issuer:   "example.com",
    jwt.Audience: "example.com",
    "lorem":      "ipsum",
  }
  token, err := claims.Sign("secret")
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println(token)
}
Consuming a Token
package main

import (
  "fmt"
  "log"

  "github.com/nubo/jwt"
)

func main() {
	rawToken := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJleGFtcGxlLmNvbSIsImlzcyI6ImV4YW1wbGUuY29tIiwibG9yZW0iOiJpcHN1bSJ9.VhJwcvoGPhr_sY_YG6-rMNwU0YnpDSGw7jlArsnj8eA"

	token, ok := jwt.ParseAndVerify(rawToken, "secret")
	if !ok {
		log.Fatal("Invalid token")
	}
	fmt.Println("Type", token.Header.Type)
	fmt.Println("Algorithm", token.Header.Algorithm)
	fmt.Println("Claim Set", token.ClaimSet)
	fmt.Println("Signature", token.Signature)
}

Features

There is currently no plan to implement other signing algorithms than HMAC SHA-256.

  • sign JWT with HMAC SHA-256
  • encrypt JWT (JWE)
  • parse a raw JWT to a Go struct
  • verify a raw JWT
  • parse and verify a raw JWT to a Go struct
  • claim set

Documentation

Overview

Copyright (c) 2015 nuboLAB UG (haftungsbeschränkt) Use of this source code is governed by the MIT license that can be found in the LICENSE file.

Package jwt provides simple helpers for producing and consuming JWT (RFC 7519) that are signed with HMAC SHA-256.

Index

Examples

Constants

View Source
const (
	Issuer         = "iss"
	Subject        = "sub"
	Audience       = "aud"
	ExpirationTime = "exp"
	NotBefore      = "nbf"
	IssuedAt       = "iat"
	ID             = "jti"
)

Registered claim names are defined as constants for convenience.

Variables

This section is empty.

Functions

func GetRequestHeader

func GetRequestHeader(r *http.Request) string

func SetRequestHeader

func SetRequestHeader(r *http.Request, token string)

func Verify

func Verify(token, secret string) bool

Verify checks the validity of the token and verifies the integrity with HMAC SHA-256 and the given secret.

Example
package main

import (
	"fmt"

	"github.com/nubo/jwt"
)

func main() {
	token := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJleGFtcGxlLmNvbSJ9.6aqSC54aR7dIsuyQgUbcTM4tSkZLcdwqPXzk3OQtOXk"

	fmt.Println(jwt.Verify(token, "secret"))
}
Output:
true

Types

type ClaimSet

type ClaimSet map[string]interface{}

ClaimSet is a map for storing JWT claims.

func (ClaimSet) Sign

func (c ClaimSet) Sign(secret string) (string, error)

Sign takes a secret and signs the ClaimSet with HMAC SHA-256. It returns the base64 encoded byte sequence of the signature or an error in case of problems marshalling the ClaimSet to JSON.

Example
package main

import (
	"fmt"
	"log"

	"github.com/nubo/jwt"
)

func main() {
	claims := jwt.ClaimSet{
		jwt.Issuer: "example.com",
	}

	token, err := claims.Sign("secret")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(token)
}
Output:
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJleGFtcGxlLmNvbSJ9.6aqSC54aR7dIsuyQgUbcTM4tSkZLcdwqPXzk3OQtOXk
type Header struct {
	Type      string `json:"typ"`
	Algorithm string `json:"alg"`
}

Header contains information about the token type (always JWT) and the algorithm used for signing the token.

type Token

type Token struct {
	Header    Header
	ClaimSet  ClaimSet
	Signature string
}

Token contains fields for a JWT.

func Parse

func Parse(token string) (Token, error)

Parse parses a JWT without verifying it's signature.

Example
package main

import (
	"fmt"

	"github.com/nubo/jwt"
)

func main() {
	token := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJleGFtcGxlLmNvbSJ9.6aqSC54aR7dIsuyQgUbcTM4tSkZLcdwqPXzk3OQtOXk"
	t, err := jwt.Parse(token)

	fmt.Println(t.Header.Type, t.Header.Algorithm, err)
}
Output:
JWT HS256 <nil>

func ParseAndVerify

func ParseAndVerify(token, secret string) (Token, bool)

ParseAndVerify verifies a JWT signate and parses the token if ok.

Example
package main

import (
	"fmt"

	"github.com/nubo/jwt"
)

func main() {
	token := "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJleGFtcGxlLmNvbSJ9.6aqSC54aR7dIsuyQgUbcTM4tSkZLcdwqPXzk3OQtOXk"
	t, ok := jwt.ParseAndVerify(token, "secret")

	fmt.Println(t.Header.Type, t.Header.Algorithm, ok)
}
Output:
JWT HS256 true

func TokenFromRequest

func TokenFromRequest(r *http.Request, secret string) (Token, bool)

Jump to

Keyboard shortcuts

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