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 ¶
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 SetRequestHeader ¶
func Verify ¶
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 ¶
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 ¶
Header contains information about the token type (always JWT) and the algorithm used for signing the token.
type Token ¶
Token contains fields for a JWT.
func Parse ¶
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 ¶
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