Skip to content

Latest commit

 

History

History
63 lines (48 loc) · 1.79 KB

README.md

File metadata and controls

63 lines (48 loc) · 1.79 KB

go-jwt JWT Compatible

Build Status Go Report Card Golang JSON Web Token builder with easy to use API for JWS and nested JWT (JWS+JWE)

It wraps and is inspired by gopkg.in/square/go-jose.v2 (especially jwt subpackage)

NOTE: Please, make sure you get fixed version of go-jose.v2. square/go-jose#142

Usage:

package main

import (
    "fmt"
    "github.com/Bplotka/go-jwt"
)

func main() {
    p := "some_payload"
    cl := jwt.Claims{
        // Your standard claims here...
    }
    
    b, err := jwt.NewDefaultBuilder() // or jwt.NewBuilder(rsaPrvKey, signAlg, keyAlg, contentAlg)
    if err != nil {
        // Handle error here...
    }
    
    token, err := b.SignedAndEncryptedJWT().
        Claims(cl).
        Payload(p).
        CompactSerialize()
    if err != nil {
        // Handle error here...
    }
    
    // Generated valid nested JWT in `token` variable!
    // (....)
    // Let's revert the process:
     
    obtainer := b.FromSignedAndEncryptedJWT(token)
    
    var fetched string
    err = obtainer.Payload(&fetched)
    if err != nil {
        // Handle error here..
    }
    
    // We have "some_payload" again in `fetched` variable.
    
    fetchedStdClaims, err := obtainer.StdClaims()
    if err != nil {
        // Handle error here..
    }
    
    // We have our standard claims again in `fetchedStdClaims` variable.
    fmt.Println(fetchedStdClaims.Issuer)
    fmt.Println(fetchedStdClaims.Subject)
    // ...
}