OAuth를 사용한 Google 계정 연결 (암시적 흐름 - 보관처리됨)

OAuth 2.0 암시적 흐름을 지원하기 위해 서비스는 HTTPS를 통해 승인 엔드포인트를 제공합니다. 이 엔드포인트는 인증을 담당하고 데이터 액세스에 대한 사용자 동의를 획득합니다. 승인 엔드포인트는 아직 로그인하지 않은 사용자에게 로그인 UI를 표시하고 요청된 액세스에 대한 동의를 기록합니다.

Google 애플리케이션이 서비스의 승인된 API 중 하나를 호출해야 하는 경우 Google은 이 엔드포인트를 사용하여 사용자를 대신하여 이러한 API를 호출할 수 있는 권한을 사용자로부터 얻습니다.

Google 계정 연결: OAuth 암시적 흐름

다음 시퀀스 다이어그램은 사용자, Google, 서비스의 엔드포인트 간의 상호작용을 자세히 설명합니다.

사용자 Google 앱 / 브라우저 인증 엔드포인트 1. 사용자가 연결을 시작합니다. 2. 인증 엔드포인트로 리디렉션 (GET) client_id, redirect_uri, state, scope 3. 로그인 및 동의 화면 표시 4. 사용자 인증 및 동의 부여 5. 토큰과 함께 Google로 다시 리디렉션 (GET) access_token, state 6. 사용자 토큰 저장 7. 사용자 리소스 액세스
그림 1. Google 계정 연결을 위한 OAuth 2.0 암시적 흐름의 이벤트 시퀀스입니다.

역할 및 책임

다음 표에서는 Google 계정 연결 (GAL) OAuth 암시적 흐름의 행위자 역할과 책임을 정의합니다. GAL에서 Google은 OAuth 클라이언트로 작동하고 서비스는 ID/서비스 제공업체로 작동합니다.

작업 수행자 / 구성요소 GAL 역할 책임
Google 앱 / 서버 OAuth 클라이언트 흐름을 시작하고, 브라우저 리디렉션을 사용하여 액세스 토큰을 수신하고, 서비스의 API에 액세스하기 위해 안전하게 저장합니다.
승인 엔드포인트 승인 서버 사용자를 인증하고, 동의를 얻고, Google에 직접 장기 액세스 토큰을 발급합니다.
Google 리디렉션 URI 콜백 엔드포인트 URL 프래그먼트에 access_tokenstate 값이 포함된 승인 서비스에서 사용자 리디렉션을 수신합니다.

Google에서 시작한 일반적인 OAuth 2.0 암시적 흐름 세션은 다음과 같은 흐름을 갖습니다.

  1. Google이 사용자의 브라우저에서 승인 엔드포인트를 엽니다. 사용자가 아직 로그인하지 않은 경우 로그인하고, 아직 권한을 부여하지 않은 경우 Google이 API로 데이터에 액세스할 수 있는 권한을 부여합니다.
  2. 서비스가 액세스 토큰을 만들어 Google에 반환합니다. 이렇게 하려면 요청에 액세스 토큰을 첨부하여 사용자의 브라우저를 Google로 다시 리디렉션하세요.
  3. Google은 서비스의 API를 호출하고 각 요청에 액세스 토큰을 첨부합니다. 서비스는 액세스 토큰이 Google에 API 액세스 권한을 부여하는지 확인한 후 API 호출을 완료합니다.

구현 레시피

다음 단계에 따라 암시적 흐름을 구현하세요.

1단계: 승인 요청 처리

Google이 계정 연결을 시작하면 사용자를 승인 엔드포인트로 리디렉션합니다. 자세한 프로토콜 계약 및 매개변수 요구사항은 인증 엔드포인트를 참고하세요.

요청을 처리하려면 다음 작업을 실행하세요.

  1. 요청 유효성 검사:

    • client_id이 Google에 할당된 클라이언트 ID와 일치하는지 확인합니다.
    • redirect_uri이 예상되는 Google 리디렉션 URL과 일치하는지 확인합니다. none https://oauth-redirect.googleusercontent.com/r/YOUR_PROJECT_ID https://oauth-redirect-sandbox.googleusercontent.com/r/YOUR_PROJECT_ID
    • response_typetoken인지 확인합니다.
  2. 사용자 인증:

    • 사용자가 서비스에 로그인했는지 확인합니다.
    • 사용자가 로그인하지 않은 경우 로그인 또는 가입 절차를 완료하라는 메시지를 표시합니다.
  3. 액세스 토큰 생성:

    • 사용자 및 클라이언트와 연결된 추측할 수 없는 고유한 액세스 토큰을 만듭니다.
  4. Google로 다시 리디렉션:

    • 브라우저를 redirect_uri에 제공된 URL로 리디렉션합니다.
    • URL 프래그먼트 (해시)에 다음 매개변수를 추가합니다.
      • access_token: 생성한 액세스 토큰입니다.
      • token_type: bearer이어야 합니다.
      • state: Google에서 수신한 수정되지 않은 상태 값입니다.
Handle userinfo requests

The userinfo endpoint is an OAuth 2.0 protected resource that return claims about the linked user. Implementing and hosting the userinfo endpoint is optional, except for the following use cases:

After the access token has been successfully retrieved from your token endpoint, Google sends a request to your userinfo endpoint to retrieve basic profile information about the linked user.

userinfo endpoint request headers
Authorization header The access token of type Bearer.

For example, if your userinfo endpoint is available at https://myservice.example.com/userinfo, a request might look like the following:

GET /userinfo HTTP/1.1
Host: myservice.example.com
Authorization: Bearer ACCESS_TOKEN

For your userinfo endpoint to handle requests, do the following steps:

  1. Extract access token from the Authorization header and return information for the user associated with the access token.
  2. If the access token is invalid, return an HTTP 401 Unauthorized error with using the WWW-Authenticate Response Header. Below is an example of a userinfo error response:
    HTTP/1.1 401 Unauthorized
    WWW-Authenticate: error="invalid_token",
    error_description="The Access Token expired"
    
    If a 401 Unauthorized, or any other unsuccessful error response is returned during the linking process, the error will be non-recoverable, the retrieved token will be discarded and the user will have to initiate the linking process again.
  3. If the access token is valid, return and HTTP 200 response with the following JSON object in the body of the HTTPS response:

    {
    "sub": "USER_UUID",
    "email": "EMAIL_ADDRESS",
    "given_name": "FIRST_NAME",
    "family_name": "LAST_NAME",
    "name": "FULL_NAME",
    "picture": "PROFILE_PICTURE",
    }
    If your userinfo endpoint returns an HTTP 200 success response, the retrieved token and claims are registered against the user's Google account.

    userinfo endpoint response
    sub A unique ID that identifies the user in your system.
    email Email address of the user.
    given_name Optional: First name of the user.
    family_name Optional: Last name of the user.
    name Optional: Full name of the user.
    picture Optional: Profile picture of the user.