Skip to content

Commit

Permalink
refactor: cleaned up eslint and ensure consistency across packages. (#34
Browse files Browse the repository at this point in the history
)

* ci: maintain consistency across various packages.
* refactor: linted files with new consistent configuration.
  • Loading branch information
hwelsters authored Dec 20, 2023
1 parent 85a194d commit 08978dc
Show file tree
Hide file tree
Showing 52 changed files with 694 additions and 804 deletions.
30 changes: 15 additions & 15 deletions apps/snakecode-cdk/lib/app.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import type { Environment } from 'aws-cdk-lib'
import { App } from 'aws-cdk-lib'
import type { Environment } from "aws-cdk-lib";
import { App } from "aws-cdk-lib";

import { INFRA_CONFIG } from '@snakecode/models'
import { INFRA_CONFIG } from "@snakecode/models";

import { StageUtils } from './utils/StageUtils'
import { StageUtils } from "./utils/StageUtils";

const app = new App()
const app = new App();

const stage = app.node.tryGetContext('stage') && app.node.tryGetContext('stage')!.toString()
const region = app.node.tryGetContext('region') && app.node.tryGetContext('region')!.toString()
const account = app.node.tryGetContext('account') && app.node.tryGetContext('account')!.toString()
const stage = app.node.tryGetContext("stage") && app.node.tryGetContext("stage")!.toString();
const region = app.node.tryGetContext("region") && app.node.tryGetContext("region")!.toString();
const account = app.node.tryGetContext("account") && app.node.tryGetContext("account")!.toString();

if (!stage || !region || !account) {
throw new Error(`Either stage, region or account arguments not passed in to CDK app, as \"-c stage={STAGE} -c region={REGION} -c account={ACCOUNT}\"`)
throw new Error(`Either stage, region or account arguments not passed in to CDK app, as "-c stage={STAGE} -c region={REGION} -c account={ACCOUNT}"`);
}

const environment: Environment = {
account: account,
region: region
}
account,
region,
};

const stageUtils = new StageUtils(app, INFRA_CONFIG, environment, stage)
const stageUtils = new StageUtils(app, INFRA_CONFIG, environment, stage);

stageUtils.setupStages()
stageUtils.setupStages();

app.synth()
app.synth();
10 changes: 5 additions & 5 deletions apps/snakecode-cdk/lib/constants/Env.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dotenv from 'dotenv'
import { cleanEnv, str } from 'envalid'
import dotenv from "dotenv";
import { cleanEnv, str } from "envalid";

dotenv.config()
dotenv.config();

// Log an error message and exit (in Node) if any required env variables are missing
const Env = cleanEnv(process.env, {
Expand All @@ -16,6 +16,6 @@ const Env = cleanEnv(process.env, {
APPLE_KEY_ID: str(),
APPLE_PRIVATE_KEY: str(),
APPLE_TEAM_ID: str(),
})
});

export default Env
export default Env;
176 changes: 96 additions & 80 deletions apps/snakecode-cdk/lib/stacks/AmplifyAuthStack.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,44 @@
import type { StackProps } from 'aws-cdk-lib'
import { Duration, NestedStack } from 'aws-cdk-lib'
import { AccountRecovery, CfnIdentityPool, CfnIdentityPoolRoleAttachment, ProviderAttribute, UserPool, UserPoolClient, UserPoolClientIdentityProvider, UserPoolIdentityProviderApple, UserPoolIdentityProviderFacebook, UserPoolIdentityProviderGoogle, VerificationEmailStyle } from 'aws-cdk-lib/aws-cognito'
import { FederatedPrincipal, Role } from 'aws-cdk-lib/aws-iam'
import type { Construct } from 'constructs'

import type { AmplifyAuthConfiguration } from '@snakecode/models'
import { APP_NAME, BASE_URL } from '@snakecode/models'
import { ENVIRONMENT_NAME } from '@snakecode/models'

import Env from '../constants/Env'
import type { StackProps } from "aws-cdk-lib";
import { Duration, NestedStack } from "aws-cdk-lib";
import {
AccountRecovery,
CfnIdentityPool,
CfnIdentityPoolRoleAttachment,
ProviderAttribute,
UserPool,
UserPoolClient,
UserPoolClientIdentityProvider,
UserPoolIdentityProviderApple,
UserPoolIdentityProviderFacebook,
UserPoolIdentityProviderGoogle,
VerificationEmailStyle,
} from "aws-cdk-lib/aws-cognito";
import { FederatedPrincipal, Role } from "aws-cdk-lib/aws-iam";
import type { Construct } from "constructs";

import type { AmplifyAuthConfiguration } from "@snakecode/models";
import { APP_NAME, BASE_URL, ENVIRONMENT_NAME } from "@snakecode/models";

import Env from "../constants/Env";

export class AmplifyAuthStack extends NestedStack {
readonly authenticatedRole: Role
readonly unauthenticatedRole: Role
readonly authenticatedRole: Role;

readonly unauthenticatedRole: Role;

// Outputs that will be used by other stacks
readonly region: string
readonly identityPoolId: string
readonly userPoolId: string
readonly userPoolClientId: string
readonly userPoolDomainUrl: string
readonly region: string;

readonly identityPoolId: string;

readonly userPoolId: string;

readonly userPoolClientId: string;

readonly userPoolDomainUrl: string;

constructor(scope: Construct, id: string, props: StackProps & { amplifyAuthConfiguration: AmplifyAuthConfiguration; stage: string }) {
super(scope, id, props)
super(scope, id, props);

// Create a User Pool with email and password login
const userPool = new UserPool(this, `${props.amplifyAuthConfiguration.userPoolName}-${props.stage}-${props.env!.region}`, {
Expand All @@ -32,144 +48,144 @@ export class AmplifyAuthStack extends NestedStack {
emailSubject: `Verify your email for ${APP_NAME}`,
emailBody: `Your ${APP_NAME} verification code is {####}. Never share it!`,
emailStyle: VerificationEmailStyle.CODE,
smsMessage: `Your ${APP_NAME} verification code is {####}. Never share it!`
smsMessage: `Your ${APP_NAME} verification code is {####}. Never share it!`,
},
userInvitation: {
emailSubject: `Invite to join ${APP_NAME}`,
emailBody: `Hello, {username}, your temporary password for your new ${APP_NAME} account is {####}. Never share it!`,
smsMessage: `Hello, {username}, your temporary password for your new ${APP_NAME} account is {####}. Never share it!`
smsMessage: `Hello, {username}, your temporary password for your new ${APP_NAME} account is {####}. Never share it!`,
},
signInAliases: {
email: true
email: true,
},
autoVerify: {
email: true
email: true,
},
standardAttributes: {
email: {
mutable: true,
required: true
}
required: true,
},
},
passwordPolicy: {
tempPasswordValidity: Duration.hours(24),
minLength: 12,
requireDigits: true,
requireSymbols: true,
requireUppercase: true,
requireLowercase: true
requireLowercase: true,
},
signInCaseSensitive: false,
accountRecovery: AccountRecovery.EMAIL_ONLY
})
accountRecovery: AccountRecovery.EMAIL_ONLY,
});

const uniquePrefix = `${ENVIRONMENT_NAME}-${props.stage}`.toLowerCase()
const uniquePrefix = `${ENVIRONMENT_NAME}-${props.stage}`.toLowerCase();
userPool.addDomain(`${props.amplifyAuthConfiguration.userPoolDomainName}-${props.stage}-${props.env!.region}`, {
cognitoDomain: {
domainPrefix: uniquePrefix
}
})
domainPrefix: uniquePrefix,
},
});

/* ======================================
* Federated Logins
====================================== */
new UserPoolIdentityProviderGoogle(this, `${props.amplifyAuthConfiguration.userPoolIdentityProviderGoogleName}-${props.stage}-${props.env!.region}`, {
userPool: userPool,
userPool,
clientId: Env.GOOGLE_CLIENT_ID,
clientSecret: Env.GOOGLE_CLIENT_SECRET,
scopes: ['email'],
scopes: ["email"],
attributeMapping: {
email: ProviderAttribute.GOOGLE_EMAIL
}
})
email: ProviderAttribute.GOOGLE_EMAIL,
},
});

new UserPoolIdentityProviderFacebook(this, `${props.amplifyAuthConfiguration.userPoolIdentityProviderFacebookName}-${props.stage}-${props.env!.region}`, {
userPool: userPool,
userPool,
clientId: Env.FACEBOOK_CLIENT_ID,
clientSecret: Env.FACEBOOK_CLIENT_SECRET,
scopes: ['email'],
scopes: ["email"],
attributeMapping: {
email: ProviderAttribute.FACEBOOK_EMAIL
}
})
email: ProviderAttribute.FACEBOOK_EMAIL,
},
});

new UserPoolIdentityProviderApple(this, `${props.amplifyAuthConfiguration.userPoolIdentityProviderAppleName}-${props.stage}-${props.env!.region}`, {
userPool: userPool,
userPool,
clientId: Env.APPLE_CLIENT_ID,
keyId: Env.APPLE_KEY_ID,
privateKey: Env.APPLE_PRIVATE_KEY,
teamId: Env.APPLE_TEAM_ID,
scopes: ['email'],
scopes: ["email"],
attributeMapping: {
email: ProviderAttribute.APPLE_EMAIL
}
})
email: ProviderAttribute.APPLE_EMAIL,
},
});

// This user pool client will be used by the Amplify frontend
const cognitoUserPoolClient = new UserPoolClient(this, `${props.amplifyAuthConfiguration.userPoolClientName}-${props.stage}-${props.env!.region}`, {
userPool: userPool,
userPool,
generateSecret: true,
supportedIdentityProviders: [UserPoolClientIdentityProvider.GOOGLE, UserPoolClientIdentityProvider.COGNITO],
oAuth: {
callbackUrls: [`${BASE_URL}`]
}
})
callbackUrls: [`${BASE_URL}`],
},
});

const cognitoIdentityPool = new CfnIdentityPool(this, `${props.amplifyAuthConfiguration.userPoolIdentityName}-${props.stage}-${props.env!.region}`, {
identityPoolName: `${props.amplifyAuthConfiguration.userPoolIdentityName}`,
allowUnauthenticatedIdentities: false
})
allowUnauthenticatedIdentities: false,
});

// Creates the authenticated role which will be used with user pool identities
this.authenticatedRole = new Role(this, `${props.amplifyAuthConfiguration.authenticatedRoleName}-${props.stage}-${props.env!.region}`, {
roleName: `${props.amplifyAuthConfiguration.authenticatedRoleName}`,
description: 'IAM Role to be used as an Unauthenticated role for the Cognito user pool identities, used by Amplify',
description: "IAM Role to be used as an Unauthenticated role for the Cognito user pool identities, used by Amplify",
assumedBy: new FederatedPrincipal(
'cognito-identity.amazon.com',
"cognito-identity.amazon.com",
{
StringEquals: {
'cognito-identity.amazonaws.com:aud': cognitoIdentityPool.ref
"cognito-identity.amazonaws.com:aud": cognitoIdentityPool.ref,
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "authenticated",
},
'ForAnyValue:StringLike': {
'cognito-identity.amazonaws.com:amr': 'authenticated'
}
},
'sts:AssumeRoleWithWebIdentity'
"sts:AssumeRoleWithWebIdentity",
),
maxSessionDuration: Duration.hours(1)
})
maxSessionDuration: Duration.hours(1),
});

// Creates the unauthenticated role which will be used with user pool identities
this.unauthenticatedRole = new Role(this, `${props.amplifyAuthConfiguration.unauthenticatedRoleName}-${props.stage}-${props.env!.region}`, {
roleName: `${props.amplifyAuthConfiguration.unauthenticatedRoleName}`,
description: 'IAM Role to be used as an Authenticated role for the Cognito user pool identities, used by Amplify',
description: "IAM Role to be used as an Authenticated role for the Cognito user pool identities, used by Amplify",
assumedBy: new FederatedPrincipal(
'cognito-identity.amazon.com',
"cognito-identity.amazon.com",
{
StringEquals: {
'cognito-identity.amazonaws.com:aud': cognitoIdentityPool.ref
"cognito-identity.amazonaws.com:aud": cognitoIdentityPool.ref,
},
"ForAnyValue:StringLike": {
"cognito-identity.amazonaws.com:amr": "unauthenticated",
},
'ForAnyValue:StringLike': {
'cognito-identity.amazonaws.com:amr': 'unauthenticated'
}
},
'sts:AssumeRoleWithWebIdentity'
"sts:AssumeRoleWithWebIdentity",
),
maxSessionDuration: Duration.hours(1)
})
maxSessionDuration: Duration.hours(1),
});

new CfnIdentityPoolRoleAttachment(this, `${props.amplifyAuthConfiguration.authenticatedRoleName}-attachment-${props.stage}-${props.env!.region}`, {
identityPoolId: cognitoIdentityPool.ref,
roles: {
unauthenticated: this.unauthenticatedRole.roleArn,
authenticated: this.authenticatedRole.roleArn
}
})

this.region = props.env!.region!
this.identityPoolId = cognitoIdentityPool.ref
this.userPoolId = userPool.userPoolId
this.userPoolClientId = cognitoUserPoolClient.userPoolClientId
this.userPoolDomainUrl = `${uniquePrefix}.auth.${props.env!.region!}.amazoncognito.com`
authenticated: this.authenticatedRole.roleArn,
},
});

this.region = props.env!.region!;
this.identityPoolId = cognitoIdentityPool.ref;
this.userPoolId = userPool.userPoolId;
this.userPoolClientId = cognitoUserPoolClient.userPoolClientId;
this.userPoolDomainUrl = `${uniquePrefix}.auth.${props.env!.region!}.amazoncognito.com`;
}
}
Loading

0 comments on commit 08978dc

Please sign in to comment.