Skip to content

Commit

Permalink
Update cdk stack
Browse files Browse the repository at this point in the history
Add api resource
Add cognito authorizer to new api resource
Add SRP authentication flow to user pool client
Output user pool ID
  • Loading branch information
Liam-Driscoll committed Nov 16, 2023
1 parent 4ebde84 commit d5d5e77
Showing 1 changed file with 50 additions and 39 deletions.
89 changes: 50 additions & 39 deletions cdk/lib/cdk-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,45 @@ export class CdkStack extends cdk.Stack {
environment: {'EMAIL_ADDRESS': ''}
});

// Cognito
const adminPool = new cognito.UserPool(this, 'adminuserpool', {
userPoolName: 'harmreduction-adminpool',
signInCaseSensitive: false,
selfSignUpEnabled: false,
mfa: cognito.Mfa.OFF,
passwordPolicy: {
minLength: 8,
requireLowercase: true,
requireUppercase: true,
requireDigits: true,
requireSymbols: true,
tempPasswordValidity: cdk.Duration.days(3),
},
accountRecovery: cognito.AccountRecovery.NONE,
deviceTracking: {
challengeRequiredOnNewDevice: false,
deviceOnlyRememberedOnUserPrompt: false
},
removalPolicy: cdk.RemovalPolicy.DESTROY,
});

const adminPoolClient = adminPool.addClient('adminpoolclient', {
authFlows: {
userPassword: true,
userSrp: true,
}
});

new cdk.CfnOutput(this, 'CognitoClientID', {
value: adminPoolClient.userPoolClientId,
description: 'Cognito user pool Client ID'
});

new cdk.CfnOutput(this, 'CognitoUserPoolID', {
value: adminPool.userPoolId,
description: 'Cognito user pool ID'
});

const prdLogGroup = new logs.LogGroup(this, "PrdLogs");

const OTPapi = new apigateway.RestApi(this, 'OTPapi', {
Expand Down Expand Up @@ -106,8 +145,14 @@ export class CdkStack extends cdk.Stack {
},
});

const cognitoAuthorizer = new apigateway.CognitoUserPoolsAuthorizer(this, 'CognitoAuthorizer', {
cognitoUserPools: [adminPool],
identitySource: 'method.request.header.Authorization',
});

const DBSample = DBapi.root.addResource('samples');
const DBUser = DBapi.root.addResource('users');
const DBAdmin = DBapi.root.addResource('admin');

DBSample.addMethod('POST', new apigateway.LambdaIntegration(DBApiHandler, {proxy: true}), {apiKeyRequired: true});
DBSample.addMethod('GET', new apigateway.LambdaIntegration(DBApiHandler, {proxy: true}), {apiKeyRequired: true});
Expand All @@ -122,6 +167,11 @@ export class CdkStack extends cdk.Stack {
DBUser.addMethod('PUT', new apigateway.LambdaIntegration(DBApiHandler, {proxy: true}), {apiKeyRequired: true});
DBUser.addMethod('DELETE', new apigateway.LambdaIntegration(DBApiHandler, {proxy: true}), {apiKeyRequired: true});
// DBUser.addMethod('OPTIONS', new apigateway.LambdaIntegration(DBApiHandler, {proxy: true}));
DBAdmin.addMethod('GET', new apigateway.LambdaIntegration(DBApiHandler, {proxy: true}), {
authorizationType: apigateway.AuthorizationType.COGNITO,
authorizer: cognitoAuthorizer,
apiKeyRequired: true
});

const methodSettingProperty: apigateway.CfnDeployment.MethodSettingProperty = {
cacheDataEncrypted: false,
Expand Down Expand Up @@ -203,45 +253,6 @@ export class CdkStack extends cdk.Stack {
batchSize: 1,
}))

// Cognito
const adminPool = new cognito.UserPool(this, 'adminuserpool', {
userPoolName: 'harmreduction-adminpool',
signInCaseSensitive: false,
selfSignUpEnabled: false,
mfa: cognito.Mfa.OFF,
passwordPolicy: {
minLength: 8,
requireLowercase: true,
requireUppercase: true,
requireDigits: true,
requireSymbols: true,
tempPasswordValidity: cdk.Duration.days(3),
},
accountRecovery: cognito.AccountRecovery.NONE,
deviceTracking: {
challengeRequiredOnNewDevice: false,
deviceOnlyRememberedOnUserPrompt: false
},
removalPolicy: cdk.RemovalPolicy.DESTROY,
});

const adminPoolClient = adminPool.addClient('adminpoolclient', {
authFlows: {
userPassword: true,
userSrp: true,
}
});

new cdk.CfnOutput(this, 'CognitoClientID', {
value: adminPoolClient.userPoolClientId,
description: 'Cognito user pool Client ID'
});

new cdk.CfnOutput(this, 'CognitoUserPoolID', {
value: adminPool.userPoolId,
description: 'Cognito user pool ID'
});

// Store the gateway ARN for use with our WAF stack
const apiGatewayARN = `arn:aws:apigateway:${Stack.of(this).region}::/restapis/${DBapi.restApiId}/stages/${DBapi.deploymentStage.stageName}`

Expand Down

0 comments on commit d5d5e77

Please sign in to comment.