Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JW* Decode #43

Merged
merged 7 commits into from
Feb 12, 2024
Merged

JW* Decode #43

merged 7 commits into from
Feb 12, 2024

Conversation

mistermoe
Copy link
Member

@mistermoe mistermoe commented Feb 10, 2024

Overview

Collaborative effort with @KendallWeihe to split Verify into two distinct functions Decode and Verify

Important

Most of the code changes in this PR are from moving things around. not much net new logic was added

Rationale

we've run into multiple scenarios where we need to:

  1. verify a JWT or JWS and then use a number of fields from within the header or the payload/claims
  2. decode a JWT or JWS, check some stuff within the payload or headers, and then verify

In order to access claims within a JWT the caller currently has to do this:

package main

import (
    "encoding/json"
    "fmt"

    "github.com/tbd54566975/web5-go/jwt"
)

func main() {
        signedJwt := "eyJhbGciOiJFZERTQSIsImtpZCI6ImRpZDpqd2s6ZXlKcmRIa2lPaUpQUzFBaUxDSmpjbllpT2lKRlpESTFOVEU1SWl3aWVDSTZJbFpyU3pKclRscDVVREY0WVZwUFoyZHBjREY2VTJwc1JXTnNTakZSTVhSQmMxRkJhVU5YUW1GaVgyOGlmUSMwIn0." +
    "eyJjX25vbmNlIjoiYWJjZDEyMyIsImlzcyI6ImV5SnJkSGtpT2lKUFMxQWlMQ0pqY25ZaU9pSkZaREkxTlRFNUlpd2llQ0k2SWxaclN6SnJUbHA1VURGNFlWcFBaMmRwY0RGNlUycHNSV05zU2pGUk1YUkJjMUZCYVVOWFFtRmlYMjhpZlEifQ." +
    "O434_IlnJOYbcIsHkVcOyKLxVvcfHvcg6tZL_gIU2TAY6qFiaPZ_w6upSzIl-I8JXYBytzR_8BfCq4P3che1Bg"

    parts := strings.Split(signedJwt, ".")
    
    base64UrlEncodedClaims := parts[1]

    decodedClaims, err := jwt.DecodeJWTClaims(base64UrlEncodedClaims)

    if err != nil {
        panic(err)
    }

The above code ends up making it such that callers have to know more details than they have to about jwt details. further, if they've already verified the jwt, the claims are now being decoded twice.

Changes

In order to remedy the above, this PR splits decoding and verifying into two distinct steps while still providing a single function call if desired

JWT

Approach 1

package main

import "github.com/tbd54566975/web5-go/jwt"

func main() {
    // Approach 1: decode then verify as two separate steps
    decoded, err := jwt.Decode("test")
    if err != nil {
        panic(err)
    }

   // application specific business logic

    err = decoded.Verify()
    if err != nil {
        panic(err)
    }
}

This approach requires the caller to first decode the JWT, perform whatever business logic is needed and then optionally call verify when desired

Approach 2

package main

import "github.com/tbd54566975/web5-go/jwt"

func main() {
    // Approach 2: decode & verify in 1 step and receive decoded jwt on successful verification
    decoded, err = jwt.Verify("test")
    if err != nil {
        panic(err)
    }
}

This allows to caller to call verify and then decide to perform additional logic using the claims within a JWT depending on the result of verification

JWS

the jws API surface is identical to jwt

Approach 1

package main

import "github.com/tbd54566975/web5-go/jws"

func main() {
    // Approach 1: decode then verify as two separate steps
    decoded, err := jws.Decode("test")
    if err != nil {
        panic(err)
    }

    err = decoded.Verify()
    if err != nil {
        panic(err)
    }
}

Approach 2

package main

import "github.com/tbd54566975/web5-go/jws"

func main() {
    // Approach 2: decode & verify in 1 step and receive decoded jwt on successful verification
    decoded, err = jws.Verify("test")
    if err != nil {
        panic(err)
    }
}

@mistermoe mistermoe marked this pull request as ready for review February 11, 2024 06:50
@mistermoe mistermoe changed the base branch from kendallw/jwt-decode to main February 11, 2024 06:51
Copy link
Contributor

@tomdaffurn tomdaffurn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious, what are the cases where you needs access to claims before verifying? There's some higher precedence validation to do?

@KendallWeihe
Copy link
Contributor

I'm curious, what are the cases where you needs access to claims before verifying? There's some higher precedence validation to do?

Actually I'm not sure we have run into that situation, @mistermoe am I forgetting something? I think this is a combination of things:

  1. We needed a function to decode the JWT (for additional verification)
  2. The jws.Verify() and jwt.Verify() function return values were semantically equivalent
  3. Someone someday somewhere may have a situation wherein they want to inspect the JWT/JWS constituents without running a verification

@KendallWeihe KendallWeihe merged commit 9518922 into main Feb 12, 2024
@mistermoe mistermoe linked an issue Feb 12, 2024 that may be closed by this pull request
@KendallWeihe KendallWeihe deleted the jw-decode branch February 20, 2024 13:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add jwt.Parse() Function
4 participants