-
Notifications
You must be signed in to change notification settings - Fork 588
/
index.ts
37 lines (32 loc) · 1.34 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { ContextPlugin } from "@webiny/handler/plugins/ContextPlugin";
import { SecurityContext, SecurityIdentity } from "@webiny/api-security/types";
import { createAuthenticator, Config as CognitoConfig } from "@webiny/api-cognito-authenticator";
export interface Config extends CognitoConfig {
identityType: string;
getIdentity?<TIdentity extends SecurityIdentity = SecurityIdentity>(params: {
identityType: string;
token: { [key: string]: any };
}): TIdentity;
}
export default (config: Config) => {
const cognitoAuthenticator = createAuthenticator({
region: config.region,
userPoolId: config.userPoolId
});
return new ContextPlugin<SecurityContext>(({ security }) => {
security.addAuthenticator(async token => {
const tokenObj = await cognitoAuthenticator(token);
if (typeof config.getIdentity === "function") {
return config.getIdentity({ identityType: config.identityType, token: tokenObj });
}
return {
id: tokenObj.sub,
type: config.identityType,
displayName: `${tokenObj.given_name} ${tokenObj.family_name}`,
email: tokenObj.email,
firstName: tokenObj.given_name,
lastName: tokenObj.family_name
};
});
});
};