@canton-network/core-wallet-auth 0.10.0 → 0.11.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/dist/auth-service.d.ts +28 -3
- package/dist/auth-service.d.ts.map +1 -1
- package/dist/auth-token-provider-self-signed.d.ts +13 -0
- package/dist/auth-token-provider-self-signed.d.ts.map +1 -0
- package/dist/auth-token-provider.d.ts +11 -0
- package/dist/auth-token-provider.d.ts.map +1 -0
- package/dist/client-credentials-service.d.ts +1 -9
- package/dist/client-credentials-service.d.ts.map +1 -1
- package/dist/config/schema.d.ts +378 -0
- package/dist/config/schema.d.ts.map +1 -0
- package/dist/index.cjs +254 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +247 -5
- package/dist/index.js.map +1 -0
- package/package.json +19 -7
- package/dist/auth-service.js +0 -3
- package/dist/auth-utils.js +0 -11
- package/dist/client-credentials-service.js +0 -64
- package/dist/client-credentials.service.test.d.ts +0 -2
- package/dist/client-credentials.service.test.d.ts.map +0 -1
- package/dist/client-credentials.service.test.js +0 -55
package/dist/auth-service.d.ts
CHANGED
|
@@ -1,13 +1,38 @@
|
|
|
1
1
|
export type UserId = string;
|
|
2
|
+
/**
|
|
3
|
+
* Authentication context containing user ID and access token
|
|
4
|
+
*/
|
|
2
5
|
export interface AuthContext {
|
|
3
6
|
userId: UserId;
|
|
4
7
|
accessToken: string;
|
|
5
8
|
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
/**
|
|
10
|
+
* Interface for types that are aware of authentication context
|
|
11
|
+
*/
|
|
9
12
|
export interface AuthAware<T> {
|
|
10
13
|
authContext: AuthContext | undefined;
|
|
11
14
|
withAuthContext: (context?: AuthContext) => T;
|
|
12
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Interface for verifying access tokens
|
|
18
|
+
*/
|
|
19
|
+
export interface AuthService {
|
|
20
|
+
verifyToken(accessToken?: string): Promise<AuthContext | undefined>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Interface for providing access tokens used to authenticate requests
|
|
24
|
+
*/
|
|
25
|
+
export interface AccessTokenProvider {
|
|
26
|
+
getUserAccessToken(): Promise<string>;
|
|
27
|
+
getAdminAccessToken(): Promise<string>;
|
|
28
|
+
}
|
|
29
|
+
export interface OIDCConfig {
|
|
30
|
+
token_endpoint: string;
|
|
31
|
+
}
|
|
32
|
+
export interface ClientCredentials {
|
|
33
|
+
clientId: string;
|
|
34
|
+
clientSecret: string;
|
|
35
|
+
scope: string | undefined;
|
|
36
|
+
audience: string | undefined;
|
|
37
|
+
}
|
|
13
38
|
//# sourceMappingURL=auth-service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth-service.d.ts","sourceRoot":"","sources":["../src/auth-service.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,MAAM,GAAG,MAAM,CAAA;AAE3B,MAAM,WAAW,WAAW;IACxB,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,WAAW,WAAW;IACxB,WAAW,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAAA;CACtE;AAED,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"auth-service.d.ts","sourceRoot":"","sources":["../src/auth-service.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,MAAM,GAAG,MAAM,CAAA;AAE3B;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB,MAAM,EAAE,MAAM,CAAA;IACd,WAAW,EAAE,MAAM,CAAA;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,SAAS,CAAC,CAAC;IACxB,WAAW,EAAE,WAAW,GAAG,SAAS,CAAA;IACpC,eAAe,EAAE,CAAC,OAAO,CAAC,EAAE,WAAW,KAAK,CAAC,CAAA;CAChD;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IACxB,WAAW,CAAC,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CAAA;CACtE;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAChC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAA;IACrC,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC,CAAA;CACzC;AAED,MAAM,WAAW,UAAU;IACvB,cAAc,EAAE,MAAM,CAAA;CACzB;AAED,MAAM,WAAW,iBAAiB;IAC9B,QAAQ,EAAE,MAAM,CAAA;IAChB,YAAY,EAAE,MAAM,CAAA;IACpB,KAAK,EAAE,MAAM,GAAG,SAAS,CAAA;IACzB,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAA;CAC/B"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Logger } from '@canton-network/core-types';
|
|
2
|
+
import { AccessTokenProvider, ClientCredentials } from './auth-service.js';
|
|
3
|
+
import { SelfSignedAuth } from './config/schema.js';
|
|
4
|
+
export declare class AuthTokenProviderSelfSigned implements AccessTokenProvider {
|
|
5
|
+
private auth;
|
|
6
|
+
private logger;
|
|
7
|
+
private expirySeconds;
|
|
8
|
+
constructor(auth: SelfSignedAuth, logger: Logger, expirySeconds?: number);
|
|
9
|
+
getUserAccessToken(): Promise<string>;
|
|
10
|
+
getAdminAccessToken(): Promise<string>;
|
|
11
|
+
static fetchToken(logger: Logger, credentials: ClientCredentials, issuer: string, expirySeconds?: number): Promise<string>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=auth-token-provider-self-signed.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-token-provider-self-signed.d.ts","sourceRoot":"","sources":["../src/auth-token-provider-self-signed.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACnD,OAAO,EAAE,mBAAmB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAA;AAC1E,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAGnD,qBAAa,2BAA4B,YAAW,mBAAmB;IAE/D,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,aAAa;gBAFb,IAAI,EAAE,cAAc,EACpB,MAAM,EAAE,MAAM,EACd,aAAa,GAAE,MAAa;IAGlC,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAerC,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC;WAkB/B,UAAU,CACnB,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,iBAAiB,EAC9B,MAAM,EAAE,MAAM,EACd,aAAa,GAAE,MAAa,GAC7B,OAAO,CAAC,MAAM,CAAC;CAgBrB"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Logger } from '@canton-network/core-types';
|
|
2
|
+
import { AccessTokenProvider } from './auth-service.js';
|
|
3
|
+
import { Auth } from './config/schema.js';
|
|
4
|
+
export declare class AuthTokenProvider implements AccessTokenProvider {
|
|
5
|
+
private auth;
|
|
6
|
+
private logger;
|
|
7
|
+
constructor(auth: Auth, logger: Logger);
|
|
8
|
+
getUserAccessToken(): Promise<string>;
|
|
9
|
+
getAdminAccessToken(): Promise<string>;
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=auth-token-provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-token-provider.d.ts","sourceRoot":"","sources":["../src/auth-token-provider.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACnD,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAA;AACvD,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAIzC,qBAAa,iBAAkB,YAAW,mBAAmB;IAErD,OAAO,CAAC,IAAI;IACZ,OAAO,CAAC,MAAM;gBADN,IAAI,EAAE,IAAI,EACV,MAAM,EAAE,MAAM;IAGpB,kBAAkB,IAAI,OAAO,CAAC,MAAM,CAAC;IAwBrC,mBAAmB,IAAI,OAAO,CAAC,MAAM,CAAC;CAuB/C"}
|
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
import { Logger } from '@canton-network/core-types';
|
|
2
|
-
|
|
3
|
-
token_endpoint: string;
|
|
4
|
-
}
|
|
5
|
-
export interface ClientCredentials {
|
|
6
|
-
clientId: string;
|
|
7
|
-
clientSecret: string;
|
|
8
|
-
scope: string | undefined;
|
|
9
|
-
audience: string | undefined;
|
|
10
|
-
}
|
|
2
|
+
import { ClientCredentials, OIDCConfig } from './auth-service.js';
|
|
11
3
|
export declare class ClientCredentialsService {
|
|
12
4
|
private configUrl;
|
|
13
5
|
private logger;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-credentials-service.d.ts","sourceRoot":"","sources":["../src/client-credentials-service.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;
|
|
1
|
+
{"version":3,"file":"client-credentials-service.d.ts","sourceRoot":"","sources":["../src/client-credentials-service.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,MAAM,4BAA4B,CAAA;AACnD,OAAO,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAA;AAEjE,qBAAa,wBAAwB;IAE7B,OAAO,CAAC,SAAS;IACjB,OAAO,CAAC,MAAM;gBADN,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GAAG,SAAS;IAGtC;;;;;OAKG;IACG,UAAU,CAAC,WAAW,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC;IA2B3D,kBAAkB,CACpB,aAAa,EAAE,MAAM,EACrB,WAAW,EAAE,iBAAiB,GAC/B,OAAO,CAAC,QAAQ,CAAC;IA4Bd,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC;CAcxD;AAED,eAAO,MAAM,wBAAwB,GACjC,WAAW,MAAM,EACjB,QAAQ,MAAM,GAAG,SAAS;8BAEM,iBAAiB;CAEnD,CAAA"}
|
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const credentials: z.ZodObject<{
|
|
3
|
+
clientId: z.ZodString;
|
|
4
|
+
clientSecret: z.ZodString;
|
|
5
|
+
}, "strip", z.ZodTypeAny, {
|
|
6
|
+
clientId: string;
|
|
7
|
+
clientSecret: string;
|
|
8
|
+
}, {
|
|
9
|
+
clientId: string;
|
|
10
|
+
clientSecret: string;
|
|
11
|
+
}>;
|
|
12
|
+
declare const passwordAuthSchema: z.ZodObject<{
|
|
13
|
+
identityProviderId: z.ZodString;
|
|
14
|
+
type: z.ZodLiteral<"password">;
|
|
15
|
+
issuer: z.ZodString;
|
|
16
|
+
configUrl: z.ZodString;
|
|
17
|
+
audience: z.ZodString;
|
|
18
|
+
tokenUrl: z.ZodString;
|
|
19
|
+
grantType: z.ZodString;
|
|
20
|
+
scope: z.ZodString;
|
|
21
|
+
clientId: z.ZodString;
|
|
22
|
+
admin: z.ZodOptional<z.ZodObject<{
|
|
23
|
+
clientId: z.ZodString;
|
|
24
|
+
clientSecret: z.ZodString;
|
|
25
|
+
}, "strip", z.ZodTypeAny, {
|
|
26
|
+
clientId: string;
|
|
27
|
+
clientSecret: string;
|
|
28
|
+
}, {
|
|
29
|
+
clientId: string;
|
|
30
|
+
clientSecret: string;
|
|
31
|
+
}>>;
|
|
32
|
+
}, "strip", z.ZodTypeAny, {
|
|
33
|
+
clientId: string;
|
|
34
|
+
type: "password";
|
|
35
|
+
identityProviderId: string;
|
|
36
|
+
issuer: string;
|
|
37
|
+
configUrl: string;
|
|
38
|
+
audience: string;
|
|
39
|
+
tokenUrl: string;
|
|
40
|
+
grantType: string;
|
|
41
|
+
scope: string;
|
|
42
|
+
admin?: {
|
|
43
|
+
clientId: string;
|
|
44
|
+
clientSecret: string;
|
|
45
|
+
} | undefined;
|
|
46
|
+
}, {
|
|
47
|
+
clientId: string;
|
|
48
|
+
type: "password";
|
|
49
|
+
identityProviderId: string;
|
|
50
|
+
issuer: string;
|
|
51
|
+
configUrl: string;
|
|
52
|
+
audience: string;
|
|
53
|
+
tokenUrl: string;
|
|
54
|
+
grantType: string;
|
|
55
|
+
scope: string;
|
|
56
|
+
admin?: {
|
|
57
|
+
clientId: string;
|
|
58
|
+
clientSecret: string;
|
|
59
|
+
} | undefined;
|
|
60
|
+
}>;
|
|
61
|
+
declare const implicitAuthSchema: z.ZodObject<{
|
|
62
|
+
identityProviderId: z.ZodString;
|
|
63
|
+
type: z.ZodLiteral<"implicit">;
|
|
64
|
+
issuer: z.ZodString;
|
|
65
|
+
configUrl: z.ZodString;
|
|
66
|
+
audience: z.ZodString;
|
|
67
|
+
scope: z.ZodString;
|
|
68
|
+
clientId: z.ZodString;
|
|
69
|
+
admin: z.ZodOptional<z.ZodObject<{
|
|
70
|
+
clientId: z.ZodString;
|
|
71
|
+
clientSecret: z.ZodString;
|
|
72
|
+
}, "strip", z.ZodTypeAny, {
|
|
73
|
+
clientId: string;
|
|
74
|
+
clientSecret: string;
|
|
75
|
+
}, {
|
|
76
|
+
clientId: string;
|
|
77
|
+
clientSecret: string;
|
|
78
|
+
}>>;
|
|
79
|
+
}, "strip", z.ZodTypeAny, {
|
|
80
|
+
clientId: string;
|
|
81
|
+
type: "implicit";
|
|
82
|
+
identityProviderId: string;
|
|
83
|
+
issuer: string;
|
|
84
|
+
configUrl: string;
|
|
85
|
+
audience: string;
|
|
86
|
+
scope: string;
|
|
87
|
+
admin?: {
|
|
88
|
+
clientId: string;
|
|
89
|
+
clientSecret: string;
|
|
90
|
+
} | undefined;
|
|
91
|
+
}, {
|
|
92
|
+
clientId: string;
|
|
93
|
+
type: "implicit";
|
|
94
|
+
identityProviderId: string;
|
|
95
|
+
issuer: string;
|
|
96
|
+
configUrl: string;
|
|
97
|
+
audience: string;
|
|
98
|
+
scope: string;
|
|
99
|
+
admin?: {
|
|
100
|
+
clientId: string;
|
|
101
|
+
clientSecret: string;
|
|
102
|
+
} | undefined;
|
|
103
|
+
}>;
|
|
104
|
+
declare const clientCredentialAuthSchema: z.ZodObject<{
|
|
105
|
+
identityProviderId: z.ZodString;
|
|
106
|
+
type: z.ZodLiteral<"client_credentials">;
|
|
107
|
+
issuer: z.ZodString;
|
|
108
|
+
configUrl: z.ZodString;
|
|
109
|
+
audience: z.ZodString;
|
|
110
|
+
scope: z.ZodString;
|
|
111
|
+
clientId: z.ZodString;
|
|
112
|
+
clientSecret: z.ZodString;
|
|
113
|
+
admin: z.ZodOptional<z.ZodObject<{
|
|
114
|
+
clientId: z.ZodString;
|
|
115
|
+
clientSecret: z.ZodString;
|
|
116
|
+
}, "strip", z.ZodTypeAny, {
|
|
117
|
+
clientId: string;
|
|
118
|
+
clientSecret: string;
|
|
119
|
+
}, {
|
|
120
|
+
clientId: string;
|
|
121
|
+
clientSecret: string;
|
|
122
|
+
}>>;
|
|
123
|
+
}, "strip", z.ZodTypeAny, {
|
|
124
|
+
clientId: string;
|
|
125
|
+
clientSecret: string;
|
|
126
|
+
type: "client_credentials";
|
|
127
|
+
identityProviderId: string;
|
|
128
|
+
issuer: string;
|
|
129
|
+
configUrl: string;
|
|
130
|
+
audience: string;
|
|
131
|
+
scope: string;
|
|
132
|
+
admin?: {
|
|
133
|
+
clientId: string;
|
|
134
|
+
clientSecret: string;
|
|
135
|
+
} | undefined;
|
|
136
|
+
}, {
|
|
137
|
+
clientId: string;
|
|
138
|
+
clientSecret: string;
|
|
139
|
+
type: "client_credentials";
|
|
140
|
+
identityProviderId: string;
|
|
141
|
+
issuer: string;
|
|
142
|
+
configUrl: string;
|
|
143
|
+
audience: string;
|
|
144
|
+
scope: string;
|
|
145
|
+
admin?: {
|
|
146
|
+
clientId: string;
|
|
147
|
+
clientSecret: string;
|
|
148
|
+
} | undefined;
|
|
149
|
+
}>;
|
|
150
|
+
declare const selfSignedAuthSchema: z.ZodObject<{
|
|
151
|
+
identityProviderId: z.ZodString;
|
|
152
|
+
type: z.ZodLiteral<"self_signed">;
|
|
153
|
+
issuer: z.ZodString;
|
|
154
|
+
audience: z.ZodString;
|
|
155
|
+
scope: z.ZodString;
|
|
156
|
+
clientId: z.ZodString;
|
|
157
|
+
clientSecret: z.ZodString;
|
|
158
|
+
admin: z.ZodOptional<z.ZodObject<{
|
|
159
|
+
clientId: z.ZodString;
|
|
160
|
+
clientSecret: z.ZodString;
|
|
161
|
+
}, "strip", z.ZodTypeAny, {
|
|
162
|
+
clientId: string;
|
|
163
|
+
clientSecret: string;
|
|
164
|
+
}, {
|
|
165
|
+
clientId: string;
|
|
166
|
+
clientSecret: string;
|
|
167
|
+
}>>;
|
|
168
|
+
}, "strip", z.ZodTypeAny, {
|
|
169
|
+
clientId: string;
|
|
170
|
+
clientSecret: string;
|
|
171
|
+
type: "self_signed";
|
|
172
|
+
identityProviderId: string;
|
|
173
|
+
issuer: string;
|
|
174
|
+
audience: string;
|
|
175
|
+
scope: string;
|
|
176
|
+
admin?: {
|
|
177
|
+
clientId: string;
|
|
178
|
+
clientSecret: string;
|
|
179
|
+
} | undefined;
|
|
180
|
+
}, {
|
|
181
|
+
clientId: string;
|
|
182
|
+
clientSecret: string;
|
|
183
|
+
type: "self_signed";
|
|
184
|
+
identityProviderId: string;
|
|
185
|
+
issuer: string;
|
|
186
|
+
audience: string;
|
|
187
|
+
scope: string;
|
|
188
|
+
admin?: {
|
|
189
|
+
clientId: string;
|
|
190
|
+
clientSecret: string;
|
|
191
|
+
} | undefined;
|
|
192
|
+
}>;
|
|
193
|
+
export declare const authSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
|
|
194
|
+
identityProviderId: z.ZodString;
|
|
195
|
+
type: z.ZodLiteral<"password">;
|
|
196
|
+
issuer: z.ZodString;
|
|
197
|
+
configUrl: z.ZodString;
|
|
198
|
+
audience: z.ZodString;
|
|
199
|
+
tokenUrl: z.ZodString;
|
|
200
|
+
grantType: z.ZodString;
|
|
201
|
+
scope: z.ZodString;
|
|
202
|
+
clientId: z.ZodString;
|
|
203
|
+
admin: z.ZodOptional<z.ZodObject<{
|
|
204
|
+
clientId: z.ZodString;
|
|
205
|
+
clientSecret: z.ZodString;
|
|
206
|
+
}, "strip", z.ZodTypeAny, {
|
|
207
|
+
clientId: string;
|
|
208
|
+
clientSecret: string;
|
|
209
|
+
}, {
|
|
210
|
+
clientId: string;
|
|
211
|
+
clientSecret: string;
|
|
212
|
+
}>>;
|
|
213
|
+
}, "strip", z.ZodTypeAny, {
|
|
214
|
+
clientId: string;
|
|
215
|
+
type: "password";
|
|
216
|
+
identityProviderId: string;
|
|
217
|
+
issuer: string;
|
|
218
|
+
configUrl: string;
|
|
219
|
+
audience: string;
|
|
220
|
+
tokenUrl: string;
|
|
221
|
+
grantType: string;
|
|
222
|
+
scope: string;
|
|
223
|
+
admin?: {
|
|
224
|
+
clientId: string;
|
|
225
|
+
clientSecret: string;
|
|
226
|
+
} | undefined;
|
|
227
|
+
}, {
|
|
228
|
+
clientId: string;
|
|
229
|
+
type: "password";
|
|
230
|
+
identityProviderId: string;
|
|
231
|
+
issuer: string;
|
|
232
|
+
configUrl: string;
|
|
233
|
+
audience: string;
|
|
234
|
+
tokenUrl: string;
|
|
235
|
+
grantType: string;
|
|
236
|
+
scope: string;
|
|
237
|
+
admin?: {
|
|
238
|
+
clientId: string;
|
|
239
|
+
clientSecret: string;
|
|
240
|
+
} | undefined;
|
|
241
|
+
}>, z.ZodObject<{
|
|
242
|
+
identityProviderId: z.ZodString;
|
|
243
|
+
type: z.ZodLiteral<"implicit">;
|
|
244
|
+
issuer: z.ZodString;
|
|
245
|
+
configUrl: z.ZodString;
|
|
246
|
+
audience: z.ZodString;
|
|
247
|
+
scope: z.ZodString;
|
|
248
|
+
clientId: z.ZodString;
|
|
249
|
+
admin: z.ZodOptional<z.ZodObject<{
|
|
250
|
+
clientId: z.ZodString;
|
|
251
|
+
clientSecret: z.ZodString;
|
|
252
|
+
}, "strip", z.ZodTypeAny, {
|
|
253
|
+
clientId: string;
|
|
254
|
+
clientSecret: string;
|
|
255
|
+
}, {
|
|
256
|
+
clientId: string;
|
|
257
|
+
clientSecret: string;
|
|
258
|
+
}>>;
|
|
259
|
+
}, "strip", z.ZodTypeAny, {
|
|
260
|
+
clientId: string;
|
|
261
|
+
type: "implicit";
|
|
262
|
+
identityProviderId: string;
|
|
263
|
+
issuer: string;
|
|
264
|
+
configUrl: string;
|
|
265
|
+
audience: string;
|
|
266
|
+
scope: string;
|
|
267
|
+
admin?: {
|
|
268
|
+
clientId: string;
|
|
269
|
+
clientSecret: string;
|
|
270
|
+
} | undefined;
|
|
271
|
+
}, {
|
|
272
|
+
clientId: string;
|
|
273
|
+
type: "implicit";
|
|
274
|
+
identityProviderId: string;
|
|
275
|
+
issuer: string;
|
|
276
|
+
configUrl: string;
|
|
277
|
+
audience: string;
|
|
278
|
+
scope: string;
|
|
279
|
+
admin?: {
|
|
280
|
+
clientId: string;
|
|
281
|
+
clientSecret: string;
|
|
282
|
+
} | undefined;
|
|
283
|
+
}>, z.ZodObject<{
|
|
284
|
+
identityProviderId: z.ZodString;
|
|
285
|
+
type: z.ZodLiteral<"client_credentials">;
|
|
286
|
+
issuer: z.ZodString;
|
|
287
|
+
configUrl: z.ZodString;
|
|
288
|
+
audience: z.ZodString;
|
|
289
|
+
scope: z.ZodString;
|
|
290
|
+
clientId: z.ZodString;
|
|
291
|
+
clientSecret: z.ZodString;
|
|
292
|
+
admin: z.ZodOptional<z.ZodObject<{
|
|
293
|
+
clientId: z.ZodString;
|
|
294
|
+
clientSecret: z.ZodString;
|
|
295
|
+
}, "strip", z.ZodTypeAny, {
|
|
296
|
+
clientId: string;
|
|
297
|
+
clientSecret: string;
|
|
298
|
+
}, {
|
|
299
|
+
clientId: string;
|
|
300
|
+
clientSecret: string;
|
|
301
|
+
}>>;
|
|
302
|
+
}, "strip", z.ZodTypeAny, {
|
|
303
|
+
clientId: string;
|
|
304
|
+
clientSecret: string;
|
|
305
|
+
type: "client_credentials";
|
|
306
|
+
identityProviderId: string;
|
|
307
|
+
issuer: string;
|
|
308
|
+
configUrl: string;
|
|
309
|
+
audience: string;
|
|
310
|
+
scope: string;
|
|
311
|
+
admin?: {
|
|
312
|
+
clientId: string;
|
|
313
|
+
clientSecret: string;
|
|
314
|
+
} | undefined;
|
|
315
|
+
}, {
|
|
316
|
+
clientId: string;
|
|
317
|
+
clientSecret: string;
|
|
318
|
+
type: "client_credentials";
|
|
319
|
+
identityProviderId: string;
|
|
320
|
+
issuer: string;
|
|
321
|
+
configUrl: string;
|
|
322
|
+
audience: string;
|
|
323
|
+
scope: string;
|
|
324
|
+
admin?: {
|
|
325
|
+
clientId: string;
|
|
326
|
+
clientSecret: string;
|
|
327
|
+
} | undefined;
|
|
328
|
+
}>, z.ZodObject<{
|
|
329
|
+
identityProviderId: z.ZodString;
|
|
330
|
+
type: z.ZodLiteral<"self_signed">;
|
|
331
|
+
issuer: z.ZodString;
|
|
332
|
+
audience: z.ZodString;
|
|
333
|
+
scope: z.ZodString;
|
|
334
|
+
clientId: z.ZodString;
|
|
335
|
+
clientSecret: z.ZodString;
|
|
336
|
+
admin: z.ZodOptional<z.ZodObject<{
|
|
337
|
+
clientId: z.ZodString;
|
|
338
|
+
clientSecret: z.ZodString;
|
|
339
|
+
}, "strip", z.ZodTypeAny, {
|
|
340
|
+
clientId: string;
|
|
341
|
+
clientSecret: string;
|
|
342
|
+
}, {
|
|
343
|
+
clientId: string;
|
|
344
|
+
clientSecret: string;
|
|
345
|
+
}>>;
|
|
346
|
+
}, "strip", z.ZodTypeAny, {
|
|
347
|
+
clientId: string;
|
|
348
|
+
clientSecret: string;
|
|
349
|
+
type: "self_signed";
|
|
350
|
+
identityProviderId: string;
|
|
351
|
+
issuer: string;
|
|
352
|
+
audience: string;
|
|
353
|
+
scope: string;
|
|
354
|
+
admin?: {
|
|
355
|
+
clientId: string;
|
|
356
|
+
clientSecret: string;
|
|
357
|
+
} | undefined;
|
|
358
|
+
}, {
|
|
359
|
+
clientId: string;
|
|
360
|
+
clientSecret: string;
|
|
361
|
+
type: "self_signed";
|
|
362
|
+
identityProviderId: string;
|
|
363
|
+
issuer: string;
|
|
364
|
+
audience: string;
|
|
365
|
+
scope: string;
|
|
366
|
+
admin?: {
|
|
367
|
+
clientId: string;
|
|
368
|
+
clientSecret: string;
|
|
369
|
+
} | undefined;
|
|
370
|
+
}>]>;
|
|
371
|
+
export type Auth = z.infer<typeof authSchema>;
|
|
372
|
+
export type ImplicitAuth = z.infer<typeof implicitAuthSchema>;
|
|
373
|
+
export type PasswordAuth = z.infer<typeof passwordAuthSchema>;
|
|
374
|
+
export type Credentials = z.infer<typeof credentials>;
|
|
375
|
+
export type ClientCredentialAuth = z.infer<typeof clientCredentialAuthSchema>;
|
|
376
|
+
export type SelfSignedAuth = z.infer<typeof selfSignedAuthSchema>;
|
|
377
|
+
export {};
|
|
378
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../src/config/schema.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,QAAA,MAAM,WAAW;;;;;;;;;EAGf,CAAA;AAEF,QAAA,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAWtB,CAAA;AAEF,QAAA,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAStB,CAAA;AAEF,QAAA,MAAM,0BAA0B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAU9B,CAAA;AAEF,QAAA,MAAM,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EASxB,CAAA;AAEF,eAAO,MAAM,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAKrB,CAAA;AAEF,MAAM,MAAM,IAAI,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,UAAU,CAAC,CAAA;AAC7C,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAC7D,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAA;AAC7D,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,WAAW,CAAC,CAAA;AACrD,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,0BAA0B,CAAC,CAAA;AAC7E,MAAM,MAAM,cAAc,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,oBAAoB,CAAC,CAAA"}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var coreRpcErrors = require('@canton-network/core-rpc-errors');
|
|
4
|
+
var zod = require('zod');
|
|
5
|
+
var jose = require('jose');
|
|
6
|
+
|
|
7
|
+
// src/client-credentials-service.ts
|
|
8
|
+
var ClientCredentialsService = class {
|
|
9
|
+
constructor(configUrl, logger) {
|
|
10
|
+
this.configUrl = configUrl;
|
|
11
|
+
this.logger = logger;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Fetches the JWT token (M2M) using client credentials.
|
|
15
|
+
*
|
|
16
|
+
* @returns The JWT access token as a string.
|
|
17
|
+
* @throws If fetching the token fails or the response is invalid.
|
|
18
|
+
*/
|
|
19
|
+
async fetchToken(credentials2) {
|
|
20
|
+
try {
|
|
21
|
+
const oidcConfig = await this.getOIDCConfig(this.configUrl);
|
|
22
|
+
this.logger?.debug({ oidcConfig }, "Fetched OIDC config");
|
|
23
|
+
const res = await this.fetchTokenEndpoint(
|
|
24
|
+
oidcConfig.token_endpoint,
|
|
25
|
+
credentials2
|
|
26
|
+
);
|
|
27
|
+
const json = await res.json();
|
|
28
|
+
this.logger?.info(
|
|
29
|
+
{ response: json },
|
|
30
|
+
`Fetched admin token for clientId: ${credentials2.clientId}`
|
|
31
|
+
);
|
|
32
|
+
if (!json.access_token) {
|
|
33
|
+
throw new Error("No access_token in token endpoint response");
|
|
34
|
+
}
|
|
35
|
+
return json.access_token;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
this.logger?.error({ err: error }, "Failed to fetch admin token");
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async fetchTokenEndpoint(tokenEndpoint, credentials2) {
|
|
42
|
+
const params = new URLSearchParams({
|
|
43
|
+
grant_type: "client_credentials",
|
|
44
|
+
client_id: credentials2.clientId,
|
|
45
|
+
client_secret: credentials2.clientSecret,
|
|
46
|
+
scope: credentials2.scope ?? "",
|
|
47
|
+
audience: credentials2.audience ?? ""
|
|
48
|
+
});
|
|
49
|
+
const res = await fetch(tokenEndpoint, {
|
|
50
|
+
method: "POST",
|
|
51
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
52
|
+
body: params.toString()
|
|
53
|
+
});
|
|
54
|
+
if (!res.ok) {
|
|
55
|
+
this.logger?.error(
|
|
56
|
+
{ status: res.status, statusText: res.statusText },
|
|
57
|
+
"Token endpoint error"
|
|
58
|
+
);
|
|
59
|
+
throw new Error(
|
|
60
|
+
`Token endpoint error: ${res.status} ${res.statusText}`
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
return res;
|
|
64
|
+
}
|
|
65
|
+
async getOIDCConfig(url) {
|
|
66
|
+
const res = await fetch(url);
|
|
67
|
+
if (!res.ok) {
|
|
68
|
+
const text = await res.text();
|
|
69
|
+
this.logger?.error(
|
|
70
|
+
{ status: res.status, statusText: res.statusText, body: text },
|
|
71
|
+
"Failed to fetch OIDC config"
|
|
72
|
+
);
|
|
73
|
+
throw new Error(
|
|
74
|
+
`OIDC config error: ${res.status} ${res.statusText}`
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
return res.json();
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
var clientCredentialsService = (configUrl, logger) => ({
|
|
81
|
+
fetchToken: async (credentials2) => new ClientCredentialsService(configUrl, logger).fetchToken(credentials2)
|
|
82
|
+
});
|
|
83
|
+
function assertConnected(authContext) {
|
|
84
|
+
if (!authContext) {
|
|
85
|
+
throw coreRpcErrors.providerErrors.unauthorized({
|
|
86
|
+
message: "User is not connected"
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
return authContext;
|
|
90
|
+
}
|
|
91
|
+
var credentials = zod.z.object({
|
|
92
|
+
clientId: zod.z.string(),
|
|
93
|
+
clientSecret: zod.z.string()
|
|
94
|
+
});
|
|
95
|
+
var passwordAuthSchema = zod.z.object({
|
|
96
|
+
identityProviderId: zod.z.string(),
|
|
97
|
+
type: zod.z.literal("password"),
|
|
98
|
+
issuer: zod.z.string(),
|
|
99
|
+
configUrl: zod.z.string(),
|
|
100
|
+
audience: zod.z.string(),
|
|
101
|
+
tokenUrl: zod.z.string(),
|
|
102
|
+
grantType: zod.z.string(),
|
|
103
|
+
scope: zod.z.string(),
|
|
104
|
+
clientId: zod.z.string(),
|
|
105
|
+
admin: zod.z.optional(credentials)
|
|
106
|
+
});
|
|
107
|
+
var implicitAuthSchema = zod.z.object({
|
|
108
|
+
identityProviderId: zod.z.string(),
|
|
109
|
+
type: zod.z.literal("implicit"),
|
|
110
|
+
issuer: zod.z.string(),
|
|
111
|
+
configUrl: zod.z.string(),
|
|
112
|
+
audience: zod.z.string(),
|
|
113
|
+
scope: zod.z.string(),
|
|
114
|
+
clientId: zod.z.string(),
|
|
115
|
+
admin: zod.z.optional(credentials)
|
|
116
|
+
});
|
|
117
|
+
var clientCredentialAuthSchema = zod.z.object({
|
|
118
|
+
identityProviderId: zod.z.string(),
|
|
119
|
+
type: zod.z.literal("client_credentials"),
|
|
120
|
+
issuer: zod.z.string(),
|
|
121
|
+
configUrl: zod.z.string(),
|
|
122
|
+
audience: zod.z.string(),
|
|
123
|
+
scope: zod.z.string(),
|
|
124
|
+
clientId: zod.z.string(),
|
|
125
|
+
clientSecret: zod.z.string(),
|
|
126
|
+
admin: zod.z.optional(credentials)
|
|
127
|
+
});
|
|
128
|
+
var selfSignedAuthSchema = zod.z.object({
|
|
129
|
+
identityProviderId: zod.z.string(),
|
|
130
|
+
type: zod.z.literal("self_signed"),
|
|
131
|
+
issuer: zod.z.string(),
|
|
132
|
+
audience: zod.z.string(),
|
|
133
|
+
scope: zod.z.string(),
|
|
134
|
+
clientId: zod.z.string(),
|
|
135
|
+
clientSecret: zod.z.string(),
|
|
136
|
+
admin: zod.z.optional(credentials)
|
|
137
|
+
});
|
|
138
|
+
var authSchema = zod.z.discriminatedUnion("type", [
|
|
139
|
+
passwordAuthSchema,
|
|
140
|
+
implicitAuthSchema,
|
|
141
|
+
clientCredentialAuthSchema,
|
|
142
|
+
selfSignedAuthSchema
|
|
143
|
+
]);
|
|
144
|
+
var AuthTokenProviderSelfSigned = class _AuthTokenProviderSelfSigned {
|
|
145
|
+
constructor(auth, logger, expirySeconds = 3600) {
|
|
146
|
+
this.auth = auth;
|
|
147
|
+
this.logger = logger;
|
|
148
|
+
this.expirySeconds = expirySeconds;
|
|
149
|
+
}
|
|
150
|
+
async getUserAccessToken() {
|
|
151
|
+
this.logger.debug("Fetching self-signed user auth token");
|
|
152
|
+
return _AuthTokenProviderSelfSigned.fetchToken(
|
|
153
|
+
this.logger,
|
|
154
|
+
{
|
|
155
|
+
clientId: this.auth.clientId,
|
|
156
|
+
clientSecret: this.auth.clientSecret,
|
|
157
|
+
scope: this.auth.scope,
|
|
158
|
+
audience: this.auth.audience
|
|
159
|
+
},
|
|
160
|
+
this.auth.issuer,
|
|
161
|
+
this.expirySeconds
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
async getAdminAccessToken() {
|
|
165
|
+
this.logger.debug("Fetching self-signed admin auth token");
|
|
166
|
+
if (!this.auth.admin) {
|
|
167
|
+
throw new Error("Admin credentials are not configured");
|
|
168
|
+
}
|
|
169
|
+
return _AuthTokenProviderSelfSigned.fetchToken(
|
|
170
|
+
this.logger,
|
|
171
|
+
{
|
|
172
|
+
clientId: this.auth.admin.clientId,
|
|
173
|
+
clientSecret: this.auth.admin.clientSecret,
|
|
174
|
+
scope: this.auth.scope,
|
|
175
|
+
audience: this.auth.audience
|
|
176
|
+
},
|
|
177
|
+
this.auth.issuer,
|
|
178
|
+
this.expirySeconds
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
static async fetchToken(logger, credentials2, issuer, expirySeconds = 3600) {
|
|
182
|
+
const secret = new TextEncoder().encode(credentials2.clientSecret);
|
|
183
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
184
|
+
const jwt = await new jose.SignJWT({
|
|
185
|
+
sub: credentials2.clientId,
|
|
186
|
+
aud: credentials2.audience || "",
|
|
187
|
+
iat: now,
|
|
188
|
+
exp: now + expirySeconds,
|
|
189
|
+
iss: issuer
|
|
190
|
+
}).setProtectedHeader({ alg: "HS256" }).sign(secret);
|
|
191
|
+
logger.info(`Generated self-signed JWT token: ${jwt}`);
|
|
192
|
+
return jwt;
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// src/auth-token-provider.ts
|
|
197
|
+
var AuthTokenProvider = class {
|
|
198
|
+
constructor(auth, logger) {
|
|
199
|
+
this.auth = auth;
|
|
200
|
+
this.logger = logger;
|
|
201
|
+
}
|
|
202
|
+
async getUserAccessToken() {
|
|
203
|
+
this.logger.debug("Fetching user auth token");
|
|
204
|
+
if (this.auth.type === "self_signed")
|
|
205
|
+
return new AuthTokenProviderSelfSigned(
|
|
206
|
+
this.auth,
|
|
207
|
+
this.logger
|
|
208
|
+
).getUserAccessToken();
|
|
209
|
+
if (this.auth.type === "client_credentials")
|
|
210
|
+
return clientCredentialsService(
|
|
211
|
+
this.auth.configUrl,
|
|
212
|
+
this.logger
|
|
213
|
+
).fetchToken({
|
|
214
|
+
clientId: this.auth.clientId,
|
|
215
|
+
clientSecret: this.auth.clientSecret,
|
|
216
|
+
scope: this.auth.scope,
|
|
217
|
+
audience: this.auth.audience
|
|
218
|
+
});
|
|
219
|
+
throw new Error(
|
|
220
|
+
`Auth type ${this.auth.type} not supported for user access token`
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
async getAdminAccessToken() {
|
|
224
|
+
this.logger.debug("Fetching admin auth token");
|
|
225
|
+
if (this.auth.type === "self_signed")
|
|
226
|
+
return new AuthTokenProviderSelfSigned(
|
|
227
|
+
this.auth,
|
|
228
|
+
this.logger
|
|
229
|
+
).getAdminAccessToken();
|
|
230
|
+
if (!this.auth.admin) {
|
|
231
|
+
throw new Error(
|
|
232
|
+
`No admin credentials configured for auth type ${this.auth.type}`
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
return clientCredentialsService(
|
|
236
|
+
this.auth.configUrl,
|
|
237
|
+
this.logger
|
|
238
|
+
).fetchToken({
|
|
239
|
+
clientId: this.auth.admin.clientId,
|
|
240
|
+
clientSecret: this.auth.admin.clientSecret,
|
|
241
|
+
scope: this.auth.scope,
|
|
242
|
+
audience: this.auth.audience
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
exports.AuthTokenProvider = AuthTokenProvider;
|
|
248
|
+
exports.AuthTokenProviderSelfSigned = AuthTokenProviderSelfSigned;
|
|
249
|
+
exports.ClientCredentialsService = ClientCredentialsService;
|
|
250
|
+
exports.assertConnected = assertConnected;
|
|
251
|
+
exports.authSchema = authSchema;
|
|
252
|
+
exports.clientCredentialsService = clientCredentialsService;
|
|
253
|
+
//# sourceMappingURL=index.cjs.map
|
|
254
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client-credentials-service.ts","../src/auth-utils.ts","../src/config/schema.ts","../src/auth-token-provider-self-signed.ts","../src/auth-token-provider.ts"],"names":["credentials","providerErrors","z","SignJWT"],"mappings":";;;;;;;AAMO,IAAM,2BAAN,MAA+B;AAAA,EAClC,WAAA,CACY,WACA,MAAA,EACV;AAFU,IAAA,IAAA,CAAA,SAAA,GAAA,SAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQH,MAAM,WAAWA,YAAAA,EAAiD;AAC9D,IAAA,IAAI;AACA,MAAA,MAAM,UAAA,GAAa,MAAM,IAAA,CAAK,aAAA,CAAc,KAAK,SAAS,CAAA;AAC1D,MAAA,IAAA,CAAK,MAAA,EAAQ,KAAA,CAAM,EAAE,UAAA,IAAc,qBAAqB,CAAA;AAExD,MAAA,MAAM,GAAA,GAAgB,MAAM,IAAA,CAAK,kBAAA;AAAA,QAC7B,UAAA,CAAW,cAAA;AAAA,QACXA;AAAA,OACJ;AACA,MAAA,MAAM,IAAA,GAAO,MAAM,GAAA,CAAI,IAAA,EAAK;AAE5B,MAAA,IAAA,CAAK,MAAA,EAAQ,IAAA;AAAA,QACT,EAAE,UAAU,IAAA,EAAK;AAAA,QACjB,CAAA,kCAAA,EAAqCA,aAAY,QAAQ,CAAA;AAAA,OAC7D;AAEA,MAAA,IAAI,CAAC,KAAK,YAAA,EAAc;AACpB,QAAA,MAAM,IAAI,MAAM,4CAA4C,CAAA;AAAA,MAChE;AAEA,MAAA,OAAO,IAAA,CAAK,YAAA;AAAA,IAChB,SAAS,KAAA,EAAO;AACZ,MAAA,IAAA,CAAK,QAAQ,KAAA,CAAM,EAAE,GAAA,EAAK,K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CAAA;AAAA,OACnE;AAAA,IACJ;AACA,IAAA,OAAO,wBAAA;AAAA,MACH,KAAK,IAAA,CAAK,SAAA;AAAA,MACV,IAAA,CAAK;AAAA,MACP,UAAA,CAAW;AAAA,MACT,QAAA,EAAU,IAAA,CAAK,IAAA,CAAK,KAAA,CAAM,QAAA;AAAA,MAC1B,YAAA,EAAc,IAAA,CAAK,IAAA,CAAK,KAAA,CAAM,YAAA;AAAA,MAC9B,KAAA,EAAO,KAAK,IAAA,CAAK,KAAA;AAAA,MACjB,QAAA,EAAU,KAAK,IAAA,CAAK;AAAA,KACvB,CAAA;AAAA,EACL;AACJ","file":"index.cjs","sourcesContent":["// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Logger } from '@canton-network/core-types'\nimport { ClientCredentials, OIDCConfig } from './auth-service.js'\n\nexport class ClientCredentialsService {\n constructor(\n private configUrl: string,\n private logger: Logger | undefined\n ) {}\n\n /**\n * Fetches the JWT token (M2M) using client credentials.\n *\n * @returns The JWT access token as a string.\n * @throws If fetching the token fails or the response is invalid.\n */\n async fetchToken(credentials: ClientCredentials): Promise<string> {\n try {\n const oidcConfig = await this.getOIDCConfig(this.configUrl)\n this.logger?.debug({ oidcConfig }, 'Fetched OIDC config')\n\n const res: Response = await this.fetchTokenEndpoint(\n oidcConfig.token_endpoint,\n credentials\n )\n const json = await res.json()\n\n this.logger?.info(\n { response: json },\n `Fetched admin token for clientId: ${credentials.clientId}`\n )\n\n if (!json.access_token) {\n throw new Error('No access_token in token endpoint response')\n }\n\n return json.access_token\n } catch (error) {\n this.logger?.error({ err: error }, 'Failed to fetch admin token')\n throw error\n }\n }\n\n async fetchTokenEndpoint(\n tokenEndpoint: string,\n credentials: ClientCredentials\n ): Promise<Response> {\n const params = new URLSearchParams({\n grant_type: 'client_credentials',\n client_id: credentials.clientId,\n client_secret: credentials.clientSecret,\n scope: credentials.scope ?? '',\n audience: credentials.audience ?? '',\n })\n\n const res = await fetch(tokenEndpoint, {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: params.toString(),\n })\n\n if (!res.ok) {\n this.logger?.error(\n { status: res.status, statusText: res.statusText },\n 'Token endpoint error'\n )\n throw new Error(\n `Token endpoint error: ${res.status} ${res.statusText}`\n )\n }\n\n return res\n }\n\n async getOIDCConfig(url: string): Promise<OIDCConfig> {\n const res = await fetch(url)\n if (!res.ok) {\n const text = await res.text()\n this.logger?.error(\n { status: res.status, statusText: res.statusText, body: text },\n 'Failed to fetch OIDC config'\n )\n throw new Error(\n `OIDC config error: ${res.status} ${res.statusText}`\n )\n }\n return res.json()\n }\n}\n\nexport const clientCredentialsService = (\n configUrl: string,\n logger: Logger | undefined\n) => ({\n fetchToken: async (credentials: ClientCredentials) =>\n new ClientCredentialsService(configUrl, logger).fetchToken(credentials),\n})\n","// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { AuthContext } from './auth-service'\nimport { providerErrors } from '@canton-network/core-rpc-errors'\n\nexport function assertConnected(\n authContext: AuthContext | undefined\n): AuthContext {\n if (!authContext) {\n throw providerErrors.unauthorized({\n message: 'User is not connected',\n })\n }\n return authContext\n}\n","// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { z } from 'zod'\n\nconst credentials = z.object({\n clientId: z.string(),\n clientSecret: z.string(),\n})\n\nconst passwordAuthSchema = z.object({\n identityProviderId: z.string(),\n type: z.literal('password'),\n issuer: z.string(),\n configUrl: z.string(),\n audience: z.string(),\n tokenUrl: z.string(),\n grantType: z.string(),\n scope: z.string(),\n clientId: z.string(),\n admin: z.optional(credentials),\n})\n\nconst implicitAuthSchema = z.object({\n identityProviderId: z.string(),\n type: z.literal('implicit'),\n issuer: z.string(),\n configUrl: z.string(),\n audience: z.string(),\n scope: z.string(),\n clientId: z.string(),\n admin: z.optional(credentials),\n})\n\nconst clientCredentialAuthSchema = z.object({\n identityProviderId: z.string(),\n type: z.literal('client_credentials'),\n issuer: z.string(),\n configUrl: z.string(),\n audience: z.string(),\n scope: z.string(),\n clientId: z.string(),\n clientSecret: z.string(),\n admin: z.optional(credentials),\n})\n\nconst selfSignedAuthSchema = z.object({\n identityProviderId: z.string(),\n type: z.literal('self_signed'),\n issuer: z.string(),\n audience: z.string(),\n scope: z.string(),\n clientId: z.string(),\n clientSecret: z.string(),\n admin: z.optional(credentials),\n})\n\nexport const authSchema = z.discriminatedUnion('type', [\n passwordAuthSchema,\n implicitAuthSchema,\n clientCredentialAuthSchema,\n selfSignedAuthSchema,\n])\n\nexport type Auth = z.infer<typeof authSchema>\nexport type ImplicitAuth = z.infer<typeof implicitAuthSchema>\nexport type PasswordAuth = z.infer<typeof passwordAuthSchema>\nexport type Credentials = z.infer<typeof credentials>\nexport type ClientCredentialAuth = z.infer<typeof clientCredentialAuthSchema>\nexport type SelfSignedAuth = z.infer<typeof selfSignedAuthSchema>\n","// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Logger } from '@canton-network/core-types'\nimport { AccessTokenProvider, ClientCredentials } from './auth-service.js'\nimport { SelfSignedAuth } from './config/schema.js'\nimport { SignJWT } from 'jose'\n\nexport class AuthTokenProviderSelfSigned implements AccessTokenProvider {\n constructor(\n private auth: SelfSignedAuth,\n private logger: Logger,\n private expirySeconds: number = 3600\n ) {}\n\n async getUserAccessToken(): Promise<string> {\n this.logger.debug('Fetching self-signed user auth token')\n return AuthTokenProviderSelfSigned.fetchToken(\n this.logger,\n {\n clientId: this.auth.clientId,\n clientSecret: this.auth.clientSecret,\n scope: this.auth.scope,\n audience: this.auth.audience,\n },\n this.auth.issuer,\n this.expirySeconds\n )\n }\n\n async getAdminAccessToken(): Promise<string> {\n this.logger.debug('Fetching self-signed admin auth token')\n if (!this.auth.admin) {\n throw new Error('Admin credentials are not configured')\n }\n return AuthTokenProviderSelfSigned.fetchToken(\n this.logger,\n {\n clientId: this.auth.admin.clientId,\n clientSecret: this.auth.admin.clientSecret,\n scope: this.auth.scope,\n audience: this.auth.audience,\n },\n this.auth.issuer,\n this.expirySeconds\n )\n }\n\n static async fetchToken(\n logger: Logger,\n credentials: ClientCredentials,\n issuer: string,\n expirySeconds: number = 3600\n ): Promise<string> {\n const secret = new TextEncoder().encode(credentials.clientSecret)\n const now = Math.floor(Date.now() / 1000)\n const jwt = await new SignJWT({\n sub: credentials.clientId,\n aud: credentials.audience || '',\n iat: now,\n exp: now + expirySeconds,\n iss: issuer,\n })\n .setProtectedHeader({ alg: 'HS256' })\n .sign(secret)\n\n logger.info(`Generated self-signed JWT token: ${jwt}`)\n return jwt\n }\n}\n","// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Logger } from '@canton-network/core-types'\nimport { AccessTokenProvider } from './auth-service.js'\nimport { Auth } from './config/schema.js'\nimport { AuthTokenProviderSelfSigned } from './auth-token-provider-self-signed.js'\nimport { clientCredentialsService } from './client-credentials-service.js'\n\nexport class AuthTokenProvider implements AccessTokenProvider {\n constructor(\n private auth: Auth,\n private logger: Logger\n ) {}\n\n async getUserAccessToken(): Promise<string> {\n this.logger.debug('Fetching user auth token')\n if (this.auth.type === 'self_signed')\n return new AuthTokenProviderSelfSigned(\n this.auth,\n this.logger\n ).getUserAccessToken()\n\n if (this.auth.type === 'client_credentials')\n return clientCredentialsService(\n this.auth.configUrl,\n this.logger\n ).fetchToken({\n clientId: this.auth.clientId,\n clientSecret: this.auth.clientSecret,\n scope: this.auth.scope,\n audience: this.auth.audience,\n })\n\n throw new Error(\n `Auth type ${this.auth.type} not supported for user access token`\n )\n }\n\n async getAdminAccessToken(): Promise<string> {\n this.logger.debug('Fetching admin auth token')\n if (this.auth.type === 'self_signed')\n return new AuthTokenProviderSelfSigned(\n this.auth,\n this.logger\n ).getAdminAccessToken()\n\n if (!this.auth.admin) {\n throw new Error(\n `No admin credentials configured for auth type ${this.auth.type}`\n )\n }\n return clientCredentialsService(\n this.auth.configUrl,\n this.logger\n ).fetchToken({\n clientId: this.auth.admin.clientId,\n clientSecret: this.auth.admin.clientSecret,\n scope: this.auth.scope,\n audience: this.auth.audience,\n })\n }\n}\n"]}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
export * from './auth-service.js';
|
|
2
2
|
export * from './client-credentials-service.js';
|
|
3
3
|
export * from './auth-utils.js';
|
|
4
|
+
export * from './config/schema.js';
|
|
5
|
+
export * from './auth-token-provider.js';
|
|
6
|
+
export * from './auth-token-provider-self-signed.js';
|
|
4
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,mBAAmB,CAAA;AACjC,cAAc,iCAAiC,CAAA;AAC/C,cAAc,iBAAiB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,cAAc,mBAAmB,CAAA;AACjC,cAAc,iCAAiC,CAAA;AAC/C,cAAc,iBAAiB,CAAA;AAC/B,cAAc,oBAAoB,CAAA;AAClC,cAAc,0BAA0B,CAAA;AACxC,cAAc,sCAAsC,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,247 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { providerErrors } from '@canton-network/core-rpc-errors';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { SignJWT } from 'jose';
|
|
4
|
+
|
|
5
|
+
// src/client-credentials-service.ts
|
|
6
|
+
var ClientCredentialsService = class {
|
|
7
|
+
constructor(configUrl, logger) {
|
|
8
|
+
this.configUrl = configUrl;
|
|
9
|
+
this.logger = logger;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Fetches the JWT token (M2M) using client credentials.
|
|
13
|
+
*
|
|
14
|
+
* @returns The JWT access token as a string.
|
|
15
|
+
* @throws If fetching the token fails or the response is invalid.
|
|
16
|
+
*/
|
|
17
|
+
async fetchToken(credentials2) {
|
|
18
|
+
try {
|
|
19
|
+
const oidcConfig = await this.getOIDCConfig(this.configUrl);
|
|
20
|
+
this.logger?.debug({ oidcConfig }, "Fetched OIDC config");
|
|
21
|
+
const res = await this.fetchTokenEndpoint(
|
|
22
|
+
oidcConfig.token_endpoint,
|
|
23
|
+
credentials2
|
|
24
|
+
);
|
|
25
|
+
const json = await res.json();
|
|
26
|
+
this.logger?.info(
|
|
27
|
+
{ response: json },
|
|
28
|
+
`Fetched admin token for clientId: ${credentials2.clientId}`
|
|
29
|
+
);
|
|
30
|
+
if (!json.access_token) {
|
|
31
|
+
throw new Error("No access_token in token endpoint response");
|
|
32
|
+
}
|
|
33
|
+
return json.access_token;
|
|
34
|
+
} catch (error) {
|
|
35
|
+
this.logger?.error({ err: error }, "Failed to fetch admin token");
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async fetchTokenEndpoint(tokenEndpoint, credentials2) {
|
|
40
|
+
const params = new URLSearchParams({
|
|
41
|
+
grant_type: "client_credentials",
|
|
42
|
+
client_id: credentials2.clientId,
|
|
43
|
+
client_secret: credentials2.clientSecret,
|
|
44
|
+
scope: credentials2.scope ?? "",
|
|
45
|
+
audience: credentials2.audience ?? ""
|
|
46
|
+
});
|
|
47
|
+
const res = await fetch(tokenEndpoint, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
50
|
+
body: params.toString()
|
|
51
|
+
});
|
|
52
|
+
if (!res.ok) {
|
|
53
|
+
this.logger?.error(
|
|
54
|
+
{ status: res.status, statusText: res.statusText },
|
|
55
|
+
"Token endpoint error"
|
|
56
|
+
);
|
|
57
|
+
throw new Error(
|
|
58
|
+
`Token endpoint error: ${res.status} ${res.statusText}`
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
return res;
|
|
62
|
+
}
|
|
63
|
+
async getOIDCConfig(url) {
|
|
64
|
+
const res = await fetch(url);
|
|
65
|
+
if (!res.ok) {
|
|
66
|
+
const text = await res.text();
|
|
67
|
+
this.logger?.error(
|
|
68
|
+
{ status: res.status, statusText: res.statusText, body: text },
|
|
69
|
+
"Failed to fetch OIDC config"
|
|
70
|
+
);
|
|
71
|
+
throw new Error(
|
|
72
|
+
`OIDC config error: ${res.status} ${res.statusText}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return res.json();
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
var clientCredentialsService = (configUrl, logger) => ({
|
|
79
|
+
fetchToken: async (credentials2) => new ClientCredentialsService(configUrl, logger).fetchToken(credentials2)
|
|
80
|
+
});
|
|
81
|
+
function assertConnected(authContext) {
|
|
82
|
+
if (!authContext) {
|
|
83
|
+
throw providerErrors.unauthorized({
|
|
84
|
+
message: "User is not connected"
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
return authContext;
|
|
88
|
+
}
|
|
89
|
+
var credentials = z.object({
|
|
90
|
+
clientId: z.string(),
|
|
91
|
+
clientSecret: z.string()
|
|
92
|
+
});
|
|
93
|
+
var passwordAuthSchema = z.object({
|
|
94
|
+
identityProviderId: z.string(),
|
|
95
|
+
type: z.literal("password"),
|
|
96
|
+
issuer: z.string(),
|
|
97
|
+
configUrl: z.string(),
|
|
98
|
+
audience: z.string(),
|
|
99
|
+
tokenUrl: z.string(),
|
|
100
|
+
grantType: z.string(),
|
|
101
|
+
scope: z.string(),
|
|
102
|
+
clientId: z.string(),
|
|
103
|
+
admin: z.optional(credentials)
|
|
104
|
+
});
|
|
105
|
+
var implicitAuthSchema = z.object({
|
|
106
|
+
identityProviderId: z.string(),
|
|
107
|
+
type: z.literal("implicit"),
|
|
108
|
+
issuer: z.string(),
|
|
109
|
+
configUrl: z.string(),
|
|
110
|
+
audience: z.string(),
|
|
111
|
+
scope: z.string(),
|
|
112
|
+
clientId: z.string(),
|
|
113
|
+
admin: z.optional(credentials)
|
|
114
|
+
});
|
|
115
|
+
var clientCredentialAuthSchema = z.object({
|
|
116
|
+
identityProviderId: z.string(),
|
|
117
|
+
type: z.literal("client_credentials"),
|
|
118
|
+
issuer: z.string(),
|
|
119
|
+
configUrl: z.string(),
|
|
120
|
+
audience: z.string(),
|
|
121
|
+
scope: z.string(),
|
|
122
|
+
clientId: z.string(),
|
|
123
|
+
clientSecret: z.string(),
|
|
124
|
+
admin: z.optional(credentials)
|
|
125
|
+
});
|
|
126
|
+
var selfSignedAuthSchema = z.object({
|
|
127
|
+
identityProviderId: z.string(),
|
|
128
|
+
type: z.literal("self_signed"),
|
|
129
|
+
issuer: z.string(),
|
|
130
|
+
audience: z.string(),
|
|
131
|
+
scope: z.string(),
|
|
132
|
+
clientId: z.string(),
|
|
133
|
+
clientSecret: z.string(),
|
|
134
|
+
admin: z.optional(credentials)
|
|
135
|
+
});
|
|
136
|
+
var authSchema = z.discriminatedUnion("type", [
|
|
137
|
+
passwordAuthSchema,
|
|
138
|
+
implicitAuthSchema,
|
|
139
|
+
clientCredentialAuthSchema,
|
|
140
|
+
selfSignedAuthSchema
|
|
141
|
+
]);
|
|
142
|
+
var AuthTokenProviderSelfSigned = class _AuthTokenProviderSelfSigned {
|
|
143
|
+
constructor(auth, logger, expirySeconds = 3600) {
|
|
144
|
+
this.auth = auth;
|
|
145
|
+
this.logger = logger;
|
|
146
|
+
this.expirySeconds = expirySeconds;
|
|
147
|
+
}
|
|
148
|
+
async getUserAccessToken() {
|
|
149
|
+
this.logger.debug("Fetching self-signed user auth token");
|
|
150
|
+
return _AuthTokenProviderSelfSigned.fetchToken(
|
|
151
|
+
this.logger,
|
|
152
|
+
{
|
|
153
|
+
clientId: this.auth.clientId,
|
|
154
|
+
clientSecret: this.auth.clientSecret,
|
|
155
|
+
scope: this.auth.scope,
|
|
156
|
+
audience: this.auth.audience
|
|
157
|
+
},
|
|
158
|
+
this.auth.issuer,
|
|
159
|
+
this.expirySeconds
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
async getAdminAccessToken() {
|
|
163
|
+
this.logger.debug("Fetching self-signed admin auth token");
|
|
164
|
+
if (!this.auth.admin) {
|
|
165
|
+
throw new Error("Admin credentials are not configured");
|
|
166
|
+
}
|
|
167
|
+
return _AuthTokenProviderSelfSigned.fetchToken(
|
|
168
|
+
this.logger,
|
|
169
|
+
{
|
|
170
|
+
clientId: this.auth.admin.clientId,
|
|
171
|
+
clientSecret: this.auth.admin.clientSecret,
|
|
172
|
+
scope: this.auth.scope,
|
|
173
|
+
audience: this.auth.audience
|
|
174
|
+
},
|
|
175
|
+
this.auth.issuer,
|
|
176
|
+
this.expirySeconds
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
static async fetchToken(logger, credentials2, issuer, expirySeconds = 3600) {
|
|
180
|
+
const secret = new TextEncoder().encode(credentials2.clientSecret);
|
|
181
|
+
const now = Math.floor(Date.now() / 1e3);
|
|
182
|
+
const jwt = await new SignJWT({
|
|
183
|
+
sub: credentials2.clientId,
|
|
184
|
+
aud: credentials2.audience || "",
|
|
185
|
+
iat: now,
|
|
186
|
+
exp: now + expirySeconds,
|
|
187
|
+
iss: issuer
|
|
188
|
+
}).setProtectedHeader({ alg: "HS256" }).sign(secret);
|
|
189
|
+
logger.info(`Generated self-signed JWT token: ${jwt}`);
|
|
190
|
+
return jwt;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
// src/auth-token-provider.ts
|
|
195
|
+
var AuthTokenProvider = class {
|
|
196
|
+
constructor(auth, logger) {
|
|
197
|
+
this.auth = auth;
|
|
198
|
+
this.logger = logger;
|
|
199
|
+
}
|
|
200
|
+
async getUserAccessToken() {
|
|
201
|
+
this.logger.debug("Fetching user auth token");
|
|
202
|
+
if (this.auth.type === "self_signed")
|
|
203
|
+
return new AuthTokenProviderSelfSigned(
|
|
204
|
+
this.auth,
|
|
205
|
+
this.logger
|
|
206
|
+
).getUserAccessToken();
|
|
207
|
+
if (this.auth.type === "client_credentials")
|
|
208
|
+
return clientCredentialsService(
|
|
209
|
+
this.auth.configUrl,
|
|
210
|
+
this.logger
|
|
211
|
+
).fetchToken({
|
|
212
|
+
clientId: this.auth.clientId,
|
|
213
|
+
clientSecret: this.auth.clientSecret,
|
|
214
|
+
scope: this.auth.scope,
|
|
215
|
+
audience: this.auth.audience
|
|
216
|
+
});
|
|
217
|
+
throw new Error(
|
|
218
|
+
`Auth type ${this.auth.type} not supported for user access token`
|
|
219
|
+
);
|
|
220
|
+
}
|
|
221
|
+
async getAdminAccessToken() {
|
|
222
|
+
this.logger.debug("Fetching admin auth token");
|
|
223
|
+
if (this.auth.type === "self_signed")
|
|
224
|
+
return new AuthTokenProviderSelfSigned(
|
|
225
|
+
this.auth,
|
|
226
|
+
this.logger
|
|
227
|
+
).getAdminAccessToken();
|
|
228
|
+
if (!this.auth.admin) {
|
|
229
|
+
throw new Error(
|
|
230
|
+
`No admin credentials configured for auth type ${this.auth.type}`
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
return clientCredentialsService(
|
|
234
|
+
this.auth.configUrl,
|
|
235
|
+
this.logger
|
|
236
|
+
).fetchToken({
|
|
237
|
+
clientId: this.auth.admin.clientId,
|
|
238
|
+
clientSecret: this.auth.admin.clientSecret,
|
|
239
|
+
scope: this.auth.scope,
|
|
240
|
+
audience: this.auth.audience
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
export { AuthTokenProvider, AuthTokenProviderSelfSigned, ClientCredentialsService, assertConnected, authSchema, clientCredentialsService };
|
|
246
|
+
//# sourceMappingURL=index.js.map
|
|
247
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client-credentials-service.ts","../src/auth-utils.ts","../src/config/schema.ts","../src/auth-token-provider-self-signed.ts","../src/auth-token-provider.ts"],"names":["credentials"],"mappings":";;;;;AAMO,IAAM,2BAAN,MAA+B;AAAA,EAClC,WAAA,CACY,WACA,MAAA,EACV;AAFU,IAAA,IAAA,CAAA,SAAA,GAAA,SAAA;AACA,IAAA,IAAA,CAAA,MAAA,GAAA,MAAA;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQH,MAAM,WAAWA,YAAAA,EAAiD;AAC9D,IAAA,IAAI;AACA,MAAA,MAAM,UAAA,GAAa,MAAM,IAAA,CAAK,aAAA,CAAc,KAAK,SAAS,CAAA;AAC1D,MAAA,IAAA,CAAK,MAAA,EAAQ,KAAA,CAAM,EAAE,UAAA,IAAc,qBAAqB,CAAA;AAExD,MAAA,MAAM,GAAA,GAAgB,MAAM,IAAA,CAAK,kBAAA;AAAA,QAC7B,UAAA,CAAW,cAAA;AAAA,QACXA;AAAA,OACJ;AACA,MAAA,MAAM,IAAA,GAAO,MAAM,GAAA,CAAI,IAAA,EAAK;AAE5B,MAAA,IAAA,CAAK,MAAA,EAAQ,IAAA;AAAA,QACT,EAAE,UAAU,IAAA,EAAK;AAAA,QACjB,CAAA,kCAAA,EAAqCA,aAAY,QAAQ,CAAA;AAAA,OAC7D;AAEA,MAAA,IAAI,CAAC,KAAK,YAAA,EAAc;AACpB,QAAA,MAAM,IAAI,MAAM,4CAA4C,CAAA;AAAA,MAChE;AAEA,MAAA,OAAO,IAAA,CAAK,YAAA;AAAA,IAChB,SAAS,KAAA,EAAO;AACZ,MAAA,IAAA,CAAK,QAAQ,KAAA,CAAM,EAAE,GAAA,EAAK,KAAA,IAAS,6BAA6B,CAAA;AAChE,MAAA,MAAM,KAAA;AAAA,IACV;AAAA,EACJ;AAA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A,CAAW;AAAA,MACT,QAAA,EAAU,IAAA,CAAK,IAAA,CAAK,KAAA,CAAM,QAAA;AAAA,MAC1B,YAAA,EAAc,IAAA,CAAK,IAAA,CAAK,KAAA,CAAM,YAAA;AAAA,MAC9B,KAAA,EAAO,KAAK,IAAA,CAAK,KAAA;AAAA,MACjB,QAAA,EAAU,KAAK,IAAA,CAAK;AAAA,KACvB,CAAA;AAAA,EACL;AACJ","file":"index.js","sourcesContent":["// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Logger } from '@canton-network/core-types'\nimport { ClientCredentials, OIDCConfig } from './auth-service.js'\n\nexport class ClientCredentialsService {\n constructor(\n private configUrl: string,\n private logger: Logger | undefined\n ) {}\n\n /**\n * Fetches the JWT token (M2M) using client credentials.\n *\n * @returns The JWT access token as a string.\n * @throws If fetching the token fails or the response is invalid.\n */\n async fetchToken(credentials: ClientCredentials): Promise<string> {\n try {\n const oidcConfig = await this.getOIDCConfig(this.configUrl)\n this.logger?.debug({ oidcConfig }, 'Fetched OIDC config')\n\n const res: Response = await this.fetchTokenEndpoint(\n oidcConfig.token_endpoint,\n credentials\n )\n const json = await res.json()\n\n this.logger?.info(\n { response: json },\n `Fetched admin token for clientId: ${credentials.clientId}`\n )\n\n if (!json.access_token) {\n throw new Error('No access_token in token endpoint response')\n }\n\n return json.access_token\n } catch (error) {\n this.logger?.error({ err: error }, 'Failed to fetch admin token')\n throw error\n }\n }\n\n async fetchTokenEndpoint(\n tokenEndpoint: string,\n credentials: ClientCredentials\n ): Promise<Response> {\n const params = new URLSearchParams({\n grant_type: 'client_credentials',\n client_id: credentials.clientId,\n client_secret: credentials.clientSecret,\n scope: credentials.scope ?? '',\n audience: credentials.audience ?? '',\n })\n\n const res = await fetch(tokenEndpoint, {\n method: 'POST',\n headers: { 'Content-Type': 'application/x-www-form-urlencoded' },\n body: params.toString(),\n })\n\n if (!res.ok) {\n this.logger?.error(\n { status: res.status, statusText: res.statusText },\n 'Token endpoint error'\n )\n throw new Error(\n `Token endpoint error: ${res.status} ${res.statusText}`\n )\n }\n\n return res\n }\n\n async getOIDCConfig(url: string): Promise<OIDCConfig> {\n const res = await fetch(url)\n if (!res.ok) {\n const text = await res.text()\n this.logger?.error(\n { status: res.status, statusText: res.statusText, body: text },\n 'Failed to fetch OIDC config'\n )\n throw new Error(\n `OIDC config error: ${res.status} ${res.statusText}`\n )\n }\n return res.json()\n }\n}\n\nexport const clientCredentialsService = (\n configUrl: string,\n logger: Logger | undefined\n) => ({\n fetchToken: async (credentials: ClientCredentials) =>\n new ClientCredentialsService(configUrl, logger).fetchToken(credentials),\n})\n","// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { AuthContext } from './auth-service'\nimport { providerErrors } from '@canton-network/core-rpc-errors'\n\nexport function assertConnected(\n authContext: AuthContext | undefined\n): AuthContext {\n if (!authContext) {\n throw providerErrors.unauthorized({\n message: 'User is not connected',\n })\n }\n return authContext\n}\n","// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { z } from 'zod'\n\nconst credentials = z.object({\n clientId: z.string(),\n clientSecret: z.string(),\n})\n\nconst passwordAuthSchema = z.object({\n identityProviderId: z.string(),\n type: z.literal('password'),\n issuer: z.string(),\n configUrl: z.string(),\n audience: z.string(),\n tokenUrl: z.string(),\n grantType: z.string(),\n scope: z.string(),\n clientId: z.string(),\n admin: z.optional(credentials),\n})\n\nconst implicitAuthSchema = z.object({\n identityProviderId: z.string(),\n type: z.literal('implicit'),\n issuer: z.string(),\n configUrl: z.string(),\n audience: z.string(),\n scope: z.string(),\n clientId: z.string(),\n admin: z.optional(credentials),\n})\n\nconst clientCredentialAuthSchema = z.object({\n identityProviderId: z.string(),\n type: z.literal('client_credentials'),\n issuer: z.string(),\n configUrl: z.string(),\n audience: z.string(),\n scope: z.string(),\n clientId: z.string(),\n clientSecret: z.string(),\n admin: z.optional(credentials),\n})\n\nconst selfSignedAuthSchema = z.object({\n identityProviderId: z.string(),\n type: z.literal('self_signed'),\n issuer: z.string(),\n audience: z.string(),\n scope: z.string(),\n clientId: z.string(),\n clientSecret: z.string(),\n admin: z.optional(credentials),\n})\n\nexport const authSchema = z.discriminatedUnion('type', [\n passwordAuthSchema,\n implicitAuthSchema,\n clientCredentialAuthSchema,\n selfSignedAuthSchema,\n])\n\nexport type Auth = z.infer<typeof authSchema>\nexport type ImplicitAuth = z.infer<typeof implicitAuthSchema>\nexport type PasswordAuth = z.infer<typeof passwordAuthSchema>\nexport type Credentials = z.infer<typeof credentials>\nexport type ClientCredentialAuth = z.infer<typeof clientCredentialAuthSchema>\nexport type SelfSignedAuth = z.infer<typeof selfSignedAuthSchema>\n","// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Logger } from '@canton-network/core-types'\nimport { AccessTokenProvider, ClientCredentials } from './auth-service.js'\nimport { SelfSignedAuth } from './config/schema.js'\nimport { SignJWT } from 'jose'\n\nexport class AuthTokenProviderSelfSigned implements AccessTokenProvider {\n constructor(\n private auth: SelfSignedAuth,\n private logger: Logger,\n private expirySeconds: number = 3600\n ) {}\n\n async getUserAccessToken(): Promise<string> {\n this.logger.debug('Fetching self-signed user auth token')\n return AuthTokenProviderSelfSigned.fetchToken(\n this.logger,\n {\n clientId: this.auth.clientId,\n clientSecret: this.auth.clientSecret,\n scope: this.auth.scope,\n audience: this.auth.audience,\n },\n this.auth.issuer,\n this.expirySeconds\n )\n }\n\n async getAdminAccessToken(): Promise<string> {\n this.logger.debug('Fetching self-signed admin auth token')\n if (!this.auth.admin) {\n throw new Error('Admin credentials are not configured')\n }\n return AuthTokenProviderSelfSigned.fetchToken(\n this.logger,\n {\n clientId: this.auth.admin.clientId,\n clientSecret: this.auth.admin.clientSecret,\n scope: this.auth.scope,\n audience: this.auth.audience,\n },\n this.auth.issuer,\n this.expirySeconds\n )\n }\n\n static async fetchToken(\n logger: Logger,\n credentials: ClientCredentials,\n issuer: string,\n expirySeconds: number = 3600\n ): Promise<string> {\n const secret = new TextEncoder().encode(credentials.clientSecret)\n const now = Math.floor(Date.now() / 1000)\n const jwt = await new SignJWT({\n sub: credentials.clientId,\n aud: credentials.audience || '',\n iat: now,\n exp: now + expirySeconds,\n iss: issuer,\n })\n .setProtectedHeader({ alg: 'HS256' })\n .sign(secret)\n\n logger.info(`Generated self-signed JWT token: ${jwt}`)\n return jwt\n }\n}\n","// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { Logger } from '@canton-network/core-types'\nimport { AccessTokenProvider } from './auth-service.js'\nimport { Auth } from './config/schema.js'\nimport { AuthTokenProviderSelfSigned } from './auth-token-provider-self-signed.js'\nimport { clientCredentialsService } from './client-credentials-service.js'\n\nexport class AuthTokenProvider implements AccessTokenProvider {\n constructor(\n private auth: Auth,\n private logger: Logger\n ) {}\n\n async getUserAccessToken(): Promise<string> {\n this.logger.debug('Fetching user auth token')\n if (this.auth.type === 'self_signed')\n return new AuthTokenProviderSelfSigned(\n this.auth,\n this.logger\n ).getUserAccessToken()\n\n if (this.auth.type === 'client_credentials')\n return clientCredentialsService(\n this.auth.configUrl,\n this.logger\n ).fetchToken({\n clientId: this.auth.clientId,\n clientSecret: this.auth.clientSecret,\n scope: this.auth.scope,\n audience: this.auth.audience,\n })\n\n throw new Error(\n `Auth type ${this.auth.type} not supported for user access token`\n )\n }\n\n async getAdminAccessToken(): Promise<string> {\n this.logger.debug('Fetching admin auth token')\n if (this.auth.type === 'self_signed')\n return new AuthTokenProviderSelfSigned(\n this.auth,\n this.logger\n ).getAdminAccessToken()\n\n if (!this.auth.admin) {\n throw new Error(\n `No admin credentials configured for auth type ${this.auth.type}`\n )\n }\n return clientCredentialsService(\n this.auth.configUrl,\n this.logger\n ).fetchToken({\n clientId: this.auth.admin.clientId,\n clientSecret: this.auth.admin.clientSecret,\n scope: this.auth.scope,\n audience: this.auth.audience,\n })\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@canton-network/core-wallet-auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Provides authentication middleware and user management for the Wallet Gateway",
|
|
6
6
|
"repository": "github:hyperledger-labs/splice-wallet-kernel",
|
|
7
7
|
"license": "Apache-2.0",
|
|
8
8
|
"author": "Marc Juchli <marc.juchli@digitalasset.com>",
|
|
9
9
|
"packageManager": "yarn@4.9.4",
|
|
10
|
-
"main": "./dist/index.
|
|
10
|
+
"main": "./dist/index.cjs",
|
|
11
|
+
"module": "./dist/index.js",
|
|
11
12
|
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js",
|
|
17
|
+
"require": "./dist/index.cjs",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
12
21
|
"scripts": {
|
|
13
|
-
"build": "tsc -
|
|
14
|
-
"dev": "tsc -
|
|
22
|
+
"build": "tsup && tsc -p tsconfig.types.json",
|
|
23
|
+
"dev": "tsup --watch --onSuccess \"tsc -p tsconfig.types.json\"",
|
|
15
24
|
"flatpack": "yarn pack --out \"$FLATPACK_OUTDIR\"",
|
|
16
25
|
"clean": "tsc -b --clean; rm -rf dist",
|
|
17
|
-
"test": "
|
|
26
|
+
"test": "jest --passWithNoTests"
|
|
18
27
|
},
|
|
19
28
|
"devDependencies": {
|
|
20
29
|
"@jest/globals": "^29.0.0",
|
|
@@ -24,11 +33,14 @@
|
|
|
24
33
|
"jest": "^30.0.0",
|
|
25
34
|
"ts-jest": "^29.4.0",
|
|
26
35
|
"ts-jest-resolver": "^2.0.1",
|
|
36
|
+
"tsup": "^8.5.0",
|
|
27
37
|
"typescript": "^5.8.3"
|
|
28
38
|
},
|
|
29
39
|
"dependencies": {
|
|
30
|
-
"@canton-network/core-rpc-errors": "^0.
|
|
31
|
-
"@canton-network/core-types": "^0.
|
|
40
|
+
"@canton-network/core-rpc-errors": "^0.7.0",
|
|
41
|
+
"@canton-network/core-types": "^0.10.0",
|
|
42
|
+
"jose": "^5.10.0",
|
|
43
|
+
"zod": "^3.25.64"
|
|
32
44
|
},
|
|
33
45
|
"files": [
|
|
34
46
|
"dist/**"
|
package/dist/auth-service.js
DELETED
package/dist/auth-utils.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { providerErrors } from '@canton-network/core-rpc-errors';
|
|
4
|
-
export function assertConnected(authContext) {
|
|
5
|
-
if (!authContext) {
|
|
6
|
-
throw providerErrors.unauthorized({
|
|
7
|
-
message: 'User is not connected',
|
|
8
|
-
});
|
|
9
|
-
}
|
|
10
|
-
return authContext;
|
|
11
|
-
}
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
export class ClientCredentialsService {
|
|
4
|
-
configUrl;
|
|
5
|
-
logger;
|
|
6
|
-
constructor(configUrl, logger) {
|
|
7
|
-
this.configUrl = configUrl;
|
|
8
|
-
this.logger = logger;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Fetches the JWT token (M2M) using client credentials.
|
|
12
|
-
*
|
|
13
|
-
* @returns The JWT access token as a string.
|
|
14
|
-
* @throws If fetching the token fails or the response is invalid.
|
|
15
|
-
*/
|
|
16
|
-
async fetchToken(credentials) {
|
|
17
|
-
try {
|
|
18
|
-
const oidcConfig = await this.getOIDCConfig(this.configUrl);
|
|
19
|
-
this.logger?.debug({ oidcConfig }, 'Fetched OIDC config');
|
|
20
|
-
const res = await this.fetchTokenEndpoint(oidcConfig.token_endpoint, credentials);
|
|
21
|
-
const json = await res.json();
|
|
22
|
-
this.logger?.info({ response: json }, `Fetched admin token for clientId: ${credentials.clientId}`);
|
|
23
|
-
if (!json.access_token) {
|
|
24
|
-
throw new Error('No access_token in token endpoint response');
|
|
25
|
-
}
|
|
26
|
-
return json.access_token;
|
|
27
|
-
}
|
|
28
|
-
catch (error) {
|
|
29
|
-
this.logger?.error({ err: error }, 'Failed to fetch admin token');
|
|
30
|
-
throw error;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
async fetchTokenEndpoint(tokenEndpoint, credentials) {
|
|
34
|
-
const params = new URLSearchParams({
|
|
35
|
-
grant_type: 'client_credentials',
|
|
36
|
-
client_id: credentials.clientId,
|
|
37
|
-
client_secret: credentials.clientSecret,
|
|
38
|
-
scope: credentials.scope ?? '',
|
|
39
|
-
audience: credentials.audience ?? '',
|
|
40
|
-
});
|
|
41
|
-
const res = await fetch(tokenEndpoint, {
|
|
42
|
-
method: 'POST',
|
|
43
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
44
|
-
body: params.toString(),
|
|
45
|
-
});
|
|
46
|
-
if (!res.ok) {
|
|
47
|
-
this.logger?.error({ status: res.status, statusText: res.statusText }, 'Token endpoint error');
|
|
48
|
-
throw new Error(`Token endpoint error: ${res.status} ${res.statusText}`);
|
|
49
|
-
}
|
|
50
|
-
return res;
|
|
51
|
-
}
|
|
52
|
-
async getOIDCConfig(url) {
|
|
53
|
-
const res = await fetch(url);
|
|
54
|
-
if (!res.ok) {
|
|
55
|
-
const text = await res.text();
|
|
56
|
-
this.logger?.error({ status: res.status, statusText: res.statusText, body: text }, 'Failed to fetch OIDC config');
|
|
57
|
-
throw new Error(`OIDC config error: ${res.status} ${res.statusText}`);
|
|
58
|
-
}
|
|
59
|
-
return res.json();
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
export const clientCredentialsService = (configUrl, logger) => ({
|
|
63
|
-
fetchToken: async (credentials) => new ClientCredentialsService(configUrl, logger).fetchToken(credentials),
|
|
64
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client-credentials.service.test.d.ts","sourceRoot":"","sources":["../src/client-credentials.service.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2025 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { jest } from '@jest/globals';
|
|
4
|
-
import { ClientCredentialsService, } from './client-credentials-service.js';
|
|
5
|
-
describe('ClientCredentialsService', () => {
|
|
6
|
-
const configUrl = 'http://idp/.well-known/openid-configuration';
|
|
7
|
-
const credentials = {
|
|
8
|
-
audience: 'aud',
|
|
9
|
-
scope: 'scope',
|
|
10
|
-
clientId: 'cid',
|
|
11
|
-
clientSecret: 'secret',
|
|
12
|
-
};
|
|
13
|
-
let service;
|
|
14
|
-
let getOIDCConfigSpy;
|
|
15
|
-
let fetchTokenEndpointSpy;
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
service = new ClientCredentialsService(configUrl, undefined);
|
|
18
|
-
getOIDCConfigSpy = jest.spyOn(service, 'getOIDCConfig');
|
|
19
|
-
fetchTokenEndpointSpy = jest.spyOn(service, 'fetchTokenEndpoint');
|
|
20
|
-
});
|
|
21
|
-
it('returns access_token on success', async () => {
|
|
22
|
-
getOIDCConfigSpy.mockResolvedValue({
|
|
23
|
-
token_endpoint: 'http://idp/token',
|
|
24
|
-
});
|
|
25
|
-
fetchTokenEndpointSpy.mockResolvedValue({
|
|
26
|
-
ok: true,
|
|
27
|
-
json: jest
|
|
28
|
-
.fn()
|
|
29
|
-
.mockResolvedValue({ access_token: 'jwt' }),
|
|
30
|
-
});
|
|
31
|
-
const token = await service.fetchToken(credentials);
|
|
32
|
-
expect(token).toBe('jwt');
|
|
33
|
-
});
|
|
34
|
-
it('throws if OIDC config fetch fails', async () => {
|
|
35
|
-
getOIDCConfigSpy.mockRejectedValue(new Error('config fail'));
|
|
36
|
-
await expect(service.fetchToken(credentials)).rejects.toThrow('config fail');
|
|
37
|
-
});
|
|
38
|
-
it('throws if token endpoint fetch fails', async () => {
|
|
39
|
-
getOIDCConfigSpy.mockResolvedValue({
|
|
40
|
-
token_endpoint: 'http://idp/token',
|
|
41
|
-
});
|
|
42
|
-
fetchTokenEndpointSpy.mockRejectedValue(new Error('token fail'));
|
|
43
|
-
await expect(service.fetchToken(credentials)).rejects.toThrow('token fail');
|
|
44
|
-
});
|
|
45
|
-
it('throws if access_token missing', async () => {
|
|
46
|
-
getOIDCConfigSpy.mockResolvedValue({
|
|
47
|
-
token_endpoint: 'http://idp/token',
|
|
48
|
-
});
|
|
49
|
-
fetchTokenEndpointSpy.mockResolvedValue({
|
|
50
|
-
ok: true,
|
|
51
|
-
json: jest.fn().mockResolvedValue({}),
|
|
52
|
-
});
|
|
53
|
-
await expect(service.fetchToken(credentials)).rejects.toThrow('No access_token in token endpoint response');
|
|
54
|
-
});
|
|
55
|
-
});
|