

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

# 사전 인증 Lambda 트리거
<a name="user-pool-lambda-pre-authentication"></a>

예비 작업을 수행하는 사용자 지정 검증을 생성할 수 있도록 사용자가 로그인을 시도할 때 Amazon Cognito가 이 트리거를 호출합니다. 예를 들어 인증 요청을 거부하거나 세션 데이터를 외부 시스템에 기록할 수 있습니다.

**참고**  
이 Lambda 트리거는 사용자 풀 앱 클라이언트의 `PreventUserExistenceErrors` 설정이 `ENABLED`로 설정되어 있지 않으면 사용자가 존재하지 않는 경우 활성화되지 않습니다. 기존 인증 세션을 갱신해도 이 트리거가 활성화되지 않습니다.

**Topics**
+ [흐름 개요](#user-pool-lambda-pre-authentication-1)
+ [사전 인증 Lambda 트리거 파라미터](#cognito-user-pools-lambda-trigger-syntax-pre-auth)
+ [사전 인증 예제](#aws-lambda-triggers-pre-authentication-example)

## 흐름 개요
<a name="user-pool-lambda-pre-authentication-1"></a>

![\[사전 인증 Lambda 트리거 - 클라이언트 흐름\]](http://docs.aws.amazon.com/ko_kr/cognito/latest/developerguide/images/lambda-pre-authentication-1.png)


요청에는 앱이 사용자 풀 `InitiateAuth` 및 `AdminInitiateAuth` API 작업에 전달하는 `ClientMetadata` 값에 있는 클라이언트 검증 데이터가 포함됩니다.

자세한 내용은 [인증 세션의 예](authentication.md#amazon-cognito-user-pools-authentication-flow) 단원을 참조하십시오.

## 사전 인증 Lambda 트리거 파라미터
<a name="cognito-user-pools-lambda-trigger-syntax-pre-auth"></a>

Amazon Cognito가 이 Lambda 함수에 전달하는 요청은 아래 파라미터와 Amazon Cognito가 모든 요청에 추가하는 [공통 파라미터](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-working-with-lambda-triggers.html#cognito-user-pools-lambda-trigger-syntax-shared)의 조합입니다.

------
#### [ JSON ]

```
{
    "request": {
        "userAttributes": {
            "string": "string",
            . . .
        },
        "validationData": {
            "string": "string",
            . . .
        },
        "userNotFound": boolean
    },
    "response": {}
}
```

------

### 사전 인증 요청 파라미터
<a name="cognito-user-pools-lambda-trigger-syntax-pre-auth-request"></a>

**userAttributes**  
사용자 속성을 나타내는 하나 이상의 이름-값 페어입니다.

**userNotFound**  
사용자 풀 클라이언트에 대해 `PreventUserExistenceErrors`를 `ENABLED`로 설정한 경우 Amazon Cognito에서 이 부울을 채웁니다.

**validationData**  
사용자 로그인 요청에 검증 데이터를 포함하는 하나 이상의 키-값 페어입니다. 이 데이터를 Lambda 함수에 전달하려면 [InitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_InitiateAuth.html) 및 [AdminInitiateAuth](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminInitiateAuth.html) API 작업에서 ClientMetadata 파라미터를 사용합니다.

### 사전 인증 응답 파라미터
<a name="cognito-user-pools-lambda-trigger-syntax-pre-auth-response"></a>

Amazon Cognito는 함수가 응답에서 반환하는 추가 정보를 처리하지 않습니다. 함수가 오류를 반환하여 로그인 시도를 거부하거나 API 작업을 사용하여 리소스를 쿼리하고 수정할 수 있습니다.

## 사전 인증 예제
<a name="aws-lambda-triggers-pre-authentication-example"></a>

이 예제 함수는 사용자가 특정 앱 클라이언트로 사용자 풀에 로그인하지 못하도록 합니다. 사전 인증 Lambda 함수는 사용자에게 기존 세션이 있을 때는 호출하지 않기 때문에 이 함수는 차단하려는 앱 클라이언트 ID가 있는 *새* 세션만 방지합니다.

------
#### [ Node.js ]

```
const handler = async (event) => {
  if (
    event.callerContext.clientId === "user-pool-app-client-id-to-be-blocked"
  ) {
    throw new Error("Cannot authenticate users from this user pool app client");
  }

  return event;
};

export { handler };
```

------
#### [ Python ]

```
def lambda_handler(event, context):
    if event['callerContext']['clientId'] == "<user pool app client id to be blocked>":
        raise Exception("Cannot authenticate users from this user pool app client")

    # Return to Amazon Cognito
    return event
```

------

Amazon Cognito는 이벤트 정보를 Lambda 함수에 전달합니다. 그런 다음 함수는 응답이 변경되면 동일한 이벤트 객체를 Amazon Cognito에 반환합니다. Lambda 콘솔에서 해당 Lambda 트리거와 관련 있는 데이터로 테스트 이벤트를 설정할 수 있습니다. 다음은 이 코드 샘플의 테스트 이벤트입니다.

------
#### [ JSON ]

```
{
    "callerContext": {
        "clientId": "<user pool app client id to be blocked>"
    },
    "response": {}
}
```

------