Skip to content

Commit

Permalink
Fix issues when running with bun by converting KeyObject to uint8
Browse files Browse the repository at this point in the history
  • Loading branch information
mvantellingen committed Nov 26, 2023
1 parent c84496c commit cfde937
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-walls-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/federated-token": patch
---

Fix issues when running with bun by converting KeyObject to uint8
1 change: 1 addition & 0 deletions src/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class GatewayAuthPlugin<TContext extends PublicFederatedTokenContext>
},
});
} else {
console.error(e)
throw new GraphQLError("Your token is invalid.", {
extensions: {
code: "INVALID_TOKEN",
Expand Down
15 changes: 13 additions & 2 deletions src/sign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as jose from "jose";
import { KeyObject } from "node:crypto";

type TokenSignerOptions = {
encryptKeys: KeyManagerInterface;
Expand Down Expand Up @@ -47,7 +48,8 @@ export class TokenSigner {
contentEncryptionAlgorithms: ["A256GCM"],
}
);
return JSON.parse(result.plaintext.toString());
const data = new TextDecoder().decode(result.plaintext.buffer)
return JSON.parse(data)
}

async signJWT(payload: any, exp: number) {
Expand Down Expand Up @@ -118,7 +120,7 @@ export class KeyManager implements KeyManagerInterface {
if (keys.length === 0) {
throw new ConfigurationError("Missing keys");
}
this.keys = keys;
this.keys = keys.map((k) => ({ id: k.id, key: this.convertKey(k.key) }));

Check failure on line 123 in src/sign.ts

View workflow job for this annotation

GitHub Actions / Build, and test on Node 18.x and ubuntu-latest

Type '{ id: string; key: Uint8Array | KeyLike; }[]' is not assignable to type 'Key[]'.

Check failure on line 123 in src/sign.ts

View workflow job for this annotation

GitHub Actions / Build, and test on Node 20.x and ubuntu-latest

Type '{ id: string; key: Uint8Array | KeyLike; }[]' is not assignable to type 'Key[]'.
}

// getActiveKey returns a key to sign the JWT. It always returns the first key
Expand All @@ -143,4 +145,13 @@ export class KeyManager implements KeyManagerInterface {
}
return key;
}

convertKey(
key: KeyObject | jose.KeyLike | Uint8Array
): Uint8Array | jose.KeyLike {
if (key instanceof KeyObject) {
return new Uint8Array(key.export());
}
return key;
}
}

0 comments on commit cfde937

Please sign in to comment.