Documentation ¶
Overview ¶
Package jwt is a JWT signer and parser, with support for HMAC, RSA and ECDSA signing methods.
Index ¶
Examples ¶
Constants ¶
const ( // Separator is the character between // a JWT's encoded parts. Separator = "." )
Variables ¶
This section is empty.
Functions ¶
func Sign ¶
Sign generates a new JWT token and returns it encoded.
Standard claims are set according to the opts variable, while public (and also private) claims are set according to the pub variable.
Example ¶
package main import ( "fmt" "time" "github.com/gbrlsnchs/jwt" "github.com/gbrlsnchs/jwt/jwtcrypto/hmacsha" ) func main() { claims := &jwt.Claims{ Standard: &jwt.StdClaims{ ExpirationTime: time.Now().Add(24 * time.Hour).Unix(), }, Public: jwt.Payload{ "admin": true, }, } token, err := jwt.Sign(hmacsha.New512("foobar_sounds_safe"), &jwt.JWT{Claims: claims}) if err != nil { // ... } fmt.Println(token) }
Output:
Types ¶
type Claims ¶
Claims is the standard JWT claims set.
func (*Claims) MarshalJSON ¶
MarshalJSON arranges standard and public claims in a single map.
func (*Claims) UnmarshalJSON ¶
UnmarshalJSON splits claims inside of a map into a standard claims struct and a public claims one.
type Header ¶
type Header struct { Algorithm jwtcrypto.SigningMethod `json:"alg,omitempty"` Type string `json:"typ,omitempty"` KeyID string `json:"kid,omitempty"` }
Header is a JOSE header adapted for JWT usage.
type JWT ¶
JWT represents a JSON Web Token.
Its claims and header properties are narrowed for JWT usage.
func Parse ¶
Parse parses a JWT encoded token and decodes it into a JWT pointer.
It only validates the signature.
Example ¶
package main import ( "fmt" "net/http" "net/http/httptest" "github.com/gbrlsnchs/jwt" "github.com/gbrlsnchs/jwt/jwtcrypto/hmacsha" "github.com/gbrlsnchs/jwt/jwtutil" ) func main() { r := httptest.NewRequest(http.MethodGet, "/", nil) s := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.t-IDcSemACt8x4iTMCda8Yhe3iZaWbvV5XKSTbuAn0M" r.Header.Set("Authorization", fmt.Sprintf("Bearer %s", s)) token, err := jwtutil.BearerToken(r) if err != nil { // Token could not be extracted. } jot, err := jwt.Parse(token, hmacsha.New256("foobar_sounds_safe")) if err != nil { // Unable to parse, can be due to malformed token. } fmt.Printf("%#v\n", jot) }
Output:
type Payload ¶
type Payload map[string]interface{}
Payload is standard, public and private claims which a JWT contains in its Payload.