我可以使用Cloudformation设置AWS Cognito用户池标识提供程序吗?

时间:2018-03-20 23:17:06

标签: amazon-web-services amazon-cloudformation

我想设置一个cognito用户池,并使用cloudformation yml文件自动配置我的Google身份提供商。

我检查了所有文档但是找不到任何与此相关的内容。关于如何做的任何想法?

enter image description here

4 个答案:

答案 0 :(得分:3)

对我有用的是这样的:

CognitoUserPoolIdentityProvider:
  Type: AWS::Cognito::UserPoolIdentityProvider
  Properties: 
    ProviderName: Google
    AttributeMapping:
      email: emailAddress
    ProviderDetails:
      client_id: <yourclientid>.apps.googleusercontent.com
      client_secret: <yourclientsecret>
      authorize_scopes: email openid
    ProviderType: Google
    UserPoolId: 
      Ref: CognitoUserPool

答案 1 :(得分:2)

截至目前,Cloudformation似乎不支持许多Cognito详细信息,但是有一些方法可以在堆栈旋转后实现所需的功能,例如使用Lambda。

请参阅以下答案:

Cannot set a property of cognito userpool client via cloudformation

Cloudformation Cognito - how to setup App Client Settings, Domain, and Federated Identities via SAM template

答案 2 :(得分:1)

您可以使用Lambda函数作为自定义Cloudformation资源来实现此目的。我已经制作了自定义资源,以允许在this repo

上创建用户池域,客户端设置和身份提供程序

您将具有用于创建身份提供者的类似信息,例如Facebook

FacebookIdp:
  Type: 'Custom::${self:service}-${self:provider.stage}-CUPIdentityProvider'
  DependsOn:
    - CFNSendResponseLambdaFunction
    - CUPIdentityProviderLambdaFunction
  Properties:
    ServiceToken:
      Fn::GetAtt: [CUPIdentityProviderLambdaFunction, Arn]
    UserPoolId:
      Ref: AppUserPool
    ProviderName: Facebook
    ProviderType: Facebook
    Client_id: 'YourFacebookAppID'
    Client_secret: 'YourFacebookAppSecert'
    Authorize_scopes: 'public_profile,email'

然后在用户池客户端设置上启用该身份提供程序

AppUserPoolClientSettings:
  Type: 'Custom::${self:service}-${self:provider.stage}-CUPClientSettings'
  DependsOn:
    - CFNSendResponseLambdaFunction
    - CUPClientSettingsLambdaFunction
    - FacebookIdp
  Properties:
    ServiceToken:
      Fn::GetAtt: [ CUPClientSettingsLambdaFunction, Arn]
    UserPoolId: 
      Ref: AppUserPool
    UserPoolClientId: 
      Ref: AppUserPoolClient
    SupportedIdentityProviders:
      - COGNITO
      - Facebook
    CallbackURL: 'https://www.yourdomain.com/callback' ##Replace this with your app callback url
    LogoutURL: 'https://www.yourdomain.com/logout' ##Replace this with your app logout url
    AllowedOAuthFlowsUserPoolClient: true
    AllowedOAuthFlows:
      - code
    AllowedOAuthScopes:
      - openid

请注意,此仓库是使用Serverless framework构建的,如果您希望使用纯cloudformation堆栈来构建此仓库,请使用文件CUPIdentityProvider.js上的代码来创建自己的自定义资源。

答案 3 :(得分:1)

使用generic custom resource provider,可以创建CFN不支持的所有资源。

给出here的示例专门为Google SAML身份验证创建和配置Cognito。

例如,通过更改传递给自定义资源处理程序的参数,将其更改为使用Google oAuth而不是SAML应该足够容易。

  UserPoolIdentityProvider:
    Type: 'Custom::CognitoUserPoolIdentityProvider'
    Condition: HasMetadata
    DependsOn: UserPool
    Properties:
      ServiceToken: !Sub '${CustomResourceLambdaArn}'
      AgentService: cognito-idp
      AgentType: client
      # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cognito-idp.html#CognitoIdentityProvider.Client.create_user_pool_domain
      AgentCreateMethod: create_identity_provider
      AgentUpdateMethod: update_identity_provider
      AgentDeleteMethod: delete_identity_provider
      AgentResourceId: ProviderName
      AgentCreateArgs:
        UserPoolId: !Sub '${UserPool}'
        ProviderName: google-provider
        AttributeMapping:
          email: emailAddress
        ProviderDetails:
          google_app_id: some_value
          google_app_secret: some_value
          google_authorize_scope: some_value
        ProviderType: Google
      AgentUpdateArgs:
        UserPoolId: !Sub '${UserPool}'
        ProviderName: google-provider
        AttributeMapping:
          email: emailAddress
        ProviderDetails:
          google_app_id: some_value
          google_app_secret: some_value
          google_authorize_scope: some_value
        ProviderType: Google
      AgentDeleteArgs:
        UserPoolId: !Sub '${UserPool}'
        ProviderName: google-provider
  

您需要在控制台中创建测试提供程序,以获取ProviderDetails下参数的正确名称,即Google app IDApp secretAuthorize scope。另外,AttributeMapping可能需要设置为其他内容。