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

Typescript JWK Key return type issues #67

Closed
HiskiRobo opened this issue Feb 12, 2020 · 5 comments
Closed

Typescript JWK Key return type issues #67

HiskiRobo opened this issue Feb 12, 2020 · 5 comments
Labels
enhancement New feature or request

Comments

@HiskiRobo
Copy link

Describe the bug
Currently all return values regarding Keys are typed under the Key interface.

This causes that no Key type specific parameters can be accessed (for example EC Key crv value) in the return values regarding keys.

*To Reproduce

// fill with EC type public / private key
const ecTypeKey = 'EC KEY';
const key = JWK.asKey(ecTypeKey);
// This will cause an error: Property 'crv' does not exist on type 'Key'.
console.log(key.crv);

Expected behaviour
Should return proper Key sub type, so parameters can be accessed

Environment:

  • jose version: [e.g. v1.22.2]
  • node version: [e.g. v13.3.0]

Additional context

  • This should be implemented to all return values regarding Key type (JWE.completeDecrypt, etc)
@HiskiRobo HiskiRobo added the bug Something isn't working label Feb 12, 2020
@panva
Copy link
Owner

panva commented Feb 12, 2020

The types can't possibly know the kty from a string or a buffer... They do know when you pass in a JWK formatted object.

const key = jose.JWK.asKey({ kty: 'EC', x: 'foo', y: 'bar', d: 'foo', crv: 'P-256' })

key.crv

Please come forth with a proposal for an improvement, otherwise i'm not seeing one other than - if you know the key type contained by the string, cast the value to e.g. JWK.ECKey, otherwise, don’t access these attributes.

const key = jose.JWK.asKey('EC KEY') as jose.JWK.ECKey

key.crv

@panva panva added enhancement New feature or request waiting for feedback The OP is asked for feedback or a proposal and removed bug Something isn't working labels Feb 12, 2020
@panva panva changed the title bug: Typescript JWK Key return type issues Typescript JWK Key return type issues Feb 12, 2020
@HiskiRobo
Copy link
Author

HiskiRobo commented Feb 13, 2020

How about creating a mixed key type for the situation when they actual key type cannot be inferred:

interface MixedKey extends Key {
    e?: string;
    n?: string;
    d?: string;
    p?: string;
    q?: string;
    dp?: string;
    dq?: string;
    qi?: string;
    crv?: ECCurve | OKPCurve;
    x?: string;
    y?: string;
    k?: string;
  }

And in asKey:

  function asKey(key: KeyObject | KeyInput, parameters?: KeyParameters): MixedKey;
  function asKey(jwk: JWKOctKey): OctKey;
  function asKey(jwk: JWKRSAKey, options?: ImportOptions): RSAKey;
  function asKey(jwk: JWKECKey): ECKey;
  function asKey(jwk: JWKOKPKey): OKPKey;

And in CompleteDecrypt

  interface completeDecrypt {
    cleartext: Buffer;
    key: JWK.MixedKey;
    cek: JWK.OctKey;
    aad?: string;
    header?: object;
    unprotected?: object;
    protected?: object;
  }

@panva
Copy link
Owner

panva commented Feb 13, 2020

Well, i can do one of two things. Your UndefinedKeyType idea doesn't sit well with me.

Easiest is to add this to JWK.Key

 [key: string]: any;

or add all of these to JWK.Key and then undefine them for the respective types.

  crv?: ECCurve | OKPCurve;
  d?: string;
  dp?: string;
  dq?: string;
  e?: string;
  k?: string;
  n?: string;
  p?: string;
  q?: string;
  qi?: string;
  x?: string;
  y?: string;

@HiskiRobo
Copy link
Author

Nice solution in the commit! This can be closed. Thanks!

@panva
Copy link
Owner

panva commented Feb 13, 2020

This branch will land with the next 13.x node release. Planned for the 18th

@panva panva removed the waiting for feedback The OP is asked for feedback or a proposal label Feb 15, 2020
@panva panva closed this as completed in b92079c Feb 18, 2020
@github-actions github-actions bot locked and limited conversation to collaborators May 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants