| 1 |
package jwt_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/golang-jwt/jwt/v4" |
| 9 |
) |
| 10 |
|
| 11 |
// Example (atypical) using the RegisteredClaims type by itself to parse a token. |
| 12 |
// The RegisteredClaims type is designed to be embedded into your custom types |
| 13 |
// to provide standard validation features. You can use it alone, but there's |
| 14 |
// no way to retrieve other fields after parsing. |
| 15 |
// See the CustomClaimsType example for intended usage. |
| 16 |
func ExampleNewWithClaims_registeredClaims() { |
| 17 |
mySigningKey := []byte("AllYourBase") |
| 18 |
|
| 19 |
// Create the Claims |
| 20 |
claims := &jwt.RegisteredClaims{ |
| 21 |
ExpiresAt: jwt.NewNumericDate(time.Unix(1516239022, 0)), |
| 22 |
Issuer: "test", |
| 23 |
} |
| 24 |
|
| 25 |
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| 26 |
ss, err := token.SignedString(mySigningKey) |
| 27 |
fmt.Printf("%v %v", ss, err) |
| 28 |
//Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.0XN_1Tpp9FszFOonIBpwha0c_SfnNI22DhTnjMshPg8 <nil> |
| 29 |
} |
| 30 |
|
| 31 |
// Example creating a token using a custom claims type. The RegisteredClaims is embedded |
| 32 |
// in the custom type to allow for easy encoding, parsing and validation of registered claims. |
| 33 |
func ExampleNewWithClaims_customClaimsType() { |
| 34 |
mySigningKey := []byte("AllYourBase") |
| 35 |
|
| 36 |
type MyCustomClaims struct { |
| 37 |
Foo string `json:"foo"` |
| 38 |
jwt.RegisteredClaims |
| 39 |
} |
| 40 |
|
| 41 |
// Create the claims |
| 42 |
claims := MyCustomClaims{ |
| 43 |
"bar", |
| 44 |
jwt.RegisteredClaims{ |
| 45 |
// A usual scenario is to set the expiration time relative to the current time |
| 46 |
ExpiresAt: jwt.NewNumericDate(time.Now().Add(24 * time.Hour)), |
| 47 |
IssuedAt: jwt.NewNumericDate(time.Now()), |
| 48 |
NotBefore: jwt.NewNumericDate(time.Now()), |
| 49 |
Issuer: "test", |
| 50 |
Subject: "somebody", |
| 51 |
ID: "1", |
| 52 |
Audience: []string{"somebody_else"}, |
| 53 |
}, |
| 54 |
} |
| 55 |
|
| 56 |
// Create claims while leaving out some of the optional fields |
| 57 |
claims = MyCustomClaims{ |
| 58 |
"bar", |
| 59 |
jwt.RegisteredClaims{ |
| 60 |
// Also fixed dates can be used for the NumericDate |
| 61 |
ExpiresAt: jwt.NewNumericDate(time.Unix(1516239022, 0)), |
| 62 |
Issuer: "test", |
| 63 |
}, |
| 64 |
} |
| 65 |
|
| 66 |
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) |
| 67 |
ss, err := token.SignedString(mySigningKey) |
| 68 |
fmt.Printf("%v %v", ss, err) |
| 69 |
|
| 70 |
//Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiZXhwIjoxNTE2MjM5MDIyfQ.xVuY2FZ_MRXMIEgVQ7J-TFtaucVFRXUzHm9LmV41goM <nil> |
| 71 |
} |
| 72 |
|
| 73 |
// Example creating a token using a custom claims type. The StandardClaim is embedded |
| 74 |
// in the custom type to allow for easy encoding, parsing and validation of standard claims. |
| 75 |
func ExampleParseWithClaims_customClaimsType() { |
| 76 |
tokenString := "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJpc3MiOiJ0ZXN0IiwiYXVkIjoic2luZ2xlIn0.QAWg1vGvnqRuCFTMcPkjZljXHh8U3L_qUjszOtQbeaA" |
| 77 |
|
| 78 |
type MyCustomClaims struct { |
| 79 |
Foo string `json:"foo"` |
| 80 |
jwt.RegisteredClaims |
| 81 |
} |
| 82 |
|
| 83 |
token, err := jwt.ParseWithClaims(tokenString, &MyCustomClaims{}, func(token *jwt.Token) (interface{}, error) { |
| 84 |
return []byte("AllYourBase"), nil |
| 85 |
}) |
| 86 |
|
| 87 |
if claims, ok := token.Claims.(*MyCustomClaims); ok && token.Valid { |
| 88 |
fmt.Printf("%v %v", claims.Foo, claims.RegisteredClaims.Issuer) |
| 89 |
} else { |
| 90 |
fmt.Println(err) |
| 91 |
} |
| 92 |
|
| 93 |
// Output: bar test |
| 94 |
} |
| 95 |
|
| 96 |
// An example of parsing the error types using [errors.Is]. |
| 97 |
func ExampleParse_errorChecking() { |
| 98 |
// Token from another example. This token is expired |
| 99 |
var tokenString = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmb28iOiJiYXIiLCJleHAiOjE1MDAwLCJpc3MiOiJ0ZXN0In0.HE7fK0xOQwFEr4WDgRWj4teRPZ6i3GLwD5YCm6Pwu_c" |
| 100 |
|
| 101 |
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { |
| 102 |
return []byte("AllYourBase"), nil |
| 103 |
}) |
| 104 |
|
| 105 |
if token.Valid { |
| 106 |
fmt.Println("You look nice today") |
| 107 |
} else if errors.Is(err, jwt.ErrTokenMalformed) { |
| 108 |
fmt.Println("That's not even a token") |
| 109 |
} else if errors.Is(err, jwt.ErrTokenUnverifiable) { |
| 110 |
fmt.Println("We could not verify this token") |
| 111 |
} else if errors.Is(err, jwt.ErrTokenSignatureInvalid) { |
| 112 |
fmt.Println("This token has an invalid signature") |
| 113 |
} else if errors.Is(err, jwt.ErrTokenExpired) || errors.Is(err, jwt.ErrTokenNotValidYet) { |
| 114 |
// Token is either expired or not active yet |
| 115 |
fmt.Println("Timing is everything") |
| 116 |
} else { |
| 117 |
fmt.Println("Couldn't handle this token:", err) |
| 118 |
} |
| 119 |
|
| 120 |
// Output: Timing is everything |
| 121 |
} |