

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 自訂範圍多租戶最佳實務
<a name="scope-based-multi-tenancy"></a>

Amazon Cognito 支援[資源伺服器的](cognito-user-pools-define-resource-servers.md)自訂 OAuth 2.0 範圍。您可以在具有自訂範圍的machine-to-machine(M2M) 授權模型的使用者集區中實作應用程式用戶端多重租用。範圍型多租用戶透過在您的應用程式用戶端或應用程式組態中定義存取權，減少實作 M2M 多租用戶所需的工作量。

 下圖說明自訂範圍多租用戶的一個選項。它會顯示每個租用戶，其具有可存取使用者集區中相關範圍的專用應用程式用戶端。

![\[說明多租戶架構中自訂範圍流程的圖表。\]](http://docs.aws.amazon.com/zh_tw/cognito/latest/developerguide/images/multi-tenancy-custom-scope.png)


**何時實作自訂範圍多租用戶**  
 當您在機密用戶端中使用用戶端憑證的 M2M 授權時。最佳實務是建立應用程式用戶端獨有的資源伺服器。自訂範圍多租用戶可以是*請求相依*或*用戶端相依*。

**請求相依**  
 實作應用程式邏輯，僅請求符合您租用戶需求的範圍。例如，應用程式用戶端可能可以發出 API A 和 API B 的讀取和寫入存取權，但租用戶應用程式 A 僅請求 API A 的讀取範圍和表示租用的範圍。此模型允許租用戶之間更複雜的共用範圍組合。

**用戶端相依**  
 請求授權請求中指派給應用程式用戶端的所有範圍。若要這樣做，請省略 `scope` 請求中的 請求參數[權杖端點](token-endpoint.md)。此模型可讓應用程式用戶端存放您要新增至自訂範圍的存取指標。

 在任何一種情況下，您的應用程式都會接收具有範圍的存取字符，這些範圍會指出其所依賴之資料來源的權限。範圍也可以向您的應用程式呈現其他資訊：
+ 指定租用
+ 有助於請求記錄
+ 指出應用程式有權查詢APIs 
+ 通知作用中客戶的初始檢查。

**努力程度**  
 自訂範圍多租用戶需要與應用程式規模不同的工作量。您必須設計應用程式邏輯，讓您的應用程式剖析存取權杖並提出適當的 API 請求。

 例如，資源伺服器範圍的格式為 `[resource server identifier]/[name]`。資源伺服器識別符不太可能與租戶範圍的授權決策相關，需要一致剖析範圍名稱。

## 資源範例
<a name="scope-based-multi-tenancy-example"></a>

 下列 AWS CloudFormation 範本會使用一個資源伺服器和應用程式用戶端，為自訂範圍多租用戶建立使用者集區。

```
AWSTemplateFormatVersion: "2010-09-09"
Description: A sample template illustrating scope-based multi-tenancy
Resources:
  MyUserPool:
    Type: "AWS::Cognito::UserPool"
  MyUserPoolDomain:
    Type: AWS::Cognito::UserPoolDomain
    Properties:
      UserPoolId: !Ref MyUserPool
      # Note that the value for "Domain" must be unique across all of AWS.
      # In production, you may want to consider using a custom domain.
      # See: https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-add-custom-domain.html#cognito-user-pools-add-custom-domain-adding
      Domain: !Sub "example-userpool-domain-${AWS::AccountId}"
  MyUserPoolResourceServer:
    Type: "AWS::Cognito::UserPoolResourceServer"
    Properties:
      Identifier: resource1
      Name: resource1
      Scopes:
        - ScopeDescription: Read-only access
          ScopeName: readScope
      UserPoolId: !Ref MyUserPool
  MyUserPoolTenantBatch1ResourceServer:
    Type: "AWS::Cognito::UserPoolResourceServer"
    Properties:
      Identifier: TenantBatch1
      Name: TenantBatch1
      Scopes:
        - ScopeDescription: tenant1 identifier
          ScopeName: tenant1
        - ScopeDescription: tenant2 identifier
          ScopeName: tenant2
      UserPoolId: !Ref MyUserPool
  MyUserPoolClientTenant1:
    Type: "AWS::Cognito::UserPoolClient"
    Properties:
      AllowedOAuthFlows:
        - client_credentials
      AllowedOAuthFlowsUserPoolClient: true
      AllowedOAuthScopes:
        - !Sub "${MyUserPoolTenantBatch1ResourceServer}/tenant1"
        - !Sub "${MyUserPoolResourceServer}/readScope"
      GenerateSecret: true
      UserPoolId: !Ref MyUserPool
Outputs:
  UserPoolClientId:
    Description: User pool client ID
    Value: !Ref MyUserPoolClientTenant1
  UserPoolDomain:
    Description: User pool domain
    Value: !Sub "https://${MyUserPoolDomain}.auth.${AWS::Region}.amazoncognito.com"
```