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

JWK Implementation for import/export #61

Merged
merged 3 commits into from Mar 11, 2024
Merged

JWK Implementation for import/export #61

merged 3 commits into from Mar 11, 2024

Conversation

olegbespalov
Copy link
Contributor

@olegbespalov olegbespalov commented Feb 22, 2024

What?

This is an implementation of the JWK export/import.

I've decided to use the popular library instead of self-tailored since, for the following algorithms, the JWK could be more complicated in terms of maintaining standards.

How did I test it?

  • I did rely on the existing Web Platform tests (uncommented cases).
  • also I've checked that the generated in nodejs key could be imported by the xk6-webcrypto

Related PRs:

This PR is based on #62

Why?

JWK is part of the specification.

Closes: #37

@olegbespalov olegbespalov changed the base branch from main to chore/maintanance February 23, 2024 09:56
@olegbespalov olegbespalov marked this pull request as ready for review February 23, 2024 09:57
@olegbespalov olegbespalov requested a review from a team as a code owner February 23, 2024 09:57
@olegbespalov olegbespalov requested review from oleiade and joanlopez and removed request for a team February 23, 2024 09:57
@olegbespalov olegbespalov self-assigned this Feb 23, 2024
@olegbespalov olegbespalov changed the title Implementation JWK import/export JWK Implementation for import/export Feb 23, 2024
Base automatically changed from chore/maintanance to main February 23, 2024 13:14
Copy link
Member

@oleiade oleiade left a comment

Choose a reason for hiding this comment

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

Great job @olegbespalov 👏🏻 LGTM 🚀 🙇🏻

@oleiade
Copy link
Member

oleiade commented Feb 28, 2024

Before I forget, we should also update the README tables to match the newly introduced support for JWK in import and export operations. At least for the sake of future selves 😸

@olegbespalov
Copy link
Contributor Author

olegbespalov commented Feb 28, 2024

@oleiade, the readme tables have no JWK (formats) dimension, but you are correct that it's worth mentioning the current state. I've made a note in ea6ac87

import { crypto } from "k6/x/webcrypto";

export default async function () {
try {

Choose a reason for hiding this comment

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

Stupid question; why do this example is surrounded by a try..catch, while other (e.g. import-export-jwk-aes-key.js) isn't, if the operations are the same? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's the other way around, I think it's just copy-paste issue, I will adjust the import-export-jwk-aes-key.js to also have a try...catch, which won't harm 👍

webcrypto/aes.go Outdated
Comment on lines 205 to 208
// check the key length
var (
has128Bits = len(keyData) == 16
has192Bits = len(keyData) == 24
has256Bits = len(keyData) == 32
)

if !has128Bits && !has192Bits && !has256Bits {
return nil, NewError(DataError, fmt.Sprintf("invalid key length %v bytes", len(keyData)))
}

Choose a reason for hiding this comment

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

Nit; this can be hide behind a method, similar to isHashAlgorithm

webcrypto/jwk.go Outdated
return nil, fmt.Errorf("failed to marshal JWK key: %w", err)
}

// wrap result result into the object that is expected to be returned

Choose a reason for hiding this comment

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

Suggested change
// wrap result result into the object that is expected to be returned
// wrap result into the object that is expected to be returned

return promise
}
default:
reject(NewError(ImplementationError, "unsupported format "+format))

Choose a reason for hiding this comment

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

Suggested change
reject(NewError(ImplementationError, "unsupported format "+format))
reject(NewError(ImplementationError, "unsupported format "+format))
return promise

Shouldn't we return the promise here as well, as we do for the rest of the error cases? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we should! Great catch!


b, ok := result.([]byte)
if !ok {
reject(NewError(ImplementationError, "for "+format+" []byte expected as result"))

Choose a reason for hiding this comment

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

Similar to the comment above, should we return here right after the reject? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here!

Copy link

@joanlopez joanlopez left a comment

Choose a reason for hiding this comment

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

It looks generally good, great work! 🎉

I left a few minor comments, but almost none of them is blocking, except the ones on webcrypto/subtle_crypto.go, which I'm wondering if needed for correctness (correct behavior).

Thanks!

@olegbespalov olegbespalov merged commit 04b2c16 into main Mar 11, 2024
10 checks passed
@olegbespalov olegbespalov deleted the feat/jwk branch March 11, 2024 13:02
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.

Implement support for the JWK format
3 participants