-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use nodejs 18 for auth lambdas
- Loading branch information
Showing
11 changed files
with
400 additions
and
359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
188 changes: 96 additions & 92 deletions
188
packages/amplify-category-auth/resources/auth-custom-resource/hostedUIProviderLambda.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,108 @@ | ||
const response = require('cfn-response'); | ||
const aws = require('aws-sdk'); | ||
const identity = new aws.CognitoIdentityServiceProvider(); | ||
exports.handler = (event, context, callback) => { | ||
try { | ||
const userPoolId = event.ResourceProperties.userPoolId; | ||
let hostedUIProviderMeta = JSON.parse(event.ResourceProperties.hostedUIProviderMeta); | ||
let hostedUIProviderCreds = JSON.parse(event.ResourceProperties.hostedUIProviderCreds); | ||
if (hostedUIProviderCreds.length === 0) { | ||
response.send(event, context, response.SUCCESS, {}); | ||
const { | ||
CognitoIdentityProviderClient, | ||
CreateIdentityProviderCommand, | ||
DeleteIdentityProviderCommand, | ||
UpdateIdentityProviderCommand, | ||
} = require('@aws-sdk/client-cognito-identity-provider'); | ||
const identity = new CognitoIdentityProviderClient({}); | ||
|
||
exports.handler = (event, context) => { | ||
// Don't return promise, response.send() marks context as done internally | ||
const ignoredPromise = handleEvent(event, context); | ||
}; | ||
|
||
function getRequestParams(providerName, hostedUIProviderMeta, hostedUIProviderCreds, userPoolId) { | ||
const providerMetaIndex = hostedUIProviderMeta.findIndex((provider) => provider.ProviderName === providerName); | ||
const providerMeta = hostedUIProviderMeta[providerMetaIndex]; | ||
const providerCredsIndex = hostedUIProviderCreds.findIndex((provider) => provider.ProviderName === providerName); | ||
const providerCreds = hostedUIProviderCreds[providerCredsIndex]; | ||
let requestParams = { | ||
ProviderName: providerMeta.ProviderName, | ||
UserPoolId: userPoolId, | ||
AttributeMapping: providerMeta.AttributeMapping, | ||
}; | ||
if (providerMeta.ProviderName === 'SignInWithApple') { | ||
if (providerCreds.client_id && providerCreds.team_id && providerCreds.key_id && providerCreds.private_key) { | ||
requestParams.ProviderDetails = { | ||
client_id: providerCreds.client_id, | ||
team_id: providerCreds.team_id, | ||
key_id: providerCreds.key_id, | ||
private_key: providerCreds.private_key, | ||
authorize_scopes: providerMeta.authorize_scopes, | ||
}; | ||
} else { | ||
requestParams = null; | ||
} | ||
if (event.RequestType == 'Delete') { | ||
response.send(event, context, response.SUCCESS, {}); | ||
} else { | ||
if (providerCreds.client_id && providerCreds.client_secret) { | ||
requestParams.ProviderDetails = { | ||
client_id: providerCreds.client_id, | ||
client_secret: providerCreds.client_secret, | ||
authorize_scopes: providerMeta.authorize_scopes, | ||
}; | ||
} else { | ||
requestParams = null; | ||
} | ||
if (event.RequestType == 'Update' || event.RequestType == 'Create') { | ||
let getRequestParams = (providerName) => { | ||
let providerMetaIndex = hostedUIProviderMeta.findIndex((provider) => provider.ProviderName === providerName); | ||
let providerMeta = hostedUIProviderMeta[providerMetaIndex]; | ||
let providerCredsIndex = hostedUIProviderCreds.findIndex((provider) => provider.ProviderName === providerName); | ||
let providerCreds = hostedUIProviderCreds[providerCredsIndex]; | ||
let requestParams = { | ||
ProviderName: providerMeta.ProviderName, | ||
UserPoolId: userPoolId, | ||
AttributeMapping: providerMeta.AttributeMapping, | ||
}; | ||
if (providerMeta.ProviderName === 'SignInWithApple') { | ||
if (providerCreds.client_id && providerCreds.team_id && providerCreds.key_id && providerCreds.private_key) { | ||
requestParams.ProviderDetails = { | ||
client_id: providerCreds.client_id, | ||
team_id: providerCreds.team_id, | ||
key_id: providerCreds.key_id, | ||
private_key: providerCreds.private_key, | ||
authorize_scopes: providerMeta.authorize_scopes, | ||
}; | ||
} else { | ||
requestParams = null; | ||
} | ||
} else { | ||
if (providerCreds.client_id && providerCreds.client_secret) { | ||
requestParams.ProviderDetails = { | ||
client_id: providerCreds.client_id, | ||
client_secret: providerCreds.client_secret, | ||
authorize_scopes: providerMeta.authorize_scopes, | ||
}; | ||
} | ||
return requestParams; | ||
} | ||
|
||
async function createIdentityProvider(providerName, hostedUIProviderMeta, hostedUIProviderCreds, userPoolId) { | ||
const requestParams = getRequestParams(providerName, hostedUIProviderMeta, hostedUIProviderCreds, userPoolId); | ||
if (!requestParams) { | ||
return; | ||
} | ||
requestParams.ProviderType = requestParams.ProviderName; | ||
await identity.send(new CreateIdentityProviderCommand(requestParams)); | ||
} | ||
|
||
async function updateIdentityProvider(providerName, hostedUIProviderMeta, hostedUIProviderCreds, userPoolId) { | ||
const requestParams = getRequestParams(providerName, hostedUIProviderMeta, hostedUIProviderCreds, userPoolId); | ||
if (!requestParams) { | ||
return; | ||
} | ||
await identity.send(new UpdateIdentityProviderCommand(requestParams)); | ||
} | ||
|
||
async function deleteIdentityProvider(providerName, userPoolId) { | ||
const params = { ProviderName: providerName, UserPoolId: userPoolId }; | ||
await identity.send(new DeleteIdentityProviderCommand(params)); | ||
} | ||
|
||
async function handleEvent(event, context) { | ||
try { | ||
const userPoolId = event.ResourceProperties.userPoolId; | ||
const hostedUIProviderMeta = JSON.parse(event.ResourceProperties.hostedUIProviderMeta); | ||
const hostedUIProviderCreds = JSON.parse(event.ResourceProperties.hostedUIProviderCreds); | ||
if (hostedUIProviderCreds.length !== 0) { | ||
if (event.RequestType === 'Update' || event.RequestType === 'Create') { | ||
const listIdentityProvidersResponse = await identity | ||
.listIdentityProviders({ | ||
UserPoolId: userPoolId, | ||
MaxResults: 60, | ||
}) | ||
.promise(); | ||
console.log(listIdentityProvidersResponse); | ||
const providerList = listIdentityProvidersResponse.Providers.map((provider) => provider.ProviderName); | ||
const providerListInParameters = hostedUIProviderMeta.map((provider) => provider.ProviderName); | ||
for (const providerMetadata of hostedUIProviderMeta) { | ||
if (providerList.indexOf(providerMetadata.ProviderName) > -1) { | ||
await updateIdentityProvider(providerMetadata.ProviderName, hostedUIProviderMeta, hostedUIProviderCreds, userPoolId); | ||
} else { | ||
requestParams = null; | ||
await createIdentityProvider(providerMetadata.ProviderName, hostedUIProviderMeta, hostedUIProviderCreds, userPoolId); | ||
} | ||
} | ||
return requestParams; | ||
}; | ||
let createIdentityProvider = (providerName) => { | ||
let requestParams = getRequestParams(providerName); | ||
if (!requestParams) { | ||
return Promise.resolve(); | ||
} | ||
requestParams.ProviderType = requestParams.ProviderName; | ||
return identity.createIdentityProvider(requestParams).promise(); | ||
}; | ||
let updateIdentityProvider = (providerName) => { | ||
let requestParams = getRequestParams(providerName); | ||
if (!requestParams) { | ||
return Promise.resolve(); | ||
for (const provider of providerList) { | ||
if (providerListInParameters.indexOf(provider) < 0) { | ||
await deleteIdentityProvider(provider, userPoolId); | ||
} | ||
} | ||
return identity.updateIdentityProvider(requestParams).promise(); | ||
}; | ||
let deleteIdentityProvider = (providerName) => { | ||
let params = { ProviderName: providerName, UserPoolId: userPoolId }; | ||
return identity.deleteIdentityProvider(params).promise(); | ||
}; | ||
let providerPromises = []; | ||
identity | ||
.listIdentityProviders({ UserPoolId: userPoolId, MaxResults: 60 }) | ||
.promise() | ||
.then((result) => { | ||
console.log(result); | ||
let providerList = result.Providers.map((provider) => provider.ProviderName); | ||
let providerListInParameters = hostedUIProviderMeta.map((provider) => provider.ProviderName); | ||
hostedUIProviderMeta.forEach((providerMetadata) => { | ||
if (providerList.indexOf(providerMetadata.ProviderName) > -1) { | ||
providerPromises.push(updateIdentityProvider(providerMetadata.ProviderName)); | ||
} else { | ||
providerPromises.push(createIdentityProvider(providerMetadata.ProviderName)); | ||
} | ||
}); | ||
providerList.forEach((provider) => { | ||
if (providerListInParameters.indexOf(provider) < 0) { | ||
providerPromises.push(deleteIdentityProvider(provider)); | ||
} | ||
}); | ||
return Promise.all(providerPromises); | ||
}) | ||
.then(() => { | ||
response.send(event, context, response.SUCCESS, {}); | ||
}) | ||
.catch((err) => { | ||
console.log(err.stack); | ||
response.send(event, context, response.FAILED, { err }); | ||
}); | ||
} | ||
} | ||
response.send(event, context, response.SUCCESS, {}); | ||
} catch (err) { | ||
console.log(err.stack); | ||
response.send(event, context, response.FAILED, { err }); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.