

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

# 사용자 지정 범위 다중 테넌시 모범 사례
<a name="scope-based-multi-tenancy"></a>

Amazon Cognito는 [리소스 서버](cognito-user-pools-define-resource-servers.md)에 대한 사용자 지정 OAuth 2.0 범위를 지원합니다. 사용자 지정 범위가 있는 M2M(기계 간) 권한 부여 모델의 사용자 풀에서 앱 클라이언트 다중 테넌시를 구현할 수 있습니다. 범위 기반 다중 테넌시는 앱 클라이언트 또는 애플리케이션 구성에서 액세스를 정의하여 M2M 다중 테넌시를 구현하는 데 필요한 노력을 줄입니다.

 다음 다이어그램은 사용자 지정 범위 다중 테넌시에 대한 한 가지 옵션을 보여줍니다. 사용자 풀의 관련 범위에 액세스할 수 있는 전용 앱 클라이언트가 있는 각 테넌트를 보여줍니다.

![\[멀티 테넌트 아키텍처에서 사용자 지정 범위의 흐름을 보여주는 다이어그램입니다.\]](http://docs.aws.amazon.com/ko_kr/cognito/latest/developerguide/images/multi-tenancy-custom-scope.png)


**사용자 지정 범위 다중 테넌시를 구현해야 하는 경우**  
 기밀 클라이언트에서 클라이언트 자격 증명을 사용한 M2M 권한 부여를 사용하는 경우. 가장 좋은 방법은 앱 클라이언트 전용 리소스 서버를 생성하는 것입니다. 사용자 지정 범위 다중 테넌시는 *요청에 따라 달라지거나* *클라이언트에 따라 달라질* 수 있습니다.

**요청에 따라 달라짐**  
 애플리케이션 로직을 구현하여 테넌트의 요구 사항과 일치하는 범위만 요청합니다. 예를 들어 앱 클라이언트는 API A 및 API B에 대한 읽기 및 쓰기 액세스를 발급할 수 있지만 테넌트 애플리케이션 A는 API A에 대한 읽기 범위와 테넌시를 나타내는 범위만 요청합니다. 이 모델을 사용하면 테넌트 간에 공유 범위를 더 복잡하게 조합할 수 있습니다.

**클라이언트에 따라 달라짐**  
 권한 부여 요청에서 앱 클라이언트에 할당된 모든 범위를 요청합니다. 이렇게 하려면 [Token 엔드포인트](token-endpoint.md)에 대한 요청에서 `scope` 요청 파라미터를 생략합니다. 이 모델을 사용하면 앱 클라이언트가 사용자 지정 범위에 추가하려는 액세스 표시기를 저장할 수 있습니다.

 어떤 경우든 애플리케이션은 종속 데이터 소스에 대한 권한을 나타내는 범위가 있는 액세스 토큰을 수신합니다. 범위는 애플리케이션에 다른 정보를 제공할 수도 있습니다.
+ 테넌시 지정
+ 로깅 요청에 기여
+ 애플리케이션이 쿼리할 수 있는 권한이 있는 API 표시
+ 활성 고객에 대한 초기 확인을 알립니다.

**노력 수준**  
 사용자 지정 범위 다중 테넌시에는 애플리케이션 규모에 따라 다양한 수준의 노력이 필요합니다. 애플리케이션이 액세스 토큰을 구문 분석하고 적절한 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"
```