如何通过 cli 或 sdk 设置 Cognito 用户池应用程序客户端的允许自定义范围?

Kaa*_*zoo 5 aws-cli aws-sdk-js

TL;DR:有没有办法通过 cli 或 sdk 设置应用程序客户端自定义范围?

我正在尝试使用 CloudFormation 自动化我的 Cognito 部署。我已经制作了一些自定义资源,因为并非所有内容都受支持。为此,我使用 AWS JS SDK。我想为特定用户池中的应用程序客户端设置“允许的自定义范围”。但是,我无法在 AWS 提供的任何文档中找到如何执行此操作。CLI 文档仅在Cognito-user-identity docs 的文档中说明了这一点:

AllowedOAuthScopes
A list of allowed OAuth scopes. Currently supported values are "phone", "email", "openid", and "Cognito".
Run Code Online (Sandbox Code Playgroud)

提到的范围有在用户池中始终可用的默认范围。但我也使用由我定义的自定义资源服务器提供的自定义范围。那些看起来像:resourceServer.com/scope。我找不到任何关于设置这些范围的文档。

那么,有没有办法通过 cli 或 sdk 设置自定义范围?

Joh*_*uez 6

AllowedOAuthScopes现场支持自定义范围。

文档:https ://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPoolClient.html#CognitoUserPools-CreateUserPoolClient-request-AllowedOAuthScopes

要通过 CLI 更新用户池客户端:https ://docs.aws.amazon.com/cli/latest/reference/cognito-idp/update-user-pool-client.html (查看--allowed-o-auth-范围选项)

请参阅下面的示例 cloudformation

UserPoolResourceServer:
    Type: AWS::Cognito::UserPoolResourceServer
    Properties: 
        Identifier: users
        Name: User API
        UserPoolId: !Ref UserPool
        Scopes: 
            - ScopeName: "write"
              ScopeDescription: "Write access"
            - ScopeName: "read"
              ScopeDescription: "Read access"

UserPoolClientAdmin:
    Type: "AWS::Cognito::UserPoolClient"
    Properties:
        AllowedOAuthFlows: 
            - client_credentials
        AllowedOAuthFlowsUserPoolClient: true
        AllowedOAuthScopes: 
            - users/read
            - users/write
Run Code Online (Sandbox Code Playgroud)


Raj*_*eev 5

对于来这里寻找解决方案的任何人,请遵循@JohnPauloRodriguez 的示例模板。但您可能需要在模板DependsOn中添加属性键UserPoolClient才能使其工作。

原因是,首先Resource Server这些自定义作用域应该存在,然后只有我们可以在客户端中引用它们。根据云形成文档

使用 DependsOn 属性,您可以指定特定资源的创建遵循另一个资源的创建。将 DependsOn 属性添加到资源时,仅在创建 DependsOn 属性中指定的资源后才会创建该资源。

所以模板UserPoolClient将变成:

CognitoUserPoolClient:
    Type: AWS::Cognito::UserPoolClient
    DependsOn: UserPoolResourceServer
    Properties:
      UserPoolId: !Ref UserPool
      AllowedOAuthFlowsUserPoolClient: true
      AllowedOAuthFlows:
        - code
      AllowedOAuthScopes: 
        - users/read
        - users/write
Run Code Online (Sandbox Code Playgroud)