@keycardai/mcp 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +5 -17
- package/README.md +15 -3
- package/dist/cjs/server/auth/credentials.d.ts +7 -52
- package/dist/cjs/server/auth/credentials.d.ts.map +1 -1
- package/dist/cjs/server/auth/credentials.js +6 -184
- package/dist/cjs/server/auth/credentials.js.map +1 -1
- package/dist/cjs/server/auth/errors.d.ts +1 -7
- package/dist/cjs/server/auth/errors.d.ts.map +1 -1
- package/dist/cjs/server/auth/errors.js +2 -14
- package/dist/cjs/server/auth/errors.js.map +1 -1
- package/dist/cjs/server/auth/middleware/bearerAuth.d.ts +16 -1
- package/dist/cjs/server/auth/middleware/bearerAuth.d.ts.map +1 -1
- package/dist/cjs/server/auth/middleware/bearerAuth.js +15 -3
- package/dist/cjs/server/auth/middleware/bearerAuth.js.map +1 -1
- package/dist/cjs/server/auth/privateKey.d.ts +2 -46
- package/dist/cjs/server/auth/privateKey.d.ts.map +1 -1
- package/dist/cjs/server/auth/privateKey.js +3 -231
- package/dist/cjs/server/auth/privateKey.js.map +1 -1
- package/dist/cjs/server/auth/provider.d.ts +3 -29
- package/dist/cjs/server/auth/provider.d.ts.map +1 -1
- package/dist/cjs/server/auth/provider.js +11 -86
- package/dist/cjs/server/auth/provider.js.map +1 -1
- package/dist/cjs/server/auth/router.d.ts +1 -0
- package/dist/cjs/server/auth/router.d.ts.map +1 -1
- package/dist/cjs/server/auth/router.js +3 -0
- package/dist/cjs/server/auth/router.js.map +1 -1
- package/dist/cjs/server/auth/verifiers/jwt.d.ts +2 -1
- package/dist/cjs/server/auth/verifiers/jwt.d.ts.map +1 -1
- package/dist/cjs/server/auth/verifiers/jwt.js +26 -5
- package/dist/cjs/server/auth/verifiers/jwt.js.map +1 -1
- package/dist/esm/server/auth/credentials.d.ts +7 -52
- package/dist/esm/server/auth/credentials.d.ts.map +1 -1
- package/dist/esm/server/auth/credentials.js +3 -148
- package/dist/esm/server/auth/credentials.js.map +1 -1
- package/dist/esm/server/auth/errors.d.ts +1 -7
- package/dist/esm/server/auth/errors.d.ts.map +1 -1
- package/dist/esm/server/auth/errors.js +1 -13
- package/dist/esm/server/auth/errors.js.map +1 -1
- package/dist/esm/server/auth/middleware/bearerAuth.d.ts +16 -1
- package/dist/esm/server/auth/middleware/bearerAuth.d.ts.map +1 -1
- package/dist/esm/server/auth/middleware/bearerAuth.js +15 -3
- package/dist/esm/server/auth/middleware/bearerAuth.js.map +1 -1
- package/dist/esm/server/auth/privateKey.d.ts +2 -46
- package/dist/esm/server/auth/privateKey.d.ts.map +1 -1
- package/dist/esm/server/auth/privateKey.js +1 -196
- package/dist/esm/server/auth/privateKey.js.map +1 -1
- package/dist/esm/server/auth/provider.d.ts +3 -29
- package/dist/esm/server/auth/provider.d.ts.map +1 -1
- package/dist/esm/server/auth/provider.js +9 -84
- package/dist/esm/server/auth/provider.js.map +1 -1
- package/dist/esm/server/auth/router.d.ts +1 -0
- package/dist/esm/server/auth/router.d.ts.map +1 -1
- package/dist/esm/server/auth/router.js +1 -0
- package/dist/esm/server/auth/router.js.map +1 -1
- package/dist/esm/server/auth/verifiers/jwt.d.ts +2 -1
- package/dist/esm/server/auth/verifiers/jwt.d.ts.map +1 -1
- package/dist/esm/server/auth/verifiers/jwt.js +26 -5
- package/dist/esm/server/auth/verifiers/jwt.js.map +1 -1
- package/package.json +4 -3
|
@@ -1,53 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
prepareTokenExchangeRequest(subjectToken: string, resource: string, options?: {
|
|
9
|
-
tokenEndpoint?: string;
|
|
10
|
-
authInfo?: Record<string, string>;
|
|
11
|
-
}): Promise<TokenExchangeRequest>;
|
|
12
|
-
}
|
|
13
|
-
export declare class ClientSecret implements ApplicationCredential {
|
|
14
|
-
#private;
|
|
15
|
-
constructor(clientId: string, clientSecret: string);
|
|
16
|
-
getAuth(): {
|
|
17
|
-
clientId: string;
|
|
18
|
-
clientSecret: string;
|
|
19
|
-
};
|
|
20
|
-
prepareTokenExchangeRequest(subjectToken: string, resource: string): Promise<TokenExchangeRequest>;
|
|
21
|
-
}
|
|
22
|
-
export interface WebIdentityOptions {
|
|
23
|
-
serverName?: string;
|
|
24
|
-
storage?: PrivateKeyStorage;
|
|
25
|
-
storageDir?: string;
|
|
26
|
-
keyId?: string;
|
|
27
|
-
audienceConfig?: string | Record<string, string>;
|
|
28
|
-
}
|
|
29
|
-
export declare class WebIdentity implements ApplicationCredential {
|
|
30
|
-
#private;
|
|
31
|
-
constructor(options?: WebIdentityOptions);
|
|
32
|
-
bootstrap(): Promise<void>;
|
|
33
|
-
getAuth(): null;
|
|
34
|
-
prepareTokenExchangeRequest(subjectToken: string, resource: string, options?: {
|
|
35
|
-
tokenEndpoint?: string;
|
|
36
|
-
authInfo?: Record<string, string>;
|
|
37
|
-
}): Promise<TokenExchangeRequest>;
|
|
38
|
-
getPublicJwks(): {
|
|
39
|
-
keys: Record<string, unknown>[];
|
|
40
|
-
};
|
|
41
|
-
getClientJwksUrl(resourceServerUrl: string): string;
|
|
42
|
-
}
|
|
43
|
-
export interface EKSWorkloadIdentityOptions {
|
|
44
|
-
tokenFilePath?: string;
|
|
45
|
-
envVarName?: string;
|
|
46
|
-
}
|
|
47
|
-
export declare class EKSWorkloadIdentity implements ApplicationCredential {
|
|
48
|
-
#private;
|
|
49
|
-
constructor(options?: EKSWorkloadIdentityOptions);
|
|
50
|
-
getAuth(): null;
|
|
51
|
-
prepareTokenExchangeRequest(subjectToken: string, resource: string): Promise<TokenExchangeRequest>;
|
|
52
|
-
}
|
|
1
|
+
export type { ApplicationCredential } from "@keycardai/oauth/credentials";
|
|
2
|
+
export { ClientSecret } from "@keycardai/oauth/server/clientSecret";
|
|
3
|
+
export type { ClientSecretCredentials } from "@keycardai/oauth/server/clientSecret";
|
|
4
|
+
export { WebIdentity } from "@keycardai/oauth/server";
|
|
5
|
+
export type { WebIdentityOptions } from "@keycardai/oauth/server";
|
|
6
|
+
export { EKSWorkloadIdentity } from "@keycardai/oauth/server";
|
|
7
|
+
export type { EKSWorkloadIdentityOptions } from "@keycardai/oauth/server";
|
|
53
8
|
//# sourceMappingURL=credentials.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../../../../src/server/auth/credentials.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"credentials.d.ts","sourceRoot":"","sources":["../../../../src/server/auth/credentials.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,qBAAqB,EAAE,MAAM,8BAA8B,CAAC;AAC1E,OAAO,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AACpE,YAAY,EAAE,uBAAuB,EAAE,MAAM,sCAAsC,CAAC;AACpF,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AACtD,YAAY,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAClE,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC;AAC9D,YAAY,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -1,149 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _ClientSecret_clientId, _ClientSecret_clientSecret, _WebIdentity_keyManager, _WebIdentity_bootstrapPromise, _EKSWorkloadIdentity_instances, _EKSWorkloadIdentity_tokenFilePath, _EKSWorkloadIdentity_validateTokenFile, _EKSWorkloadIdentity_readToken;
|
|
13
|
-
import * as fs from "node:fs";
|
|
14
|
-
import { PrivateKeyManager, FilePrivateKeyStorage } from "./privateKey.js";
|
|
15
|
-
import { EKSWorkloadIdentityConfigurationError } from "./errors.js";
|
|
16
|
-
// =============================================================================
|
|
17
|
-
// ClientSecret
|
|
18
|
-
// =============================================================================
|
|
19
|
-
export class ClientSecret {
|
|
20
|
-
constructor(clientId, clientSecret) {
|
|
21
|
-
_ClientSecret_clientId.set(this, void 0);
|
|
22
|
-
_ClientSecret_clientSecret.set(this, void 0);
|
|
23
|
-
__classPrivateFieldSet(this, _ClientSecret_clientId, clientId, "f");
|
|
24
|
-
__classPrivateFieldSet(this, _ClientSecret_clientSecret, clientSecret, "f");
|
|
25
|
-
}
|
|
26
|
-
getAuth() {
|
|
27
|
-
return { clientId: __classPrivateFieldGet(this, _ClientSecret_clientId, "f"), clientSecret: __classPrivateFieldGet(this, _ClientSecret_clientSecret, "f") };
|
|
28
|
-
}
|
|
29
|
-
async prepareTokenExchangeRequest(subjectToken, resource) {
|
|
30
|
-
return {
|
|
31
|
-
subjectToken,
|
|
32
|
-
resource,
|
|
33
|
-
subjectTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
_ClientSecret_clientId = new WeakMap(), _ClientSecret_clientSecret = new WeakMap();
|
|
38
|
-
export class WebIdentity {
|
|
39
|
-
constructor(options = {}) {
|
|
40
|
-
_WebIdentity_keyManager.set(this, void 0);
|
|
41
|
-
_WebIdentity_bootstrapPromise.set(this, void 0);
|
|
42
|
-
const storage = options.storage ?? new FilePrivateKeyStorage(options.storageDir ?? "./mcp_keys");
|
|
43
|
-
let keyId = options.keyId;
|
|
44
|
-
if (!keyId && options.serverName) {
|
|
45
|
-
keyId = options.serverName.replace(/[^a-zA-Z0-9\-_]/g, "_");
|
|
46
|
-
}
|
|
47
|
-
__classPrivateFieldSet(this, _WebIdentity_keyManager, new PrivateKeyManager({
|
|
48
|
-
storage,
|
|
49
|
-
keyId,
|
|
50
|
-
audienceConfig: options.audienceConfig,
|
|
51
|
-
}), "f");
|
|
52
|
-
}
|
|
53
|
-
async bootstrap() {
|
|
54
|
-
if (!__classPrivateFieldGet(this, _WebIdentity_bootstrapPromise, "f")) {
|
|
55
|
-
__classPrivateFieldSet(this, _WebIdentity_bootstrapPromise, __classPrivateFieldGet(this, _WebIdentity_keyManager, "f").bootstrapIdentity(), "f");
|
|
56
|
-
}
|
|
57
|
-
return __classPrivateFieldGet(this, _WebIdentity_bootstrapPromise, "f");
|
|
58
|
-
}
|
|
59
|
-
getAuth() {
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
async prepareTokenExchangeRequest(subjectToken, resource, options) {
|
|
63
|
-
await this.bootstrap();
|
|
64
|
-
const issuer = options?.authInfo?.resource_client_id ?? __classPrivateFieldGet(this, _WebIdentity_keyManager, "f").getClientId();
|
|
65
|
-
const audience = options?.tokenEndpoint ?? issuer;
|
|
66
|
-
const clientAssertion = await __classPrivateFieldGet(this, _WebIdentity_keyManager, "f").createClientAssertion(issuer, audience);
|
|
67
|
-
return {
|
|
68
|
-
subjectToken,
|
|
69
|
-
resource,
|
|
70
|
-
subjectTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
|
71
|
-
clientAssertionType: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
|
72
|
-
clientAssertion,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
getPublicJwks() {
|
|
76
|
-
return __classPrivateFieldGet(this, _WebIdentity_keyManager, "f").getPublicJwks();
|
|
77
|
-
}
|
|
78
|
-
getClientJwksUrl(resourceServerUrl) {
|
|
79
|
-
return __classPrivateFieldGet(this, _WebIdentity_keyManager, "f").getClientJwksUrl(resourceServerUrl);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
_WebIdentity_keyManager = new WeakMap(), _WebIdentity_bootstrapPromise = new WeakMap();
|
|
83
|
-
// =============================================================================
|
|
84
|
-
// EKSWorkloadIdentity
|
|
85
|
-
// =============================================================================
|
|
86
|
-
const DEFAULT_EKS_ENV_VARS = [
|
|
87
|
-
"KEYCARD_EKS_WORKLOAD_IDENTITY_TOKEN_FILE",
|
|
88
|
-
"AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE",
|
|
89
|
-
"AWS_WEB_IDENTITY_TOKEN_FILE",
|
|
90
|
-
];
|
|
91
|
-
export class EKSWorkloadIdentity {
|
|
92
|
-
constructor(options) {
|
|
93
|
-
_EKSWorkloadIdentity_instances.add(this);
|
|
94
|
-
_EKSWorkloadIdentity_tokenFilePath.set(this, void 0);
|
|
95
|
-
if (options?.tokenFilePath) {
|
|
96
|
-
__classPrivateFieldSet(this, _EKSWorkloadIdentity_tokenFilePath, options.tokenFilePath, "f");
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
const envNames = options?.envVarName
|
|
100
|
-
? [options.envVarName, ...DEFAULT_EKS_ENV_VARS]
|
|
101
|
-
: DEFAULT_EKS_ENV_VARS;
|
|
102
|
-
const found = envNames.find((name) => process.env[name]);
|
|
103
|
-
if (!found || !process.env[found]) {
|
|
104
|
-
throw new EKSWorkloadIdentityConfigurationError("Could not find token file path in environment variables. " +
|
|
105
|
-
`Checked: ${envNames.join(", ")}`);
|
|
106
|
-
}
|
|
107
|
-
__classPrivateFieldSet(this, _EKSWorkloadIdentity_tokenFilePath, process.env[found], "f");
|
|
108
|
-
}
|
|
109
|
-
__classPrivateFieldGet(this, _EKSWorkloadIdentity_instances, "m", _EKSWorkloadIdentity_validateTokenFile).call(this);
|
|
110
|
-
}
|
|
111
|
-
getAuth() {
|
|
112
|
-
return null;
|
|
113
|
-
}
|
|
114
|
-
async prepareTokenExchangeRequest(subjectToken, resource) {
|
|
115
|
-
const eksToken = __classPrivateFieldGet(this, _EKSWorkloadIdentity_instances, "m", _EKSWorkloadIdentity_readToken).call(this);
|
|
116
|
-
return {
|
|
117
|
-
subjectToken,
|
|
118
|
-
resource,
|
|
119
|
-
subjectTokenType: "urn:ietf:params:oauth:token-type:access_token",
|
|
120
|
-
clientAssertionType: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",
|
|
121
|
-
clientAssertion: eksToken,
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
_EKSWorkloadIdentity_tokenFilePath = new WeakMap(), _EKSWorkloadIdentity_instances = new WeakSet(), _EKSWorkloadIdentity_validateTokenFile = function _EKSWorkloadIdentity_validateTokenFile() {
|
|
126
|
-
try {
|
|
127
|
-
const token = fs.readFileSync(__classPrivateFieldGet(this, _EKSWorkloadIdentity_tokenFilePath, "f"), "utf-8").trim();
|
|
128
|
-
if (!token) {
|
|
129
|
-
throw new EKSWorkloadIdentityConfigurationError(`Token file is empty: ${__classPrivateFieldGet(this, _EKSWorkloadIdentity_tokenFilePath, "f")}`);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
catch (error) {
|
|
133
|
-
if (error instanceof EKSWorkloadIdentityConfigurationError)
|
|
134
|
-
throw error;
|
|
135
|
-
throw new EKSWorkloadIdentityConfigurationError(`Error reading token file "${__classPrivateFieldGet(this, _EKSWorkloadIdentity_tokenFilePath, "f")}": ${error}`);
|
|
136
|
-
}
|
|
137
|
-
}, _EKSWorkloadIdentity_readToken = function _EKSWorkloadIdentity_readToken() {
|
|
138
|
-
try {
|
|
139
|
-
const token = fs.readFileSync(__classPrivateFieldGet(this, _EKSWorkloadIdentity_tokenFilePath, "f"), "utf-8").trim();
|
|
140
|
-
if (!token) {
|
|
141
|
-
throw new Error(`Token file is empty: ${__classPrivateFieldGet(this, _EKSWorkloadIdentity_tokenFilePath, "f")}`);
|
|
142
|
-
}
|
|
143
|
-
return token;
|
|
144
|
-
}
|
|
145
|
-
catch (error) {
|
|
146
|
-
throw new Error(`Failed to read EKS token from "${__classPrivateFieldGet(this, _EKSWorkloadIdentity_tokenFilePath, "f")}": ${error}`);
|
|
147
|
-
}
|
|
148
|
-
};
|
|
1
|
+
export { ClientSecret } from "@keycardai/oauth/server/clientSecret";
|
|
2
|
+
export { WebIdentity } from "@keycardai/oauth/server";
|
|
3
|
+
export { EKSWorkloadIdentity } from "@keycardai/oauth/server";
|
|
149
4
|
//# sourceMappingURL=credentials.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"credentials.js","sourceRoot":"","sources":["../../../../src/server/auth/credentials.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"credentials.js","sourceRoot":"","sources":["../../../../src/server/auth/credentials.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,sCAAsC,CAAC;AAEpE,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
export { HTTPError, BadRequestError, UnauthorizedError, OAuthError, InvalidTokenError, InsufficientScopeError } from "@keycardai/oauth/errors";
|
|
2
|
-
export declare class ResourceAccessError extends Error {
|
|
3
|
-
constructor(message?: string);
|
|
4
|
-
}
|
|
5
|
-
export declare class AuthProviderConfigurationError extends Error {
|
|
6
|
-
constructor(message?: string);
|
|
7
|
-
}
|
|
1
|
+
export { HTTPError, BadRequestError, UnauthorizedError, OAuthError, InvalidTokenError, InsufficientScopeError, ResourceAccessError, AuthProviderConfigurationError, } from "@keycardai/oauth/errors";
|
|
8
2
|
export declare class EKSWorkloadIdentityConfigurationError extends Error {
|
|
9
3
|
constructor(message?: string);
|
|
10
4
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../../src/server/auth/errors.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../../../src/server/auth/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,UAAU,EACV,iBAAiB,EACjB,sBAAsB,EACtB,mBAAmB,EACnB,8BAA8B,GAC/B,MAAM,yBAAyB,CAAC;AAEjC,qBAAa,qCAAsC,SAAQ,KAAK;gBAClD,OAAO,CAAC,EAAE,MAAM;CAI7B"}
|
|
@@ -1,16 +1,4 @@
|
|
|
1
|
-
export { HTTPError, BadRequestError, UnauthorizedError, OAuthError, InvalidTokenError, InsufficientScopeError } from "@keycardai/oauth/errors";
|
|
2
|
-
export class ResourceAccessError extends Error {
|
|
3
|
-
constructor(message) {
|
|
4
|
-
super(message ?? "Resource access denied or token not available");
|
|
5
|
-
this.name = "ResourceAccessError";
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
export class AuthProviderConfigurationError extends Error {
|
|
9
|
-
constructor(message) {
|
|
10
|
-
super(message ?? "AuthProvider configuration is invalid");
|
|
11
|
-
this.name = "AuthProviderConfigurationError";
|
|
12
|
-
}
|
|
13
|
-
}
|
|
1
|
+
export { HTTPError, BadRequestError, UnauthorizedError, OAuthError, InvalidTokenError, InsufficientScopeError, ResourceAccessError, AuthProviderConfigurationError, } from "@keycardai/oauth/errors";
|
|
14
2
|
export class EKSWorkloadIdentityConfigurationError extends Error {
|
|
15
3
|
constructor(message) {
|
|
16
4
|
super(message ?? "EKS workload identity configuration is invalid");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../../src/server/auth/errors.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../../src/server/auth/errors.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,eAAe,EACf,iBAAiB,EACjB,UAAU,EACV,iBAAiB,EACjB,sBAAsB,EACtB,mBAAmB,EACnB,8BAA8B,GAC/B,MAAM,yBAAyB,CAAC;AAEjC,MAAM,OAAO,qCAAsC,SAAQ,KAAK;IAC9D,YAAY,OAAgB;QAC1B,KAAK,CAAC,OAAO,IAAI,gDAAgD,CAAC,CAAC;QACnE,IAAI,CAAC,IAAI,GAAG,uCAAuC,CAAC;IACtD,CAAC;CACF"}
|
|
@@ -5,8 +5,23 @@ export interface AuthenticatedRequest extends Request {
|
|
|
5
5
|
auth: AuthInfo;
|
|
6
6
|
}
|
|
7
7
|
export type BearerAuthMiddlewareOptions = {
|
|
8
|
+
/**
|
|
9
|
+
* Token verifier implementation. If omitted, a `JWTOAuthTokenVerifier` is
|
|
10
|
+
* constructed from `issuers` / `audiences`. Exactly one of `verifier` or
|
|
11
|
+
* `issuers` must be provided.
|
|
12
|
+
*/
|
|
8
13
|
verifier?: OAuthTokenVerifier;
|
|
14
|
+
/**
|
|
15
|
+
* Issuer(s) to trust when auto-constructing the default verifier. Tokens
|
|
16
|
+
* whose `iss` doesn't match are rejected before any key lookup.
|
|
17
|
+
*/
|
|
18
|
+
issuers?: string | readonly string[];
|
|
19
|
+
/**
|
|
20
|
+
* Audience(s) to enforce when auto-constructing the default verifier. When
|
|
21
|
+
* set, tokens must present an `aud` that contains one of these values.
|
|
22
|
+
*/
|
|
23
|
+
audiences?: string | readonly string[];
|
|
9
24
|
requiredScopes?: string[];
|
|
10
25
|
};
|
|
11
|
-
export declare function requireBearerAuth({ verifier, requiredScopes }: BearerAuthMiddlewareOptions): RequestHandler;
|
|
26
|
+
export declare function requireBearerAuth({ verifier, issuers, audiences, requiredScopes, }: BearerAuthMiddlewareOptions): RequestHandler;
|
|
12
27
|
//# sourceMappingURL=bearerAuth.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bearerAuth.d.ts","sourceRoot":"","sources":["../../../../../src/server/auth/middleware/bearerAuth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAA0B,cAAc,EAAE,MAAM,SAAS,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,mDAAmD,CAAA;AACtF,OAAO,EAAE,QAAQ,EAAE,MAAM,gDAAgD,CAAC;AAM1E,MAAM,WAAW,oBAAqB,SAAQ,OAAO;IACnD,IAAI,EAAE,QAAQ,CAAC;CAChB;AAED,MAAM,MAAM,2BAA2B,GAAG;IACxC,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAC9B,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,
|
|
1
|
+
{"version":3,"file":"bearerAuth.d.ts","sourceRoot":"","sources":["../../../../../src/server/auth/middleware/bearerAuth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAA0B,cAAc,EAAE,MAAM,SAAS,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,mDAAmD,CAAA;AACtF,OAAO,EAAE,QAAQ,EAAE,MAAM,gDAAgD,CAAC;AAM1E,MAAM,WAAW,oBAAqB,SAAQ,OAAO;IACnD,IAAI,EAAE,QAAQ,CAAC;CAChB;AAED,MAAM,MAAM,2BAA2B,GAAG;IACxC;;;;OAIG;IACH,QAAQ,CAAC,EAAE,kBAAkB,CAAC;IAC9B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IACrC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CAAC;IACvC,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,EAChC,QAAQ,EACR,OAAO,EACP,SAAS,EACT,cAAmB,GACpB,EAAE,2BAA2B,GAAG,cAAc,CAmF9C"}
|
|
@@ -2,10 +2,22 @@ import { JWTOAuthTokenVerifier } from "../verifiers/jwt.js";
|
|
|
2
2
|
import { JWKSOAuthKeyring } from "@keycardai/oauth/keyring";
|
|
3
3
|
import { getOAuthProtectedResourceMetadataUrl } from "../router.js";
|
|
4
4
|
import { BadRequestError, UnauthorizedError, InvalidTokenError, InsufficientScopeError } from "../errors.js";
|
|
5
|
-
export function requireBearerAuth({ verifier, requiredScopes = [] }) {
|
|
5
|
+
export function requireBearerAuth({ verifier, issuers, audiences, requiredScopes = [], }) {
|
|
6
6
|
if (!verifier) {
|
|
7
|
+
const configuredIssuers = typeof issuers === "string" && issuers.length > 0
|
|
8
|
+
? issuers
|
|
9
|
+
: Array.isArray(issuers) && issuers.length > 0
|
|
10
|
+
? issuers
|
|
11
|
+
: undefined;
|
|
12
|
+
if (!configuredIssuers) {
|
|
13
|
+
throw new Error("requireBearerAuth: provide either `verifier` or a non-empty `issuers` — " +
|
|
14
|
+
"passing neither would accept any signed JWT");
|
|
15
|
+
}
|
|
7
16
|
const keyring = new JWKSOAuthKeyring();
|
|
8
|
-
verifier = new JWTOAuthTokenVerifier(keyring
|
|
17
|
+
verifier = new JWTOAuthTokenVerifier(keyring, {
|
|
18
|
+
issuers: configuredIssuers,
|
|
19
|
+
audiences,
|
|
20
|
+
});
|
|
9
21
|
}
|
|
10
22
|
return async (req, res, next) => {
|
|
11
23
|
const url = `${req.protocol}://${req.host}${req.originalUrl}`;
|
|
@@ -22,7 +34,7 @@ export function requireBearerAuth({ verifier, requiredScopes = [] }) {
|
|
|
22
34
|
throw new InvalidTokenError("Unsupported authentication scheme");
|
|
23
35
|
}
|
|
24
36
|
const authInfo = await verifier.verifyAccessToken(token);
|
|
25
|
-
if (!!authInfo.resource &&
|
|
37
|
+
if (!!authInfo.resource && new URL(url).origin !== authInfo.resource.origin) {
|
|
26
38
|
throw new InvalidTokenError("Token not intended for resource");
|
|
27
39
|
}
|
|
28
40
|
if (requiredScopes.length > 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bearerAuth.js","sourceRoot":"","sources":["../../../../../src/server/auth/middleware/bearerAuth.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,oCAAoC,EAAE,MAAM,cAAc,CAAA;AACnE,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"bearerAuth.js","sourceRoot":"","sources":["../../../../../src/server/auth/middleware/bearerAuth.ts"],"names":[],"mappings":"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"}
|
|
@@ -1,47 +1,3 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
alg?: string;
|
|
4
|
-
use?: string;
|
|
5
|
-
kid?: string;
|
|
6
|
-
n?: string;
|
|
7
|
-
e?: string;
|
|
8
|
-
[key: string]: unknown;
|
|
9
|
-
}
|
|
10
|
-
export interface PrivateKeyStorage {
|
|
11
|
-
exists(keyId: string): Promise<boolean>;
|
|
12
|
-
storeKeyPair(keyId: string, privateKeyPem: string, publicKeyJwk: JsonWebKey): Promise<void>;
|
|
13
|
-
loadKeyPair(keyId: string): Promise<{
|
|
14
|
-
privateKeyPem: string;
|
|
15
|
-
publicKeyJwk: JsonWebKey;
|
|
16
|
-
}>;
|
|
17
|
-
deleteKeyPair(keyId: string): Promise<boolean>;
|
|
18
|
-
listKeyIds(): Promise<string[]>;
|
|
19
|
-
}
|
|
20
|
-
export declare class FilePrivateKeyStorage implements PrivateKeyStorage {
|
|
21
|
-
#private;
|
|
22
|
-
constructor(storageDir: string);
|
|
23
|
-
exists(keyId: string): Promise<boolean>;
|
|
24
|
-
storeKeyPair(keyId: string, privateKeyPem: string, publicKeyJwk: JsonWebKey): Promise<void>;
|
|
25
|
-
loadKeyPair(keyId: string): Promise<{
|
|
26
|
-
privateKeyPem: string;
|
|
27
|
-
publicKeyJwk: JsonWebKey;
|
|
28
|
-
}>;
|
|
29
|
-
deleteKeyPair(keyId: string): Promise<boolean>;
|
|
30
|
-
listKeyIds(): Promise<string[]>;
|
|
31
|
-
}
|
|
32
|
-
export declare class PrivateKeyManager {
|
|
33
|
-
#private;
|
|
34
|
-
constructor(options: {
|
|
35
|
-
storage: PrivateKeyStorage;
|
|
36
|
-
keyId?: string;
|
|
37
|
-
audienceConfig?: string | Record<string, string>;
|
|
38
|
-
});
|
|
39
|
-
bootstrapIdentity(): Promise<void>;
|
|
40
|
-
createClientAssertion(issuer: string, audience: string, expirySeconds?: number): Promise<string>;
|
|
41
|
-
getPublicJwks(): {
|
|
42
|
-
keys: JsonWebKey[];
|
|
43
|
-
};
|
|
44
|
-
getClientId(): string;
|
|
45
|
-
getClientJwksUrl(resourceServerUrl: string): string;
|
|
46
|
-
}
|
|
1
|
+
export { FilePrivateKeyStorage, PrivateKeyManager, } from "@keycardai/oauth/server";
|
|
2
|
+
export type { PrivateKeyStorage, JsonWebKey } from "@keycardai/oauth/server";
|
|
47
3
|
//# sourceMappingURL=privateKey.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"privateKey.d.ts","sourceRoot":"","sources":["../../../../src/server/auth/privateKey.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"privateKey.d.ts","sourceRoot":"","sources":["../../../../src/server/auth/privateKey.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,iBAAiB,GAClB,MAAM,yBAAyB,CAAC;AACjC,YAAY,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -1,197 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
-
};
|
|
7
|
-
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
-
};
|
|
12
|
-
var _FilePrivateKeyStorage_instances, _FilePrivateKeyStorage_storageDir, _FilePrivateKeyStorage_keyPath, _FilePrivateKeyStorage_metadataPath, _PrivateKeyManager_instances, _PrivateKeyManager_storage, _PrivateKeyManager_keyId, _PrivateKeyManager_audienceConfig, _PrivateKeyManager_privateKeyPem, _PrivateKeyManager_publicKeyJwk, _PrivateKeyManager_generateAndStoreKeyPair, _PemPrivateKeyring_pem, _PemPrivateKeyring_kid, _PemPrivateKeyring_issuer;
|
|
13
|
-
import * as crypto from "node:crypto";
|
|
14
|
-
import * as fs from "node:fs/promises";
|
|
15
|
-
import * as path from "node:path";
|
|
16
|
-
import { JWTSigner } from "@keycardai/oauth/jwt/signer";
|
|
17
|
-
// =============================================================================
|
|
18
|
-
// File-Based Storage
|
|
19
|
-
// =============================================================================
|
|
20
|
-
export class FilePrivateKeyStorage {
|
|
21
|
-
constructor(storageDir) {
|
|
22
|
-
_FilePrivateKeyStorage_instances.add(this);
|
|
23
|
-
_FilePrivateKeyStorage_storageDir.set(this, void 0);
|
|
24
|
-
__classPrivateFieldSet(this, _FilePrivateKeyStorage_storageDir, storageDir, "f");
|
|
25
|
-
}
|
|
26
|
-
async exists(keyId) {
|
|
27
|
-
try {
|
|
28
|
-
await fs.access(__classPrivateFieldGet(this, _FilePrivateKeyStorage_instances, "m", _FilePrivateKeyStorage_keyPath).call(this, keyId));
|
|
29
|
-
await fs.access(__classPrivateFieldGet(this, _FilePrivateKeyStorage_instances, "m", _FilePrivateKeyStorage_metadataPath).call(this, keyId));
|
|
30
|
-
return true;
|
|
31
|
-
}
|
|
32
|
-
catch {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
async storeKeyPair(keyId, privateKeyPem, publicKeyJwk) {
|
|
37
|
-
await fs.mkdir(__classPrivateFieldGet(this, _FilePrivateKeyStorage_storageDir, "f"), { recursive: true });
|
|
38
|
-
const metadata = {
|
|
39
|
-
key_id: keyId,
|
|
40
|
-
public_key_jwk: publicKeyJwk,
|
|
41
|
-
created_at: Date.now() / 1000,
|
|
42
|
-
algorithm: "RS256",
|
|
43
|
-
};
|
|
44
|
-
await fs.writeFile(__classPrivateFieldGet(this, _FilePrivateKeyStorage_instances, "m", _FilePrivateKeyStorage_keyPath).call(this, keyId), privateKeyPem, { encoding: "utf-8", mode: 0o600 });
|
|
45
|
-
await fs.writeFile(__classPrivateFieldGet(this, _FilePrivateKeyStorage_instances, "m", _FilePrivateKeyStorage_metadataPath).call(this, keyId), JSON.stringify(metadata, null, 2), { encoding: "utf-8", mode: 0o644 });
|
|
46
|
-
}
|
|
47
|
-
async loadKeyPair(keyId) {
|
|
48
|
-
const [privateKeyPem, metadataRaw] = await Promise.all([
|
|
49
|
-
fs.readFile(__classPrivateFieldGet(this, _FilePrivateKeyStorage_instances, "m", _FilePrivateKeyStorage_keyPath).call(this, keyId), "utf-8"),
|
|
50
|
-
fs.readFile(__classPrivateFieldGet(this, _FilePrivateKeyStorage_instances, "m", _FilePrivateKeyStorage_metadataPath).call(this, keyId), "utf-8"),
|
|
51
|
-
]);
|
|
52
|
-
const metadata = JSON.parse(metadataRaw);
|
|
53
|
-
return { privateKeyPem, publicKeyJwk: metadata.public_key_jwk };
|
|
54
|
-
}
|
|
55
|
-
async deleteKeyPair(keyId) {
|
|
56
|
-
let deleted = false;
|
|
57
|
-
try {
|
|
58
|
-
await fs.unlink(__classPrivateFieldGet(this, _FilePrivateKeyStorage_instances, "m", _FilePrivateKeyStorage_keyPath).call(this, keyId));
|
|
59
|
-
deleted = true;
|
|
60
|
-
}
|
|
61
|
-
catch { /* ignore */ }
|
|
62
|
-
try {
|
|
63
|
-
await fs.unlink(__classPrivateFieldGet(this, _FilePrivateKeyStorage_instances, "m", _FilePrivateKeyStorage_metadataPath).call(this, keyId));
|
|
64
|
-
deleted = true;
|
|
65
|
-
}
|
|
66
|
-
catch { /* ignore */ }
|
|
67
|
-
return deleted;
|
|
68
|
-
}
|
|
69
|
-
async listKeyIds() {
|
|
70
|
-
try {
|
|
71
|
-
const files = await fs.readdir(__classPrivateFieldGet(this, _FilePrivateKeyStorage_storageDir, "f"));
|
|
72
|
-
const jsonFiles = files.filter((f) => f.endsWith(".json"));
|
|
73
|
-
const keyIds = [];
|
|
74
|
-
for (const file of jsonFiles) {
|
|
75
|
-
const keyId = file.replace(/\.json$/, "");
|
|
76
|
-
if (await this.exists(keyId)) {
|
|
77
|
-
keyIds.push(keyId);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
return keyIds.sort();
|
|
81
|
-
}
|
|
82
|
-
catch {
|
|
83
|
-
return [];
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
_FilePrivateKeyStorage_storageDir = new WeakMap(), _FilePrivateKeyStorage_instances = new WeakSet(), _FilePrivateKeyStorage_keyPath = function _FilePrivateKeyStorage_keyPath(keyId) {
|
|
88
|
-
return path.join(__classPrivateFieldGet(this, _FilePrivateKeyStorage_storageDir, "f"), `${keyId}.pem`);
|
|
89
|
-
}, _FilePrivateKeyStorage_metadataPath = function _FilePrivateKeyStorage_metadataPath(keyId) {
|
|
90
|
-
return path.join(__classPrivateFieldGet(this, _FilePrivateKeyStorage_storageDir, "f"), `${keyId}.json`);
|
|
91
|
-
};
|
|
92
|
-
// =============================================================================
|
|
93
|
-
// Private Key Manager
|
|
94
|
-
// =============================================================================
|
|
95
|
-
export class PrivateKeyManager {
|
|
96
|
-
constructor(options) {
|
|
97
|
-
_PrivateKeyManager_instances.add(this);
|
|
98
|
-
_PrivateKeyManager_storage.set(this, void 0);
|
|
99
|
-
_PrivateKeyManager_keyId.set(this, void 0);
|
|
100
|
-
_PrivateKeyManager_audienceConfig.set(this, void 0);
|
|
101
|
-
_PrivateKeyManager_privateKeyPem.set(this, void 0);
|
|
102
|
-
_PrivateKeyManager_publicKeyJwk.set(this, void 0);
|
|
103
|
-
__classPrivateFieldSet(this, _PrivateKeyManager_storage, options.storage, "f");
|
|
104
|
-
__classPrivateFieldSet(this, _PrivateKeyManager_keyId, options.keyId ?? crypto.randomUUID(), "f");
|
|
105
|
-
__classPrivateFieldSet(this, _PrivateKeyManager_audienceConfig, options.audienceConfig, "f");
|
|
106
|
-
}
|
|
107
|
-
async bootstrapIdentity() {
|
|
108
|
-
if (await __classPrivateFieldGet(this, _PrivateKeyManager_storage, "f").exists(__classPrivateFieldGet(this, _PrivateKeyManager_keyId, "f"))) {
|
|
109
|
-
const { privateKeyPem, publicKeyJwk } = await __classPrivateFieldGet(this, _PrivateKeyManager_storage, "f").loadKeyPair(__classPrivateFieldGet(this, _PrivateKeyManager_keyId, "f"));
|
|
110
|
-
__classPrivateFieldSet(this, _PrivateKeyManager_privateKeyPem, privateKeyPem, "f");
|
|
111
|
-
__classPrivateFieldSet(this, _PrivateKeyManager_publicKeyJwk, publicKeyJwk, "f");
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
await __classPrivateFieldGet(this, _PrivateKeyManager_instances, "m", _PrivateKeyManager_generateAndStoreKeyPair).call(this);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
async createClientAssertion(issuer, audience, expirySeconds = 300) {
|
|
118
|
-
if (!__classPrivateFieldGet(this, _PrivateKeyManager_privateKeyPem, "f") || !__classPrivateFieldGet(this, _PrivateKeyManager_publicKeyJwk, "f")) {
|
|
119
|
-
throw new Error("Identity not bootstrapped. Call bootstrapIdentity() first.");
|
|
120
|
-
}
|
|
121
|
-
const keyring = new PemPrivateKeyring(__classPrivateFieldGet(this, _PrivateKeyManager_privateKeyPem, "f"), __classPrivateFieldGet(this, _PrivateKeyManager_keyId, "f"), issuer);
|
|
122
|
-
const signer = new JWTSigner(keyring);
|
|
123
|
-
const now = Math.floor(Date.now() / 1000);
|
|
124
|
-
return signer.sign({
|
|
125
|
-
iss: issuer,
|
|
126
|
-
sub: issuer,
|
|
127
|
-
aud: audience,
|
|
128
|
-
jti: crypto.randomUUID(),
|
|
129
|
-
iat: now,
|
|
130
|
-
exp: now + expirySeconds,
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
getPublicJwks() {
|
|
134
|
-
if (!__classPrivateFieldGet(this, _PrivateKeyManager_publicKeyJwk, "f")) {
|
|
135
|
-
throw new Error("Identity not bootstrapped. Call bootstrapIdentity() first.");
|
|
136
|
-
}
|
|
137
|
-
return { keys: [__classPrivateFieldGet(this, _PrivateKeyManager_publicKeyJwk, "f")] };
|
|
138
|
-
}
|
|
139
|
-
getClientId() {
|
|
140
|
-
return __classPrivateFieldGet(this, _PrivateKeyManager_keyId, "f");
|
|
141
|
-
}
|
|
142
|
-
getClientJwksUrl(resourceServerUrl) {
|
|
143
|
-
const url = new URL(resourceServerUrl);
|
|
144
|
-
return `${url.protocol}//${url.host}/.well-known/jwks.json`;
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
_PrivateKeyManager_storage = new WeakMap(), _PrivateKeyManager_keyId = new WeakMap(), _PrivateKeyManager_audienceConfig = new WeakMap(), _PrivateKeyManager_privateKeyPem = new WeakMap(), _PrivateKeyManager_publicKeyJwk = new WeakMap(), _PrivateKeyManager_instances = new WeakSet(), _PrivateKeyManager_generateAndStoreKeyPair = async function _PrivateKeyManager_generateAndStoreKeyPair() {
|
|
148
|
-
const keyPair = crypto.generateKeyPairSync("rsa", {
|
|
149
|
-
modulusLength: 2048,
|
|
150
|
-
publicKeyEncoding: { type: "spki", format: "pem" },
|
|
151
|
-
privateKeyEncoding: { type: "pkcs8", format: "pem" },
|
|
152
|
-
});
|
|
153
|
-
const privateKeyPem = String(keyPair.privateKey);
|
|
154
|
-
const publicKeyPem = String(keyPair.publicKey);
|
|
155
|
-
// Convert public key PEM to JWK format
|
|
156
|
-
const publicKeyObj = crypto.createPublicKey(publicKeyPem);
|
|
157
|
-
const jwk = publicKeyObj.export({ format: "jwk" });
|
|
158
|
-
const publicKeyJwk = {
|
|
159
|
-
kty: jwk.kty,
|
|
160
|
-
n: jwk.n,
|
|
161
|
-
e: jwk.e,
|
|
162
|
-
kid: __classPrivateFieldGet(this, _PrivateKeyManager_keyId, "f"),
|
|
163
|
-
alg: "RS256",
|
|
164
|
-
use: "sig",
|
|
165
|
-
};
|
|
166
|
-
await __classPrivateFieldGet(this, _PrivateKeyManager_storage, "f").storeKeyPair(__classPrivateFieldGet(this, _PrivateKeyManager_keyId, "f"), privateKeyPem, publicKeyJwk);
|
|
167
|
-
__classPrivateFieldSet(this, _PrivateKeyManager_privateKeyPem, privateKeyPem, "f");
|
|
168
|
-
__classPrivateFieldSet(this, _PrivateKeyManager_publicKeyJwk, publicKeyJwk, "f");
|
|
169
|
-
};
|
|
170
|
-
// =============================================================================
|
|
171
|
-
// PEM-based PrivateKeyring adapter (implements PrivateKeyring from @keycardai/oauth)
|
|
172
|
-
// =============================================================================
|
|
173
|
-
class PemPrivateKeyring {
|
|
174
|
-
constructor(pem, kid, issuer) {
|
|
175
|
-
_PemPrivateKeyring_pem.set(this, void 0);
|
|
176
|
-
_PemPrivateKeyring_kid.set(this, void 0);
|
|
177
|
-
_PemPrivateKeyring_issuer.set(this, void 0);
|
|
178
|
-
__classPrivateFieldSet(this, _PemPrivateKeyring_pem, pem, "f");
|
|
179
|
-
__classPrivateFieldSet(this, _PemPrivateKeyring_kid, kid, "f");
|
|
180
|
-
__classPrivateFieldSet(this, _PemPrivateKeyring_issuer, issuer, "f");
|
|
181
|
-
}
|
|
182
|
-
async key(_usage) {
|
|
183
|
-
// Import PEM private key as CryptoKey
|
|
184
|
-
const keyObj = crypto.createPrivateKey(__classPrivateFieldGet(this, _PemPrivateKeyring_pem, "f"));
|
|
185
|
-
const jwk = keyObj.export({ format: "jwk" });
|
|
186
|
-
const cryptoKey = await crypto.subtle.importKey("jwk", jwk, { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } }, false, ["sign"]);
|
|
187
|
-
return {
|
|
188
|
-
// node:crypto.webcrypto.CryptoKey and global CryptoKey are identical at runtime
|
|
189
|
-
// but TypeScript treats them as separate declarations
|
|
190
|
-
key: cryptoKey,
|
|
191
|
-
kid: __classPrivateFieldGet(this, _PemPrivateKeyring_kid, "f"),
|
|
192
|
-
issuer: __classPrivateFieldGet(this, _PemPrivateKeyring_issuer, "f"),
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
_PemPrivateKeyring_pem = new WeakMap(), _PemPrivateKeyring_kid = new WeakMap(), _PemPrivateKeyring_issuer = new WeakMap();
|
|
1
|
+
export { FilePrivateKeyStorage, PrivateKeyManager, } from "@keycardai/oauth/server";
|
|
197
2
|
//# sourceMappingURL=privateKey.js.map
|