

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

# 자격 증명 얻기
<a name="getting-credentials"></a>

Amazon Cognito를 사용하여 애플리케이션에 제한된 권한의 임시 자격 증명을 제공하여 사용자가 AWS 리소스에 액세스할 수 있도록 할 수 있습니다. 이 섹션에서는 자격 증명을 가져오는 방법과 자격 증명 풀에서 Amazon Cognito 자격 증명을 가져오는 방법을 설명합니다.

Amazon Cognito는 인증된 자격 증명과 인증되지 않은 자격 증명을 모두 지원합니다. 미인증 사용자는 자격 증명이 인증되지 않았으므로 앱 혹은 자격 증명의 인증이 중요하지 않은 경우 게스트 사용자 역할에 적합합니다. 인증받은 사용자는 타사 자격 증명 공급자(IdP)를 통해, 또는 자격 증명을 인증받은 사용자 풀을 통해 애플리케이션에 로그인합니다. 미인증 사용자의 액세스 권한을 허용하지 않도록 리소스 권한 범위를 충분히 정했는지 확인하세요.

Amazon Cognito 자격 증명(identity)은 자격 증명(credential)이 아닙니다. ()에서 웹 자격 증명 연동 지원을 사용하여 자격 증명으로 교환됩니다 AWS Security Token Service AWS STS. 앱 사용자의 AWS 자격 증명을 얻으려면 AWS SDK의 Cognito 자격 증명 풀 자격 증명 공급자를 사용합니다. 예를 들어 AWS SDK for JavaScript(v3)에서 `@aws-sdk/credential-providers` 패키지의 `fromCognitoIdentityPool` 함수를 사용합니다. 그런 다음 자격 증명은를 사용하여 자격 증명으로 교환됩니다 AWS STS.

웹 자격 증명 보안 인증 공급자는 AWS SDK의 기본 보안 인증 공급자 체인의 일부입니다. SDK 또는의 로컬 `config` 파일에서 자격 증명 풀 토큰을 AWS 설정하려면 `web_identity_token_file` 프로필 항목을 AWS CLI추가합니다. SDK 및 도구 참조 안내서의 [역할 자격 증명 공급자 수임을](https://docs.aws.amazon.com/sdkref/latest/guide/feature-assume-role-credentials.html) 참조하세요. AWS SDKs 

SDK에 웹 자격 증명 보안 인증을 채우는 방법에 대한 자세한 내용은 SDK 개발자 안내서를 참조하세요. 최상의 결과를 얻으려면에 내장된 자격 증명 풀 통합으로 프로젝트를 시작합니다 AWS Amplify.

**AWS 자격 증명 풀을 사용하여 자격 증명을 가져오고 설정하기 위한 SDK 리소스**
+ Amplify 개발자 센터의 [자격 증명 풀 페더레이션](https://docs.amplify.aws/lib/auth/advanced/q/platform/android/#identity-pool-federation)(Android)
+ Amplify 개발자 센터의 [자격 증명 풀 페더레이션](https://docs.amplify.aws/lib/auth/advanced/q/platform/ios/#identity-pool-federation)(iOS)
+  AWS SDK for JavaScript 개발자 안내서의[ Amazon Cognito 자격 증명을 사용하여 사용자 인증](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/loading-browser-credentials-cognito.html) 
+  AWS SDK for .NET 개발자 안내서의 [Amazon Cognito 자격 증명 공급자](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/cognito-creds-provider.html) 
+  AWS SDK for Go 개발자 안내서의 [프로그래밍 방식으로 자격 증명 지정](https://docs.aws.amazon.com/sdk-for-go/v2/developer-guide/configure-gosdk.html#specify-credentials-programmatically) 
+  AWS SDK for Java 2.x 개발자 안내서[의 코드에 임시 자격 증명 제공](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/credentials-explicit.html) 
+  AWS SDK for PHP 개발자 안내서의 [assumeRoleWithWebIdentityCredentialProvider](https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_provider.html#assume-role-with-web-identity-provider) 공급자
+  AWS SDK for Python (Boto3)  설명서의 [웹 ID 제공업체를 사용하여 역할 수임](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html#assume-role-with-web-identity-provider)
+  AWS SDK for Rust 개발자 안내서의 [자격 증명 및 기본 리전 지정](https://docs.aws.amazon.com/sdk-for-rust/latest/dg/credentials.html) 

다음 섹션에서는 일부 AWS SDKs의 예제 코드를 제공합니다.

## Android
<a name="getting-credentials-1.android"></a>

Amazon Cognito를 사용하여 애플리케이션에 제한된 권한의 임시 자격 증명을 제공하여 사용자가 AWS 리소스에 액세스할 수 있도록 할 수 있습니다. Amazon Cognito는 인증된 자격 증명과 인증되지 않은 자격 증명을 모두 지원합니다. 앱에 AWS 자격 증명을 제공하려면 다음 단계를 따르세요.

Android 앱에서 Amazon Cognito 자격 증명 풀을 사용하려면를 설정합니다 AWS Amplify. 자세한 내용은 *Amplify 개발자 센터*의 [인증](https://docs.amplify.aws/lib/auth/getting-started/q/platform/android/)을 참조하세요.

**Amazon Cognito 자격 증명 검색**

인증되지 않은 사용자를 허용하는 경우 최종 사용자의 고유한 Amazon Cognito 식별자(자격 증명 ID)를 즉시 검색할 수 있습니다. 사용자를 인증하는 경우 자격 증명 공급자에서 로그인 토큰을 설정한 후 자격 증명 ID를 검색할 수 있습니다.

```
String identityId = credentialsProvider.getIdentityId();
Log.d("LogTag", "my ID is " + identityId);
```

**참고**  
 애플리케이션의 기본 스레드에서 `getIdentityId()`, `refresh()` 또는 `getCredentials()`를 호출하지 마세요. Android 3.0(API 레벨 11)을 기준으로 기본 애플리케이션 스레드에서 네트워크 I/O를 수행하는 경우 앱이 자동으로 실패하고 [NetworkOnMainThreadException](https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html)이 발생합니다. `AsyncTask`를 사용하여 코드를 백그라운드 스레드로 이동해야 합니다. 자세한 내용은 [Android 설명서](https://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask)를 참조하세요. 이미 로컬에 캐시된 경우에만 `getCachedIdentityId()`를 호출하여 ID를 검색할 수 있습니다. 그렇지 않으면 메서드가 null을 반환합니다.

## iOS - Objective-C
<a name="getting-credentials-1.ios-objc"></a>

Amazon Cognito를 사용하여 애플리케이션에 제한된 권한의 임시 자격 증명을 제공하여 사용자가 AWS 리소스에 액세스할 수 있도록 할 수 있습니다. Amazon Cognito 자격 증명 풀은 인증된 자격 증명과 인증되지 않은 자격 증명을 모두 지원합니다. 앱에 AWS 자격 증명을 제공하려면 다음 단계를 완료하세요.

iOS 앱에서 Amazon Cognito 자격 증명 풀을 사용하려면를 설정합니다 AWS Amplify. 자세한 내용은 *Amplify 개발자 센터*의 [Swift 인증](https://docs.amplify.aws/lib/auth/getting-started/q/platform/ios/) 및 [Flutter 인증](https://docs.amplify.aws/lib/auth/getting-started/q/platform/flutter/)을 참조하세요.

**Amazon Cognito 자격 증명 검색**

인증되지 않은 사용자를 허용하는 경우 또는 사용자를 인증하는 경우 자격 증명 공급자에서 로그인 토큰을 설정한 후에 최종 사용자의 Amazon Cognito 식별자(자격 증명 ID)를 즉시 검색할 수 있습니다.

```
// Retrieve your Amazon Cognito ID
[[credentialsProvider getIdentityId] continueWithBlock:^id(AWSTask *task) {
    if (task.error) {
        NSLog(@"Error: %@", task.error);
    }
    else {
        // the task result will contain the identity id
        NSString *cognitoId = task.result;
    }
    return nil;
}];
```

**참고**  
 `getIdentityId`는 비동기식 호출입니다. 자격 증명 ID가 이미 공급자에 설정된 경우 `credentialsProvider.identityId`를 호출하여 해당 자격 증명을 검색할 수 있으며 이 자격 증명은 로컬에 캐시됩니다. 그러나 공급자에 자격 증명 ID가 설정되지 않은 경우를 호출하면가 `credentialsProvider.identityId` 반환됩니다`nil`. 자세한 내용은 [Amplify iOS SDK 참조](https://github.com/aws-amplify/aws-sdk-ios)를 참조하세요.

## iOS - Swift
<a name="getting-credentials-1.ios-swift"></a>

사용자가 AWS 리소스에 액세스할 수 있도록 Amazon Cognito를 사용하여 애플리케이션에 제한된 권한의 임시 자격 증명을 제공할 수 있습니다. Amazon Cognito는 인증된 자격 증명과 인증되지 않은 자격 증명을 모두 지원합니다. 앱에 AWS 자격 증명을 제공하려면 다음 단계를 따르세요.

iOS 앱에서 Amazon Cognito 자격 증명 풀을 사용하려면를 설정합니다 AWS Amplify. 자세한 내용은 *Amplify 개발자 센터*의 [Swift 인증](https://docs.amplify.aws/lib/auth/getting-started/q/platform/ios/)을 참조하세요.

**Amazon Cognito 자격 증명 검색**

인증되지 않은 사용자를 허용하는 경우 또는 사용자를 인증하는 경우 자격 증명 공급자에서 로그인 토큰을 설정한 후에 최종 사용자의 Amazon Cognito 식별자(자격 증명 ID)를 즉시 검색할 수 있습니다.

```
// Retrieve your Amazon Cognito ID
credentialsProvider.getIdentityId().continueWith(block: { (task) -> AnyObject? in
    if (task.error != nil) {
        print("Error: " + task.error!.localizedDescription)
    }
    else {
        // the task result will contain the identity id
        let cognitoId = task.result!
        print("Cognito id: \(cognitoId)")
    }
    return task;
})
```

**참고**  
 `getIdentityId`는 비동기식 호출입니다. 자격 증명 ID가 이미 공급자에 설정된 경우 `credentialsProvider.identityId`를 호출하여 해당 자격 증명을 검색할 수 있으며 이 자격 증명은 로컬에 캐시됩니다. 그러나 공급자에 자격 증명 ID가 설정되지 않은 경우를 호출하면가 `credentialsProvider.identityId` 반환됩니다`nil`. 자세한 내용은 [Amplify iOS SDK 참조](https://github.com/aws-amplify/aws-sdk-ios)를 참조하세요.

## JavaScript
<a name="getting-credentials-1.javascript"></a>

자격 증명 풀을 아직 만들지 않았다면 `fromCognitoIdentityPool`를 사용하기 전에 [Amazon Cognito 콘솔](https://console.aws.amazon.com/cognito)에서 자격 증명 풀을 만듭니다.

자격 증명 공급자로 자격 증명 풀을 구성한 후 `@aws-sdk/credential-providers` 패키지의 `fromCognitoIdentityPool` 함수를 사용하여 사용자를 인증할 수 있습니다. 글로벌를 설정하는 대신 `credentials` 속성을 통해 자격 증명 공급자를 각 서비스 클라이언트에 직접 `AWS.config.credentials`연결합니다. v3 SDK는 클라이언트가 처음 요청할 때 공급자를 느리게 확인하므로 자격 증명을 얻기 위한 별도의 호출이 없습니다. 다음 예제에서는 공급자를에 연결합니다. `S3Client` 

```
import { S3Client } from "@aws-sdk/client-s3";
import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers";

// Set the region where your identity pool exists (for example, us-east-1 or eu-west-1)
const region = "REGION";

// Attach the credentials provider to your service client
const client = new S3Client({
  region,
  credentials: fromCognitoIdentityPool({
    identityPoolId: "IDENTITY_POOL_ID",
    logins: { // optional tokens, used for authenticated login
      "graph.facebook.com": "FBTOKEN",
      "www.amazon.com": "AMAZONTOKEN",
      "accounts.google.com": "GOOGLETOKEN",
      "appleid.apple.com": "APPLETOKEN"
    },
    clientConfig: { region },
  }),
});

// Credentials are resolved lazily when the client first makes a request.
// To inspect them directly, call the provider and await the result
// (from inside an async function):
const credentials = await client.config.credentials();
const { accessKeyId, secretAccessKey, sessionToken } = credentials;
```

선택 사항인 `logins` 속성은 공급자의 자격 증명 토큰에 대한 자격 증명 공급자 이름의 맵입니다. 자격 증명 공급자에게서 토큰을 받는 방법은 어떤 공급자를 사용하느냐에 따라 다릅니다. 예를 들어 Facebook이 자격 증명 공급자 중 하나인 경우 [Facebook SDK](https://developers.facebook.com/docs/facebook-login/web)의 `FB.login` 함수를 사용하여 자격 증명 공급자 토큰을 얻을 수 있습니다.

```
FB.login(function (response) {
    if (response.authResponse) { // logged in
        const region = "us-east-1";
        const client = new S3Client({
          region,
          credentials: fromCognitoIdentityPool({
            identityPoolId: "us-east-1:1699ebc0-7900-4099-b910-2df94f52a030",
            logins: {
              "graph.facebook.com": response.authResponse.accessToken
            },
            clientConfig: { region },
          }),
        });

        console.log('You are now logged in.');
    } else {
        console.log('There was a problem logging you in.');
    }
});
```

**Amazon Cognito 자격 증명 검색**

최종 사용자의 고유한 Amazon Cognito 식별자(자격 증명 ID)를 검색할 수 있습니다. 인증되지 않은 사용자를 허용하는 경우 즉시 검색할 수 있습니다. 사용자를 인증하는 경우 로그인 토큰을 설정한 후 검색합니다. `fromCognitoIdentityPool` 공급자는 확인된 자격 증명`identityId`에를 노출하므로 공급자를 사용하는 클라이언트의 확인된 자격 증명에서 읽을 수 있습니다.

```
// Inside an async function:
const { identityId } = await client.config.credentials();
```

## Unity
<a name="getting-credentials-1.unity"></a>

 Amazon Cognito를 사용하여 애플리케이션에 제한된 권한의 임시 자격 증명을 제공하여 사용자가 AWS 리소스에 액세스할 수 있도록 할 수 있습니다. Amazon Cognito는 인증된 자격 증명과 인증되지 않은 자격 증명을 모두 지원합니다. 앱에 AWS 자격 증명을 제공하려면 다음 단계를 따르세요.

[AWS  SDK for Unity](https://docs.aws.amazon.com/mobile/sdkforunity/developerguide/what-is-unity-plugin.html)는 이제 [SDK for .NET](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/welcome.html)의 일부입니다. 에서 Amazon Cognito를 시작하려면 AWS SDK for .NET 개발자 안내서의 [Amazon Cognito 자격 증명 공급자](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/cognito-creds-provider.html)를 SDK for .NET참조하세요. 또는를 사용하여 앱을 빌드하는 옵션은 [Amplify Dev Center](https://docs.amplify.aws/)를 참조하세요 AWS Amplify.

**Amazon Cognito 자격 증명 검색**

 인증되지 않은 사용자를 허용하는 경우 또는 사용자를 인증하는 경우 자격 증명 공급자에서 로그인 토큰을 설정한 후에 최종 사용자의 Amazon Cognito 식별자(자격 증명 ID)를 즉시 검색할 수 있습니다.

```
string identityId = await credentials.GetIdentityIdAsync();
```