zhevron / jwt

JSON Web Token library for Google Go.

Home Page:https://godoc.org/github.com/zhevron/jwt

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON Web Tokens for Go

Coverage Status Build Status GoDoc Gitter

jwt is a simple library for handling JSON Web Tokens in Go.
The library is developed based on draft-ietf-oauth-json-web-token-32.

Installation

You can install the library using the standard go get command:

go get gopkg.in/zhevron/jwt.v1

Note: This package requires Go 1.3 or higher.

Examples

JSON Web Token (HMAC, HS256)

import (
  "fmt"

  "gopkg.in/zhevron/jwt.v1"
)

func main() {
  key := "secret"

  token := jwt.NewToken()
  token.Claims["username"] = "my_username"

  tokenstring, err := token.Sign(key)
  if err != nil {
    panic(err)
  }

  token, err = jwt.DecodeToken(tokenstring, jwt.HS256, key)
  if err != nil {
    panic(err)
  }

  fmt.Printf("Your username is: %s\n", token.Claims["username"])
}

JSON Web Token (RSA, RS256)

import (
  "fmt"

  "gopkg.in/zhevron/jwt.v1"
)

func main() {
  privateKey = `-----BEGIN RSA PRIVATE KEY-----
myprivatekeyhere
-----END RSA PRIVATE KEY-----`
  publicKey = `-----BEGIN PUBLIC KEY-----
mypublickeyhere  
-----END PUBLIC KEY-----`

  token := jwt.NewToken()
  token.Algorithm = jwt.RS256
  token.Claims["username"] = "my_username"

  tokenstring, err := token.Sign(privateKey)
  if err != nil {
    panic(err)
  }

  token, err = jwt.DecodeToken(tokenstring, jwt.RS256, publicKey)
  if err != nil {
    panic(err)
  }

  fmt.Printf("Your username is: %s\n", token.Claims["username"])
}

JSON Web Token (ECDSA, ES256)

import (
  "fmt"

  "gopkg.in/zhevron/jwt.v1"
)

func main() {
  privateKey = `-----BEGIN EC PRIVATE KEY-----
myprivatekeyhere
-----END EC PRIVATE KEY-----`
  publicKey = `-----BEGIN PUBLIC KEY-----
mypublickeyhere  
-----END PUBLIC KEY-----`

  token := jwt.NewToken()
  token.Algorithm = jwt.ES256
  token.Claims["username"] = "my_username"

  tokenstring, err := token.Sign(privateKey)
  if err != nil {
    panic(err)
  }

  token, err = jwt.DecodeToken(tokenstring, jwt.ES256, publicKey)
  if err != nil {
    panic(err)
  }

  fmt.Printf("Your username is: %s\n", token.Claims["username"])
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

About

JSON Web Token library for Google Go.

https://godoc.org/github.com/zhevron/jwt

License:Apache License 2.0


Languages

Language:Go 100.0%