Skip to content

Typescript JWK Key return type issues #67

Description

@HiskiRobo

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)

Activity

  1. panva commented on Feb 12, 2020

    @panva
    Owner

    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
  2. added
    enhancementNew feature or request
    waiting for feedbackThe OP is asked for feedback or a proposal
    and removed
    bugSomething isn't working
    on Feb 12, 2020
  3. changed the title [-]bug: Typescript JWK Key return type issues[/-] [+]Typescript JWK Key return type issues[/+] on Feb 12, 2020
  4. HiskiRobo commented on Feb 13, 2020

    @HiskiRobo
    Author

    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;
      }
    
  5. panva commented on Feb 13, 2020

    @panva
    Owner

    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;
  6. added a commit that references this issue on Feb 13, 2020
    b92079c
  7. HiskiRobo commented on Feb 13, 2020

    @HiskiRobo
    Author

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

  8. panva commented on Feb 13, 2020

    @panva
    Owner

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

  9. locked and limited conversation to collaborators on May 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions