Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: lestrrat-go/jwx
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.13
Choose a base ref
...
head repository: lestrrat-go/jwx
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v2.0.14
Choose a head ref

Commits on Jun 14, 2023

  1. Merge pull request from GHSA-rm8v-mxj3-5rmq

    ### Summary
    
    Decrypting AES-CBC encrypted JWE has Potential Padding Oracle Attack Vulnerability.
    
    ### Details
    
    On [v2.0.10](https://github.com/lestrrat-go/jwx/releases/tag/v2.0.10), decrypting AES-CBC encrypted JWE may return an error "failed to generate plaintext from decrypted blocks: invalid padding":
    
    https://github.com/lestrrat-go/jwx/blob/8840ffd4afc5839f591ff0e9ba9034af52b1643e/jwe/internal/aescbc/aescbc.go#L210-L213
    
    ```go
    	plaintext, err := unpad(buf, c.blockCipher.BlockSize())
    	if err != nil {
    		return nil, fmt.Errorf(`failed to generate plaintext from decrypted blocks: %w`, err)
    	}
    ```
    
    Reporting padding error causes [Padding Oracle Attack](https://en.wikipedia.org/wiki/Padding_oracle_attack) Vulnerability.
    RFC 7516 JSON Web Encryption (JWE) says that we MUST NOT do this.
    
    > 11.5.  Timing Attacks
    > To mitigate the attacks described in RFC 3218 [RFC3218], the
    > recipient MUST NOT distinguish between format, padding, and length
    > errors of encrypted keys.  It is strongly recommended, in the event
    > of receiving an improperly formatted key, that the recipient
    > substitute a randomly generated CEK and proceed to the next step, to
    > mitigate timing attacks.
    
    In addition, the time to remove padding depends on the length of the padding.
    It may leak the length of the padding by Timing Attacks.
    
    https://github.com/lestrrat-go/jwx/blob/796b2a9101cf7e7cb66455e4d97f3c158ee10904/jwe/internal/aescbc/aescbc.go#L33-L66
    
    ```go
    func unpad(buf []byte, n int) ([]byte, error) {
    	lbuf := len(buf)
    	rem := lbuf % n
    
    	// First, `buf` must be a multiple of `n`
    	if rem != 0 {
    		return nil, fmt.Errorf("input buffer must be multiple of block size %d", n)
    	}
    
    	// Find the last byte, which is the encoded padding
    	// i.e. 0x1 == 1 byte worth of padding
    	last := buf[lbuf-1]
    
    	// This is the number of padding bytes that we expect
    	expected := int(last)
    
    	if expected == 0 || /* we _have_ to have padding here. therefore, 0x0 is not an option */
    		expected > n || /* we also must make sure that we don't go over the block size (n) */
    		expected > lbuf /* finally, it can't be more than the buffer itself. unlikely, but could happen */ {
    		return nil, fmt.Errorf(`invalid padding byte at the end of buffer`)
    	}
    
    	// start i = 1 because we have already established that expected == int(last) where
    	// last = buf[lbuf-1].
    	//
    	// we also don't check against lbuf-i in range, because we have established expected <= lbuf
    	for i := 1; i < expected; i++ {
    		if buf[lbuf-i] != last {
    			return nil, fmt.Errorf(`invalid padding`)
    		}
    	}
    
    	return buf[:lbuf-expected], nil
    }
    ```
    
    To mitigate Timing Attacks, it MUST be done in constant time.
    
    ### Impact
    
    The authentication tag is verified, so it is not an immediate attack.
    
    Co-authored-by: ICHINOSE Shogo <shogo82148@gmail.com>
    lestrrat and shogo82148 authored Jun 14, 2023
    Copy the full SHA
    c8b6bec View commit details
  2. Update Changes

    lestrrat committed Jun 14, 2023
    Copy the full SHA
    a86a658 View commit details

Commits on Jun 20, 2023

  1. Bump golang.org/x/crypto from 0.9.0 to 0.10.0 (#938)

    * Bump golang.org/x/crypto from 0.9.0 to 0.10.0
    
    Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.9.0 to 0.10.0.
    - [Commits](golang/crypto@v0.9.0...v0.10.0)
    
    ---
    updated-dependencies:
    - dependency-name: golang.org/x/crypto
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    * run gazelle-update-repos
    
    ---------
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: Daisuke Maki <lestrrat+github@gmail.com>
    dependabot[bot] and lestrrat authored Jun 20, 2023
    Copy the full SHA
    5db9579 View commit details
  2. Bump github.com/lestrrat-go/jwx/v2 from 2.0.8 to 2.0.11 in /cmd/jwx (#…

    …942)
    
    Bumps [github.com/lestrrat-go/jwx/v2](https://github.com/lestrrat-go/jwx) from 2.0.8 to 2.0.11.
    - [Release notes](https://github.com/lestrrat-go/jwx/releases)
    - [Changelog](https://github.com/lestrrat-go/jwx/blob/develop/v2/Changes)
    - [Commits](v2.0.8...v2.0.11)
    
    ---
    updated-dependencies:
    - dependency-name: github.com/lestrrat-go/jwx/v2
      dependency-type: direct:production
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Jun 20, 2023
    Copy the full SHA
    886c4a0 View commit details
  3. Bump github.com/lestrrat-go/jwx/v2 from 2.0.8 to 2.0.11 in /examples (#…

    …943)
    
    Bumps [github.com/lestrrat-go/jwx/v2](https://github.com/lestrrat-go/jwx) from 2.0.8 to 2.0.11.
    - [Release notes](https://github.com/lestrrat-go/jwx/releases)
    - [Changelog](https://github.com/lestrrat-go/jwx/blob/develop/v2/Changes)
    - [Commits](v2.0.8...v2.0.11)
    
    ---
    updated-dependencies:
    - dependency-name: github.com/lestrrat-go/jwx/v2
      dependency-type: direct:production
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Jun 20, 2023
    Copy the full SHA
    0283140 View commit details
  4. Bump github.com/lestrrat-go/jwx/v2 in /bench/performance (#944)

    Bumps [github.com/lestrrat-go/jwx/v2](https://github.com/lestrrat-go/jwx) from 2.0.8 to 2.0.11.
    - [Release notes](https://github.com/lestrrat-go/jwx/releases)
    - [Changelog](https://github.com/lestrrat-go/jwx/blob/develop/v2/Changes)
    - [Commits](v2.0.8...v2.0.11)
    
    ---
    updated-dependencies:
    - dependency-name: github.com/lestrrat-go/jwx/v2
      dependency-type: direct:production
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Jun 20, 2023
    Copy the full SHA
    2fa2a3b View commit details

Commits on Jun 21, 2023

  1. remove unnecessarily err checks (#948)

    godoc says hash.Hash.Write never returns an error: https://pkg.go.dev/hash#Hash
    
    > Write (via the embedded io.Writer interface) adds more data to the running hash.
    > It never returns an error.
    
    So, we don't need to check errors of Write in Hmac.ComputeAuthTag.
    shogo82148 authored Jun 21, 2023
    Copy the full SHA
    82f7d5c View commit details

Commits on Jun 22, 2023

  1. Adam korcz fix 1 (#949)

    * fix panic from empty seed
    
    Signed-off-by: AdamKorcz <adam@adalogics.com>
    
    * Add test case
    
    ---------
    
    Signed-off-by: AdamKorcz <adam@adalogics.com>
    Co-authored-by: AdamKorcz <adam@adalogics.com>
    lestrrat and AdamKorcz authored Jun 22, 2023
    Copy the full SHA
    246dde8 View commit details

Commits on Jun 30, 2023

  1. add size check (#950)

    Signed-off-by: AdamKorcz <adam@adalogics.com>
    AdamKorcz authored Jun 30, 2023
    Copy the full SHA
    8149455 View commit details

Commits on Jul 3, 2023

  1. Add test case to #952 (#953)

    * Do not ignore custom encrypt and sign options in jwt package
    
    Fixes #951
    
    * Add test case
    
    * Update Changes
    
    ---------
    
    Co-authored-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
    lestrrat and ItalyPaleAle authored Jul 3, 2023
    Copy the full SHA
    2d138a3 View commit details

Commits on Jul 7, 2023

  1. Bump golang.org/x/crypto from 0.10.0 to 0.11.0 (#956)

    * Bump golang.org/x/crypto from 0.10.0 to 0.11.0
    
    Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.10.0 to 0.11.0.
    - [Commits](golang/crypto@v0.10.0...v0.11.0)
    
    ---
    updated-dependencies:
    - dependency-name: golang.org/x/crypto
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    * Update bazel repos
    
    ---------
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: Daisuke Maki <lestrrat+github@gmail.com>
    dependabot[bot] and lestrrat authored Jul 7, 2023
    Copy the full SHA
    976a058 View commit details

Commits on Aug 8, 2023

  1. Bump golang.org/x/crypto from 0.11.0 to 0.12.0 (#963)

    * Bump golang.org/x/crypto from 0.11.0 to 0.12.0
    
    Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.11.0 to 0.12.0.
    - [Commits](golang/crypto@v0.11.0...v0.12.0)
    
    ---
    updated-dependencies:
    - dependency-name: golang.org/x/crypto
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    * Run gazelle-update-repos
    
    ---------
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: Daisuke Maki <lestrrat+github@gmail.com>
    dependabot[bot] and lestrrat authored Aug 8, 2023
    Copy the full SHA
    907b093 View commit details

Commits on Aug 10, 2023

  1. Add documentation that explains #959 (#964)

    * Add documentation that explains #959
    
    * run genoptions
    lestrrat authored Aug 10, 2023
    Copy the full SHA
    006a93c View commit details
  2. Update Changes for v2.0.12

    lestrrat committed Aug 10, 2023
    Copy the full SHA
    d882071 View commit details
  3. Copy the full SHA
    bc71a61 View commit details

Commits on Aug 14, 2023

  1. Add example for using raw JWT (#967)

    * Add example for using raw JWT
    
    * typo
    lestrrat authored Aug 14, 2023
    Copy the full SHA
    f592f32 View commit details
  2. autodoc updates (#968)

    Co-authored-by: lestrrat <lestrrat@users.noreply.github.com>
    github-actions[bot] and lestrrat authored Aug 14, 2023
    Copy the full SHA
    883af1b View commit details

Commits on Sep 1, 2023

  1. Copy the full SHA
    a536a34 View commit details
  2. autodoc updates (#972)

    Co-authored-by: lestrrat <lestrrat@users.noreply.github.com>
    github-actions[bot] and lestrrat authored Sep 1, 2023
    Copy the full SHA
    7037a55 View commit details

Commits on Sep 7, 2023

  1. Bump actions/checkout from 3 to 4 (#974)

    Bumps [actions/checkout](https://github.com/actions/checkout) from 3 to 4.
    - [Release notes](https://github.com/actions/checkout/releases)
    - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md)
    - [Commits](actions/checkout@v3...v4)
    
    ---
    updated-dependencies:
    - dependency-name: actions/checkout
      dependency-type: direct:production
      update-type: version-update:semver-major
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    dependabot[bot] authored Sep 7, 2023
    Copy the full SHA
    8f8a15d View commit details

Commits on Sep 8, 2023

  1. Update HWK option documentation (#979)

    * Update option documentation
    
    * reinstance CacheOption
    lestrrat authored Sep 8, 2023
    Copy the full SHA
    d45d8eb View commit details
  2. Copy the full SHA
    0afb828 View commit details

Commits on Sep 11, 2023

  1. Bump golang.org/x/crypto from 0.12.0 to 0.13.0 (#976)

    * Bump golang.org/x/crypto from 0.12.0 to 0.13.0
    
    Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.12.0 to 0.13.0.
    - [Commits](golang/crypto@v0.12.0...v0.13.0)
    
    ---
    updated-dependencies:
    - dependency-name: golang.org/x/crypto
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    * Run gazelle-update-repos & make tidy
    
    ---------
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: Daisuke Maki <lestrrat+github@gmail.com>
    dependabot[bot] and lestrrat authored Sep 11, 2023
    Copy the full SHA
    f589fb8 View commit details

Commits on Sep 24, 2023

  1. Bump github.com/lestrrat-go/blackmagic from 1.0.1 to 1.0.2 (#983)

    * Bump github.com/lestrrat-go/blackmagic from 1.0.1 to 1.0.2
    
    Bumps [github.com/lestrrat-go/blackmagic](https://github.com/lestrrat-go/blackmagic) from 1.0.1 to 1.0.2.
    - [Commits](lestrrat-go/blackmagic@v1.0.1...v1.0.2)
    
    ---
    updated-dependencies:
    - dependency-name: github.com/lestrrat-go/blackmagic
      dependency-type: direct:production
      update-type: version-update:semver-patch
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    * Run gazelle-update-repos
    
    * Run make tidy
    
    ---------
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: Daisuke Maki <lestrrat+github@gmail.com>
    dependabot[bot] and lestrrat authored Sep 24, 2023
    Copy the full SHA
    2cce330 View commit details

Commits on Sep 25, 2023

  1. adapt to change in notation

    lestrrat committed Sep 25, 2023
    Copy the full SHA
    c41f2a8 View commit details

Commits on Sep 26, 2023

  1. Copy the full SHA
    c68c06c View commit details
  2. Update Changes

    lestrrat committed Sep 26, 2023
    Copy the full SHA
    d0e9b47 View commit details
  3. Copy the full SHA
    de6bd31 View commit details

Commits on Oct 14, 2023

  1. add jwk.IsPrivate to check if an asymmetric key is public or private (#…

    …994)
    
    * update jwk codegen for new unexported asymmetricKey extension interface
    
    * run the updated jwk codegen
    
    * add jwk asym key type constraint option to options.yaml
    
    * go run tools/cmd/genoptions/main.go -objects jwk/options.yaml
    
    * add WithPrivate validation logic to jwk.ParseKey
    
    * Revert "add WithPrivate validation logic to jwk.ParseKey"
    
    This reverts commit a921b2a.
    
    * Revert "go run tools/cmd/genoptions/main.go -objects jwk/options.yaml"
    
    This reverts commit 52a6af0.
    
    * Revert "add jwk asym key type constraint option to options.yaml"
    
    This reverts commit e7e20a1.
    
    * add IsPrivate function
    
    * export jwk.AsymmetricKey interface & move unit tests out of jwk_internal_test.go
    
    * rename local variable per code review suggestion
    
    * rename IsPrivate to IsPrivateKey
    
    ---------
    
    Co-authored-by: Shang Ding <$USER@epic.com>
    sding3 and Shang Ding authored Oct 14, 2023
    Copy the full SHA
    7aa6fec View commit details

Commits on Oct 16, 2023

  1. Bump golang.org/x/crypto from 0.13.0 to 0.14.0 (#993)

    * Bump golang.org/x/crypto from 0.13.0 to 0.14.0
    
    Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.13.0 to 0.14.0.
    - [Commits](golang/crypto@v0.13.0...v0.14.0)
    
    ---
    updated-dependencies:
    - dependency-name: golang.org/x/crypto
      dependency-type: direct:production
      update-type: version-update:semver-minor
    ...
    
    Signed-off-by: dependabot[bot] <support@github.com>
    
    * Run make tidy / gazelle-update-repos
    
    ---------
    
    Signed-off-by: dependabot[bot] <support@github.com>
    Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
    Co-authored-by: Daisuke Maki <lestrrat+github@gmail.com>
    dependabot[bot] and lestrrat authored Oct 16, 2023
    Copy the full SHA
    051a1c6 View commit details
  2. Fix typo from #994

    lestrrat committed Oct 16, 2023
    Copy the full SHA
    2c061ee View commit details

Commits on Oct 17, 2023

  1. Tweak 994 (#995)

    * Move location where AsymmetricKey is defined
    
    * Tweak docs
    
    * Update Changes
    lestrrat authored Oct 17, 2023
    Copy the full SHA
    2503747 View commit details
  2. Update Changes

    lestrrat committed Oct 17, 2023
    Copy the full SHA
    42d47a7 View commit details
2 changes: 1 addition & 1 deletion .github/workflows/autodoc.yml
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ jobs:
if: github.event.pull_request.merged == true
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Process markdown files
run: |
find . -name '*.md' | xargs perl tools/autodoc.pl
2 changes: 1 addition & 1 deletion .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ jobs:
name: "Test [ Go ${{ matrix.go }} / JSON Backend ${{ matrix.json_backend }} ]"
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Cache Go modules
uses: actions/cache@v3
with:
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -15,7 +15,7 @@ jobs:
name: "Test [ Go ${{ matrix.go }} / Tags ${{ matrix.go_tags }} ]"
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Cache Go modules
uses: actions/cache@v3
with:
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -40,7 +40,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
2 changes: 1 addition & 1 deletion .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: |
make tidy
- run: |
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -5,14 +5,14 @@ jobs:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 1.19
check-latest: true
- uses: golangci/golangci-lint-action@v3
with:
version: v1.49.0
version: v1.54.2
- name: Run go vet
run: |
go vet ./...
2 changes: 1 addition & 1 deletion .github/workflows/smoke.yml
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ jobs:
name: "Smoke [ Go ${{ matrix.go }} / Tags ${{ matrix.go_tags }} ]"
steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Check documentation generator
run: |
find . -name '*.md' | xargs env AUTODOC_DRYRUN=1 perl tools/autodoc.pl
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@ linters:
enable-all: true
disable:
- cyclop
- deadcode # deprecated
- depguard
- dupl
- exhaustive
- exhaustivestruct
@@ -44,9 +46,11 @@ linters:
- nosnakecase
- paralleltest
- scopelint # deprecated
- structcheck # deprecated
- tagliatelle
- testpackage
- thelper # Tests are fine
- varcheck # deprecated
- varnamelen # Short names are ok
- wrapcheck
- wsl
22 changes: 22 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
@@ -4,6 +4,28 @@ Changes
v2 has many incompatibilities with v1. To see the full list of differences between
v1 and v2, please read the Changes-v2.md file (https://github.com/lestrrat-go/jwx/blob/develop/v2/Changes-v2.md)

v2.0.14 17 Oct 2023
[New Features]
* [jwk] jwk.IsPrivateKey(), as well as jwk.AsymmetricKey has been added.
The function can be used to tell if a jwk.Key is a private key of an
asymmetric key pair.
[Security]
* golang.org/x/crypto has been updated to 0.14.0. The update contains a fix for HTTP/2
rapid reset DoS vulnerability, which some security scanning softwares may flag.
However, do note that this library is NOT affected by the issue, as it does not have
the capability to serve as an HTTP/2 server. This is included in this release
document so that users will be able to tell why this library may be flagged
when/if their scanning software do so.

v2.0.13 26 Sep 2023
[New Features]
* [jwk] jwk.Equal has been added. Please note that this is equivalent to
comparing the keys' thumbprints, therefore it does NOT take in consideration
non-essential fields.

[Miscellaneous]
* Various documentation fixes and additions.

v2.0.12 - 11 Aug 2023
[Bug fixes]
* [jwt] jwt.Serializer was ignoring JWE flags (#951)
20 changes: 10 additions & 10 deletions deps.bzl
Original file line number Diff line number Diff line change
@@ -33,8 +33,8 @@ def go_dependencies():
name = "com_github_lestrrat_go_blackmagic",
build_file_proto_mode = "disable_global",
importpath = "github.com/lestrrat-go/blackmagic",
sum = "h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=",
version = "v1.0.1",
sum = "h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=",
version = "v1.0.2",
)
go_repository(
name = "com_github_lestrrat_go_httpcc",
@@ -122,8 +122,8 @@ def go_dependencies():
name = "org_golang_x_crypto",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/crypto",
sum = "h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=",
version = "v0.12.0",
sum = "h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=",
version = "v0.14.0",
)
go_repository(
name = "org_golang_x_mod",
@@ -152,23 +152,23 @@ def go_dependencies():
name = "org_golang_x_sys",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/sys",
sum = "h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=",
version = "v0.11.0",
sum = "h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=",
version = "v0.13.0",
)
go_repository(
name = "org_golang_x_term",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/term",
sum = "h1:F9tnn/DA/Im8nCwm+fX+1/eBwi4qFjRT++MhtVC4ZX0=",
version = "v0.11.0",
sum = "h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=",
version = "v0.13.0",
)

go_repository(
name = "org_golang_x_text",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/text",
sum = "h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=",
version = "v0.12.0",
sum = "h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=",
version = "v0.13.0",
)
go_repository(
name = "org_golang_x_tools",
61 changes: 61 additions & 0 deletions docs/01-jwt.md
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ In this document we describe how to work with JWT using `github.com/lestrrat-go/
* [Serialize using JWE and JWS](#serialize-using-jwe-and-jws)
* [Serialize the `aud` field as a string](#serialize-aud-field-as-a-string)
* [Working with JWT](#working-with-jwt)
* [Performance](#performance)
* [Access JWS headers](#access-jws-headers)
* [Get/Set fields](#getset-fields)

@@ -1157,6 +1158,66 @@ source: [examples/jwt_flatten_audience_example_test.go](https://github.com/lestr

# Working with JWT

## Performance

github.com/lestrrat-go/jwx is focused on usability / stable API. If you are worried about performance while processing JWTs, the best path is just to use a plain struct after handling JWS yourself:

<!-- INCLUDE(examples/jwt_raw_struct_example_test.go) -->
```go
package examples

import (
"encoding/json"
"fmt"
"os"

"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jws"
"github.com/lestrrat-go/jwx/v2/jwt"
)

func ExampleJWTPlainStruct() {
t1, err := jwt.NewBuilder().
Issuer("https://github.com/lestrrat-go/jwx/v2/examples").
Subject("raw_struct").
Claim("private", "foobar").
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to build JWT: %s\n", err)
}

key := []byte("secret")
signed, err := jwt.Sign(t1, jwt.WithKey(jwa.HS256, key))
if err != nil {
fmt.Printf("failed to sign JWT: %s\n", err)
}

rawJWT, err := jws.Verify(signed, jws.WithKey(jwa.HS256, key))
if err != nil {
fmt.Printf("failed to verify JWS: %s\n", err)
}

type MyToken struct {
Issuer string `json:"iss"`
Subject string `json:"sub"`
Private string `json:"private"`
}

var t2 MyToken
if err := json.Unmarshal(rawJWT, &t2); err != nil {
fmt.Printf("failed to unmarshal JWT: %s\n", err)
}

fmt.Printf("%s\n", t2.Private)
// OUTPUT:
// foobar
}
```
source: [examples/jwt_raw_struct_example_test.go](https://github.com/lestrrat-go/jwx/blob/v2/examples/jwt_raw_struct_example_test.go)
<!-- END INCLUDE -->

This makes sure that you do not go through any extra layers of abstraction that causes performance panalties, and you get exactly the type of field that you want.

## Access JWS headers

The RFC defines JWS as an envelope to JWT (JWS can carry any payload, you just happened to assign a JWT to it). A JWT is just a bag of arbitrary key/value pairs, where some of them are predefined for validation. This means that JWS headers are NOT part of a JWT -- and thus you will not be able to access them through the `jwt.Token` itself.
2 changes: 2 additions & 0 deletions examples/go.mod
Original file line number Diff line number Diff line change
@@ -8,3 +8,5 @@ require (
)

replace github.com/cloudflare/circl v1.0.0 => github.com/cloudflare/circl v1.0.1-0.20210104183656-96a0695de3c3

replace github.com/lestrrat-go/jwx/v2 v2.0.11 => ../
15 changes: 8 additions & 7 deletions examples/go.sum
Original file line number Diff line number Diff line change
@@ -9,16 +9,14 @@ github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etly
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/lestrrat-go/blackmagic v1.0.1 h1:lS5Zts+5HIC/8og6cGHb0uCcNCa3OUt1ygh3Qz2Fe80=
github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
github.com/lestrrat-go/blackmagic v1.0.2 h1:Cg2gVSc9h7sz9NOByczrbUvLopQmXrfFx//N+AkAr5k=
github.com/lestrrat-go/blackmagic v1.0.2/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
github.com/lestrrat-go/httpcc v1.0.1 h1:ydWCStUeJLkpYyjLDHihupbn2tYmZ7m22BGkcvZZrIE=
github.com/lestrrat-go/httpcc v1.0.1/go.mod h1:qiltp3Mt56+55GPVCbTdM9MlqhvzyuL6W/NMDA8vA5E=
github.com/lestrrat-go/httprc v1.0.4 h1:bAZymwoZQb+Oq8MEbyipag7iSq6YIga8Wj6GOiJGdI8=
github.com/lestrrat-go/httprc v1.0.4/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
github.com/lestrrat-go/jwx/v2 v2.0.11 h1:ViHMnaMeaO0qV16RZWBHM7GTrAnX2aFLVKofc7FuKLQ=
github.com/lestrrat-go/jwx/v2 v2.0.11/go.mod h1:ZtPtMFlrfDrH2Y0iwfa3dRFn8VzwBrB+cyrm3IBWdDg=
github.com/lestrrat-go/option v1.0.0/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
@@ -38,8 +36,8 @@ github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5t
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.3.1-0.20221117191849-2c476679df9a/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.9.0/go.mod h1:yrmDGqONDYtNj3tH8X9dzUun2m2lzPa9ngI6/RUPGR0=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
@@ -60,19 +58,22 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
53 changes: 53 additions & 0 deletions examples/jwk_comparison_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package examples

import (
"crypto/rand"
"crypto/rsa"
"fmt"

"github.com/lestrrat-go/jwx/v2/jwk"
)

func ExampleJWK_Comparison() {
genKey := func() (jwk.Key, error) {
raw, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, fmt.Errorf("failed to generate new RSA private key: %s", err)
}

key, err := jwk.FromRaw(raw)
if err != nil {
return nil, fmt.Errorf("failed to create RSA key: %s", err)
}
if _, ok := key.(jwk.RSAPrivateKey); !ok {
return nil, fmt.Errorf("expected jwk.SymmetricKey, got %T", key)
}

return key, nil
}

k1, err := genKey()
if err != nil {
fmt.Printf("failed to generate key 1: %T", err)
return
}
k2, err := genKey()
if err != nil {
fmt.Printf("failed to generate key 2: %T", err)
return
}

// This comparison only compares Thumbprints of each key. It does NOT take into
// account fields that could differ even when thumbprints match. For example,
// it is totally possible to have a key with the same thumbprint, but different
// Key IDs, or key usages.
if jwk.Equal(k1, k2) {
fmt.Printf("k1 and k2 should be different")
return
}

if !jwk.Equal(k1, k1) {
fmt.Printf("k1 and k1 should be equal")
return
}
}
2 changes: 1 addition & 1 deletion examples/jwk_example_test.go
Original file line number Diff line number Diff line change
@@ -27,7 +27,7 @@ func ExampleJWK_Usage() {
log.Printf("%s", jsonbuf)
}

for it := set.Iterate(context.Background()); it.Next(context.Background()); {
for it := set.Keys(context.Background()); it.Next(context.Background()); {
pair := it.Pair()
key := pair.Value.(jwk.Key)

Loading