@node-c/domain-iam 1.0.0-beta6 → 1.0.0-beta8
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/common/definitions/common.constants.d.ts +13 -9
- package/dist/common/definitions/common.constants.js +13 -10
- package/dist/common/definitions/common.constants.js.map +1 -1
- package/dist/services/authentication/iam.authentication.definitions.d.ts +11 -2
- package/dist/services/authentication/iam.authentication.definitions.js +1 -1
- package/dist/services/authentication/iam.authentication.service.d.ts +9 -4
- package/dist/services/authentication/iam.authentication.service.js +95 -4
- package/dist/services/authentication/iam.authentication.service.js.map +1 -1
- package/dist/services/authenticationConsumer/iam.authenticationConsumer.definitions.d.ts +15 -0
- package/dist/services/authenticationConsumer/iam.authenticationConsumer.definitions.js +3 -0
- package/dist/services/authenticationConsumer/iam.authenticationConsumer.definitions.js.map +1 -0
- package/dist/services/authenticationConsumer/iam.authenticationConsumer.service.d.ts +12 -0
- package/dist/services/authenticationConsumer/iam.authenticationConsumer.service.js +135 -0
- package/dist/services/authenticationConsumer/iam.authenticationConsumer.service.js.map +1 -0
- package/dist/services/authenticationConsumer/index.d.ts +2 -0
- package/dist/services/authenticationConsumer/index.js +19 -0
- package/dist/services/authenticationConsumer/index.js.map +1 -0
- package/dist/services/{userManager/iam.userManager.definitions.d.ts → authenticationManager/iam.authenticationManager.definitions.d.ts} +11 -15
- package/dist/services/authenticationManager/iam.authenticationManager.definitions.js +8 -0
- package/dist/services/authenticationManager/iam.authenticationManager.definitions.js.map +1 -0
- package/dist/services/authenticationManager/iam.authenticationManager.service.d.ts +33 -0
- package/dist/services/{userManager/iam.userManager.service.js → authenticationManager/iam.authenticationManager.service.js} +140 -72
- package/dist/services/authenticationManager/iam.authenticationManager.service.js.map +1 -0
- package/dist/services/authenticationManager/index.d.ts +2 -0
- package/dist/services/authenticationManager/index.js +19 -0
- package/dist/services/authenticationManager/index.js.map +1 -0
- package/dist/services/authenticationOAuth2/iam.authenticationOAuth2.definitions.d.ts +5 -2
- package/dist/services/authenticationOAuth2/iam.authenticationOAuth2.service.d.ts +2 -14
- package/dist/services/authenticationOAuth2/iam.authenticationOAuth2.service.js +53 -95
- package/dist/services/authenticationOAuth2/iam.authenticationOAuth2.service.js.map +1 -1
- package/dist/services/authenticationOAuth2Consumer/iam.authenticationOAuth2Consumer.definitions.d.ts +19 -0
- package/dist/services/authenticationOAuth2Consumer/iam.authenticationOAuth2Consumer.definitions.js +3 -0
- package/dist/services/authenticationOAuth2Consumer/iam.authenticationOAuth2Consumer.definitions.js.map +1 -0
- package/dist/services/authenticationOAuth2Consumer/iam.authenticationOAuth2Consumer.service.d.ts +11 -0
- package/dist/services/authenticationOAuth2Consumer/iam.authenticationOAuth2Consumer.service.js +69 -0
- package/dist/services/authenticationOAuth2Consumer/iam.authenticationOAuth2Consumer.service.js.map +1 -0
- package/dist/services/authenticationOAuth2Consumer/index.d.ts +2 -0
- package/dist/services/authenticationOAuth2Consumer/index.js +19 -0
- package/dist/services/authenticationOAuth2Consumer/index.js.map +1 -0
- package/dist/services/authenticationPassthrough/iam.authenticationPassthrough.definitions.d.ts +14 -0
- package/dist/services/authenticationPassthrough/iam.authenticationPassthrough.definitions.js +3 -0
- package/dist/services/authenticationPassthrough/iam.authenticationPassthrough.definitions.js.map +1 -0
- package/dist/services/authenticationPassthrough/iam.authenticationPassthrough.service.d.ts +9 -0
- package/dist/services/authenticationPassthrough/iam.authenticationPassthrough.service.js +70 -0
- package/dist/services/authenticationPassthrough/iam.authenticationPassthrough.service.js.map +1 -0
- package/dist/services/authenticationPassthrough/index.d.ts +2 -0
- package/dist/services/authenticationPassthrough/index.js +19 -0
- package/dist/services/authenticationPassthrough/index.js.map +1 -0
- package/dist/services/authenticationPassthroughConsumer/iam.authenticationPassthroughConsumer.definitions.d.ts +18 -0
- package/dist/services/authenticationPassthroughConsumer/iam.authenticationPassthroughConsumer.definitions.js +3 -0
- package/dist/services/authenticationPassthroughConsumer/iam.authenticationPassthroughConsumer.definitions.js.map +1 -0
- package/dist/services/authenticationPassthroughConsumer/iam.authenticationPassthroughConsumer.service.d.ts +10 -0
- package/dist/services/authenticationPassthroughConsumer/iam.authenticationPassthroughConsumer.service.js +68 -0
- package/dist/services/authenticationPassthroughConsumer/iam.authenticationPassthroughConsumer.service.js.map +1 -0
- package/dist/services/authenticationPassthroughConsumer/index.d.ts +2 -0
- package/dist/services/authenticationPassthroughConsumer/index.js +19 -0
- package/dist/services/authenticationPassthroughConsumer/index.js.map +1 -0
- package/dist/services/authenticationUserLocal/iam.authenticationUserLocal.definitions.d.ts +2 -2
- package/dist/services/authenticationUserLocal/iam.authenticationUserLocal.service.d.ts +2 -6
- package/dist/services/authenticationUserLocal/iam.authenticationUserLocal.service.js +4 -8
- package/dist/services/authenticationUserLocal/iam.authenticationUserLocal.service.js.map +1 -1
- package/dist/services/authenticationUserLocalConsumer/iam.authenticationUserLocalConsumer.definitions.d.ts +8 -0
- package/dist/services/authenticationUserLocalConsumer/iam.authenticationUserLocalConsumer.definitions.js +3 -0
- package/dist/services/authenticationUserLocalConsumer/iam.authenticationUserLocalConsumer.definitions.js.map +1 -0
- package/dist/services/authenticationUserLocalConsumer/iam.authenticationUserLocalConsumer.service.d.ts +8 -0
- package/dist/services/authenticationUserLocalConsumer/iam.authenticationUserLocalConsumer.service.js +36 -0
- package/dist/services/authenticationUserLocalConsumer/iam.authenticationUserLocalConsumer.service.js.map +1 -0
- package/dist/services/authenticationUserLocalConsumer/index.d.ts +2 -0
- package/dist/services/authenticationUserLocalConsumer/index.js +19 -0
- package/dist/services/authenticationUserLocalConsumer/index.js.map +1 -0
- package/dist/services/authorization/iam.authorization.definitions.d.ts +24 -24
- package/dist/services/authorization/iam.authorization.definitions.js +7 -7
- package/dist/services/authorization/iam.authorization.definitions.js.map +1 -1
- package/dist/services/authorization/iam.authorization.service.d.ts +8 -11
- package/dist/services/authorization/iam.authorization.service.js +26 -30
- package/dist/services/authorization/iam.authorization.service.js.map +1 -1
- package/dist/services/index.d.ts +7 -1
- package/dist/services/index.js +7 -1
- package/dist/services/index.js.map +1 -1
- package/dist/services/tokenManager/iam.tokenManager.definitions.d.ts +4 -1
- package/dist/services/tokenManager/iam.tokenManager.definitions.js.map +1 -1
- package/dist/services/tokenManager/iam.tokenManager.service.d.ts +2 -2
- package/dist/services/tokenManager/iam.tokenManager.service.js +73 -35
- package/dist/services/tokenManager/iam.tokenManager.service.js.map +1 -1
- package/dist/services/users/index.d.ts +2 -0
- package/dist/services/{userManager → users}/index.js +2 -2
- package/dist/services/users/index.js.map +1 -0
- package/dist/services/users/users.definitions.d.ts +7 -0
- package/dist/services/users/users.definitions.js +3 -0
- package/dist/services/users/users.definitions.js.map +1 -0
- package/dist/services/users/users.service.d.ts +6 -0
- package/dist/services/users/users.service.js +39 -0
- package/dist/services/users/users.service.js.map +1 -0
- package/package.json +2 -2
- package/src/common/definitions/common.constants.ts +13 -16
- package/src/services/authentication/iam.authentication.definitions.ts +11 -4
- package/src/services/authentication/iam.authentication.service.ts +79 -15
- package/src/services/authenticationConsumer/iam.authenticationConsumer.definitions.ts +43 -0
- package/src/services/authenticationConsumer/iam.authenticationConsumer.service.ts +192 -0
- package/src/services/authenticationConsumer/index.ts +2 -0
- package/src/services/{userManager/iam.userManager.definitions.ts → authenticationManager/iam.authenticationManager.definitions.ts} +11 -19
- package/src/services/{userManager/iam.userManager.service.ts → authenticationManager/iam.authenticationManager.service.ts} +174 -111
- package/src/services/authenticationManager/index.ts +2 -0
- package/src/services/authenticationOAuth2/iam.authenticationOAuth2.definitions.ts +6 -3
- package/src/services/authenticationOAuth2/iam.authenticationOAuth2.service.ts +97 -85
- package/src/services/authenticationOAuth2Consumer/iam.authenticationOAuth2Consumer.definitions.ts +56 -0
- package/src/services/authenticationOAuth2Consumer/iam.authenticationOAuth2Consumer.service.ts +93 -0
- package/src/services/authenticationOAuth2Consumer/index.ts +2 -0
- package/src/services/authenticationPassthrough/iam.authenticationPassthrough.definitions.ts +32 -0
- package/src/services/authenticationPassthrough/iam.authenticationPassthrough.service.ts +100 -0
- package/src/services/authenticationPassthrough/index.ts +2 -0
- package/src/services/authenticationPassthroughConsumer/iam.authenticationPassthroughConsumer.definitions.ts +52 -0
- package/src/services/authenticationPassthroughConsumer/iam.authenticationPassthroughConsumer.service.ts +96 -0
- package/src/services/authenticationPassthroughConsumer/index.ts +2 -0
- package/src/services/authenticationUserLocal/iam.authenticationUserLocal.definitions.ts +3 -3
- package/src/services/authenticationUserLocal/iam.authenticationUserLocal.service.ts +17 -12
- package/src/services/authenticationUserLocalConsumer/iam.authenticationUserLocalConsumer.definitions.ts +29 -0
- package/src/services/authenticationUserLocalConsumer/iam.authenticationUserLocalConsumer.service.ts +40 -0
- package/src/services/authenticationUserLocalConsumer/index.ts +2 -0
- package/src/services/authorization/iam.authorization.definitions.ts +26 -27
- package/src/services/authorization/iam.authorization.service.ts +40 -65
- package/src/services/index.ts +7 -1
- package/src/services/tokenManager/iam.tokenManager.definitions.ts +4 -1
- package/src/services/tokenManager/iam.tokenManager.service.ts +98 -45
- package/src/services/users/index.ts +2 -0
- package/src/services/users/users.definitions.ts +11 -0
- package/src/services/users/users.service.ts +46 -0
- package/dist/services/userManager/iam.userManager.definitions.js +0 -8
- package/dist/services/userManager/iam.userManager.definitions.js.map +0 -1
- package/dist/services/userManager/iam.userManager.service.d.ts +0 -33
- package/dist/services/userManager/iam.userManager.service.js.map +0 -1
- package/dist/services/userManager/index.d.ts +0 -2
- package/dist/services/userManager/index.js.map +0 -1
- package/src/services/userManager/index.ts +0 -2
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { ApplicationError, ConfigProviderService, LoggerService } from '@node-c/core';
|
|
2
2
|
|
|
3
|
+
import * as jwt from 'jsonwebtoken';
|
|
4
|
+
|
|
3
5
|
import {
|
|
4
6
|
IAMAuthenticationCompleteData,
|
|
5
7
|
IAMAuthenticationCompleteOptions,
|
|
6
8
|
IAMAuthenticationCompleteResult,
|
|
7
9
|
IAMAuthenticationGetPayloadsFromExternalTokensData,
|
|
8
10
|
IAMAuthenticationGetPayloadsFromExternalTokensResult,
|
|
9
|
-
|
|
11
|
+
IAMAuthenticationGetUserAuthenticationConfigResult,
|
|
10
12
|
IAMAuthenticationGetUserDataFromExternalTokenPayloadsData,
|
|
11
13
|
IAMAuthenticationGetUserDataFromExternalTokenPayloadsResult,
|
|
12
14
|
IAMAuthenticationInitiateData,
|
|
@@ -15,9 +17,12 @@ import {
|
|
|
15
17
|
IAMAuthenticationRefreshExternalAccessTokenData,
|
|
16
18
|
IAMAuthenticationRefreshExternalAccessTokenResult,
|
|
17
19
|
IAMAuthenticationVerifyExternalAccessTokenData,
|
|
18
|
-
IAMAuthenticationVerifyExternalAccessTokenResult
|
|
20
|
+
IAMAuthenticationVerifyExternalAccessTokenResult,
|
|
21
|
+
IAMAuthenticationVerifyTokenOptions
|
|
19
22
|
} from './iam.authentication.definitions';
|
|
20
23
|
|
|
24
|
+
import { Constants } from '../../common/definitions';
|
|
25
|
+
|
|
21
26
|
export class IAMAuthenticationService<CompleteContext extends object, InitiateContext extends object> {
|
|
22
27
|
protected isLocal: boolean;
|
|
23
28
|
|
|
@@ -27,11 +32,13 @@ export class IAMAuthenticationService<CompleteContext extends object, InitiateCo
|
|
|
27
32
|
// eslint-disable-next-line no-unused-vars
|
|
28
33
|
protected logger: LoggerService,
|
|
29
34
|
// eslint-disable-next-line no-unused-vars
|
|
30
|
-
protected moduleName: string
|
|
35
|
+
protected moduleName: string,
|
|
36
|
+
// eslint-disable-next-line no-unused-vars
|
|
37
|
+
protected serviceName: string
|
|
31
38
|
) {}
|
|
32
39
|
|
|
33
|
-
|
|
34
|
-
* Step 2 of the
|
|
40
|
+
/**
|
|
41
|
+
* Step 2 of the authentication process. Mandatory.
|
|
35
42
|
*/
|
|
36
43
|
async complete(
|
|
37
44
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -42,26 +49,42 @@ export class IAMAuthenticationService<CompleteContext extends object, InitiateCo
|
|
|
42
49
|
throw new ApplicationError(`[${this.moduleName}][IAMAuthenticationService]: Method "complete" not implemented.`);
|
|
43
50
|
}
|
|
44
51
|
|
|
45
|
-
|
|
52
|
+
getUserAuthenticationConfig(): IAMAuthenticationGetUserAuthenticationConfigResult {
|
|
46
53
|
throw new ApplicationError(
|
|
47
54
|
`[${this.moduleName}][IAMAuthenticationService]: Method "getUserAccessTokenConfig" not implemented.`
|
|
48
55
|
);
|
|
49
56
|
}
|
|
50
57
|
|
|
51
|
-
|
|
58
|
+
/**
|
|
52
59
|
* Method for decoding JWTs and returning their payloads.
|
|
60
|
+
*
|
|
53
61
|
* If the tokens aren't JWTs, other ways for retreiving the payloads can be implemented, such as the OAuth introspection endpoint.
|
|
54
62
|
*/
|
|
55
63
|
async getPayloadsFromExternalTokens(
|
|
56
|
-
|
|
57
|
-
_data: IAMAuthenticationGetPayloadsFromExternalTokensData
|
|
64
|
+
data: IAMAuthenticationGetPayloadsFromExternalTokensData
|
|
58
65
|
): Promise<IAMAuthenticationGetPayloadsFromExternalTokensResult> {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
66
|
+
const { logger, moduleName, serviceName } = this;
|
|
67
|
+
const { accessToken, idToken } = data;
|
|
68
|
+
const returnData: IAMAuthenticationGetPayloadsFromExternalTokensResult = {};
|
|
69
|
+
if (accessToken) {
|
|
70
|
+
const { content: accessTokenPayload, error } = await this.verifyToken(accessToken);
|
|
71
|
+
if (error) {
|
|
72
|
+
logger.error(
|
|
73
|
+
`[${moduleName}][${serviceName}]: Method "getPayloadsFromExternalTokens" has produced an error:`,
|
|
74
|
+
error
|
|
75
|
+
);
|
|
76
|
+
throw new ApplicationError(`[${moduleName}][${serviceName}]: Error getting data from external tokens.`);
|
|
77
|
+
}
|
|
78
|
+
returnData.accessTokenPayload = accessTokenPayload;
|
|
79
|
+
}
|
|
80
|
+
if (idToken) {
|
|
81
|
+
const idTokenData = await this.verifyToken(idToken);
|
|
82
|
+
returnData.idTokenPayload = idTokenData.content;
|
|
83
|
+
}
|
|
84
|
+
return returnData;
|
|
62
85
|
}
|
|
63
86
|
|
|
64
|
-
|
|
87
|
+
/**
|
|
65
88
|
* Method for mapping token payload data, such as username and scopes, to local user data, such as email and roles.
|
|
66
89
|
*/
|
|
67
90
|
async getUserDataFromExternalTokenPayloads(
|
|
@@ -73,8 +96,8 @@ export class IAMAuthenticationService<CompleteContext extends object, InitiateCo
|
|
|
73
96
|
);
|
|
74
97
|
}
|
|
75
98
|
|
|
76
|
-
|
|
77
|
-
* Step 1 of the
|
|
99
|
+
/**
|
|
100
|
+
* Step 1 of the authentication process. Mandatory.
|
|
78
101
|
*/
|
|
79
102
|
async initiate(
|
|
80
103
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -102,4 +125,45 @@ export class IAMAuthenticationService<CompleteContext extends object, InitiateCo
|
|
|
102
125
|
`[${this.moduleName}][IAMAuthenticationService]: Method "verifyExternalAccessToken" not implemented.`
|
|
103
126
|
);
|
|
104
127
|
}
|
|
128
|
+
|
|
129
|
+
async verifyToken<DecodedTokenContent = unknown>(
|
|
130
|
+
token: string,
|
|
131
|
+
options?: IAMAuthenticationVerifyTokenOptions
|
|
132
|
+
): Promise<{ content?: DecodedTokenContent; error?: unknown }> {
|
|
133
|
+
const { audiences, issuer, secret } = options || {};
|
|
134
|
+
let returnData: { content?: DecodedTokenContent; error?: unknown } = {};
|
|
135
|
+
if (secret) {
|
|
136
|
+
returnData = await new Promise<{ content?: DecodedTokenContent; error?: unknown }>(resolve => {
|
|
137
|
+
jwt.verify(token, secret, (err, decoded) => {
|
|
138
|
+
if (err) {
|
|
139
|
+
resolve({ content: decoded as DecodedTokenContent, error: err });
|
|
140
|
+
}
|
|
141
|
+
resolve({ content: decoded as DecodedTokenContent });
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
} else {
|
|
145
|
+
const tokenContent = jwt.decode(token) as DecodedTokenContent & { aud?: string; exp?: number; iss?: string };
|
|
146
|
+
if (tokenContent.exp) {
|
|
147
|
+
// tokenContent.exp < new Date().valueOf()
|
|
148
|
+
let currentTimeStamp = `${new Date().valueOf()}`;
|
|
149
|
+
let expString = `${tokenContent.exp}`;
|
|
150
|
+
if (expString.length < currentTimeStamp.length) {
|
|
151
|
+
currentTimeStamp = currentTimeStamp.substring(0, expString.length);
|
|
152
|
+
} else if (expString.length > currentTimeStamp.length) {
|
|
153
|
+
expString = expString.substring(0, currentTimeStamp.length);
|
|
154
|
+
}
|
|
155
|
+
if (parseInt(expString, 10) < parseInt(currentTimeStamp, 10)) {
|
|
156
|
+
returnData.error = Constants.TOKEN_EXPIRED_ERROR;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
if (tokenContent.aud && audiences && !audiences.includes(tokenContent.aud)) {
|
|
160
|
+
returnData.error = Constants.TOKEN_MISMATCHED_AUDIENCES_ERROR;
|
|
161
|
+
}
|
|
162
|
+
if (tokenContent.iss && issuer && issuer !== tokenContent.iss) {
|
|
163
|
+
returnData.error = Constants.TOKEN_MISMATCHED_ISSUER_ERROR;
|
|
164
|
+
}
|
|
165
|
+
returnData.content = tokenContent;
|
|
166
|
+
}
|
|
167
|
+
return returnData;
|
|
168
|
+
}
|
|
105
169
|
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IAMAuthenticationCompleteData,
|
|
3
|
+
IAMAuthenticationCompleteOptions,
|
|
4
|
+
IAMAuthenticationCompleteResult,
|
|
5
|
+
IAMAuthenticationGetUserAuthenticationConfigResult,
|
|
6
|
+
IAMAuthenticationGetUserDataFromExternalTokenPayloadsData,
|
|
7
|
+
IAMAuthenticationGetUserDataFromExternalTokenPayloadsResult,
|
|
8
|
+
IAMAuthenticationInitiateData,
|
|
9
|
+
IAMAuthenticationInitiateOptions,
|
|
10
|
+
IAMAuthenticationInitiateResult,
|
|
11
|
+
IAMAuthenticationRefreshExternalAccessTokenData,
|
|
12
|
+
IAMAuthenticationRefreshExternalAccessTokenResult
|
|
13
|
+
} from '../authentication';
|
|
14
|
+
|
|
15
|
+
export type IAMAuthenticationConsumerCompleteData = IAMAuthenticationCompleteData;
|
|
16
|
+
|
|
17
|
+
export type IAMAuthenticationConsumerCompleteOptions<Context extends object> =
|
|
18
|
+
IAMAuthenticationCompleteOptions<Context>;
|
|
19
|
+
|
|
20
|
+
export interface IAMAuthenticationConsumerCompleteResult extends IAMAuthenticationCompleteResult {
|
|
21
|
+
idToken?: string;
|
|
22
|
+
refreshToken?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type IAMAuthenticationConsumerGetUserAuthenticationConfigResult =
|
|
26
|
+
IAMAuthenticationGetUserAuthenticationConfigResult;
|
|
27
|
+
|
|
28
|
+
export type IAMAuthenticationConsumerGetUserDataFromExternalTokenPayloadsData =
|
|
29
|
+
IAMAuthenticationGetUserDataFromExternalTokenPayloadsData;
|
|
30
|
+
|
|
31
|
+
export type IAMAuthenticationConsumerGetUserDataFromExternalTokenPayloadsResult =
|
|
32
|
+
IAMAuthenticationGetUserDataFromExternalTokenPayloadsResult;
|
|
33
|
+
|
|
34
|
+
export type IAMAuthenticationConsumerInitiateData = IAMAuthenticationInitiateData;
|
|
35
|
+
|
|
36
|
+
export type IAMAuthenticationConsumerInitiateOptions<Context extends object> =
|
|
37
|
+
IAMAuthenticationInitiateOptions<Context>;
|
|
38
|
+
|
|
39
|
+
export type IAMAuthenticationConsumerInitiateResult = IAMAuthenticationInitiateResult;
|
|
40
|
+
|
|
41
|
+
export type IAMAuthenticationConsumerRefreshExternalAccessTokenData = IAMAuthenticationRefreshExternalAccessTokenData;
|
|
42
|
+
export type IAMAuthenticationConsumerRefreshExternalAccessTokenResult =
|
|
43
|
+
IAMAuthenticationRefreshExternalAccessTokenResult;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AppConfigDomainIAM,
|
|
3
|
+
AppConfigDomainIAMAuthenticationStep,
|
|
4
|
+
ApplicationError,
|
|
5
|
+
ConfigProviderService,
|
|
6
|
+
GenericObject,
|
|
7
|
+
HttpMethod,
|
|
8
|
+
LoggerService,
|
|
9
|
+
httpRequest
|
|
10
|
+
} from '@node-c/core';
|
|
11
|
+
|
|
12
|
+
import ld from 'lodash';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
IAMAuthenticationConsumerCompleteData,
|
|
16
|
+
IAMAuthenticationConsumerCompleteOptions,
|
|
17
|
+
IAMAuthenticationConsumerCompleteResult,
|
|
18
|
+
IAMAuthenticationConsumerGetUserAuthenticationConfigResult,
|
|
19
|
+
IAMAuthenticationConsumerGetUserDataFromExternalTokenPayloadsData,
|
|
20
|
+
IAMAuthenticationConsumerGetUserDataFromExternalTokenPayloadsResult,
|
|
21
|
+
IAMAuthenticationConsumerInitiateData,
|
|
22
|
+
IAMAuthenticationConsumerInitiateOptions,
|
|
23
|
+
IAMAuthenticationConsumerInitiateResult,
|
|
24
|
+
IAMAuthenticationConsumerRefreshExternalAccessTokenData,
|
|
25
|
+
IAMAuthenticationConsumerRefreshExternalAccessTokenResult
|
|
26
|
+
} from './iam.authenticationConsumer.definitions';
|
|
27
|
+
|
|
28
|
+
import { IAMAuthenticationService } from '../authentication';
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The base service for integrating authenticationServices via other Node-C Apps as a consumer.
|
|
32
|
+
*
|
|
33
|
+
* This service is intended to be extended by services that will be used in the consumer environment.
|
|
34
|
+
*/
|
|
35
|
+
export class IAMAuthenticationConsumerService<
|
|
36
|
+
CompleteContext extends object,
|
|
37
|
+
InitiateContext extends object
|
|
38
|
+
> extends IAMAuthenticationService<CompleteContext, InitiateContext> {
|
|
39
|
+
constructor(configProvider: ConfigProviderService, logger: LoggerService, moduleName: string, serviceName: string) {
|
|
40
|
+
super(configProvider, logger, moduleName, serviceName);
|
|
41
|
+
this.isLocal = false;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async complete(
|
|
45
|
+
data: IAMAuthenticationConsumerCompleteData,
|
|
46
|
+
options: IAMAuthenticationConsumerCompleteOptions<CompleteContext>
|
|
47
|
+
): Promise<IAMAuthenticationConsumerCompleteResult> {
|
|
48
|
+
const responseData = await this.runRequest<IAMAuthenticationConsumerCompleteResult>(
|
|
49
|
+
AppConfigDomainIAMAuthenticationStep.Complete,
|
|
50
|
+
{
|
|
51
|
+
auth: { ...data, type: this.serviceName },
|
|
52
|
+
step: AppConfigDomainIAMAuthenticationStep.Complete,
|
|
53
|
+
...(options?.contextIdentifierField
|
|
54
|
+
? {
|
|
55
|
+
filters: {
|
|
56
|
+
[options.contextIdentifierField]:
|
|
57
|
+
options.context[options.contextIdentifierField as keyof CompleteContext]
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
: {})
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
return {
|
|
64
|
+
...responseData,
|
|
65
|
+
valid: typeof responseData.valid !== 'undefined' ? responseData.valid : !!responseData.accessToken?.length
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* This config is intended for use by the consumer environment.
|
|
71
|
+
*
|
|
72
|
+
* User data from: provider
|
|
73
|
+
*
|
|
74
|
+
* Internal tokens from: provider
|
|
75
|
+
*
|
|
76
|
+
* External tokens from: provider
|
|
77
|
+
*
|
|
78
|
+
* Authentication happens in: provider
|
|
79
|
+
*/
|
|
80
|
+
getUserAuthenticationConfig(): IAMAuthenticationConsumerGetUserAuthenticationConfigResult {
|
|
81
|
+
const { configProvider, moduleName, serviceName } = this;
|
|
82
|
+
const moduleConfig = configProvider.config.domain[moduleName] as AppConfigDomainIAM;
|
|
83
|
+
const { steps } = moduleConfig.authServiceSettings![serviceName];
|
|
84
|
+
const defaultConfig: IAMAuthenticationConsumerGetUserAuthenticationConfigResult = {
|
|
85
|
+
// this step just extracts the user data from the returned data and saves it in the consumer environment,
|
|
86
|
+
// together with the tokens
|
|
87
|
+
[AppConfigDomainIAMAuthenticationStep.Complete]: {
|
|
88
|
+
authReturnsTokens: true,
|
|
89
|
+
decodeReturnedTokens: true,
|
|
90
|
+
findUser: true,
|
|
91
|
+
findUserBeforeAuth: false,
|
|
92
|
+
findUserInExternalTokenPayloads: true,
|
|
93
|
+
useReturnedTokens: true,
|
|
94
|
+
useReturnedTokensAsLocal: true,
|
|
95
|
+
validWithoutUser: false
|
|
96
|
+
},
|
|
97
|
+
// this step simply does nothing
|
|
98
|
+
[AppConfigDomainIAMAuthenticationStep.Initiate]: {
|
|
99
|
+
findUser: false,
|
|
100
|
+
validWithoutUser: true
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
return ld.merge(defaultConfig, steps || {});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async getUserDataFromExternalTokenPayloads(
|
|
107
|
+
data: IAMAuthenticationConsumerGetUserDataFromExternalTokenPayloadsData
|
|
108
|
+
): Promise<IAMAuthenticationConsumerGetUserDataFromExternalTokenPayloadsResult | null> {
|
|
109
|
+
const { idTokenPayload } = data;
|
|
110
|
+
if (!idTokenPayload?.data?.user) {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return idTokenPayload.data.user as unknown as IAMAuthenticationConsumerGetUserDataFromExternalTokenPayloadsResult;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async initiate(
|
|
117
|
+
data: IAMAuthenticationConsumerInitiateData,
|
|
118
|
+
options: IAMAuthenticationConsumerInitiateOptions<InitiateContext>
|
|
119
|
+
): Promise<IAMAuthenticationConsumerInitiateResult> {
|
|
120
|
+
const responseData = await this.runRequest<
|
|
121
|
+
IAMAuthenticationConsumerInitiateResult | IAMAuthenticationConsumerCompleteResult
|
|
122
|
+
>(AppConfigDomainIAMAuthenticationStep.Initiate, {
|
|
123
|
+
auth: { ...data, type: this.serviceName },
|
|
124
|
+
step: AppConfigDomainIAMAuthenticationStep.Initiate,
|
|
125
|
+
...(options?.contextIdentifierField
|
|
126
|
+
? {
|
|
127
|
+
filters: {
|
|
128
|
+
[options.contextIdentifierField]: options.context[options.contextIdentifierField as keyof InitiateContext]
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
: {})
|
|
132
|
+
});
|
|
133
|
+
return {
|
|
134
|
+
...responseData,
|
|
135
|
+
valid:
|
|
136
|
+
typeof responseData.valid !== 'undefined'
|
|
137
|
+
? responseData.valid
|
|
138
|
+
: 'accessToken' in responseData && !!responseData.accessToken?.length,
|
|
139
|
+
...('nextStepsRequired' in responseData && responseData.nextStepsRequired
|
|
140
|
+
? { mfaUsed: true, mfaValid: true }
|
|
141
|
+
: { mfaUsed: false })
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
protected async runRequest<ReturnData>(
|
|
146
|
+
endpoint: AppConfigDomainIAMAuthenticationStep | 'refreshExternalAccessToken',
|
|
147
|
+
data: GenericObject
|
|
148
|
+
): Promise<ReturnData> {
|
|
149
|
+
const { configProvider, logger, moduleName, serviceName } = this;
|
|
150
|
+
const moduleConfig = configProvider.config.domain[moduleName] as AppConfigDomainIAM;
|
|
151
|
+
const { apiKey, apiSecret, apiSecretHashingAlgorithm, baseUrl, ...configData } =
|
|
152
|
+
moduleConfig.authServiceSettings![serviceName].nodeC!;
|
|
153
|
+
const endpointMethod = configData[`${endpoint}EndpointMethod`];
|
|
154
|
+
const endpointUri = configData[`${endpoint}Endpoint`];
|
|
155
|
+
if (!baseUrl) {
|
|
156
|
+
logger.error(`[${moduleName}][${serviceName}]: Base URL not configured.`);
|
|
157
|
+
throw new ApplicationError('Authentication failed.');
|
|
158
|
+
}
|
|
159
|
+
if (!endpointUri) {
|
|
160
|
+
logger.error(`[${moduleName}][${serviceName}]: Endpoint URI for "${endpoint}" not configured.`);
|
|
161
|
+
throw new ApplicationError('Authentication failed.');
|
|
162
|
+
}
|
|
163
|
+
if (!endpointMethod) {
|
|
164
|
+
logger.error(`[${moduleName}][${serviceName}]: Endpoint method for "${endpoint}" not configured.`);
|
|
165
|
+
throw new ApplicationError('Authentication failed.');
|
|
166
|
+
}
|
|
167
|
+
const { data: responseData, hasError } = await httpRequest<ReturnData>(`${baseUrl}${endpointUri}`, {
|
|
168
|
+
apiKey,
|
|
169
|
+
apiSecret,
|
|
170
|
+
apiSecretHashingAlgorithm,
|
|
171
|
+
isJSON: true,
|
|
172
|
+
method: endpointMethod,
|
|
173
|
+
...(endpointMethod === HttpMethod.GET ? { query: data } : { body: data })
|
|
174
|
+
});
|
|
175
|
+
if (hasError || !responseData) {
|
|
176
|
+
logger.error(`[${moduleName}][${serviceName}]: Endpoint ${endpointUri} failed.`, responseData);
|
|
177
|
+
throw new ApplicationError('Authentication failed.');
|
|
178
|
+
}
|
|
179
|
+
return responseData;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
async refreshExternalAccessToken(
|
|
183
|
+
data: IAMAuthenticationConsumerRefreshExternalAccessTokenData
|
|
184
|
+
): Promise<IAMAuthenticationConsumerRefreshExternalAccessTokenResult> {
|
|
185
|
+
return await this.runRequest<IAMAuthenticationConsumerRefreshExternalAccessTokenResult>(
|
|
186
|
+
'refreshExternalAccessToken',
|
|
187
|
+
{
|
|
188
|
+
data
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
@@ -2,7 +2,6 @@ import {
|
|
|
2
2
|
AppConfigCommonDomainIAMAuthServiceConfigCompleteSettings,
|
|
3
3
|
AppConfigCommonDomainIAMAuthServiceConfigInitiateSettings,
|
|
4
4
|
AppConfigDomainIAMAuthenticationStep,
|
|
5
|
-
DomainFindOnePrivateOptions,
|
|
6
5
|
GenericObject
|
|
7
6
|
} from '@node-c/core';
|
|
8
7
|
|
|
@@ -12,10 +11,10 @@ import {
|
|
|
12
11
|
IAMAuthenticationService,
|
|
13
12
|
IAMAuthenticationType
|
|
14
13
|
} from '../authentication';
|
|
15
|
-
import { AuthorizationUser } from '../authorization';
|
|
16
14
|
import { IAMMFAType } from '../mfa';
|
|
15
|
+
import { IAMUserWithPermissionsData } from '../users';
|
|
17
16
|
|
|
18
|
-
export interface
|
|
17
|
+
export interface IAMAuthenticationManagerAuthenticateOptions<AuthData = unknown> {
|
|
19
18
|
auth: {
|
|
20
19
|
mfaType?: IAMMFAType;
|
|
21
20
|
type: IAMAuthenticationType | string;
|
|
@@ -26,7 +25,7 @@ export interface IAMUserManagerCreateAccessTokenOptions<AuthData = unknown> {
|
|
|
26
25
|
step?: AppConfigDomainIAMAuthenticationStep;
|
|
27
26
|
}
|
|
28
27
|
|
|
29
|
-
export type
|
|
28
|
+
export type IAMAuthenticationManagerAuthenticateReturnData<UserData> =
|
|
30
29
|
| {
|
|
31
30
|
accessToken: string;
|
|
32
31
|
idToken: string;
|
|
@@ -35,12 +34,12 @@ export type IAMUserManagerCreateAccessTokenReturnData<UserData> =
|
|
|
35
34
|
}
|
|
36
35
|
| { nextStepsRequired: boolean };
|
|
37
36
|
|
|
38
|
-
export type
|
|
39
|
-
|
|
37
|
+
export type IAMAuthenticationManagerExecuteStepData<AuthData = unknown> = Omit<
|
|
38
|
+
IAMAuthenticationManagerAuthenticateOptions<AuthData>,
|
|
40
39
|
'rememberUser' | 'step'
|
|
41
40
|
>;
|
|
42
41
|
|
|
43
|
-
export interface
|
|
42
|
+
export interface IAMAuthenticationManagerExecuteStepOptions<User extends object> {
|
|
44
43
|
authService: IAMAuthenticationService<User, User>;
|
|
45
44
|
name: AppConfigDomainIAMAuthenticationStep;
|
|
46
45
|
stepConfig:
|
|
@@ -48,28 +47,21 @@ export interface IAMUserManagerExecuteStepOptions<User extends object> {
|
|
|
48
47
|
| AppConfigCommonDomainIAMAuthServiceConfigInitiateSettings;
|
|
49
48
|
}
|
|
50
49
|
|
|
51
|
-
export interface
|
|
50
|
+
export interface IAMAuthenticationManagerExecuteStepResult<User extends object> {
|
|
52
51
|
stepResult: IAMAuthenticationCompleteResult | IAMAuthenticationInitiateResult;
|
|
53
|
-
user:
|
|
52
|
+
user: IAMUserWithPermissionsData<User, unknown> | null;
|
|
54
53
|
userFilterField?: string | undefined;
|
|
55
54
|
userFilterValue?: unknown | undefined;
|
|
56
55
|
}
|
|
57
56
|
|
|
58
|
-
export interface
|
|
59
|
-
keepPassword?: boolean;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type IAMUserManagerUserWithPermissionsData<UserData, AuthorizationPointId> =
|
|
63
|
-
AuthorizationUser<AuthorizationPointId> & UserData;
|
|
64
|
-
|
|
65
|
-
export interface IAMUserManagerUserTokenEnityFields<UserId = unknown> {
|
|
57
|
+
export interface IAMAuthenticationManagerUserTokenEnityFields<UserId = unknown> {
|
|
66
58
|
accessToken?: string;
|
|
67
59
|
refreshToken?: string;
|
|
68
60
|
userId: UserId;
|
|
69
|
-
user?:
|
|
61
|
+
user?: IAMUserWithPermissionsData<object, unknown>;
|
|
70
62
|
}
|
|
71
63
|
|
|
72
|
-
export enum
|
|
64
|
+
export enum IAMAuthenticationManagerUserTokenUserIdentifier {
|
|
73
65
|
// eslint-disable-next-line no-unused-vars
|
|
74
66
|
FieldName = 'userId'
|
|
75
67
|
}
|