package
1.204.0-devpreview
Repository: https://github.com/aws/aws-cdk-go.git
Documentation: pkg.go.dev

# README

Amazon Cognito Identity Pool Construct Library

Identity Pools are in a separate module while the API is being stabilized. Once we stabilize the module, they will be included into the stable aws-cognito library. Please provide feedback on this experience by creating an issue here

Amazon Cognito Identity Pools enable you to grant your users access to other AWS services.

Identity Pools are one of the two main components of Amazon Cognito, which provides authentication, authorization, and user management for your web and mobile apps. Your users can sign in directly with a user name and password, or through a third party such as Facebook, Amazon, Google or Apple.

The other main component in Amazon Cognito is user pools. User Pools are user directories that provide sign-up and sign-in options for your app users.

This module is part of the AWS Cloud Development Kit project.

import "github.com/aws/aws-cdk-go/awscdk"

Table of Contents

Identity Pools

Identity pools provide temporary AWS credentials for users who are guests (unauthenticated) and for users who have been authenticated and received a token. An identity pool is a store of user identity data specific to an account.

Identity pools can be used in conjunction with Cognito User Pools or by accessing external federated identity providers directly. Learn more at Amazon Cognito Identity Pools.

Authenticated and Unauthenticated Identities

Identity pools define two types of identities: authenticated(user) and unauthenticated (guest). Every identity in an identity pool is either authenticated or unauthenticated. Each identity pool has a default role for authenticated identities, and a default role for unauthenticated identities. Absent other overriding rules (see below), these are the roles that will be assumed by the corresponding users in the authentication process.

A basic Identity Pool with minimal configuration has no required props, with default authenticated (user) and unauthenticated (guest) roles applied to the identity pool:

awscdk.NewIdentityPool(this, jsii.String("myIdentityPool"))

By default, both the authenticated and unauthenticated roles will have no permissions attached. Grant permissions to roles using the public authenticatedRole and unauthenticatedRole properties:

import dynamodb "github.com/aws/aws-cdk-go/awscdk"
var table table


identityPool := awscdk.NewIdentityPool(this, jsii.String("myIdentityPool"))

// Grant permissions to authenticated users
table.grantReadWriteData(identityPool.AuthenticatedRole)
// Grant permissions to unauthenticated guest users
table.grantReadData(identityPool.UnauthenticatedRole)

//Or add policy statements straight to the role
identityPool.AuthenticatedRole.AddToPrincipalPolicy(iam.NewPolicyStatement(&PolicyStatementProps{
	Effect: iam.Effect_ALLOW,
	Actions: []*string{
		jsii.String("dynamodb:*"),
	},
	Resources: []*string{
		jsii.String("*"),
	},
}))

The default roles can also be supplied in IdentityPoolProps:

stack := awscdk.Newstack()
authenticatedRole := iam.NewRole(this, jsii.String("authRole"), &RoleProps{
	AssumedBy: iam.NewServicePrincipal(jsii.String("service.amazonaws.com")),
})
unauthenticatedRole := iam.NewRole(this, jsii.String("unauthRole"), &RoleProps{
	AssumedBy: iam.NewServicePrincipal(jsii.String("service.amazonaws.com")),
})
identityPool := awscdk.NewIdentityPool(this, jsii.String("TestIdentityPoolActions"), &IdentityPoolProps{
	AuthenticatedRole: AuthenticatedRole,
	UnauthenticatedRole: UnauthenticatedRole,
})

Authentication Providers

Authenticated identities belong to users who are authenticated by a public login provider (Amazon Cognito user pools, Login with Amazon, Sign in with Apple, Facebook, Google, SAML, or any OpenID Connect Providers) or a developer provider (your own backend authentication process).

Authentication providers can be associated with an Identity Pool by first associating them with a Cognito User Pool or by associating the provider directly with the identity pool.

User Pool Authentication Provider

In order to attach a user pool to an identity pool as an authentication provider, the identity pool needs properties from both the user pool and the user pool client. For this reason identity pools use a UserPoolAuthenticationProvider to gather the necessary properties from the user pool constructs.

userPool := cognito.NewUserPool(this, jsii.String("Pool"))

awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	AuthenticationProviders: &IdentityPoolAuthenticationProviders{
		UserPools: []iUserPoolAuthenticationProvider{
			awscdk.NewUserPoolAuthenticationProvider(&UserPoolAuthenticationProviderProps{
				UserPool: *UserPool,
			}),
		},
	},
})

User pools can also be associated with an identity pool after instantiation. The Identity Pool's addUserPoolAuthentication method returns the User Pool Client that has been created:

var identityPool identityPool

userPool := cognito.NewUserPool(this, jsii.String("Pool"))
userPoolClient := identityPool.AddUserPoolAuthentication(awscdk.NewUserPoolAuthenticationProvider(&UserPoolAuthenticationProviderProps{
	UserPool: UserPool,
}))

Server Side Token Check

With the IdentityPool CDK Construct, by default the pool is configured to check with the integrated user pools to make sure that the user has not been globally signed out or deleted before the identity pool provides an OIDC token or AWS credentials for the user.

If the user is signed out or deleted, the identity pool will return a 400 Not Authorized error. This setting can be disabled, however, in several ways.

Setting disableServerSideTokenCheck to true will change the default behavior to no server side token check. Learn more here:

var identityPool identityPool

userPool := cognito.NewUserPool(this, jsii.String("Pool"))
identityPool.AddUserPoolAuthentication(awscdk.NewUserPoolAuthenticationProvider(&UserPoolAuthenticationProviderProps{
	UserPool: UserPool,
	DisableServerSideTokenCheck: jsii.Boolean(true),
}))

Associating an External Provider Directly

One or more external identity providers can be associated with an identity pool directly using authenticationProviders:

awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	AuthenticationProviders: &IdentityPoolAuthenticationProviders{
		Amazon: &IdentityPoolAmazonLoginProvider{
			AppId: jsii.String("amzn1.application.12312k3j234j13rjiwuenf"),
		},
		Facebook: &IdentityPoolFacebookLoginProvider{
			AppId: jsii.String("1234567890123"),
		},
		Google: &IdentityPoolGoogleLoginProvider{
			ClientId: jsii.String("12345678012.apps.googleusercontent.com"),
		},
		Apple: &IdentityPoolAppleLoginProvider{
			ServicesId: jsii.String("com.myappleapp.auth"),
		},
		Twitter: &IdentityPoolTwitterLoginProvider{
			ConsumerKey: jsii.String("my-twitter-id"),
			ConsumerSecret: jsii.String("my-twitter-secret"),
		},
	},
})

To associate more than one provider of the same type with the identity pool, use User Pools, OpenIdConnect, or SAML. Only one provider per external service can be attached directly to the identity pool.

OpenId Connect and Saml

OpenID Connect is an open standard for authentication that is supported by a number of login providers. Amazon Cognito supports linking of identities with OpenID Connect providers that are configured through AWS Identity and Access Management.

An identity provider that supports Security Assertion Markup Language 2.0 (SAML 2.0) can be used to provide a simple onboarding flow for users. The SAML-supporting identity provider specifies the IAM roles that can be assumed by users so that different users can be granted different sets of permissions. Associating an OpenId Connect or Saml provider with an identity pool:

var openIdConnectProvider openIdConnectProvider
var samlProvider samlProvider


awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	AuthenticationProviders: &IdentityPoolAuthenticationProviders{
		OpenIdConnectProviders: []iOpenIdConnectProvider{
			openIdConnectProvider,
		},
		SamlProviders: []iSamlProvider{
			samlProvider,
		},
	},
})

Custom Providers

The identity pool's behavior can be customized further using custom developer authenticated identities. With developer authenticated identities, users can be registered and authenticated via an existing authentication process while still using Amazon Cognito to synchronize user data and access AWS resources.

Like the supported external providers, though, only one custom provider can be directly associated with the identity pool.

var openIdConnectProvider openIdConnectProvider

awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	AuthenticationProviders: &IdentityPoolAuthenticationProviders{
		Google: &IdentityPoolGoogleLoginProvider{
			ClientId: jsii.String("12345678012.apps.googleusercontent.com"),
		},
		OpenIdConnectProviders: []iOpenIdConnectProvider{
			openIdConnectProvider,
		},
		CustomProvider: jsii.String("my-custom-provider.example.com"),
	},
})

Role Mapping

In addition to setting default roles for authenticated and unauthenticated users, identity pools can also be used to define rules to choose the role for each user based on claims in the user's ID token by using Role Mapping. When using role mapping, it's important to be aware of some of the permissions the role will need. An in depth review of roles and role mapping can be found here.

Using a token-based approach to role mapping will allow mapped roles to be passed through the cognito:roles or cognito:preferred_role claims from the identity provider:

import "github.com/aws/aws-cdk-go/awscdk"


awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	RoleMappings: []identityPoolRoleMapping{
		&identityPoolRoleMapping{
			ProviderUrl: awscdk.IdentityPoolProviderUrl_AMAZON(),
			UseToken: jsii.Boolean(true),
		},
	},
})

Using a rule-based approach to role mapping allows roles to be assigned based on custom claims passed from the identity provider:

import "github.com/aws/aws-cdk-go/awscdk"

var adminRole role
var nonAdminRole role

awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	// Assign specific roles to users based on whether or not the custom admin claim is passed from the identity provider
	RoleMappings: []identityPoolRoleMapping{
		&identityPoolRoleMapping{
			ProviderUrl: awscdk.IdentityPoolProviderUrl_AMAZON(),
			Rules: []roleMappingRule{
				&roleMappingRule{
					Claim: jsii.String("custom:admin"),
					ClaimValue: jsii.String("admin"),
					MappedRole: adminRole,
				},
				&roleMappingRule{
					Claim: jsii.String("custom:admin"),
					ClaimValue: jsii.String("admin"),
					MatchType: awscdk.RoleMappingMatchType_NOTEQUAL,
					MappedRole: nonAdminRole,
				},
			},
		},
	},
})

Role mappings can also be added after instantiation with the Identity Pool's addRoleMappings method:

import "github.com/aws/aws-cdk-go/awscdk"

var identityPool identityPool
var myAddedRoleMapping1 identityPoolRoleMapping
var myAddedRoleMapping2 identityPoolRoleMapping
var myAddedRoleMapping3 identityPoolRoleMapping


identityPool.AddRoleMappings(myAddedRoleMapping1, myAddedRoleMapping2, myAddedRoleMapping3)

Provider Urls

Role mappings must be associated with the url of an Identity Provider which can be supplied IdentityPoolProviderUrl. Supported Providers have static Urls that can be used:

import "github.com/aws/aws-cdk-go/awscdk"


awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	RoleMappings: []identityPoolRoleMapping{
		&identityPoolRoleMapping{
			ProviderUrl: awscdk.IdentityPoolProviderUrl_FACEBOOK(),
			UseToken: jsii.Boolean(true),
		},
	},
})

For identity providers that don't have static Urls, a custom Url or User Pool Client Url can be supplied:

import "github.com/aws/aws-cdk-go/awscdk"


awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	RoleMappings: []identityPoolRoleMapping{
		&identityPoolRoleMapping{
			ProviderUrl: awscdk.IdentityPoolProviderUrl_UserPool(jsii.String("cognito-idp.my-idp-region.amazonaws.com/my-idp-region_abcdefghi:app_client_id")),
			UseToken: jsii.Boolean(true),
		},
		&identityPoolRoleMapping{
			ProviderUrl: awscdk.IdentityPoolProviderUrl_Custom(jsii.String("my-custom-provider.com")),
			UseToken: jsii.Boolean(true),
		},
	},
})

See here for more information.

Authentication Flow

Identity Pool Authentication Flow defaults to the enhanced, simplified flow. The Classic (basic) Authentication Flow can also be implemented using allowClassicFlow:

awscdk.NewIdentityPool(this, jsii.String("myidentitypool"), &IdentityPoolProps{
	IdentityPoolName: jsii.String("myidentitypool"),
	AllowClassicFlow: jsii.Boolean(true),
})

Cognito Sync

It's now recommended to integrate AWS AppSync for synchronizing app data across devices, so Cognito Sync features like PushSync, CognitoEvents, and CognitoStreams are not a part of IdentityPool. More information can be found here.

Importing Identity Pools

You can import existing identity pools into your stack using Identity Pool static methods with the Identity Pool Id or Arn:

awscdk.IdentityPool_FromIdentityPoolId(this, jsii.String("my-imported-identity-pool"), jsii.String("us-east-1:dj2823ryiwuhef937"))
awscdk.IdentityPool_FromIdentityPoolArn(this, jsii.String("my-imported-identity-pool"), jsii.String("arn:aws:cognito-identity:us-east-1:123456789012:identitypool/us-east-1:dj2823ryiwuhef937"))

# Functions

Import an existing Identity Pool from its Arn.
Import an existing Identity Pool from its id.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
No description provided by the author
No description provided by the author
Custom Provider Url.
No description provided by the author
No description provided by the author
No description provided by the author
OpenId Provider Url.
Saml Provider Url.
No description provided by the author
User Pool Provider Url.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.

# Constants

Amazon Provider Type.
Apple Provider Type.
Custom Provider Type.
Digits Provider Type.
Facebook Provider type.
Google Provider Type.
Open Id Provider Type.
Saml Provider Type.
Twitter Provider Type.
User Pool Provider Type.
The Claim from the token must contain the given value in order for a match.
The Claim from the token must equal the given value in order for a match.
The Claim from the token must not equal the given value in order for a match.
The Claim from the token must start with the given value in order for a match.

# Structs

Login Provider for Identity Federation using Amazon Credentials.
Login Provider for Identity Federation using Apple Credentials.
Authentication providers for using in identity pool.
Login Provider for Identity Federation using Digits Credentials.
Login Provider for Identity Federation using Facebook Credentials.
Login Provider for Identity Federation using Google Credentials.
Props for the IdentityPool construct.
External Identity Providers To Connect to User Pools and Identity Pools.
Props for an Identity Pool Role Attachment.
Map roles to users in the identity pool based on claims from the Identity Provider.
Login Provider for Identity Federation using Twitter Credentials.
Represents an Identity Pool Role Attachment Role Mapping Rule.
Represents a UserPoolAuthenticationProvider Bind Configuration.
Represents UserPoolAuthenticationProvider Bind Options.
Props for the User Pool Authentication Provider.

# Interfaces

Define a Cognito Identity Pool.
Keys for Login Providers - correspond to client id's of respective federation identity providers.
Defines an Identity Pool Role Attachment.
Represents a Cognito IdentityPool.
Represents an Identity Pool Role Attachment.
Represents the concept of a User Pool Authentication Provider.
Defines a User Pool Authentication Provider.

# Type aliases

Types of Identity Pool Login Providers.
Types of matches allowed for Role Mapping.