@icure/api 7.1.21 → 7.1.23

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.
Files changed (33) hide show
  1. package/icc-api/api/IccAuthApi.d.ts +2 -1
  2. package/icc-api/api/IccAuthApi.js +3 -2
  3. package/icc-api/api/IccAuthApi.js.map +1 -1
  4. package/icc-api/api/IccGroupApi.d.ts +2 -1
  5. package/icc-api/api/IccGroupApi.js +4 -2
  6. package/icc-api/api/IccGroupApi.js.map +1 -1
  7. package/icc-api/api/IccUserApi.d.ts +6 -0
  8. package/icc-api/api/IccUserApi.js +13 -0
  9. package/icc-api/api/IccUserApi.js.map +1 -1
  10. package/icc-api/api/XHR.d.ts +1 -1
  11. package/icc-api/api/XHR.js +4 -3
  12. package/icc-api/api/XHR.js.map +1 -1
  13. package/icc-x-api/auth/AuthService.d.ts +2 -1
  14. package/icc-x-api/auth/AuthService.js.map +1 -1
  15. package/icc-x-api/auth/AuthenticationProvider.d.ts +1 -1
  16. package/icc-x-api/auth/AuthenticationProvider.js +1 -1
  17. package/icc-x-api/auth/AuthenticationProvider.js.map +1 -1
  18. package/icc-x-api/auth/EnsembleAuthService.d.ts +1 -1
  19. package/icc-x-api/auth/EnsembleAuthService.js +2 -2
  20. package/icc-x-api/auth/EnsembleAuthService.js.map +1 -1
  21. package/icc-x-api/auth/JwtAuthService.d.ts +0 -2
  22. package/icc-x-api/auth/JwtAuthService.js +3 -15
  23. package/icc-x-api/auth/JwtAuthService.js.map +1 -1
  24. package/icc-x-api/auth/JwtUtils.d.ts +10 -0
  25. package/icc-x-api/auth/JwtUtils.js +31 -0
  26. package/icc-x-api/auth/JwtUtils.js.map +1 -0
  27. package/icc-x-api/auth/SmartAuthProvider.d.ts +120 -0
  28. package/icc-x-api/auth/SmartAuthProvider.js +375 -0
  29. package/icc-x-api/auth/SmartAuthProvider.js.map +1 -0
  30. package/icc-x-api/index.d.ts +54 -0
  31. package/icc-x-api/index.js +11 -11
  32. package/icc-x-api/index.js.map +1 -1
  33. package/package.json +1 -1
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodeJwtClaims = exports.isJwtInvalidOrExpired = void 0;
4
+ const utils_1 = require("../utils");
5
+ /**
6
+ * @internal this function is for internal use only and may be changed without notice
7
+ * Returns true if the jwt is invalid or expired, false otherwise.
8
+ */
9
+ function isJwtInvalidOrExpired(jwt) {
10
+ try {
11
+ const claims = decodeJwtClaims(jwt);
12
+ // Using the 'exp' string is safe to use as it is part of the JWT RFC and cannot be modified by us.
13
+ return !('exp' in claims) || claims['exp'] * 1000 < new Date().getTime();
14
+ }
15
+ catch (e) {
16
+ return true;
17
+ }
18
+ }
19
+ exports.isJwtInvalidOrExpired = isJwtInvalidOrExpired;
20
+ /**
21
+ * @internal this function is for internal use only and may be changed without notice
22
+ * Get the claims of the jwt.
23
+ */
24
+ function decodeJwtClaims(jwt) {
25
+ const parts = jwt.split('.');
26
+ if (parts.length !== 3)
27
+ throw new Error('Invalid JWT: should be 3 parts');
28
+ return JSON.parse((0, utils_1.a2b)(parts[1]));
29
+ }
30
+ exports.decodeJwtClaims = decodeJwtClaims;
31
+ //# sourceMappingURL=JwtUtils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JwtUtils.js","sourceRoot":"","sources":["../../../icc-x-api/auth/JwtUtils.ts"],"names":[],"mappings":";;;AAAA,oCAA8B;AAE9B;;;GAGG;AACH,SAAgB,qBAAqB,CAAC,GAAW;IAC/C,IAAI;QACF,MAAM,MAAM,GAAG,eAAe,CAAC,GAAG,CAAC,CAAA;QACnC,mGAAmG;QACnG,OAAO,CAAC,CAAC,KAAK,IAAI,MAAM,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,GAAG,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC,OAAO,EAAE,CAAA;KACzE;IAAC,OAAO,CAAC,EAAE;QACV,OAAO,IAAI,CAAA;KACZ;AACH,CAAC;AARD,sDAQC;AAED;;;GAGG;AACH,SAAgB,eAAe,CAAC,GAAW;IACzC,MAAM,KAAK,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC5B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAA;IACzE,OAAO,IAAI,CAAC,KAAK,CAAC,IAAA,WAAG,EAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;AAClC,CAAC;AAJD,0CAIC","sourcesContent":["import { a2b } from '../utils'\n\n/**\n * @internal this function is for internal use only and may be changed without notice\n * Returns true if the jwt is invalid or expired, false otherwise.\n */\nexport function isJwtInvalidOrExpired(jwt: string): boolean {\n try {\n const claims = decodeJwtClaims(jwt)\n // Using the 'exp' string is safe to use as it is part of the JWT RFC and cannot be modified by us.\n return !('exp' in claims) || claims['exp'] * 1000 < new Date().getTime()\n } catch (e) {\n return true\n }\n}\n\n/**\n * @internal this function is for internal use only and may be changed without notice\n * Get the claims of the jwt.\n */\nexport function decodeJwtClaims(jwt: string): any {\n const parts = jwt.split('.')\n if (parts.length !== 3) throw new Error('Invalid JWT: should be 3 parts')\n return JSON.parse(a2b(parts[1]))\n}\n"]}
@@ -0,0 +1,120 @@
1
+ import { AuthenticationProvider } from './AuthenticationProvider';
2
+ import { UserGroup } from '../../icc-api/model/UserGroup';
3
+ import { AuthService } from './AuthService';
4
+ import { IccAuthApi } from '../../icc-api';
5
+ /**
6
+ * Needed by a {@link SmartAuthProvider} to get the secrets (password, token, etc.) for authentication to the iCure SDK as needed.
7
+ */
8
+ export interface AuthSecretProvider {
9
+ /**
10
+ * Provides a secret for authentication to the iCure SDK.
11
+ *
12
+ * ## Accepted secrets
13
+ *
14
+ * The method will be provided with an array of the secrets types that are acceptable (`acceptedSecrets`). Usually this array will contain multiple
15
+ * elements, but this depends on the group configuration, the user (if he has 2fa setup or not), or the operation being performed. For groups using
16
+ * default configurations and for patients without 2fa enabled for example the array will always contain the {@link AuthSecretType.PASSWORD} element.
17
+ * Usually the array contain also the {@link AuthSecretType.LONG_LIVED_TOKEN} element, but if the user is attempting to perform a sensitive operations
18
+ * such as changing his password the default group configuration does not allow for the user to authenticate using a JWT obtained from a long-lived
19
+ * token for this operation, meaning the array will not contain the {@link AuthSecretType.LONG_LIVED_TOKEN} element.
20
+ *
21
+ * Regardless of the number of elements in the array only one secret of the accepted types is sufficient for the operation to succeed.
22
+ *
23
+ * ## TWO_FACTOR_AUTHENTICATION_TOKEN secret type
24
+ *
25
+ * The {@link AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN} secret type is only used when the user has 2fa enabled. In this case the SDK will call
26
+ * this method twice, once containing the {@link AuthSecretType.PASSWORD} element in the `acceptedSecrets` array, and if the provided secret is a
27
+ * valid password the SDK will immediately call this method again, this time containing the {@link AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN}
28
+ * instead of the {@link AuthSecretType.PASSWORD} element.
29
+ *
30
+ * Any future call to this method from the same provider instance will not contain the {@link AuthSecretType.PASSWORD} element anymore, as it is
31
+ * cached, but it may contain the {@link AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN} element instead.
32
+ *
33
+ * Note that the 2fa token is not needed for logging in through a long-lived or short-lived token, it is only used in combination with a password.
34
+ * If the user is using 2fa, and you get in input as `acceptedSecrets` an array `[PASSWORD, LONG_LIVED_TOKEN, SHORT_LIVED_TOKEN]`, and you pass to
35
+ * authenticate a long-lived token, the SDK will not call this method again to ask for the 2fa token.
36
+ *
37
+ * @param acceptedSecrets the types of secrets that are acceptable for the operation being performed.
38
+ * @param previousAttempts the secrets that were previously attempted by the SDK for this operation. This array will be empty the first time this
39
+ * method is called for a given operation, but it may contain multiple elements if the SDK has already called this method multiple times because the
40
+ * previously returned secrets were not valid. The first element is the first secret that was attempted, and the last element is the most recently
41
+ * attempted.
42
+ * @return a promise that resolves with the secret and the secret type to use for authentication. If the promise rejects then the ongoing SDK
43
+ * operation will fail without being re-attempted.
44
+ */
45
+ getSecret(acceptedSecrets: AuthSecretType[], previousAttempts: {
46
+ secret: string;
47
+ secretType: AuthSecretType;
48
+ }[]): Promise<{
49
+ secret: string;
50
+ secretType: AuthSecretType;
51
+ }>;
52
+ }
53
+ /**
54
+ * Represents a type of secret that can be used for authentication with iCure.
55
+ */
56
+ export declare enum AuthSecretType {
57
+ /**
58
+ * Password chosen by the user.
59
+ */
60
+ PASSWORD = "PASSWORD",
61
+ /**
62
+ * Time based one time password provided by authenticator applications, generated on the basis of a timestamp and a shared secret between the iCure
63
+ * server and the authenticator application.
64
+ */
65
+ TWO_FACTOR_AUTHENTICATION_TOKEN = "TWO_FACTOR_AUTHENTICATION_TOKEN",
66
+ /**
67
+ * A short-lived iCure token, an internal authentication token that lasts 5 minutes or less. Unlike passwords these tokens usually are generated by
68
+ * some component of iCure, and are not chosen by the user.
69
+ */
70
+ SHORT_LIVED_TOKEN = "SHORT_LIVED_TOKEN",
71
+ /**
72
+ * A long-lived iCure token, an internal authentication token that lasts longer than 5 minutes. Unlike passwords these tokens usually are generated
73
+ * by some component of iCure, and are not chosen by the user.
74
+ */
75
+ LONG_LIVED_TOKEN = "LONG_LIVED_TOKEN"
76
+ }
77
+ /**
78
+ * @internal this class is meant for internal use only and may be changed without notice. The SmartAuthProvider will be initialised automatically
79
+ * by the iCure api depending on the authentication options you provide.
80
+ *
81
+ * An authentication provider that automatically requests secrets for authentication as needed.
82
+ *
83
+ * This authentication provider can be initialised already with some secrets or tokens, and the provider will cache them and use them as needed for
84
+ * as long as they remain valid. If at any point however the provider needs an updated secret or a secret of a different kind it will automatically
85
+ * request this to the {@link SmartAuthProvider} to get the secret.
86
+ *
87
+ * An advantage of using this provider over others is that in case all the cached tokens and secrets were to expire while performing a request,
88
+ * instead of having the request fail the provider will ask for new secrets from the {@link SmartAuthProvider} and the request will automatically
89
+ * be retried with the new secret. Additionally, the provider may request updated secrets also for performing some sensitive operations (e.g. changing
90
+ * password of the user) even if the cached tokens and/or did not expire. This could be the case for example if the cached secret is a long-lived
91
+ * token, but in order to change the password the user needs to provide his current password.
92
+ *
93
+ * Note that in this context the cache of secrets and token is in memory only, and is not persisted in any way. Different instances of this provider
94
+ * will not share the same cache.
95
+ *
96
+ */
97
+ export declare class SmartAuthProvider implements AuthenticationProvider {
98
+ private readonly tokenProvider;
99
+ private readonly groupId;
100
+ /**
101
+ * Initialises a {@link SmartAuthProvider}.
102
+ * @param authApi an "anonymous" {@link IccAuthApi} to use for authentication.
103
+ * @param login
104
+ * @param secretProvider
105
+ * @param props optional initialisation properties.
106
+ */
107
+ static initialise(authApi: IccAuthApi, login: string, secretProvider: AuthSecretProvider, props?: {
108
+ initialSecret?: string;
109
+ initialAuthToken?: string;
110
+ initialRefreshToken?: string;
111
+ loginGroupId?: string;
112
+ }): SmartAuthProvider;
113
+ private constructor();
114
+ getAuthService(): AuthService;
115
+ switchGroup(newGroupId: string, matches: Array<UserGroup>): Promise<AuthenticationProvider>;
116
+ getIcureTokens(): Promise<{
117
+ token: string;
118
+ refreshToken: string;
119
+ } | undefined>;
120
+ }
@@ -0,0 +1,375 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.SmartAuthProvider = exports.AuthSecretType = void 0;
13
+ const XHR_1 = require("../../icc-api/api/XHR");
14
+ var XHRError = XHR_1.XHR.XHRError;
15
+ const JwtUtils_1 = require("./JwtUtils");
16
+ /**
17
+ * Represents a type of secret that can be used for authentication with iCure.
18
+ */
19
+ var AuthSecretType;
20
+ (function (AuthSecretType) {
21
+ /**
22
+ * Password chosen by the user.
23
+ */
24
+ AuthSecretType["PASSWORD"] = "PASSWORD";
25
+ /**
26
+ * Time based one time password provided by authenticator applications, generated on the basis of a timestamp and a shared secret between the iCure
27
+ * server and the authenticator application.
28
+ */
29
+ AuthSecretType["TWO_FACTOR_AUTHENTICATION_TOKEN"] = "TWO_FACTOR_AUTHENTICATION_TOKEN";
30
+ /**
31
+ * A short-lived iCure token, an internal authentication token that lasts 5 minutes or less. Unlike passwords these tokens usually are generated by
32
+ * some component of iCure, and are not chosen by the user.
33
+ */
34
+ AuthSecretType["SHORT_LIVED_TOKEN"] = "SHORT_LIVED_TOKEN";
35
+ /**
36
+ * A long-lived iCure token, an internal authentication token that lasts longer than 5 minutes. Unlike passwords these tokens usually are generated
37
+ * by some component of iCure, and are not chosen by the user.
38
+ */
39
+ AuthSecretType["LONG_LIVED_TOKEN"] = "LONG_LIVED_TOKEN";
40
+ /**
41
+ * A token provided by an external authentication provider (e.g. Oauth/Google).
42
+ * Not yet in use.
43
+ */
44
+ // EXTERNAL_AUTHENTICATION = 'EXTERNAL_AUTHENTICATION',
45
+ /**
46
+ * A special case of external authentication where the provider is a digital identity provider.
47
+ * Not yet in use.
48
+ */
49
+ // DIGITAL_ID = 'DIGITAL_ID',
50
+ })(AuthSecretType = exports.AuthSecretType || (exports.AuthSecretType = {}));
51
+ // Here starts internal entities that should not be used directly.
52
+ /**
53
+ * @internal this class is meant for internal use only and may be changed without notice. The SmartAuthProvider will be initialised automatically
54
+ * by the iCure api depending on the authentication options you provide.
55
+ *
56
+ * An authentication provider that automatically requests secrets for authentication as needed.
57
+ *
58
+ * This authentication provider can be initialised already with some secrets or tokens, and the provider will cache them and use them as needed for
59
+ * as long as they remain valid. If at any point however the provider needs an updated secret or a secret of a different kind it will automatically
60
+ * request this to the {@link SmartAuthProvider} to get the secret.
61
+ *
62
+ * An advantage of using this provider over others is that in case all the cached tokens and secrets were to expire while performing a request,
63
+ * instead of having the request fail the provider will ask for new secrets from the {@link SmartAuthProvider} and the request will automatically
64
+ * be retried with the new secret. Additionally, the provider may request updated secrets also for performing some sensitive operations (e.g. changing
65
+ * password of the user) even if the cached tokens and/or did not expire. This could be the case for example if the cached secret is a long-lived
66
+ * token, but in order to change the password the user needs to provide his current password.
67
+ *
68
+ * Note that in this context the cache of secrets and token is in memory only, and is not persisted in any way. Different instances of this provider
69
+ * will not share the same cache.
70
+ *
71
+ */
72
+ class SmartAuthProvider {
73
+ /**
74
+ * Initialises a {@link SmartAuthProvider}.
75
+ * @param authApi an "anonymous" {@link IccAuthApi} to use for authentication.
76
+ * @param login
77
+ * @param secretProvider
78
+ * @param props optional initialisation properties.
79
+ */
80
+ static initialise(authApi, login, secretProvider, props = {}) {
81
+ return new SmartAuthProvider(new TokenProvider(login, props.loginGroupId, props.initialSecret ? { value: props.initialSecret, type: undefined } : undefined, props.initialAuthToken, props.initialRefreshToken, authApi, secretProvider), props.loginGroupId);
82
+ }
83
+ constructor(tokenProvider, groupId) {
84
+ this.tokenProvider = tokenProvider;
85
+ this.groupId = groupId;
86
+ }
87
+ getAuthService() {
88
+ return new SmartAuthService(this.tokenProvider);
89
+ }
90
+ switchGroup(newGroupId, matches) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ if (newGroupId == this.groupId)
93
+ return Promise.resolve(this);
94
+ if (!matches.find((match) => match.groupId == newGroupId))
95
+ throw new Error('New group id not found in matches.');
96
+ const switchedProvider = yield this.tokenProvider.switchedGroup(newGroupId);
97
+ return new SmartAuthProvider(switchedProvider, this.groupId);
98
+ });
99
+ }
100
+ getIcureTokens() {
101
+ return Promise.resolve(undefined);
102
+ }
103
+ }
104
+ exports.SmartAuthProvider = SmartAuthProvider;
105
+ var ServerAuthenticationClass;
106
+ (function (ServerAuthenticationClass) {
107
+ // DIGITAL_ID = 60,
108
+ ServerAuthenticationClass[ServerAuthenticationClass["TWO_FACTOR_AUTHENTICATION"] = 50] = "TWO_FACTOR_AUTHENTICATION";
109
+ ServerAuthenticationClass[ServerAuthenticationClass["SHORT_LIVED_TOKEN"] = 40] = "SHORT_LIVED_TOKEN";
110
+ // EXTERNAL_AUTHENTICATION = 30,
111
+ ServerAuthenticationClass[ServerAuthenticationClass["PASSWORD"] = 20] = "PASSWORD";
112
+ ServerAuthenticationClass[ServerAuthenticationClass["LONG_LIVED_TOKEN"] = 10] = "LONG_LIVED_TOKEN";
113
+ })(ServerAuthenticationClass || (ServerAuthenticationClass = {}));
114
+ class TokenProvider {
115
+ constructor(login, groupId, currentLongLivedSecret, cachedToken, cachedRefreshToken, authApi, authSecretProvider) {
116
+ this.login = login;
117
+ this.groupId = groupId;
118
+ this.currentLongLivedSecret = currentLongLivedSecret;
119
+ this.cachedToken = cachedToken;
120
+ this.cachedRefreshToken = cachedRefreshToken;
121
+ this.authApi = authApi;
122
+ this.authSecretProvider = authSecretProvider;
123
+ }
124
+ getCachedOrRefreshedOrNewToken() {
125
+ return __awaiter(this, void 0, void 0, function* () {
126
+ if (!!this.cachedToken && !(0, JwtUtils_1.isJwtInvalidOrExpired)(this.cachedToken)) {
127
+ return { token: this.cachedToken, type: RetrievedTokenType.CACHED };
128
+ }
129
+ else if (!!this.cachedRefreshToken && !(0, JwtUtils_1.isJwtInvalidOrExpired)(this.cachedRefreshToken)) {
130
+ return this.refreshAndCacheToken(this.cachedRefreshToken);
131
+ }
132
+ else {
133
+ return { token: yield this.getAndCacheNewToken(undefined), type: RetrievedTokenType.NEW };
134
+ }
135
+ });
136
+ }
137
+ getNewTokenWithClass(minimumAuthenticationClass) {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ return yield this.getAndCacheNewToken(minimumAuthenticationClass);
140
+ });
141
+ }
142
+ getAndCacheNewToken(minimumAuthenticationClassLevel) {
143
+ return __awaiter(this, void 0, void 0, function* () {
144
+ const { token, refreshToken } = yield this.getNewToken(minimumAuthenticationClassLevel !== null && minimumAuthenticationClassLevel !== void 0 ? minimumAuthenticationClassLevel : 0);
145
+ this.cachedToken = token;
146
+ this.cachedRefreshToken = refreshToken;
147
+ return token;
148
+ });
149
+ }
150
+ refreshAndCacheToken(refreshToken) {
151
+ return __awaiter(this, void 0, void 0, function* () {
152
+ return yield this.authApi.refreshAuthenticationJWT(refreshToken).then((authResult) => {
153
+ if (!authResult.token)
154
+ throw new Error('Internal error: refresh succeeded but no token was returned. Unsupported backend version?');
155
+ this.cachedToken = authResult.token;
156
+ return { token: authResult.token, type: RetrievedTokenType.REFRESHED };
157
+ }, () => __awaiter(this, void 0, void 0, function* () { return ({ token: yield this.getAndCacheNewToken(undefined), type: RetrievedTokenType.NEW }); }));
158
+ });
159
+ }
160
+ getNewToken(minimumAuthenticationClassLevel) {
161
+ return __awaiter(this, void 0, void 0, function* () {
162
+ if (!!this.currentLongLivedSecret && (!this.currentLongLivedSecret.type || this.currentLongLivedSecret.type >= minimumAuthenticationClassLevel)) {
163
+ const resultWithCachedSecret = yield this.doGetTokenWithSecret(this.currentLongLivedSecret.value, minimumAuthenticationClassLevel);
164
+ if ('success' in resultWithCachedSecret) {
165
+ return resultWithCachedSecret.success;
166
+ }
167
+ else if (resultWithCachedSecret.failure === DoGetTokenResultFailureReason.NEEDS_2FA &&
168
+ minimumAuthenticationClassLevel <= ServerAuthenticationClass.TWO_FACTOR_AUTHENTICATION) {
169
+ return this.askTotpAndGetToken(this.currentLongLivedSecret.value, minimumAuthenticationClassLevel);
170
+ }
171
+ else
172
+ return this.askSecretAndGetToken(minimumAuthenticationClassLevel, true);
173
+ }
174
+ else {
175
+ return this.askSecretAndGetToken(minimumAuthenticationClassLevel, true);
176
+ }
177
+ });
178
+ }
179
+ askSecretAndGetToken(minimumAuthenticationClassLevel, passwordIsValidAs2fa) {
180
+ return __awaiter(this, void 0, void 0, function* () {
181
+ const acceptedSecrets = [
182
+ minimumAuthenticationClassLevel <= ServerAuthenticationClass.LONG_LIVED_TOKEN ? [AuthSecretType.LONG_LIVED_TOKEN] : [],
183
+ minimumAuthenticationClassLevel <= ServerAuthenticationClass.SHORT_LIVED_TOKEN ? [AuthSecretType.SHORT_LIVED_TOKEN] : [],
184
+ minimumAuthenticationClassLevel <= ServerAuthenticationClass.TWO_FACTOR_AUTHENTICATION &&
185
+ (passwordIsValidAs2fa || minimumAuthenticationClassLevel <= ServerAuthenticationClass.PASSWORD)
186
+ ? [AuthSecretType.PASSWORD]
187
+ : [],
188
+ ].flat();
189
+ if (!acceptedSecrets.length)
190
+ throw new Error('Internal error: no secret type is accepted for this request. Group may be misconfigured, or client may be outdated.');
191
+ const attempts = [];
192
+ while (true) {
193
+ const { secret, secretType } = yield this.authSecretProvider.getSecret([...acceptedSecrets], attempts);
194
+ if (!acceptedSecrets.includes(secretType))
195
+ throw new Error(`Accepted secret types are ${JSON.stringify(acceptedSecrets)}, but got a secret of type ${secretType}.`);
196
+ attempts.push({ secret, secretType });
197
+ const result = yield this.doGetTokenWithSecret(secret, minimumAuthenticationClassLevel);
198
+ if ('success' in result) {
199
+ this.updateCachedSecret(secret, secretType);
200
+ return result.success;
201
+ }
202
+ else if (result.failure == DoGetTokenResultFailureReason.NEEDS_2FA) {
203
+ return this.askTotpAndGetToken(secret, minimumAuthenticationClassLevel);
204
+ }
205
+ else if (secretType == AuthSecretType.PASSWORD && result.failure == DoGetTokenResultFailureReason.INVALID_AUTH_CLASS_LEVEL) {
206
+ return this.askSecretAndGetToken(minimumAuthenticationClassLevel, false);
207
+ } // else retry
208
+ }
209
+ });
210
+ }
211
+ askTotpAndGetToken(password, minimumAuthenticationClassLevel) {
212
+ return __awaiter(this, void 0, void 0, function* () {
213
+ if (minimumAuthenticationClassLevel > ServerAuthenticationClass.TWO_FACTOR_AUTHENTICATION)
214
+ throw new Error("Internal error: asking for totp to login but minimumAuthenticationClassLevel is higher than TWO_FACTOR_AUTHENTICATION's level.");
215
+ const attempts = [];
216
+ while (true) {
217
+ const { secret, secretType } = yield this.authSecretProvider.getSecret([AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN], attempts);
218
+ if (secretType != AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN)
219
+ throw new Error(`Was expecting a 2fa token but got a secret of type ${secretType}.`);
220
+ attempts.push({ secret, secretType });
221
+ const result = yield this.doGetTokenWithSecret(`${password}|${secret}`, minimumAuthenticationClassLevel);
222
+ if ('success' in result) {
223
+ this.updateCachedSecret(password, AuthSecretType.PASSWORD);
224
+ return result.success;
225
+ }
226
+ else if (result.failure != DoGetTokenResultFailureReason.INVALID_2FA) {
227
+ throw new Error(`Unexpected error while trying to login with (previously) valid password and 2fa token ${result.failure}.`);
228
+ } // else retry
229
+ }
230
+ });
231
+ }
232
+ doGetTokenWithSecret(secret, minimumAuthenticationClassLevel) {
233
+ return __awaiter(this, void 0, void 0, function* () {
234
+ return this.authApi.login({ username: this.login, password: secret }, this.groupId).then((authResult) => {
235
+ const { token, refreshToken } = authResult;
236
+ if (!token || !refreshToken)
237
+ throw new Error('Internal error: login succeeded but no token was returned. Unsupported backend version?');
238
+ const claims = (0, JwtUtils_1.decodeJwtClaims)(token);
239
+ const authClassLevel = claims['tac'];
240
+ if (!authClassLevel || typeof authClassLevel !== 'number')
241
+ throw new Error('Internal error: authClassLevel is not a number. Unsupported backend version?');
242
+ if (authClassLevel < minimumAuthenticationClassLevel) {
243
+ return { failure: DoGetTokenResultFailureReason.INVALID_AUTH_CLASS_LEVEL };
244
+ }
245
+ else {
246
+ return { success: { token, refreshToken } };
247
+ }
248
+ }, (error) => {
249
+ if (!(error instanceof XHRError))
250
+ throw error;
251
+ if (error.statusCode == 401 || error.statusCode == 412) {
252
+ // Password is wrong (401) or unacceptable (e.g. too short, 412)
253
+ return { failure: DoGetTokenResultFailureReason.INVALID_PW_OR_TOKEN };
254
+ }
255
+ else if (error.statusCode == 406) {
256
+ // Password is correct, but 2fa token is not
257
+ return { failure: DoGetTokenResultFailureReason.INVALID_2FA };
258
+ }
259
+ else if (error.statusCode == 417) {
260
+ // Password is correct, but the user has 2fa enabled and no 2fa token was provided
261
+ return { failure: DoGetTokenResultFailureReason.NEEDS_2FA };
262
+ }
263
+ else
264
+ throw error;
265
+ });
266
+ });
267
+ }
268
+ switchedGroup(newGroupId) {
269
+ return __awaiter(this, void 0, void 0, function* () {
270
+ const groupSwitchedTokens = this.cachedRefreshToken
271
+ ? yield this.authApi.switchGroup(this.cachedRefreshToken, newGroupId).then((response) => {
272
+ if (!response.token || !response.refreshToken)
273
+ throw new Error('Internal error: group switch succeeded but no token was returned. Unsupported backend version?');
274
+ return { token: response.token, refreshToken: response.refreshToken };
275
+ }, () => ({ token: undefined, refreshToken: undefined }))
276
+ : { token: undefined, refreshToken: undefined };
277
+ return new TokenProvider(this.login, newGroupId, this.currentLongLivedSecret ? { value: this.currentLongLivedSecret.value, type: undefined } : undefined, groupSwitchedTokens.token, groupSwitchedTokens.refreshToken, this.authApi, this.authSecretProvider);
278
+ });
279
+ }
280
+ updateCachedSecret(secret, secretType) {
281
+ if (secretType == AuthSecretType.LONG_LIVED_TOKEN || secretType == AuthSecretType.PASSWORD) {
282
+ this.currentLongLivedSecret = { value: secret, type: ServerAuthenticationClass.LONG_LIVED_TOKEN };
283
+ }
284
+ }
285
+ }
286
+ var DoGetTokenResultFailureReason;
287
+ (function (DoGetTokenResultFailureReason) {
288
+ DoGetTokenResultFailureReason[DoGetTokenResultFailureReason["NEEDS_2FA"] = 0] = "NEEDS_2FA";
289
+ DoGetTokenResultFailureReason[DoGetTokenResultFailureReason["INVALID_2FA"] = 1] = "INVALID_2FA";
290
+ DoGetTokenResultFailureReason[DoGetTokenResultFailureReason["INVALID_PW_OR_TOKEN"] = 2] = "INVALID_PW_OR_TOKEN";
291
+ DoGetTokenResultFailureReason[DoGetTokenResultFailureReason["INVALID_AUTH_CLASS_LEVEL"] = 3] = "INVALID_AUTH_CLASS_LEVEL";
292
+ })(DoGetTokenResultFailureReason || (DoGetTokenResultFailureReason = {}));
293
+ var RetrievedTokenType;
294
+ (function (RetrievedTokenType) {
295
+ RetrievedTokenType[RetrievedTokenType["CACHED"] = 0] = "CACHED";
296
+ RetrievedTokenType[RetrievedTokenType["REFRESHED"] = 1] = "REFRESHED";
297
+ RetrievedTokenType[RetrievedTokenType["NEW"] = 2] = "NEW";
298
+ })(RetrievedTokenType || (RetrievedTokenType = {}));
299
+ var SmartAuthServiceState;
300
+ (function (SmartAuthServiceState) {
301
+ SmartAuthServiceState[SmartAuthServiceState["INITIAL"] = 0] = "INITIAL";
302
+ SmartAuthServiceState[SmartAuthServiceState["DONE_INITIAL"] = 1] = "DONE_INITIAL";
303
+ SmartAuthServiceState[SmartAuthServiceState["REATTEMPT"] = 2] = "REATTEMPT";
304
+ SmartAuthServiceState[SmartAuthServiceState["REATTEMPTED_WITH_NEW_UNBOUND_TOKEN"] = 3] = "REATTEMPTED_WITH_NEW_UNBOUND_TOKEN";
305
+ SmartAuthServiceState[SmartAuthServiceState["REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN"] = 4] = "REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN";
306
+ SmartAuthServiceState[SmartAuthServiceState["EXPECT_REQUEST_WITH_SPECIFIC_AUTH_CLASS"] = 5] = "EXPECT_REQUEST_WITH_SPECIFIC_AUTH_CLASS";
307
+ SmartAuthServiceState[SmartAuthServiceState["TERMINAL_ERROR"] = 6] = "TERMINAL_ERROR";
308
+ })(SmartAuthServiceState || (SmartAuthServiceState = {}));
309
+ class SmartAuthService {
310
+ constructor(tokenProvider) {
311
+ this.tokenProvider = tokenProvider;
312
+ this.currentState = { id: SmartAuthServiceState.INITIAL };
313
+ }
314
+ getAuthHeaders(minimumAuthenticationClassLevel) {
315
+ return __awaiter(this, void 0, void 0, function* () {
316
+ return [new XHR_1.XHR.Header('Authorization', `Bearer ${yield this.getAuthToken(minimumAuthenticationClassLevel)}`)];
317
+ });
318
+ }
319
+ getAuthToken(minimumAuthenticationClassLevel) {
320
+ return __awaiter(this, void 0, void 0, function* () {
321
+ switch (this.currentState.id) {
322
+ case SmartAuthServiceState.INITIAL:
323
+ if (minimumAuthenticationClassLevel != undefined) {
324
+ throw new Error('Illegal state: cannot ask for a specific auth class level at the first request attempt.');
325
+ }
326
+ else {
327
+ const { token } = yield this.tokenProvider.getCachedOrRefreshedOrNewToken();
328
+ this.currentState = { id: SmartAuthServiceState.DONE_INITIAL, initialToken: token };
329
+ return token;
330
+ }
331
+ case SmartAuthServiceState.REATTEMPT:
332
+ if (minimumAuthenticationClassLevel != undefined) {
333
+ const token = yield this.tokenProvider.getNewTokenWithClass(minimumAuthenticationClassLevel);
334
+ this.currentState = { id: SmartAuthServiceState.REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN };
335
+ return token;
336
+ }
337
+ else {
338
+ const { token } = yield this.tokenProvider.getCachedOrRefreshedOrNewToken();
339
+ if (token == this.currentState.initialToken)
340
+ throw this.currentState.initialError;
341
+ this.currentState = { id: SmartAuthServiceState.REATTEMPTED_WITH_NEW_UNBOUND_TOKEN };
342
+ return token;
343
+ }
344
+ case SmartAuthServiceState.EXPECT_REQUEST_WITH_SPECIFIC_AUTH_CLASS:
345
+ if (minimumAuthenticationClassLevel != undefined) {
346
+ const token = yield this.tokenProvider.getNewTokenWithClass(minimumAuthenticationClassLevel);
347
+ this.currentState = { id: SmartAuthServiceState.REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN };
348
+ return token;
349
+ }
350
+ else
351
+ throw this.currentState.errorFromNewToken;
352
+ case SmartAuthServiceState.TERMINAL_ERROR:
353
+ throw this.currentState.error;
354
+ default:
355
+ throw new Error(`Illegal state: cannot get token in state ${this.currentState.id}.`);
356
+ }
357
+ });
358
+ }
359
+ invalidateHeader(error) {
360
+ switch (this.currentState.id) {
361
+ case SmartAuthServiceState.DONE_INITIAL:
362
+ this.currentState = { id: SmartAuthServiceState.REATTEMPT, initialToken: this.currentState.initialToken, initialError: error };
363
+ break;
364
+ case SmartAuthServiceState.REATTEMPTED_WITH_NEW_UNBOUND_TOKEN:
365
+ this.currentState = { id: SmartAuthServiceState.EXPECT_REQUEST_WITH_SPECIFIC_AUTH_CLASS, errorFromNewToken: error };
366
+ break;
367
+ case SmartAuthServiceState.REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN:
368
+ this.currentState = { id: SmartAuthServiceState.TERMINAL_ERROR, error: error };
369
+ break;
370
+ default:
371
+ throw new Error(`Illegal state: cannot invalidate header in state ${this.currentState.id}.`);
372
+ }
373
+ }
374
+ }
375
+ //# sourceMappingURL=SmartAuthProvider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SmartAuthProvider.js","sourceRoot":"","sources":["../../../icc-x-api/auth/SmartAuthProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;AAIA,+CAA2C;AAC3C,IAAO,QAAQ,GAAG,SAAG,CAAC,QAAQ,CAAA;AAC9B,yCAAmE;AAgDnE;;GAEG;AACH,IAAY,cA8BX;AA9BD,WAAY,cAAc;IACxB;;OAEG;IACH,uCAAqB,CAAA;IACrB;;;OAGG;IACH,qFAAmE,CAAA;IACnE;;;OAGG;IACH,yDAAuC,CAAA;IACvC;;;OAGG;IACH,uDAAqC,CAAA;IACrC;;;OAGG;IACH,uDAAuD;IACvD;;;OAGG;IACH,6BAA6B;AAC/B,CAAC,EA9BW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QA8BzB;AAED,kEAAkE;AAElE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAa,iBAAiB;IAC5B;;;;;;OAMG;IACH,MAAM,CAAC,UAAU,CACf,OAAmB,EACnB,KAAa,EACb,cAAkC,EAClC,QAKI,EAAE;QAEN,OAAO,IAAI,iBAAiB,CAC1B,IAAI,aAAa,CACf,KAAK,EACL,KAAK,CAAC,YAAY,EAClB,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE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AAC,CAAA;SAC/F;IACH,CAAC;CACF","sourcesContent":["import { AuthenticationProvider, NoAuthenticationProvider } from './AuthenticationProvider'\nimport { UserGroup } from '../../icc-api/model/UserGroup'\nimport { AuthService } from './AuthService'\nimport { IccAuthApi } from '../../icc-api'\nimport { XHR } from '../../icc-api/api/XHR'\nimport XHRError = XHR.XHRError\nimport { decodeJwtClaims, isJwtInvalidOrExpired } from './JwtUtils'\n\n/**\n * Needed by a {@link SmartAuthProvider} to get the secrets (password, token, etc.) for authentication to the iCure SDK as needed.\n */\nexport interface AuthSecretProvider {\n /**\n * Provides a secret for authentication to the iCure SDK.\n *\n * ## Accepted secrets\n *\n * The method will be provided with an array of the secrets types that are acceptable (`acceptedSecrets`). Usually this array will contain multiple\n * elements, but this depends on the group configuration, the user (if he has 2fa setup or not), or the operation being performed. For groups using\n * default configurations and for patients without 2fa enabled for example the array will always contain the {@link AuthSecretType.PASSWORD} element.\n * Usually the array contain also the {@link AuthSecretType.LONG_LIVED_TOKEN} element, but if the user is attempting to perform a sensitive operations\n * such as changing his password the default group configuration does not allow for the user to authenticate using a JWT obtained from a long-lived\n * token for this operation, meaning the array will not contain the {@link AuthSecretType.LONG_LIVED_TOKEN} element.\n *\n * Regardless of the number of elements in the array only one secret of the accepted types is sufficient for the operation to succeed.\n *\n * ## TWO_FACTOR_AUTHENTICATION_TOKEN secret type\n *\n * The {@link AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN} secret type is only used when the user has 2fa enabled. In this case the SDK will call\n * this method twice, once containing the {@link AuthSecretType.PASSWORD} element in the `acceptedSecrets` array, and if the provided secret is a\n * valid password the SDK will immediately call this method again, this time containing the {@link AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN}\n * instead of the {@link AuthSecretType.PASSWORD} element.\n *\n * Any future call to this method from the same provider instance will not contain the {@link AuthSecretType.PASSWORD} element anymore, as it is\n * cached, but it may contain the {@link AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN} element instead.\n *\n * Note that the 2fa token is not needed for logging in through a long-lived or short-lived token, it is only used in combination with a password.\n * If the user is using 2fa, and you get in input as `acceptedSecrets` an array `[PASSWORD, LONG_LIVED_TOKEN, SHORT_LIVED_TOKEN]`, and you pass to\n * authenticate a long-lived token, the SDK will not call this method again to ask for the 2fa token.\n *\n * @param acceptedSecrets the types of secrets that are acceptable for the operation being performed.\n * @param previousAttempts the secrets that were previously attempted by the SDK for this operation. This array will be empty the first time this\n * method is called for a given operation, but it may contain multiple elements if the SDK has already called this method multiple times because the\n * previously returned secrets were not valid. The first element is the first secret that was attempted, and the last element is the most recently\n * attempted.\n * @return a promise that resolves with the secret and the secret type to use for authentication. If the promise rejects then the ongoing SDK\n * operation will fail without being re-attempted.\n */\n getSecret(\n acceptedSecrets: AuthSecretType[],\n previousAttempts: { secret: string; secretType: AuthSecretType }[]\n ): Promise<{ secret: string; secretType: AuthSecretType }> // We may want to add some onSuccess callback in future or similar\n}\n\n/**\n * Represents a type of secret that can be used for authentication with iCure.\n */\nexport enum AuthSecretType {\n /**\n * Password chosen by the user.\n */\n PASSWORD = 'PASSWORD', // pragma: allowlist secret\n /**\n * Time based one time password provided by authenticator applications, generated on the basis of a timestamp and a shared secret between the iCure\n * server and the authenticator application.\n */\n TWO_FACTOR_AUTHENTICATION_TOKEN = 'TWO_FACTOR_AUTHENTICATION_TOKEN',\n /**\n * A short-lived iCure token, an internal authentication token that lasts 5 minutes or less. Unlike passwords these tokens usually are generated by\n * some component of iCure, and are not chosen by the user.\n */\n SHORT_LIVED_TOKEN = 'SHORT_LIVED_TOKEN',\n /**\n * A long-lived iCure token, an internal authentication token that lasts longer than 5 minutes. Unlike passwords these tokens usually are generated\n * by some component of iCure, and are not chosen by the user.\n */\n LONG_LIVED_TOKEN = 'LONG_LIVED_TOKEN',\n /**\n * A token provided by an external authentication provider (e.g. Oauth/Google).\n * Not yet in use.\n */\n // EXTERNAL_AUTHENTICATION = 'EXTERNAL_AUTHENTICATION',\n /**\n * A special case of external authentication where the provider is a digital identity provider.\n * Not yet in use.\n */\n // DIGITAL_ID = 'DIGITAL_ID',\n}\n\n// Here starts internal entities that should not be used directly.\n\n/**\n * @internal this class is meant for internal use only and may be changed without notice. The SmartAuthProvider will be initialised automatically\n * by the iCure api depending on the authentication options you provide.\n *\n * An authentication provider that automatically requests secrets for authentication as needed.\n *\n * This authentication provider can be initialised already with some secrets or tokens, and the provider will cache them and use them as needed for\n * as long as they remain valid. If at any point however the provider needs an updated secret or a secret of a different kind it will automatically\n * request this to the {@link SmartAuthProvider} to get the secret.\n *\n * An advantage of using this provider over others is that in case all the cached tokens and secrets were to expire while performing a request,\n * instead of having the request fail the provider will ask for new secrets from the {@link SmartAuthProvider} and the request will automatically\n * be retried with the new secret. Additionally, the provider may request updated secrets also for performing some sensitive operations (e.g. changing\n * password of the user) even if the cached tokens and/or did not expire. This could be the case for example if the cached secret is a long-lived\n * token, but in order to change the password the user needs to provide his current password.\n *\n * Note that in this context the cache of secrets and token is in memory only, and is not persisted in any way. Different instances of this provider\n * will not share the same cache.\n *\n */\nexport class SmartAuthProvider implements AuthenticationProvider {\n /**\n * Initialises a {@link SmartAuthProvider}.\n * @param authApi an \"anonymous\" {@link IccAuthApi} to use for authentication.\n * @param login\n * @param secretProvider\n * @param props optional initialisation properties.\n */\n static initialise(\n authApi: IccAuthApi,\n login: string,\n secretProvider: AuthSecretProvider,\n props: {\n initialSecret?: string\n initialAuthToken?: string\n initialRefreshToken?: string\n loginGroupId?: string\n } = {}\n ): SmartAuthProvider {\n return new SmartAuthProvider(\n new TokenProvider(\n login,\n props.loginGroupId,\n props.initialSecret ? { value: props.initialSecret, type: undefined } : undefined,\n props.initialAuthToken,\n props.initialRefreshToken,\n authApi,\n secretProvider\n ),\n props.loginGroupId\n )\n }\n\n private constructor(private readonly tokenProvider: TokenProvider, private readonly groupId: string | undefined) {}\n\n getAuthService(): AuthService {\n return new SmartAuthService(this.tokenProvider)\n }\n\n async switchGroup(newGroupId: string, matches: Array<UserGroup>): Promise<AuthenticationProvider> {\n if (newGroupId == this.groupId) return Promise.resolve(this)\n if (!matches.find((match) => match.groupId == newGroupId)) throw new Error('New group id not found in matches.')\n const switchedProvider = await this.tokenProvider.switchedGroup(newGroupId)\n return new SmartAuthProvider(switchedProvider, this.groupId)\n }\n\n getIcureTokens(): Promise<{ token: string; refreshToken: string } | undefined> {\n return Promise.resolve(undefined)\n }\n}\n\nenum ServerAuthenticationClass {\n // DIGITAL_ID = 60,\n TWO_FACTOR_AUTHENTICATION = 50,\n SHORT_LIVED_TOKEN = 40,\n // EXTERNAL_AUTHENTICATION = 30,\n PASSWORD = 20,\n LONG_LIVED_TOKEN = 10,\n}\ntype LongLivedSecretType =\n | ServerAuthenticationClass.LONG_LIVED_TOKEN\n | ServerAuthenticationClass.PASSWORD\n | ServerAuthenticationClass.TWO_FACTOR_AUTHENTICATION\nclass TokenProvider {\n constructor(\n private login: string,\n private groupId: string | undefined,\n private currentLongLivedSecret: { value: string; type: LongLivedSecretType | undefined } | undefined,\n private cachedToken: string | undefined,\n private cachedRefreshToken: string | undefined,\n private readonly authApi: IccAuthApi,\n private readonly authSecretProvider: AuthSecretProvider\n ) {}\n\n async getCachedOrRefreshedOrNewToken(): Promise<{ token: string; type: RetrievedTokenType }> {\n if (!!this.cachedToken && !isJwtInvalidOrExpired(this.cachedToken)) {\n return { token: this.cachedToken, type: RetrievedTokenType.CACHED }\n } else if (!!this.cachedRefreshToken && !isJwtInvalidOrExpired(this.cachedRefreshToken)) {\n return this.refreshAndCacheToken(this.cachedRefreshToken)\n } else {\n return { token: await this.getAndCacheNewToken(undefined), type: RetrievedTokenType.NEW }\n }\n }\n\n async getNewTokenWithClass(minimumAuthenticationClass: number): Promise<string> {\n return await this.getAndCacheNewToken(minimumAuthenticationClass)\n }\n\n private async getAndCacheNewToken(minimumAuthenticationClassLevel: number | undefined): Promise<string> {\n const { token, refreshToken } = await this.getNewToken(minimumAuthenticationClassLevel ?? 0)\n this.cachedToken = token\n this.cachedRefreshToken = refreshToken\n return token\n }\n\n private async refreshAndCacheToken(refreshToken: string): Promise<{ token: string; type: RetrievedTokenType }> {\n return await this.authApi.refreshAuthenticationJWT(refreshToken).then(\n (authResult) => {\n if (!authResult.token) throw new Error('Internal error: refresh succeeded but no token was returned. Unsupported backend version?')\n this.cachedToken = authResult.token\n return { token: authResult.token, type: RetrievedTokenType.REFRESHED }\n },\n async () => ({ token: await this.getAndCacheNewToken(undefined), type: RetrievedTokenType.NEW })\n )\n }\n\n private async getNewToken(minimumAuthenticationClassLevel: number): Promise<{ token: string; refreshToken: string }> {\n if (!!this.currentLongLivedSecret && (!this.currentLongLivedSecret.type || this.currentLongLivedSecret.type >= minimumAuthenticationClassLevel)) {\n const resultWithCachedSecret = await this.doGetTokenWithSecret(this.currentLongLivedSecret.value, minimumAuthenticationClassLevel)\n if ('success' in resultWithCachedSecret) {\n return resultWithCachedSecret.success\n } else if (\n resultWithCachedSecret.failure === DoGetTokenResultFailureReason.NEEDS_2FA &&\n minimumAuthenticationClassLevel <= ServerAuthenticationClass.TWO_FACTOR_AUTHENTICATION\n ) {\n return this.askTotpAndGetToken(this.currentLongLivedSecret.value, minimumAuthenticationClassLevel)\n } else return this.askSecretAndGetToken(minimumAuthenticationClassLevel, true)\n } else {\n return this.askSecretAndGetToken(minimumAuthenticationClassLevel, true)\n }\n }\n\n private async askSecretAndGetToken(\n minimumAuthenticationClassLevel: number,\n passwordIsValidAs2fa: boolean\n ): Promise<{ token: string; refreshToken: string }> {\n const acceptedSecrets = [\n minimumAuthenticationClassLevel <= ServerAuthenticationClass.LONG_LIVED_TOKEN ? [AuthSecretType.LONG_LIVED_TOKEN] : [],\n minimumAuthenticationClassLevel <= ServerAuthenticationClass.SHORT_LIVED_TOKEN ? [AuthSecretType.SHORT_LIVED_TOKEN] : [],\n minimumAuthenticationClassLevel <= ServerAuthenticationClass.TWO_FACTOR_AUTHENTICATION &&\n (passwordIsValidAs2fa || minimumAuthenticationClassLevel <= ServerAuthenticationClass.PASSWORD)\n ? [AuthSecretType.PASSWORD]\n : [],\n ].flat()\n if (!acceptedSecrets.length)\n throw new Error('Internal error: no secret type is accepted for this request. Group may be misconfigured, or client may be outdated.')\n const attempts: { secret: string; secretType: AuthSecretType }[] = []\n while (true) {\n const { secret, secretType } = await this.authSecretProvider.getSecret([...acceptedSecrets], attempts)\n if (!acceptedSecrets.includes(secretType))\n throw new Error(`Accepted secret types are ${JSON.stringify(acceptedSecrets)}, but got a secret of type ${secretType}.`)\n attempts.push({ secret, secretType })\n const result = await this.doGetTokenWithSecret(secret, minimumAuthenticationClassLevel)\n if ('success' in result) {\n this.updateCachedSecret(secret, secretType)\n return result.success\n } else if (result.failure == DoGetTokenResultFailureReason.NEEDS_2FA) {\n return this.askTotpAndGetToken(secret, minimumAuthenticationClassLevel)\n } else if (secretType == AuthSecretType.PASSWORD && result.failure == DoGetTokenResultFailureReason.INVALID_AUTH_CLASS_LEVEL) {\n return this.askSecretAndGetToken(minimumAuthenticationClassLevel, false)\n } // else retry\n }\n }\n\n private async askTotpAndGetToken(password: string, minimumAuthenticationClassLevel: number): Promise<{ token: string; refreshToken: string }> {\n if (minimumAuthenticationClassLevel > ServerAuthenticationClass.TWO_FACTOR_AUTHENTICATION)\n throw new Error(\n \"Internal error: asking for totp to login but minimumAuthenticationClassLevel is higher than TWO_FACTOR_AUTHENTICATION's level.\"\n )\n const attempts: { secret: string; secretType: AuthSecretType }[] = []\n while (true) {\n const { secret, secretType } = await this.authSecretProvider.getSecret([AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN], attempts)\n if (secretType != AuthSecretType.TWO_FACTOR_AUTHENTICATION_TOKEN)\n throw new Error(`Was expecting a 2fa token but got a secret of type ${secretType}.`)\n attempts.push({ secret, secretType })\n const result = await this.doGetTokenWithSecret(`${password}|${secret}`, minimumAuthenticationClassLevel)\n if ('success' in result) {\n this.updateCachedSecret(password, AuthSecretType.PASSWORD)\n return result.success\n } else if (result.failure != DoGetTokenResultFailureReason.INVALID_2FA) {\n throw new Error(`Unexpected error while trying to login with (previously) valid password and 2fa token ${result.failure}.`)\n } // else retry\n }\n }\n\n private async doGetTokenWithSecret(secret: string, minimumAuthenticationClassLevel: number): Promise<DoGetTokenResult> {\n return this.authApi.login({ username: this.login, password: secret }, this.groupId).then(\n (authResult) => {\n const { token, refreshToken } = authResult\n if (!token || !refreshToken) throw new Error('Internal error: login succeeded but no token was returned. Unsupported backend version?')\n const claims = decodeJwtClaims(token)\n const authClassLevel = claims['tac']\n if (!authClassLevel || typeof authClassLevel !== 'number')\n throw new Error('Internal error: authClassLevel is not a number. Unsupported backend version?')\n if (authClassLevel < minimumAuthenticationClassLevel) {\n return { failure: DoGetTokenResultFailureReason.INVALID_AUTH_CLASS_LEVEL }\n } else {\n return { success: { token, refreshToken } }\n }\n },\n (error) => {\n if (!(error instanceof XHRError)) throw error\n if (error.statusCode == 401 || error.statusCode == 412) {\n // Password is wrong (401) or unacceptable (e.g. too short, 412)\n return { failure: DoGetTokenResultFailureReason.INVALID_PW_OR_TOKEN }\n } else if (error.statusCode == 406) {\n // Password is correct, but 2fa token is not\n return { failure: DoGetTokenResultFailureReason.INVALID_2FA }\n } else if (error.statusCode == 417) {\n // Password is correct, but the user has 2fa enabled and no 2fa token was provided\n return { failure: DoGetTokenResultFailureReason.NEEDS_2FA }\n } else throw error\n }\n )\n }\n\n async switchedGroup(newGroupId: string): Promise<TokenProvider> {\n const groupSwitchedTokens = this.cachedRefreshToken\n ? await this.authApi.switchGroup(this.cachedRefreshToken, newGroupId).then(\n (response) => {\n if (!response.token || !response.refreshToken)\n throw new Error('Internal error: group switch succeeded but no token was returned. Unsupported backend version?')\n return { token: response.token, refreshToken: response.refreshToken }\n },\n () => ({ token: undefined, refreshToken: undefined })\n )\n : { token: undefined, refreshToken: undefined }\n return new TokenProvider(\n this.login,\n newGroupId,\n this.currentLongLivedSecret ? { value: this.currentLongLivedSecret.value, type: undefined } : undefined,\n groupSwitchedTokens.token,\n groupSwitchedTokens.refreshToken,\n this.authApi,\n this.authSecretProvider\n )\n }\n\n private updateCachedSecret(secret: string, secretType: AuthSecretType) {\n if (secretType == AuthSecretType.LONG_LIVED_TOKEN || secretType == AuthSecretType.PASSWORD) {\n this.currentLongLivedSecret = { value: secret, type: ServerAuthenticationClass.LONG_LIVED_TOKEN }\n }\n }\n}\ntype DoGetTokenResult = { success: { token: string; refreshToken: string } } | { failure: DoGetTokenResultFailureReason }\nenum DoGetTokenResultFailureReason {\n NEEDS_2FA,\n INVALID_2FA,\n INVALID_PW_OR_TOKEN,\n INVALID_AUTH_CLASS_LEVEL,\n}\nenum RetrievedTokenType {\n CACHED,\n REFRESHED,\n NEW,\n}\n\nenum SmartAuthServiceState {\n INITIAL,\n DONE_INITIAL,\n REATTEMPT,\n REATTEMPTED_WITH_NEW_UNBOUND_TOKEN,\n REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN,\n EXPECT_REQUEST_WITH_SPECIFIC_AUTH_CLASS,\n TERMINAL_ERROR,\n}\nclass SmartAuthService implements AuthService {\n private currentState:\n | { id: SmartAuthServiceState.INITIAL }\n | { id: SmartAuthServiceState.DONE_INITIAL; initialToken: string }\n | { id: SmartAuthServiceState.REATTEMPT; initialToken: string; initialError: Error }\n | { id: SmartAuthServiceState.REATTEMPTED_WITH_NEW_UNBOUND_TOKEN }\n | { id: SmartAuthServiceState.REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN }\n | { id: SmartAuthServiceState.EXPECT_REQUEST_WITH_SPECIFIC_AUTH_CLASS; errorFromNewToken: Error }\n | { id: SmartAuthServiceState.TERMINAL_ERROR; error: Error } = { id: SmartAuthServiceState.INITIAL }\n\n constructor(private readonly tokenProvider: TokenProvider) {}\n\n async getAuthHeaders(minimumAuthenticationClassLevel: number | undefined): Promise<Array<XHR.Header>> {\n return [new XHR.Header('Authorization', `Bearer ${await this.getAuthToken(minimumAuthenticationClassLevel)}`)]\n }\n\n private async getAuthToken(minimumAuthenticationClassLevel: number | undefined): Promise<string> {\n switch (this.currentState.id) {\n case SmartAuthServiceState.INITIAL:\n if (minimumAuthenticationClassLevel != undefined) {\n throw new Error('Illegal state: cannot ask for a specific auth class level at the first request attempt.')\n } else {\n const { token } = await this.tokenProvider.getCachedOrRefreshedOrNewToken()\n this.currentState = { id: SmartAuthServiceState.DONE_INITIAL, initialToken: token }\n return token\n }\n case SmartAuthServiceState.REATTEMPT:\n if (minimumAuthenticationClassLevel != undefined) {\n const token = await this.tokenProvider.getNewTokenWithClass(minimumAuthenticationClassLevel)\n this.currentState = { id: SmartAuthServiceState.REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN }\n return token\n } else {\n const { token } = await this.tokenProvider.getCachedOrRefreshedOrNewToken()\n if (token == this.currentState.initialToken) throw this.currentState.initialError\n this.currentState = { id: SmartAuthServiceState.REATTEMPTED_WITH_NEW_UNBOUND_TOKEN }\n return token\n }\n case SmartAuthServiceState.EXPECT_REQUEST_WITH_SPECIFIC_AUTH_CLASS:\n if (minimumAuthenticationClassLevel != undefined) {\n const token = await this.tokenProvider.getNewTokenWithClass(minimumAuthenticationClassLevel)\n this.currentState = { id: SmartAuthServiceState.REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN }\n return token\n } else throw this.currentState.errorFromNewToken\n case SmartAuthServiceState.TERMINAL_ERROR:\n throw this.currentState.error\n default:\n throw new Error(`Illegal state: cannot get token in state ${this.currentState.id}.`)\n }\n }\n\n invalidateHeader(error: Error): void {\n switch (this.currentState.id) {\n case SmartAuthServiceState.DONE_INITIAL:\n this.currentState = { id: SmartAuthServiceState.REATTEMPT, initialToken: this.currentState.initialToken, initialError: error }\n break\n case SmartAuthServiceState.REATTEMPTED_WITH_NEW_UNBOUND_TOKEN:\n this.currentState = { id: SmartAuthServiceState.EXPECT_REQUEST_WITH_SPECIFIC_AUTH_CLASS, errorFromNewToken: error }\n break\n case SmartAuthServiceState.REATTEMPTED_WITH_AUTH_CLASS_SPECIFIC_TOKEN:\n this.currentState = { id: SmartAuthServiceState.TERMINAL_ERROR, error: error }\n break\n default:\n throw new Error(`Illegal state: cannot invalidate header in state ${this.currentState.id}.`)\n }\n }\n}\n"]}