Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: headless update auth #5308

Merged
merged 4 commits into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 3 additions & 41 deletions packages/amplify-category-auth/src/commands/auth/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ const { messages } = require('../../provider-utils/awscloudformation/assets/stri
const { getAuthResourceName } = require('../../utils/getAuthResourceName');
const { transformUserPoolGroupSchema } = require('../../provider-utils/awscloudformation/utils/transform-user-pool-group');
const path = require('path');
const { category } = require('../..');
const { attachPrevParamsToContext } = require('../../provider-utils/awscloudformation/utils/attach-prev-params-to-context');

const subcommand = 'update';
const category = 'auth';
let options;

module.exports = {
Expand Down Expand Up @@ -48,48 +49,9 @@ module.exports = {
context.print.error('Provider not configured for this category');
return;
}
return providerController.updateResource(context, category, options);
return providerController.updateResource(context, options);
})
.then(async name => {
// eslint-disable-line no-shadow
const resourceDirPath = path.join(amplify.pathManager.getBackendDirPath(), 'auth', name, 'parameters.json');
const authParameters = amplify.readJsonFile(resourceDirPath);
if (authParameters.dependsOn) {
amplify.updateamplifyMetaAfterResourceUpdate(category, name, 'dependsOn', authParameters.dependsOn);
}

let customAuthConfigured = false;
if (authParameters.triggers) {
const triggers = JSON.parse(authParameters.triggers);
customAuthConfigured =
triggers.DefineAuthChallenge &&
triggers.DefineAuthChallenge.length > 0 &&
triggers.CreateAuthChallenge &&
triggers.CreateAuthChallenge.length > 0 &&
triggers.VerifyAuthChallengeResponse &&
triggers.VerifyAuthChallengeResponse.length > 0;
}
amplify.updateamplifyMetaAfterResourceUpdate(category, resourceName, 'customAuth', customAuthConfigured);

// Update Identity Pool dependency attributes on userpool groups
const allResources = context.amplify.getProjectMeta();
if (allResources.auth && allResources.auth.userPoolGroups) {
let attributes = ['UserPoolId', 'AppClientIDWeb', 'AppClientID'];
if (authParameters.identityPoolName) {
attributes.push('IdentityPoolId');
}
const userPoolGroupDependsOn = [
{
category: 'auth',
resourceName,
attributes,
},
];

amplify.updateamplifyMetaAfterResourceUpdate('auth', 'userPoolGroups', 'dependsOn', userPoolGroupDependsOn);
await transformUserPoolGroupSchema(context);
}

const { print } = context;
print.success(`Successfully updated resource ${name} locally`);
print.info('');
Expand Down
13 changes: 10 additions & 3 deletions packages/amplify-category-auth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ const { ENV_SPECIFIC_PARAMS } = require('./provider-utils/awscloudformation/cons

const { transformUserPoolGroupSchema } = require('./provider-utils/awscloudformation/utils/transform-user-pool-group');
const { uploadFiles } = require('./provider-utils/awscloudformation/utils/trigger-file-uploader');
const { validateAddAuthRequest } = require('amplify-util-headless-input');
const { getAddAuthRequestAdaptor } = require('./provider-utils/awscloudformation/utils/add-auth-request-adaptor');
const { getAddAuthHandler } = require('./provider-utils/awscloudformation/handlers/get-add-auth-handler');
const { validateAddAuthRequest, validateUpdateAuthRequest } = require('amplify-util-headless-input');
const { getAddAuthRequestAdaptor, getUpdateAuthRequestAdaptor } = require('./provider-utils/awscloudformation/utils/auth-request-adaptors');
const { getAddAuthHandler, getUpdateAuthHandler } = require('./provider-utils/awscloudformation/handlers/resource-handlers');
const { projectHasAuth } = require('./provider-utils/awscloudformation/utils/project-has-auth');
const { attachPrevParamsToContext } = require('./provider-utils/awscloudformation/utils/attach-prev-params-to-context');

// this function is being kept for temporary compatability.
async function add(context) {
Expand Down Expand Up @@ -316,6 +317,12 @@ const executeAmplifyHeadlessCommand = async (context, headlessPayload) => {
.then(getAddAuthRequestAdaptor(context.amplify.getProjectConfig().frontend))
.then(getAddAuthHandler(context));
return;
case 'update':
await attachPrevParamsToContext(context);
await validateUpdateAuthRequest(headlessPayload)
.then(getUpdateAuthRequestAdaptor(context.amplify.getProjectConfig().frontend, context.updatingAuth.requiredAttributes))
.then(getUpdateAuthHandler(context));
return;
default:
context.print.error(`Headless mode for ${context.input.command} auth is not implemented yet`);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const safeDefaults = [
'userpoolClientRefreshTokenValidity',
];

export const protectedValues = [
// These attributes cannot be modified once the auth resource is created
export const immutableAttributes = [
'resourceName',
'userPoolName',
'identityPoolName',
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { ServiceQuestionsResult } from '../service-walkthrough-types';
import { getAddAuthDefaultsApplier, getUpdateAuthDefaultsApplier } from '../utils/auth-defaults-appliers';
import { getResourceSynthesizer, getResourceUpdater } from '../utils/synthesize-resources';
import { getPostAddAuthMetaUpdater, getPostUpdateAuthMetaUpdater } from '../utils/amplify-meta-updaters';
import { getPostAddAuthMessagePrinter, getPostUpdateAuthMessagePrinter } from '../utils/message-printer';
import { supportedServices } from '../../supported-services';

/**
* Factory function that returns a ServiceQuestionsResult consumer that handles all of the resource generation logic.
* The consumer returns the resourceName of the generated resource.
* @param context The amplify context
*/
export const getAddAuthHandler = (context: any) => async (request: ServiceQuestionsResult) => {
const serviceMetadata = supportedServices[request.serviceName];
const { cfnFilename, defaultValuesFilename, provider } = serviceMetadata;
let projectName = context.amplify.getProjectConfig().projectName.toLowerCase();
const disallowedChars = /[^A-Za-z0-9]+/g;
projectName = projectName.replace(disallowedChars, '');
const requestWithDefaults = await getAddAuthDefaultsApplier(context, defaultValuesFilename, projectName)(request);
await getResourceSynthesizer(
context,
cfnFilename,
provider,
)(requestWithDefaults)
.then(req => req.resourceName!)
.then(getPostAddAuthMetaUpdater(context, { service: requestWithDefaults.serviceName, providerName: provider }))
.then(getPostAddAuthMessagePrinter(context.print))
.catch(err => {
context.print.info(err.stack);
context.print.error('There was an error adding the auth resource');
context.usageData.emitError(err);
process.exitCode = 1;
});
return requestWithDefaults.resourceName!;
};

export const getUpdateAuthHandler = (context: any) => async (request: ServiceQuestionsResult) => {
const { cfnFilename, defaultValuesFilename, provider } = supportedServices[request.serviceName];
const requestWithDefaults = await getUpdateAuthDefaultsApplier(defaultValuesFilename, context.updatingAuth)(request);
await getResourceUpdater(
context,
cfnFilename,
provider,
)(requestWithDefaults)
.then(req => req.resourceName!)
.then(getPostUpdateAuthMetaUpdater(context))
.then(getPostUpdateAuthMessagePrinter(context.print))
.catch(err => {
context.print.info(err.stack);
context.print.error('There was an error updating the auth resource');
context.usageData.emitError(err);
process.exitCode = 1;
});
return requestWithDefaults.resourceName!;
};
Loading