更新版本至2.0.3,重构JWKS相关文件路径管理,移除common.ts,新增index.ts以集中管理路径
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@kevisual/auth",
|
||||
"version": "2.0.2",
|
||||
"version": "2.0.3",
|
||||
"description": "",
|
||||
"scripts": {
|
||||
"build": "bun run bun.config.ts"
|
||||
|
||||
@@ -37,3 +37,4 @@ export const generate = async (opts: GenerateOpts = {}) => {
|
||||
publicPEM
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import path from 'node:path';
|
||||
const dir = path.join(process.cwd(), 'jwt');
|
||||
|
||||
export const JWKS_PATH = path.join(dir, 'jwks.json');
|
||||
export const PRIVATE_JWK_PATH = path.join(dir, 'privateKey.json');
|
||||
|
||||
export const PRIVATE_KEY_PATH = path.join(dir, 'privateKey.txt');
|
||||
export const PUBLIC_KEY_PATH = path.join(dir, 'publicKey.txt');
|
||||
@@ -1,21 +1,21 @@
|
||||
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import * as common from './common.ts';
|
||||
import { generate } from '../generate.ts'
|
||||
|
||||
import { getPath } from './index.ts';
|
||||
const commonDir = getPath(path.join(process.cwd(), 'jwt'));
|
||||
async function main() {
|
||||
const { jwks, privateJWK, privatePEM, publicPEM } = await generate();
|
||||
const dir = path.join(process.cwd(), 'jwt');
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir);
|
||||
}
|
||||
fs.writeFileSync(common.PUBLIC_KEY_PATH, publicPEM);
|
||||
fs.writeFileSync(commonDir.PUBLIC_KEY_PATH, publicPEM);
|
||||
|
||||
fs.writeFileSync(common.PRIVATE_KEY_PATH, privatePEM);
|
||||
fs.writeFileSync(commonDir.PRIVATE_KEY_PATH, privatePEM);
|
||||
|
||||
fs.writeFileSync(common.PRIVATE_JWK_PATH, JSON.stringify(privateJWK, null, 2));
|
||||
fs.writeFileSync(common.JWKS_PATH, JSON.stringify(jwks, null, 2));
|
||||
fs.writeFileSync(commonDir.PRIVATE_JWK_PATH, JSON.stringify(privateJWK, null, 2));
|
||||
fs.writeFileSync(commonDir.JWKS_PATH, JSON.stringify(jwks, null, 2));
|
||||
|
||||
console.log('Private key and JWKS have been saved to files.');
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import * as common from './common.ts';
|
||||
|
||||
import { getPath } from './index.ts';
|
||||
const commonDir = getPath(path.join(process.cwd(), 'jwt'));
|
||||
export const getValues = () => {
|
||||
if (!fs.existsSync(common.JWKS_PATH)) {
|
||||
if (!fs.existsSync(commonDir.JWKS_PATH)) {
|
||||
throw new Error('JWKS file does not exist. Please create it first.');
|
||||
}
|
||||
const jwksData = fs.readFileSync(common.JWKS_PATH, 'utf-8');
|
||||
const privateJWKData = fs.readFileSync(common.PRIVATE_JWK_PATH, 'utf-8');
|
||||
const publicKeyPEM = fs.readFileSync(common.PUBLIC_KEY_PATH, 'utf-8');
|
||||
const privateKeyPEM = fs.readFileSync(common.PRIVATE_KEY_PATH, 'utf-8');
|
||||
const jwksData = fs.readFileSync(commonDir.JWKS_PATH, 'utf-8');
|
||||
const privateJWKData = fs.readFileSync(commonDir.PRIVATE_JWK_PATH, 'utf-8');
|
||||
const publicKeyPEM = fs.readFileSync(commonDir.PUBLIC_KEY_PATH, 'utf-8');
|
||||
const privateKeyPEM = fs.readFileSync(commonDir.PRIVATE_KEY_PATH, 'utf-8');
|
||||
|
||||
return {
|
||||
jwks: JSON.parse(jwksData),
|
||||
|
||||
14
src/jwks/index.ts
Normal file
14
src/jwks/index.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import path from 'node:path';
|
||||
export const getPath = (dir: string = process.cwd()) => {
|
||||
const JWKS_PATH = path.join(dir, 'jwks.json');
|
||||
const PRIVATE_JWK_PATH = path.join(dir, 'privateKey.json');
|
||||
|
||||
const PRIVATE_KEY_PATH = path.join(dir, 'privateKey.txt');
|
||||
const PUBLIC_KEY_PATH = path.join(dir, 'publicKey.txt');
|
||||
return {
|
||||
JWKS_PATH,
|
||||
PRIVATE_JWK_PATH,
|
||||
PRIVATE_KEY_PATH,
|
||||
PUBLIC_KEY_PATH,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user