-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Allow escape hatch for those who (incorrectly) use RFC3339 in Numeric Date fields (#735) * Add the ability to parse RFC3339 dates for NumericDate types * appease linter * Tweaks (#737) * Update go version * Tweak documentation * Update Changes * Tweak doc (#738) * show jwt.Sign using raw and jwk.Key (#739) * autodoc updates (#740) Co-authored-by: lestrrat <[email protected]> * Add example for using JWT fields (#741) * fix typo (#742) * [jwe/v2] Fix possible excessive unpadding for AESCBC (#745) * Fix possible excessive unpadding for AESCBC * Update Changes * Update Changes * Update Changes * Update Changes Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: lestrrat <[email protected]> Co-authored-by: Satoru Kitaguchi <[email protected]>
- Loading branch information
1 parent
0d365e4
commit dc603b6
Showing
14 changed files
with
258 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package examples_test | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/lestrrat-go/jwx/v2/jwt" | ||
) | ||
|
||
func ExampleJWT_GetClaims() { | ||
tok, err := jwt.NewBuilder(). | ||
IssuedAt(time.Now()). | ||
Issuer(`github.com/lestrrat-go/jwx`). | ||
Subject(`example`). | ||
Claim(`claim1`, `value1`). | ||
Claim(`claim2`, `2022-05-16T07:35:56+00:00`). | ||
Build() | ||
if err != nil { | ||
fmt.Printf("failed to build token: %s\n", err) | ||
return | ||
} | ||
|
||
// Pre-defined fields have typed accessors. | ||
var _ time.Time = tok.IssuedAt() | ||
var _ string = tok.Issuer() | ||
var _ string = tok.Subject() | ||
|
||
var v interface{} | ||
var ok bool | ||
|
||
// But you can also get them via the generic `.Get()` method. | ||
// However, v is of type interface{}, so you might need to | ||
// use a type switch to properly use its value. | ||
// | ||
// For the key name you could also use jwt.IssuedAtKey constant | ||
v, ok = tok.Get(`iat`) | ||
|
||
// Private claims | ||
v, ok = tok.Get(`claim1`) | ||
v, ok = tok.Get(`claim2`) | ||
|
||
// However, it is possible to globally specify that a private | ||
// claim should be parsed into a custom type. | ||
// In the sample below `claim2` is to be an instance of time.Time | ||
jwt.RegisterCustomField(`claim2`, time.Time{}) | ||
|
||
tok = jwt.New() | ||
if err := json.Unmarshal([]byte(`{"claim2":"2022-05-16T07:35:56+00:00"}`), tok); err != nil { | ||
fmt.Printf(`failed to parse token: %s`, err) | ||
return | ||
} | ||
v, ok = tok.Get(`claim2`) | ||
if !ok { | ||
fmt.Printf(`failed to get private claim "claim2"`) | ||
return | ||
} | ||
if _, ok := v.(time.Time); !ok { | ||
fmt.Printf(`claim2 expected to be time.Time, but got %T`, v) | ||
return | ||
} | ||
|
||
_ = v | ||
_ = ok | ||
|
||
// OUTPUT: | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.