亚马逊 Cognitto 身份池构建库
此API 可能会发出警告 。 后向兼容性没有保证 .
所有构造已移入 aws-cdk-lib/aws-cognito-identitypool。 。 。 .
身份池在一个单独的模块中,而API正在被稳定. 一旦我们稳定了模块,他们会 包含在马厩中 记录 图书馆。 请就这一经验提供反馈意见,具体方式如下: 创建一个 这里的问题
亚马逊认知身份池 允许您的用户访问其他 AWS 服务.
身份池是: 亚马逊-科尼奥托,提供认证、授权和 您的网络和移动应用程序的用户管理 。 您的用户可以像用户一样通过信任的身份提供者签名 pool或SAML 2.0服务,以及与Facebook,Amazon,Google或Apple等第三方供应商.
亚马逊科尼奥多的另一个主要组成部分是: 用户池的用户目录 用于应用程序用户的签名选项.
这个模块是 AWS 云开发套件 项目.
import { IdentityPool, UserPoolAuthenticationProvider } from '@aws-cdk/aws-cognito-identitypool-alpha';
目录
身份库
身份池为作为客人(未经认证)的用户和拥有 通过出示另一个身份提供者的证明书认证。 身份库是用户身份数据的存储库 账户专用.
身份池可以与Cognitto用户池一起使用,也可以通过访问外部联邦身份提供者的方式使用
直接说 学习更多 亚马逊认知身份池。 。 。 .
认证和未认证的身份
身份集合定义两种身份类型: 认证( E)user)和无认证(guest) (中文(简体) ). 每一个身份在
身份资料库要么经过认证,要么未经认证。 每个身份库都有一个默认的角色来认证
身份,以及未认证身份的默认角色. 在没有其他压倒一切的规则(见下文)的情况下
由相应的用户在认证过程中承担的角色.
配置最小的基本身份池不需要道具,默认认证(用户)和
未认证的(贵宾)角色适用于身份集合:
new IdentityPool(this, 'myIdentityPool');
默认情况下,被认证的角色和未经认证的角色都将没有附加权限. 当批准时,
您应当确保您获得最不需要的许可。 许可
利用公众发挥作用 authenticatedRole 财务报告和审定财务报表 unauthenticatedRole 属性 :
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
const identityPool = new IdentityPool(this, 'myIdentityPool');
declare const table: dynamodb.Table;
// 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(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['dynamodb:UpdateItem'],
resources: [table.tableArn],
}));
默认角色也可以在 IdentityPoolProps数字 :
const stack = new Stack();
const authenticatedRole = new iam.Role(this, 'authRole', {
assumedBy: new iam.ServicePrincipal('service.amazonaws.com'),
});
const unauthenticatedRole = new iam.Role(this, 'unauthRole', {
assumedBy: new iam.ServicePrincipal('service.amazonaws.com'),
});
const identityPool = new IdentityPool(this, 'TestIdentityPoolActions', {
authenticatedRole,
unauthenticatedRole,
});
认证提供者
认证身份属于经公共登录提供者认证的用户(Amazon Cognitto用户池,
登录到亚马逊, 签入苹果, Facebook, Google, SAML, 或任何 OpenID 连接供应商) 或开发商供应商
(你自己的后端认证程序).
认证提供者 可以通过先将其与Cognito用户池连接或通过
将提供者与身份池直接联系起来.
用户池认证提供者
为了将用户池附加到身份池中作为认证提供者,身份池需要属性
从用户池和用户池客户端。 因此,身份集合使用 UserPoolAuthenticationProvider
从用户池构造中收集必要的属性.
const userPool = new cognito.UserPool(this, 'Pool');
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
authenticationProviders: {
userPools: [new UserPoolAuthenticationProvider({ userPool })],
},
});
用户池也可以与即时化后的身份池相接. 身份宝库 addUserPoolAuthentication 方法
返回已创建的用户池客户端 :
declare const identityPool: IdentityPool;
const userPool = new cognito.UserPool(this, 'Pool');
const userPoolClient = identityPool.addUserPoolAuthentication(new UserPoolAuthenticationProvider({
userPool,
}));
服务器侧托肯检查
与 IdentityPool CDK Construction 默认情况下, 池的配置将检查集成用户池到
确保用户在身份池提供OIDC令牌之前没有全球签名或删除,或
用户的AWS证书.
如果用户被签名或删除, 身份池将返回一个400个非授权错误 。 此设置可以是
但是,在几个方面,残疾人是残疾人.
设置 disableServerSideTokenCheck 将默认行为更改为没有服务器侧式符号检查。 学习
更多 这里数字 :
declare const identityPool: IdentityPool;
const userPool = new cognito.UserPool(this, 'Pool');
identityPool.addUserPoolAuthentication(new UserPoolAuthenticationProvider({
userPool,
disableServerSideTokenCheck: true,
}));
直接参与外部提供者
一个或多个 外部身份提供者 可直接使用身份集合连接
authenticationProviders数字 :
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
authenticationProviders: {
amazon: {
appId: 'amzn1.application.12312k3j234j13rjiwuenf',
},
facebook: {
appId: '1234567890123',
},
google: {
clientId: '12345678012.apps.googleusercontent.com',
},
apple: {
servicesId: 'com.myappleapp.auth',
},
twitter: {
consumerKey: 'my-twitter-id',
consumerSecret: 'my-twitter-secret',
},
},
});
要将多个同类型供应商与身份池关联,请使用用户
Pools, OpenIdConnect, 或称 SAML. 每个外部服务只能有一个提供者直接附属于身份库.
打开 Id 连接和 Saml
OpenID 连接 是打开的标准,用于
由多个登录提供者支持的认证 。 亚马逊·科尼托支持将身份与
通过配置的 OpenID 连接提供者 AWS 身份和访问管理。 。 。 .
支持的身份提供者 安全标记语言2.0( SAML 2. 0) 可用于提供简单的
用户的入行流程。 SAML 支持身份提供者指定了用户可以承担的 IAM 角色
这样,不同的用户可以获得不同的权限. 连接 OpenId 连接或 Saml 提供者
带有身份池:
declare const openIdConnectProvider: iam.OpenIdConnectProvider;
declare const samlProvider: iam.SamlProvider;
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
authenticationProviders: {
openIdConnectProviders: [openIdConnectProvider],
samlProviders: [samlProvider],
},
});
自定义提供者
使用自定义可以进一步定制身份池的行为 开发者认证的身份。 。 。 .
有了开发者认证的身份,用户可以通过现有的认证进行注册和认证
使用 Amazon Cognitto 来同步用户数据和访问 AWS 资源.
和被支持的外部供应商一样,只有一个自定义供应商可以直接与身份相关
泳池 游泳池 游泳池.
declare const openIdConnectProvider: iam.OpenIdConnectProvider;
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
authenticationProviders: {
google: {
clientId: '12345678012.apps.googleusercontent.com',
},
openIdConnectProviders: [openIdConnectProvider],
customProvider: 'my-custom-provider.example.com',
},
});
角色绘图
除了为经认证和未经认证的用户设定默认角色外,还可以使用身份集合
定义规则,通过使用"角色映射"来根据用户ID符中的要求来选择每个用户的角色. 使用时
角色映射 了解角色需要的一些权限 以及最没有特权的
为您指定特定用途所需的角色。 深处
审查作用和作用图 这里。 。 。 .
使用a 基于符号的方法 角色映射可以让绘制的角色通过 cognito:roles 或者说
cognito:preferred_role 身份提供者的债权:
import { IdentityPoolProviderUrl } from '@aws-cdk/aws-cognito-identitypool-alpha';
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
roleMappings: [{
providerUrl: IdentityPoolProviderUrl.AMAZON,
useToken: true,
}],
});
使用基于规则的方法绘制角色图,可以根据身份提供者的自定义要求分配角色:
import { IdentityPoolProviderUrl, RoleMappingMatchType } from '@aws-cdk/aws-cognito-identitypool-alpha';
declare const adminRole: iam.Role;
declare const nonAdminRole: iam.Role;
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
// Assign specific roles to users based on whether or not the custom admin claim is passed from the identity provider
roleMappings: [{
providerUrl: IdentityPoolProviderUrl.AMAZON,
rules: [
{
claim: 'custom:admin',
claimValue: 'admin',
mappedRole: adminRole,
},
{
claim: 'custom:admin',
claimValue: 'admin',
matchType: RoleMappingMatchType.NOTEQUAL,
mappedRole: nonAdminRole,
}
],
}],
});
提供方 Uls
角色映射必须与可以提供的身份提供者的url相关
IdentityPoolProviderUrl。支持的供应商有静态Urls,可用于:
import { IdentityPoolProviderUrl } from '@aws-cdk/aws-cognito-identitypool-alpha';
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
roleMappings: [{
providerUrl: IdentityPoolProviderUrl.FACEBOOK,
useToken: true,
}],
});
对于没有静态 Urls 的身份提供者,可以提供自定义 Url :
import { IdentityPoolProviderUrl } from '@aws-cdk/aws-cognito-identitypool-alpha';
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
roleMappings: [
{
providerUrl: IdentityPoolProviderUrl.custom('my-custom-provider.com'),
useToken: true,
},
],
});
如果一个提供者的 URL 是 CDK Token, 那么如果您正在尝试使用先前定义的 Cognito 用户池, 您也需要提供映射Key . 这是因为默认情况下 Cloudform 角色映射散列中的密钥是提供者Url, Cloudform 映射密钥必须是混凝土字符串, 它们 无法作为参考。 例如:
import { UserPool, UserPoolClient } from 'aws-cdk-lib/aws-cognito';
import { IdentityPoolProviderUrl } from '@aws-cdk/aws-cognito-identitypool-alpha';
declare const userPool: UserPool;
declare const userPoolClient: UserPoolClient;
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
roleMappings: [{
mappingKey: 'cognito',
providerUrl: IdentityPoolProviderUrl.userPool(userPool, userPoolClient),
useToken: true,
}],
});
见 这里 更多信息.
认证流程
身份库 认证流程 默认为增强的简化流量。 经典( 基本) 认证流程
也可以使用 allowClassicFlow数字 :
new IdentityPool(this, 'myidentitypool', {
identityPoolName: 'myidentitypool',
allowClassicFlow: true,
});
认知同步
现在建议整合 AWS 应用同步 用于在设备之间同步应用数据,所以
认知同步功能 PushSync, (中文(简体) ), CognitoEvents,以及 CognitoStreams 不属于 IdentityPool更多内容
可找到信息 这里。 。 。 .
导入身份池
您可以使用身份池静态方法, 使用身份池 Id 或
亚恩:
IdentityPool.fromIdentityPoolId(this, 'my-imported-identity-pool',
'us-east-1:dj2823ryiwuhef937');
IdentityPool.fromIdentityPoolArn(this, 'my-imported-identity-pool',
'arn:aws:cognito-identity:us-east-1:123456789012:identitypool/us-east-1:dj2823ryiwuhef937');