Skip to content
This repository has been archived by the owner on Nov 17, 2024. It is now read-only.

Commit

Permalink
style: fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
El-Fitz committed Aug 22, 2024
1 parent 597e7cb commit 5f7bc4c
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 78 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * as JWKSFactory from './jwks-factory';
export * as PrivateKeysProvisioner from './private-keys-provisioner';
export * as JWKSFactory from "./jwks-factory";
export * as PrivateKeysProvisioner from "./private-keys-provisioner";
4 changes: 2 additions & 2 deletions src/jwks-factory/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './jwks-factory';
export * from './key';
export * from "./jwks-factory";
export * from "./key";
12 changes: 6 additions & 6 deletions src/jwks-factory/api/jwks-factory.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Key } from './key';
import { Key } from "./key";

export interface JWKSFactory {
create: () => Promise<{
accessToken: Key,
refreshToken: Key,
}>
}
create: () => Promise<{
accessToken: Key;
refreshToken: Key;
}>;
}
12 changes: 6 additions & 6 deletions src/jwks-factory/api/key.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { JWK } from 'jose';
import { JWK } from "jose";

export interface Key {
keyID: string;
jwk: JWK;
publicKey: string;
privateKey: string;
}
keyID: string;
jwk: JWK;
publicKey: string;
privateKey: string;
}
10 changes: 5 additions & 5 deletions src/jwks-factory/bindings/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { API } from '..';
import * as Implementation from '../implementation';
import { API } from "..";
import * as Implementation from "../implementation";

export const create = (): API.JWKSFactory => {
const jwksFactory = Implementation.jwksFactory.create();
const jwksFactory = Implementation.jwksFactory.create();

return jwksFactory
}
return jwksFactory;
};
61 changes: 34 additions & 27 deletions src/jwks-factory/implementation/generateKey.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
import * as crypto from "crypto";
import { exportJWK, importSPKI, JWK } from 'jose';
import { generateKeyPair } from './generateKeypair';
import { exportJWK, importSPKI, JWK } from "jose";
import { generateKeyPair } from "./generateKeypair";

// Generate a key pair, convert it to JWK, and return the key ID, JWK, public key, and private key
export const generateKey = async (name: string): Promise<{ keyID: string, jwk: JWK, publicKey: string, privateKey: string }> => {
// Generate an ES512 key pair
export const generateKey = async (
name: string,
): Promise<{
keyID: string;
jwk: JWK;
publicKey: string;
privateKey: string;
}> => {
// Generate an ES512 key pair

// Generate a key ID (kid) for the JWK
const generateKeyId = (keyType: 'ES512', name: string): string => {
const timestamp = Date.now().toString();
const randomComponent = crypto.randomBytes(8).toString('hex');
return `${name}-${keyType}-${timestamp}-${randomComponent}`;
};
// Generate a key ID (kid) for the JWK
const generateKeyId = (keyType: "ES512", name: string): string => {
const timestamp = Date.now().toString();
const randomComponent = crypto.randomBytes(8).toString("hex");
return `${name}-${keyType}-${timestamp}-${randomComponent}`;
};

// Convert PEM to JWK
const convertToJWK = async (publicKey: string, keyId: string) => {
const key = await importSPKI(publicKey, 'ES512', { extractable: true });
const jwkKey = await exportJWK(key);
jwkKey.kid = keyId; // Add the Key ID
jwkKey.alg = "ES512"; // Algorithm
jwkKey.use = "sig"; // Key use: signature
return jwkKey;
};
// Convert PEM to JWK
const convertToJWK = async (publicKey: string, keyId: string) => {
const key = await importSPKI(publicKey, "ES512", { extractable: true });
const jwkKey = await exportJWK(key);
jwkKey.kid = keyId; // Add the Key ID
jwkKey.alg = "ES512"; // Algorithm
jwkKey.use = "sig"; // Key use: signature
return jwkKey;
};

// 1. Generate the key pair
const { publicKey, privateKey } = generateKeyPair();
// 2. Generate the key ID
const keyID = generateKeyId('ES512', name);
// 3. Convert the public key to JWK
const jwk = await convertToJWK(publicKey, keyID);
// 1. Generate the key pair
const { publicKey, privateKey } = generateKeyPair();
// 2. Generate the key ID
const keyID = generateKeyId("ES512", name);
// 3. Convert the public key to JWK
const jwk = await convertToJWK(publicKey, keyID);

return { keyID, jwk, publicKey, privateKey };
};
return { keyID, jwk, publicKey, privateKey };
};
30 changes: 16 additions & 14 deletions src/jwks-factory/implementation/generateKeypair.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@

import * as crypto from "crypto";

export const generateKeyPair = (): { publicKey: string, privateKey: string } => {
const { publicKey, privateKey } = crypto.generateKeyPairSync("ec", {
namedCurve: "P-521",
publicKeyEncoding: {
type: "spki",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
},
});
export const generateKeyPair = (): {
publicKey: string;
privateKey: string;
} => {
const { publicKey, privateKey } = crypto.generateKeyPairSync("ec", {
namedCurve: "P-521",
publicKeyEncoding: {
type: "spki",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
},
});

return { publicKey, privateKey };
return { publicKey, privateKey };
};
2 changes: 1 addition & 1 deletion src/jwks-factory/implementation/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * as jwksFactory from './jwks-factory-implementation';
export * as jwksFactory from "./jwks-factory-implementation";
26 changes: 13 additions & 13 deletions src/jwks-factory/implementation/jwks-factory-implementation.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { API } from '..';
import { generateKey } from './generateKey';
import { API } from "..";
import { generateKey } from "./generateKey";

export const create = (): API.JWKSFactory => {
return {
create: async () => {
// 1. Generate a new key pair for the access token
const accessTokenKey = await generateKey('AccessToken');
return {
create: async () => {
// 1. Generate a new key pair for the access token
const accessTokenKey = await generateKey("AccessToken");

// 2. Generate a new key pair for the refresh token
const refreshTokenKey = await generateKey('RefreshToken');
return { accessToken: accessTokenKey, refreshToken: refreshTokenKey };
},
}
}
// 2. Generate a new key pair for the refresh token
const refreshTokenKey = await generateKey("RefreshToken");

return { accessToken: accessTokenKey, refreshToken: refreshTokenKey };
},
};
};
4 changes: 2 additions & 2 deletions src/jwks-factory/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * as API from './api';
export * as Bindings from './bindings';
export * as API from "./api";
export * as Bindings from "./bindings";

0 comments on commit 5f7bc4c

Please sign in to comment.