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

Refactor request token to be a JWT #125

Merged
merged 15 commits into from
Jan 18, 2024
Merged

Refactor request token to be a JWT #125

merged 15 commits into from
Jan 18, 2024

Conversation

jiyoontbd
Copy link
Contributor

@jiyoontbd jiyoontbd commented Jan 8, 2024

closes #121

Copy link

codecov bot commented Jan 8, 2024

Codecov Report

Merging #125 (0de27d2) into main (fe212cb) will decrease coverage by 1.71%.
The diff coverage is 68.00%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #125      +/-   ##
==========================================
- Coverage   78.39%   76.68%   -1.71%     
==========================================
  Files          30       32       +2     
  Lines         671      725      +54     
  Branches       65       71       +6     
==========================================
+ Hits          526      556      +30     
- Misses        111      129      +18     
- Partials       34       40       +6     
Components Coverage Δ
protocol 85.53% <ø> (ø)
httpclient 77.40% <72.41%> (-5.34%) ⬇️

val jwt: SignedJWT
try {
jwt = SignedJWT.parse(token)
// todo: resolving header.kid against a didresolver
Copy link
Contributor Author

Choose a reason for hiding this comment

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

put these todos in a new issue: #128

@jiyoontbd jiyoontbd linked an issue Jan 11, 2024 that may be closed by this pull request
3 tasks
Copy link
Member

Choose a reason for hiding this comment

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

@jiyoontbd thoughts on doing the same as this here?

@@ -28,6 +28,7 @@ dependencies {
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.8")
implementation("com.fasterxml.jackson.core:jackson-databind:2.15.2")
implementation("decentralized-identity:did-common-java:1.9.0") // would like to grab this via web5 dids
implementation("com.github.f4b6a3:uuid-creator:5.3.3")
Copy link
Member

Choose a reason for hiding this comment

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

can we use import java.util.UUID or naw?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i believe java uuid does not natively support uuidv7 - but we can resort to just regular java uuid for now and punt on whether we want to require uuidv7 for all token generation. i will remove this dep and use java uuid for now

* @param pfiDid DID of the PFI
* @return DID of the requester/JWT token issuer
*/
fun verifyRequestToken(token: String, pfiDid: String): String {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should use this in http-server in protected endpoints.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would separate out the request token related methods into a RequestTokenUtils file

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

httpclient/src/main/kotlin/tbdex/sdk/httpclient/Utils.kt Outdated Show resolved Hide resolved
}
}

if (expirationTime.before(Date.from(Instant.now()))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also convert to require if we invert, i.e

require(Instant.now().isBefore(expirationTime.toInstant()))

val expirationTime = claimsSet.expirationTime

val requiredKeys = listOf("aud", "iss", "exp", "jti", "iat")
requiredKeys.forEach { key ->
Copy link
Contributor

Choose a reason for hiding this comment

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

Since this is checking every key, we can use all. i.e

  require(requiredKeys.all { key ->
    claimsSet.claims.containsKey(key)
  }) {
    throw RequestTokenMissingClaimsException("Missing required claims.")
  }

or similar. If we want to log out which specific claim is missing, can keep as forEach and require inside the loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh yea. in js, we do log out specifically which claim is missing. i adjusted the exception message to log which one's missing.

val token = generateRequestToken(did, pfiDid)
val claimsSet = SignedJWT.parse(token).jwtClaimsSet

val requiredKeys = listOf("aud", "iss", "exp", "jti", "iat")
Copy link
Contributor

Choose a reason for hiding this comment

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

this list is duplicated between test and impl, should extract out to a const in the utils file

Comment on lines 31 to 33
requiredKeys.forEach {
assertTrue(claimsSet.claims.containsKey(it))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Using assertk, can do:

assertThat(claimsSet.claims.keys).containsExactlyInAnyOrder(requiredKeys)

jiyoontbd and others added 4 commits January 16, 2024 15:40
…rations.kt

Co-authored-by: phoebe-lew <plew@squareup.com>
Co-authored-by: phoebe-lew <plew@squareup.com>
…to httpserver getexchange handler with a todo
// verifying JWT token: https://github.com/TBD54566975/tbdex/issues/210

val token = arr[1]
// TODO: how to access pfiDid here?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@phoebe-lew so i want to actually call RequestToken.verify() in this protected endpoint, but not sure how to access pfiDid from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

punting it to a separate issue #135

Copy link
Member

@mistermoe mistermoe left a comment

Choose a reason for hiding this comment

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

woooo

@jiyoontbd jiyoontbd linked an issue Jan 17, 2024 that may be closed by this pull request
@jiyoontbd jiyoontbd merged commit 6a83857 into main Jan 18, 2024
1 of 5 checks passed
@jiyoontbd jiyoontbd deleted the 121-request-token-jwt branch January 18, 2024 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants