@icure/api 8.0.34 → 8.0.35
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/icc-x-api/auth/EnsembleAuthService.d.ts +4 -0
- package/icc-x-api/auth/EnsembleAuthService.js +1 -0
- package/icc-x-api/auth/EnsembleAuthService.js.map +1 -1
- package/package.json +1 -1
- package/test/icc-x-api/auth/jwt-provider-test.js +59 -0
- package/test/icc-x-api/auth/jwt-provider-test.js.map +1 -1
|
@@ -12,6 +12,10 @@ export declare class EnsembleAuthService implements AuthService {
|
|
|
12
12
|
private currentState;
|
|
13
13
|
private error;
|
|
14
14
|
private stateMap;
|
|
15
|
+
readonly jwtGetter: () => Promise<{
|
|
16
|
+
token: string;
|
|
17
|
+
refreshToken: string;
|
|
18
|
+
} | undefined>;
|
|
15
19
|
constructor(jwtAuth: JwtBridgedAuthService | JwtAuthService | null, sessionAuth: NoAuthService, basicAuth: BasicAuthService);
|
|
16
20
|
getIcureTokens(): Promise<{
|
|
17
21
|
token: string;
|
|
@@ -18,6 +18,7 @@ class EnsembleAuthService {
|
|
|
18
18
|
this.sessionAuth = sessionAuth;
|
|
19
19
|
this.basicAuth = basicAuth;
|
|
20
20
|
this.error = null;
|
|
21
|
+
this.jwtGetter = this.getIcureTokens;
|
|
21
22
|
this.stateMap = {
|
|
22
23
|
start: { state: null, next: 'jwt' },
|
|
23
24
|
jwt: { state: this.jwtAuth, next: 'session' },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EnsembleAuthService.js","sourceRoot":"","sources":["../../../icc-x-api/auth/EnsembleAuthService.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,+CAA2C;AAK3C,IAAO,QAAQ,GAAG,SAAG,CAAC,QAAQ,CAAA;AAG9B,MAAa,mBAAmB;
|
|
1
|
+
{"version":3,"file":"EnsembleAuthService.js","sourceRoot":"","sources":["../../../icc-x-api/auth/EnsembleAuthService.ts"],"names":[],"mappings":";;;;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AAC,KAAK;oBAC7C,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,KAAM,CAAC,cAAc,CAAC,0BAA0B,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE;wBAC7F,IAAI,CAAC,YAAY,QAAQ,IAAI,CAAC,CAAC,UAAU,KAAK,GAAG,EAAE;4BACjD,MAAM,CAAC,CAAA;yBACR;wBACD,sFAAsF;wBACtF,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAA;wBACvB,IAAI,CAAC,KAAK,GAAG,CAAC,CAAA;wBAEd,OAAO,EAAE,CAAA;oBACX,CAAC,CAAC;oBACJ,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;aACxB;iBAAM,IAAI,CAAC,CAAC,IAAI,CAAC,KAAK,EAAE;gBACvB,oEAAoE;gBACpE,MAAM,IAAI,CAAC,KAAK,CAAA;aACjB;iBAAM;gBACL,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAA;aACvC;QACH,CAAC;KAAA;IAED,gBAAgB,CAAC,KAAY;;QAC3B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAA;QAClB,IAAI,CAAC,CAAC,IAAI,CAAC,YAAY,EAAE;YACvB,MAAA,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,KAAK,0CAAE,gBAAgB,CAAC,KAAK,CAAC,CAAA;SAChE;IACH,CAAC;CACF;AAxDD,kDAwDC","sourcesContent":["import { AuthService } from './AuthService'\nimport { XHR } from '../../icc-api/api/XHR'\nimport { JwtBridgedAuthService } from './JwtBridgedAuthService'\nimport { NoAuthService } from './NoAuthService'\nimport { BasicAuthService } from './BasicAuthService'\nimport Header = XHR.Header\nimport XHRError = XHR.XHRError\nimport { JwtAuthService } from './JwtAuthService'\n\nexport class EnsembleAuthService implements AuthService {\n private currentState: string | null\n private error: Error | null = null\n private stateMap: { [key: string]: { state: AuthService | null; next: string | null } }\n readonly jwtGetter = this.getIcureTokens\n\n constructor(\n private readonly jwtAuth: JwtBridgedAuthService | JwtAuthService | null,\n private readonly sessionAuth: NoAuthService,\n private readonly basicAuth: BasicAuthService\n ) {\n this.stateMap = {\n start: { state: null, next: 'jwt' },\n jwt: { state: this.jwtAuth, next: 'session' },\n session: { state: this.sessionAuth, next: 'basic' },\n basic: { state: this.basicAuth, next: null },\n }\n this.currentState = 'start'\n }\n\n getIcureTokens(): Promise<{ token: string; refreshToken: string } | undefined> {\n return this.jwtAuth?.getIcureTokens() ?? (Promise.resolve() as Promise<undefined>)\n }\n\n async getAuthHeaders(minimumAuthenticationClass: number | undefined): Promise<Array<Header>> {\n this.currentState = !!this.currentState ? this.stateMap[this.currentState].next : this.currentState\n\n if (!!this.currentState) {\n // If I have a state I return the headers, otherwise I return empty headers\n // And delegate the responsibility of handling the error to the next call\n return !!this.stateMap[this.currentState].state\n ? this.stateMap[this.currentState].state!.getAuthHeaders(minimumAuthenticationClass).catch((e) => {\n if (e instanceof XHRError && e.statusCode === 417) {\n throw e\n }\n // If I get an error, it is also responsibility of the next call, but I give a warning\n console.warn(e.message)\n this.error = e\n\n return []\n })\n : Promise.resolve([])\n } else if (!!this.error) {\n // If I have no more states, I throw the last error or a default one\n throw this.error\n } else {\n throw new Error('Cannot authenticate')\n }\n }\n\n invalidateHeader(error: Error): void {\n this.error = error\n if (!!this.currentState) {\n this.stateMap[this.currentState].state?.invalidateHeader(error)\n }\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -18,6 +18,10 @@ const JwtAuthService_1 = require("../../../icc-x-api/auth/JwtAuthService");
|
|
|
18
18
|
const XHR_1 = require("../../../icc-api/api/XHR");
|
|
19
19
|
var XHRError = XHR_1.XHR.XHRError;
|
|
20
20
|
const JwtError_1 = require("../../../icc-x-api/auth/JwtError");
|
|
21
|
+
const BasicAuthService_1 = require("../../../icc-x-api/auth/BasicAuthService");
|
|
22
|
+
const EnsembleAuthService_1 = require("../../../icc-x-api/auth/EnsembleAuthService");
|
|
23
|
+
const NoAuthService_1 = require("../../../icc-x-api/auth/NoAuthService");
|
|
24
|
+
const SmartAuthProvider_1 = require("../../../icc-x-api/auth/SmartAuthProvider");
|
|
21
25
|
describe('JWT provider resiliency test', () => {
|
|
22
26
|
function generateJwt() {
|
|
23
27
|
return `jwt.${(0, ModelHelper_1.b2a)(JSON.stringify({ exp: 1000 }))}.${Math.floor(100000 + Math.random() * 900000)}`;
|
|
@@ -134,5 +138,60 @@ describe('JWT provider resiliency test', () => {
|
|
|
134
138
|
(0, chai_1.expect)(secondHeader[0].data).to.be.equal(`Bearer ${secondJwt}`);
|
|
135
139
|
(0, chai_1.expect)(secondJwt).not.to.be.equal(initialJwt);
|
|
136
140
|
}));
|
|
141
|
+
it('A BasicAuthService does not have a jwtGetter property', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
142
|
+
const service = new BasicAuthService_1.BasicAuthService('username', 'password');
|
|
143
|
+
(0, chai_1.expect)(service.jwtGetter).to.be.undefined;
|
|
144
|
+
}));
|
|
145
|
+
it('A NoAuthService does not have a jwtGetter property', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
146
|
+
const service = new NoAuthService_1.NoAuthService();
|
|
147
|
+
(0, chai_1.expect)(service.jwtGetter).to.be.undefined;
|
|
148
|
+
}));
|
|
149
|
+
it('A JwtBridgedAuthService supports the jwtGetter', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
150
|
+
const fakeAuthApi = new FakeAuthApi();
|
|
151
|
+
const authService = new JwtBridgedAuthService_1.JwtBridgedAuthService(fakeAuthApi, 'username', 'password', {}, undefined);
|
|
152
|
+
(0, chai_1.expect)(authService.jwtGetter).not.to.be.undefined;
|
|
153
|
+
if (!!authService.jwtGetter) {
|
|
154
|
+
const token = yield authService.jwtGetter();
|
|
155
|
+
(0, chai_1.expect)(token === null || token === void 0 ? void 0 : token.token).to.be.equal(fakeAuthApi.jwt);
|
|
156
|
+
}
|
|
157
|
+
}));
|
|
158
|
+
it('A JwtAuthService supports the jwtGetter', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
159
|
+
const initialJwt = generateJwt();
|
|
160
|
+
const initialRefresh = generateRefreshJwt();
|
|
161
|
+
const fakeAuthApi = new FakeAuthApi();
|
|
162
|
+
const jwtService = new JwtAuthService_1.JwtAuthService(fakeAuthApi, { authJwt: initialJwt, refreshJwt: initialRefresh });
|
|
163
|
+
(0, chai_1.expect)(jwtService.jwtGetter).not.to.be.undefined;
|
|
164
|
+
if (!!jwtService.jwtGetter) {
|
|
165
|
+
const token = yield jwtService.jwtGetter();
|
|
166
|
+
(0, chai_1.expect)(token === null || token === void 0 ? void 0 : token.token).to.be.equal(initialJwt);
|
|
167
|
+
}
|
|
168
|
+
}));
|
|
169
|
+
it('An EnsembleAuthService supports the jwtGetter', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
170
|
+
const initialJwt = generateJwt();
|
|
171
|
+
const initialRefresh = generateRefreshJwt();
|
|
172
|
+
const fakeAuthApi = new FakeAuthApi();
|
|
173
|
+
const basic = new BasicAuthService_1.BasicAuthService('username', 'password');
|
|
174
|
+
const jwtService = new JwtAuthService_1.JwtAuthService(fakeAuthApi, { authJwt: initialJwt, refreshJwt: initialRefresh });
|
|
175
|
+
const service = new EnsembleAuthService_1.EnsembleAuthService(jwtService, new NoAuthService_1.NoAuthService(), basic);
|
|
176
|
+
(0, chai_1.expect)(service.jwtGetter).not.to.be.undefined;
|
|
177
|
+
if (!!service.jwtGetter) {
|
|
178
|
+
const token = yield service.jwtGetter();
|
|
179
|
+
(0, chai_1.expect)(token === null || token === void 0 ? void 0 : token.token).to.be.equal(initialJwt);
|
|
180
|
+
}
|
|
181
|
+
}));
|
|
182
|
+
it('A SmartAuthService supports the jwtGetter', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
183
|
+
const authApi = new FakeAuthApi();
|
|
184
|
+
const authProvider = SmartAuthProvider_1.SmartAuthProvider.initialise(authApi, 'username', {
|
|
185
|
+
getSecret: (acceptedSecrets, previousAttempts) => __awaiter(void 0, void 0, void 0, function* () {
|
|
186
|
+
return { value: 'wrong', secretType: SmartAuthProvider_1.AuthSecretType.LONG_LIVED_TOKEN }; // pragma: allowlist secret
|
|
187
|
+
}),
|
|
188
|
+
});
|
|
189
|
+
const service = authProvider.getAuthService();
|
|
190
|
+
(0, chai_1.expect)(service.jwtGetter).not.to.be.undefined;
|
|
191
|
+
if (!!service.jwtGetter) {
|
|
192
|
+
const token = yield service.jwtGetter();
|
|
193
|
+
(0, chai_1.expect)(token === null || token === void 0 ? void 0 : token.token).to.be.equal(authApi.jwt);
|
|
194
|
+
}
|
|
195
|
+
}));
|
|
137
196
|
});
|
|
138
197
|
//# sourceMappingURL=jwt-provider-test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"jwt-provider-test.js","sourceRoot":"","sources":["../../../../test/icc-x-api/auth/jwt-provider-test.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,8CAA6C;AAE7C,0FAAsF;AACtF,oEAAwD;AACxD,+BAA6B;AAC7B,yFAAqF;AAErF,2EAAuE;AACvE,kDAA8C;AAC9C,IAAO,QAAQ,GAAG,SAAG,CAAC,QAAQ,CAAA;AAC9B,+DAA2D;AAE3D,QAAQ,CAAC,8BAA8B,EAAE,GAAG,EAAE;IAC5C,SAAS,WAAW;QAClB,OAAO,OAAO,IAAA,iBAAG,EAAC,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,GAAG,MAAM,CAAC,EAAE,CAAA;IACnG,CAAC;IAED,SAAS,kBAAkB;QACzB,OAAO,cAAc,IAAA,iBAAG,EAAC,IAAI,CAAC,SAAS,CAAC,EAAE,GAAG,EAAE,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CA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,EAAE,cAAc,EAAE,CAAC,CAAA;QAExG,WAAW,CAAC,gBAAgB,GAAG,KAAK,CAAA;QACpC,MAAM,uBAAuB,CAAC,WAAW,CAAC,CAAA;QAE1C,WAAW,CAAC,gBAAgB,GAAG,IAAI,CAAA;QACnC,MAAM,YAAY,GAAG,MAAM,WAAW,CAAC,cAAc,EAAE,CAAA;QACvD,MAAM,SAAS,GAAG,WAAW,CAAC,GAAG,CAAA;QACjC,IAAA,aAAM,EAAC,YAAY,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,UAAU,SAAS,EAAE,CAAC,CAAA;QAC/D,IAAA,aAAM,EAAC,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;IAC/C,CAAC,CAAA,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { IccAuthApi } from '../../../icc-api'\nimport { LoginCredentials } from '../../../icc-api/model/LoginCredentials'\nimport { AuthenticationResponse } from '../../../icc-api/model/AuthenticationResponse'\nimport { b2a } from '../../../icc-api/model/ModelHelper'\nimport { expect } from 'chai'\nimport { JwtBridgedAuthService } from '../../../icc-x-api/auth/JwtBridgedAuthService'\nimport { AuthService } from '../../../icc-x-api/auth/AuthService'\nimport { JwtAuthService } from '../../../icc-x-api/auth/JwtAuthService'\nimport { XHR } from '../../../icc-api/api/XHR'\nimport XHRError = XHR.XHRError\nimport { JwtError } from '../../../icc-x-api/auth/JwtError'\n\ndescribe('JWT provider resiliency test', () => {\n function generateJwt() {\n return `jwt.${b2a(JSON.stringify({ exp: 1000 }))}.${Math.floor(100000 + Math.random() * 900000)}`\n }\n\n function generateRefreshJwt() {\n return `refreshJwt.${b2a(JSON.stringify({ exp: new Date().getTime() * 10 }))}.${Math.floor(100000 + Math.random() * 900000)}`\n }\n\n class FakeAuthApi extends IccAuthApi {\n refreshException: Error | undefined | null\n loginException: Error | undefined | null\n jwt = `jwt.${b2a(JSON.stringify({ exp: 1000 }))}.signature`\n refreshJwt = `refreshJwt.${b2a(JSON.stringify({ exp: new Date().getTime() * 10 }))}.signature`\n\n constructor() {\n super('', {})\n }\n\n login(body?: LoginCredentials): Promise<AuthenticationResponse> {\n if (!!this.loginException) {\n throw this.loginException\n }\n\n this.jwt = generateJwt()\n this.refreshJwt = generateRefreshJwt()\n return Promise.resolve(\n new AuthenticationResponse({\n token: this.jwt,\n refreshToken: this.refreshJwt,\n })\n )\n }\n\n refreshAuthenticationJWT(refreshJWT: string): Promise<AuthenticationResponse> {\n if (!!this.refreshException) {\n throw this.refreshException\n }\n\n this.jwt = generateJwt()\n return Promise.resolve(\n new AuthenticationResponse({\n token: this.jwt,\n refreshToken: this.refreshJwt ?? generateRefreshJwt(),\n })\n )\n }\n }\n\n async function expectThrowOnGetHeaders(service: AuthService): Promise<void> {\n let caught: Error | null = null\n try {\n await service.getAuthHeaders()\n } catch (e) {\n caught = e as Error\n }\n expect(caught).not.to.be.null\n expect(caught).to.be.instanceof(JwtError)\n }\n\n it('JwtBridgedAuthService - If the login fails for any type of error it is possible to retry', async () => {\n const errors: Error[] = [\n new Error('A generic error'),\n new XHRError('', '', 403, 'FORBIDDEN', new Headers()),\n new XHRError('', '', 503, 'TIMEOUT', new Headers()),\n ]\n for (const e of errors) {\n const fakeAuthApi = new FakeAuthApi()\n const authService = new JwtBridgedAuthService(fakeAuthApi, 'username', 'password', {}, undefined)\n\n fakeAuthApi.loginException = e\n await expectThrowOnGetHeaders(authService)\n\n fakeAuthApi.loginException = null\n const firstHeader = await authService.getAuthHeaders()\n expect(firstHeader[0].data).to.be.equal(`Bearer ${fakeAuthApi.jwt}`)\n }\n })\n\n async function jwtBridgedTest(error: Error, loginAfterError: boolean) {\n const fakeAuthApi = new FakeAuthApi()\n const authService = new JwtBridgedAuthService(fakeAuthApi, 'username', 'password', {}, undefined)\n\n fakeAuthApi.refreshException = null\n const firstHeader = await authService.getAuthHeaders()\n const initialJwt = fakeAuthApi.jwt\n const initialRefresh = fakeAuthApi.refreshJwt\n expect(firstHeader[0].data).to.be.equal(`Bearer ${initialJwt}`)\n\n fakeAuthApi.refreshException = error\n await expectThrowOnGetHeaders(authService)\n\n fakeAuthApi.refreshException = null\n const secondHeader = await authService.getAuthHeaders()\n const secondJwt = fakeAuthApi.jwt\n const secondRefresh = fakeAuthApi.refreshJwt\n expect(secondHeader[0].data).to.be.equal(`Bearer ${secondJwt}`)\n expect(secondJwt).not.to.be.equal(initialJwt)\n if (loginAfterError) {\n expect(secondRefresh).not.to.be.equal(initialRefresh)\n } else {\n expect(secondRefresh).to.be.equal(initialRefresh)\n }\n }\n\n it('JwtBridgedAuthService - If the refresh fails for a generic error, it is possible to retry', async () => {\n const error = new Error('A generic error')\n await jwtBridgedTest(error, false)\n })\n\n it('JwtBridgedAuthService - If the refresh fails for a 500 error, the login is performed again', async () => {\n const error = new XHRError('', '', 500, 'INTERNAL SERVER ERROR', new Headers())\n await jwtBridgedTest(error, false)\n })\n\n it('JwtBridgedAuthService - If the refresh fails for a 400 error, the login is performed again', async () => {\n const error = new XHRError('', '', 403, 'FORBIDDEN', new Headers())\n await jwtBridgedTest(error, true)\n })\n\n it('JwtAuthService - If the refresh fails for an error, it is possible to retry', async () => {\n const fakeAuthApi = new FakeAuthApi()\n const error = new Error('A generic error')\n const initialJwt = generateJwt()\n const initialRefresh = generateRefreshJwt()\n const authService = new JwtAuthService(fakeAuthApi, { authJwt: initialJwt, refreshJwt: initialRefresh })\n\n fakeAuthApi.refreshException = error\n await expectThrowOnGetHeaders(authService)\n\n fakeAuthApi.refreshException = null\n const secondHeader = await authService.getAuthHeaders()\n const secondJwt = fakeAuthApi.jwt\n expect(secondHeader[0].data).to.be.equal(`Bearer ${secondJwt}`)\n expect(secondJwt).not.to.be.equal(initialJwt)\n })\n})\n"]}
|
|
1
|
+
{"version":3,"file":"jwt-provider-test.js","sourceRoot":"","sources":["../../../../test/icc-x-api/auth/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E,CAAA;YAEvC,IAAA,aAAM,EAAC,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,CAAA;SAC7C;IACH,CAAC,CAAA,CAAC,CAAA;IAEF,EAAE,CAAC,2CAA2C,EAAE,GAAS,EAAE;QACzD,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;QACjC,MAAM,YAAY,GAAG,qCAAiB,CAAC,UAAU,CAAC,OAAO,EAAE,UAAU,EAAE;YACrE,SAAS,EAAE,CAAO,eAAiC,EAAE,gBAAqC,EAAE,EAAE;gBAC5F,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,EAAE,kCAAc,CAAC,gBAAgB,EAAE,CAAA,CAAC,2BAA2B;YACpG,CAAC,CAAA;SACF,CAAC,CAAA;QACF,MAAM,OAAO,GAAG,YAAY,CAAC,cAAc,EAAE,CAAA;QAE7C,IAAA,aAAM,EAAC,OAAO,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,CAAC,SAAS,CAAA;QAE7C,IAAI,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE;YACvB,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,SAAS,EAAE,CAAA;YAEvC,IAAA,aAAM,EAAC,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;SAC9C;IACH,CAAC,CAAA,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { IccAuthApi } from '../../../icc-api'\nimport { LoginCredentials } from '../../../icc-api/model/LoginCredentials'\nimport { AuthenticationResponse } from '../../../icc-api/model/AuthenticationResponse'\nimport { b2a } from '../../../icc-api/model/ModelHelper'\nimport { expect } from 'chai'\nimport { JwtBridgedAuthService } from '../../../icc-x-api/auth/JwtBridgedAuthService'\nimport { AuthService } from '../../../icc-x-api/auth/AuthService'\nimport { JwtAuthService } from '../../../icc-x-api/auth/JwtAuthService'\nimport { XHR } from '../../../icc-api/api/XHR'\nimport XHRError = XHR.XHRError\nimport { JwtError } from '../../../icc-x-api/auth/JwtError'\nimport { BasicAuthService } from '../../../icc-x-api/auth/BasicAuthService'\nimport { EnsembleAuthService } from '../../../icc-x-api/auth/EnsembleAuthService'\nimport { NoAuthService } from '../../../icc-x-api/auth/NoAuthService'\nimport { AuthSecretDetails, AuthSecretType, SmartAuthProvider } from '../../../icc-x-api/auth/SmartAuthProvider'\n\ndescribe('JWT provider resiliency test', () => {\n function generateJwt() {\n return `jwt.${b2a(JSON.stringify({ exp: 1000 }))}.${Math.floor(100000 + Math.random() * 900000)}`\n }\n\n function generateRefreshJwt() {\n return `refreshJwt.${b2a(JSON.stringify({ exp: new Date().getTime() * 10 }))}.${Math.floor(100000 + Math.random() * 900000)}`\n }\n\n class FakeAuthApi extends IccAuthApi {\n refreshException: Error | undefined | null\n loginException: Error | undefined | null\n jwt = `jwt.${b2a(JSON.stringify({ exp: 1000 }))}.signature`\n refreshJwt = `refreshJwt.${b2a(JSON.stringify({ exp: new Date().getTime() * 10 }))}.signature`\n\n constructor() {\n super('', {})\n }\n\n login(body?: LoginCredentials): Promise<AuthenticationResponse> {\n if (!!this.loginException) {\n throw this.loginException\n }\n\n this.jwt = generateJwt()\n this.refreshJwt = generateRefreshJwt()\n return Promise.resolve(\n new AuthenticationResponse({\n token: this.jwt,\n refreshToken: this.refreshJwt,\n })\n )\n }\n\n refreshAuthenticationJWT(refreshJWT: string): Promise<AuthenticationResponse> {\n if (!!this.refreshException) {\n throw this.refreshException\n }\n\n this.jwt = generateJwt()\n return Promise.resolve(\n new AuthenticationResponse({\n token: this.jwt,\n refreshToken: this.refreshJwt ?? generateRefreshJwt(),\n })\n )\n }\n }\n\n async function expectThrowOnGetHeaders(service: AuthService): Promise<void> {\n let caught: Error | null = null\n try {\n await service.getAuthHeaders()\n } catch (e) {\n caught = e as Error\n }\n expect(caught).not.to.be.null\n expect(caught).to.be.instanceof(JwtError)\n }\n\n it('JwtBridgedAuthService - If the login fails for any type of error it is possible to retry', async () => {\n const errors: Error[] = [\n new Error('A generic error'),\n new XHRError('', '', 403, 'FORBIDDEN', new Headers()),\n new XHRError('', '', 503, 'TIMEOUT', new Headers()),\n ]\n for (const e of errors) {\n const fakeAuthApi = new FakeAuthApi()\n const authService = new JwtBridgedAuthService(fakeAuthApi, 'username', 'password', {}, undefined)\n\n fakeAuthApi.loginException = e\n await expectThrowOnGetHeaders(authService)\n\n fakeAuthApi.loginException = null\n const firstHeader = await authService.getAuthHeaders()\n expect(firstHeader[0].data).to.be.equal(`Bearer ${fakeAuthApi.jwt}`)\n }\n })\n\n async function jwtBridgedTest(error: Error, loginAfterError: boolean) {\n const fakeAuthApi = new FakeAuthApi()\n const authService = new JwtBridgedAuthService(fakeAuthApi, 'username', 'password', {}, undefined)\n\n fakeAuthApi.refreshException = null\n const firstHeader = await authService.getAuthHeaders()\n const initialJwt = fakeAuthApi.jwt\n const initialRefresh = fakeAuthApi.refreshJwt\n expect(firstHeader[0].data).to.be.equal(`Bearer ${initialJwt}`)\n\n fakeAuthApi.refreshException = error\n await expectThrowOnGetHeaders(authService)\n\n fakeAuthApi.refreshException = null\n const secondHeader = await authService.getAuthHeaders()\n const secondJwt = fakeAuthApi.jwt\n const secondRefresh = fakeAuthApi.refreshJwt\n expect(secondHeader[0].data).to.be.equal(`Bearer ${secondJwt}`)\n expect(secondJwt).not.to.be.equal(initialJwt)\n if (loginAfterError) {\n expect(secondRefresh).not.to.be.equal(initialRefresh)\n } else {\n expect(secondRefresh).to.be.equal(initialRefresh)\n }\n }\n\n it('JwtBridgedAuthService - If the refresh fails for a generic error, it is possible to retry', async () => {\n const error = new Error('A generic error')\n await jwtBridgedTest(error, false)\n })\n\n it('JwtBridgedAuthService - If the refresh fails for a 500 error, the login is performed again', async () => {\n const error = new XHRError('', '', 500, 'INTERNAL SERVER ERROR', new Headers())\n await jwtBridgedTest(error, false)\n })\n\n it('JwtBridgedAuthService - If the refresh fails for a 400 error, the login is performed again', async () => {\n const error = new XHRError('', '', 403, 'FORBIDDEN', new Headers())\n await jwtBridgedTest(error, true)\n })\n\n it('JwtAuthService - If the refresh fails for an error, it is possible to retry', async () => {\n const fakeAuthApi = new FakeAuthApi()\n const error = new Error('A generic error')\n const initialJwt = generateJwt()\n const initialRefresh = generateRefreshJwt()\n const authService = new JwtAuthService(fakeAuthApi, { authJwt: initialJwt, refreshJwt: initialRefresh })\n\n fakeAuthApi.refreshException = error\n await expectThrowOnGetHeaders(authService)\n\n fakeAuthApi.refreshException = null\n const secondHeader = await authService.getAuthHeaders()\n const secondJwt = fakeAuthApi.jwt\n expect(secondHeader[0].data).to.be.equal(`Bearer ${secondJwt}`)\n expect(secondJwt).not.to.be.equal(initialJwt)\n })\n\n it('A BasicAuthService does not have a jwtGetter property', async () => {\n const service = new BasicAuthService('username', 'password') as AuthService\n expect(service.jwtGetter).to.be.undefined\n })\n\n it('A NoAuthService does not have a jwtGetter property', async () => {\n const service = new NoAuthService() as AuthService\n expect(service.jwtGetter).to.be.undefined\n })\n\n it('A JwtBridgedAuthService supports the jwtGetter', async () => {\n const fakeAuthApi = new FakeAuthApi()\n const authService = new JwtBridgedAuthService(fakeAuthApi, 'username', 'password', {}, undefined) as AuthService\n expect(authService.jwtGetter).not.to.be.undefined\n\n if (!!authService.jwtGetter) {\n const token = await authService.jwtGetter()\n\n expect(token?.token).to.be.equal(fakeAuthApi.jwt)\n }\n })\n\n it('A JwtAuthService supports the jwtGetter', async () => {\n const initialJwt = generateJwt()\n const initialRefresh = generateRefreshJwt()\n const fakeAuthApi = new FakeAuthApi()\n const jwtService = new JwtAuthService(fakeAuthApi, { authJwt: initialJwt, refreshJwt: initialRefresh }) as AuthService\n expect(jwtService.jwtGetter).not.to.be.undefined\n\n if (!!jwtService.jwtGetter) {\n const token = await jwtService.jwtGetter()\n\n expect(token?.token).to.be.equal(initialJwt)\n }\n })\n\n it('An EnsembleAuthService supports the jwtGetter', async () => {\n const initialJwt = generateJwt()\n const initialRefresh = generateRefreshJwt()\n const fakeAuthApi = new FakeAuthApi()\n const basic = new BasicAuthService('username', 'password')\n const jwtService = new JwtAuthService(fakeAuthApi, { authJwt: initialJwt, refreshJwt: initialRefresh })\n const service = new EnsembleAuthService(jwtService, new NoAuthService(), basic) as AuthService\n expect(service.jwtGetter).not.to.be.undefined\n\n if (!!service.jwtGetter) {\n const token = await service.jwtGetter()\n\n expect(token?.token).to.be.equal(initialJwt)\n }\n })\n\n it('A SmartAuthService supports the jwtGetter', async () => {\n const authApi = new FakeAuthApi()\n const authProvider = SmartAuthProvider.initialise(authApi, 'username', {\n getSecret: async (acceptedSecrets: AuthSecretType[], previousAttempts: AuthSecretDetails[]) => {\n return { value: 'wrong', secretType: AuthSecretType.LONG_LIVED_TOKEN } // pragma: allowlist secret\n },\n })\n const service = authProvider.getAuthService()\n\n expect(service.jwtGetter).not.to.be.undefined\n\n if (!!service.jwtGetter) {\n const token = await service.jwtGetter()\n\n expect(token?.token).to.be.equal(authApi.jwt)\n }\n })\n})\n"]}
|