diff --git a/src/index.ts b/src/index.ts index 3af92cd..aa3d0b4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,2 +1,2 @@ -export * as JWKSFactory from './jwks-factory'; -export * as PrivateKeysProvisioner from './private-keys-provisioner'; \ No newline at end of file +export * as JWKSFactory from "./jwks-factory"; +export * as PrivateKeysProvisioner from "./private-keys-provisioner"; diff --git a/src/jwks-factory/api/index.ts b/src/jwks-factory/api/index.ts index ba7b274..58f3732 100644 --- a/src/jwks-factory/api/index.ts +++ b/src/jwks-factory/api/index.ts @@ -1,2 +1,2 @@ -export * from './jwks-factory'; -export * from './key'; \ No newline at end of file +export * from "./jwks-factory"; +export * from "./key"; diff --git a/src/jwks-factory/api/jwks-factory.ts b/src/jwks-factory/api/jwks-factory.ts index 81d27c7..9afba7a 100644 --- a/src/jwks-factory/api/jwks-factory.ts +++ b/src/jwks-factory/api/jwks-factory.ts @@ -1,8 +1,8 @@ -import { Key } from './key'; +import { Key } from "./key"; export interface JWKSFactory { - create: () => Promise<{ - accessToken: Key, - refreshToken: Key, - }> -} \ No newline at end of file + create: () => Promise<{ + accessToken: Key; + refreshToken: Key; + }>; +} diff --git a/src/jwks-factory/api/key.ts b/src/jwks-factory/api/key.ts index 1381129..75ef4f2 100644 --- a/src/jwks-factory/api/key.ts +++ b/src/jwks-factory/api/key.ts @@ -1,8 +1,8 @@ -import { JWK } from 'jose'; +import { JWK } from "jose"; export interface Key { - keyID: string; - jwk: JWK; - publicKey: string; - privateKey: string; -} \ No newline at end of file + keyID: string; + jwk: JWK; + publicKey: string; + privateKey: string; +} diff --git a/src/jwks-factory/bindings/index.ts b/src/jwks-factory/bindings/index.ts index 4da913f..8e534b4 100644 --- a/src/jwks-factory/bindings/index.ts +++ b/src/jwks-factory/bindings/index.ts @@ -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 -} \ No newline at end of file + return jwksFactory; +}; diff --git a/src/jwks-factory/implementation/generateKey.ts b/src/jwks-factory/implementation/generateKey.ts index dfad903..3f9462a 100644 --- a/src/jwks-factory/implementation/generateKey.ts +++ b/src/jwks-factory/implementation/generateKey.ts @@ -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 }; -}; \ No newline at end of file + return { keyID, jwk, publicKey, privateKey }; +}; diff --git a/src/jwks-factory/implementation/generateKeypair.ts b/src/jwks-factory/implementation/generateKeypair.ts index bb8e9b7..3136f5f 100644 --- a/src/jwks-factory/implementation/generateKeypair.ts +++ b/src/jwks-factory/implementation/generateKeypair.ts @@ -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 }; }; diff --git a/src/jwks-factory/implementation/index.ts b/src/jwks-factory/implementation/index.ts index f72c1b7..545c388 100644 --- a/src/jwks-factory/implementation/index.ts +++ b/src/jwks-factory/implementation/index.ts @@ -1 +1 @@ -export * as jwksFactory from './jwks-factory-implementation'; \ No newline at end of file +export * as jwksFactory from "./jwks-factory-implementation"; diff --git a/src/jwks-factory/implementation/jwks-factory-implementation.ts b/src/jwks-factory/implementation/jwks-factory-implementation.ts index e2d20e5..f374921 100644 --- a/src/jwks-factory/implementation/jwks-factory-implementation.ts +++ b/src/jwks-factory/implementation/jwks-factory-implementation.ts @@ -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 }; - }, - } -} \ No newline at end of file + // 2. Generate a new key pair for the refresh token + const refreshTokenKey = await generateKey("RefreshToken"); + + return { accessToken: accessTokenKey, refreshToken: refreshTokenKey }; + }, + }; +}; diff --git a/src/jwks-factory/index.ts b/src/jwks-factory/index.ts index b36ee03..8320c4e 100644 --- a/src/jwks-factory/index.ts +++ b/src/jwks-factory/index.ts @@ -1,2 +1,2 @@ -export * as API from './api'; -export * as Bindings from './bindings'; \ No newline at end of file +export * as API from "./api"; +export * as Bindings from "./bindings";