-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
807: CDK auth stack (AWS Cognito) (#818)
- Loading branch information
1 parent
dbef537
commit 7a1f226
Showing
9 changed files
with
212 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
*.js | ||
*.d.ts | ||
node_modules | ||
.env | ||
|
||
# CDK asset staging directory | ||
.cdk.staging | ||
|
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
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
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { | ||
Mfa, | ||
OAuthScope, | ||
ProviderAttribute, | ||
UserPool, | ||
UserPoolClient, | ||
UserPoolClientIdentityProvider, | ||
UserPoolDomain, | ||
UserPoolIdentityProviderSaml, | ||
UserPoolIdentityProviderSamlMetadata, | ||
} from 'aws-cdk-lib/aws-cognito'; | ||
import { CfnOutput, Duration, Stack, StackProps, Tags } from 'aws-cdk-lib/core'; | ||
import { Construct } from 'constructs'; | ||
|
||
import { resourceName } from './resourceNamingUtils'; | ||
|
||
export class AuthStack extends Stack { | ||
userPool: UserPool; | ||
userPoolClient: UserPoolClient; | ||
userPoolDomain: UserPoolDomain; | ||
|
||
constructor(scope: Construct, id: string, props: StackProps) { | ||
super(scope, id, props); | ||
|
||
const azureTenantId = process.env.AZURE_TENANT_ID; | ||
const azureApplicationId = process.env.AZURE_APPLICATION_ID; | ||
if (!azureTenantId || !azureApplicationId) { | ||
throw new Error( | ||
'Need AZURE_TENANT_ID and AZURE_APPLICATION_ID environment vars!' | ||
); | ||
} | ||
|
||
const generateResourceName = resourceName(scope); | ||
|
||
// Cognito UserPool | ||
const userPoolName = generateResourceName('userpool'); | ||
this.userPool = new UserPool(this, userPoolName, { | ||
userPoolName, | ||
enableSmsRole: false, | ||
mfa: Mfa.OFF, | ||
//email // not configured, we're not going to send email from here | ||
signInCaseSensitive: false, | ||
autoVerify: { email: false }, // will be sending email invite anyway | ||
selfSignUpEnabled: false, // only users we explicity allow | ||
standardAttributes: { | ||
givenName: { required: true }, | ||
familyName: { required: true }, | ||
email: { required: true }, | ||
}, | ||
signInAliases: { email: true }, | ||
deletionProtection: false, | ||
}); | ||
// Tags not correctly assigned from parent stack: https://github.com/aws/aws-cdk/issues/14127 | ||
Object.entries(props.tags ?? {}).forEach(([key, value]) => { | ||
Tags.of(this.userPool).add(key, value); | ||
}); | ||
|
||
new CfnOutput(this, 'UserPool.Identifier', { | ||
value: `urn:amazon:cognito:sp:${this.userPool.userPoolId}`, | ||
}); | ||
new CfnOutput(this, 'UserPool.ReplyUrl', { | ||
value: `https://${userPoolName}.auth.${this.region}.amazoncognito.com/saml2/idpresponse`, | ||
}); | ||
|
||
const idpName = generateResourceName('userpool-idp'); | ||
const identityProvider = new UserPoolIdentityProviderSaml(this, idpName, { | ||
name: idpName, | ||
idpSignout: true, | ||
metadata: UserPoolIdentityProviderSamlMetadata.url( | ||
`https://login.microsoftonline.com/${azureTenantId}/federationmetadata/2007-06/federationmetadata.xml?appid=${azureApplicationId}` | ||
), | ||
userPool: this.userPool, | ||
attributeMapping: { | ||
email: ProviderAttribute.other( | ||
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name' | ||
), | ||
familyName: ProviderAttribute.other( | ||
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname' | ||
), | ||
givenName: ProviderAttribute.other( | ||
'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname' | ||
), | ||
}, | ||
}); | ||
|
||
const userPoolClientName = generateResourceName('userpool-client'); | ||
this.userPoolClient = this.userPool.addClient(userPoolClientName, { | ||
userPoolClientName, | ||
authFlows: { | ||
userSrp: true, | ||
}, | ||
supportedIdentityProviders: [ | ||
UserPoolClientIdentityProvider.custom(identityProvider.providerName), | ||
], | ||
generateSecret: true, | ||
oAuth: { | ||
flows: { | ||
authorizationCodeGrant: true, | ||
}, | ||
scopes: [OAuthScope.OPENID, OAuthScope.EMAIL, OAuthScope.PROFILE], | ||
//callbackUrls, | ||
//logoutUrls: callbackUrls, | ||
}, | ||
accessTokenValidity: Duration.minutes(60), | ||
idTokenValidity: Duration.minutes(60), | ||
refreshTokenValidity: Duration.days(30), | ||
authSessionValidity: Duration.minutes(3), | ||
enableTokenRevocation: true, | ||
preventUserExistenceErrors: true, | ||
}); | ||
|
||
const userPoolDomainName = generateResourceName('userpool-domain'); | ||
this.userPoolDomain = this.userPool.addDomain(userPoolDomainName, { | ||
cognitoDomain: { | ||
domainPrefix: userPoolName, | ||
}, | ||
}); | ||
} | ||
} |
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,2 +1,3 @@ | ||
export * from './resourceNamingUtils'; | ||
export { ApiStack } from './api-stack'; | ||
export { AuthStack } from './auth-stack'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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