외부 ID로 서비스 계정 사용

이 문서에서는 외부 ID와 함께 IAP(Identity-Aware Proxy)를 사용할 때 서비스 계정을 사용하여 인증하는 방법을 보여줍니다.

클라이언트 ID 및 보안 비밀번호 가져오기

  1. Google Cloud 콘솔에서 IAP 페이지로 이동합니다.

    IAP 페이지로 이동

  2. 애플리케이션 탭을 클릭합니다.

  3. 서비스 계정을 사용하도록 구성할 앱을 찾습니다.

  4. 더보기 메뉴에서 OAuth 구성으로 이동을 선택합니다.

앱의 클라이언트 ID와 보안 비밀번호를 표시하는 페이지가 나타납니다. 다음 섹션에서 Identity Platform을 구성하려면 이러한 구성요소가 필요합니다.

ID 공급업체로 Google 구성

Identity Platform 프로젝트가 인증에 아직 Google을 사용하지 않는 경우 클라이언트 ID와 보안 비밀번호를 사용하여 새 구성을 만드세요.

  1. Google Cloud 콘솔에서 Identity Platform 공급업체 페이지로 이동합니다.
    ID 공급업체 페이지로 이동

  2. Identity Platform 멀티 테넌시를 사용하는 경우 IAP 리소스와 관련된 테넌트를 선택합니다.

  3. 공급업체 추가를 클릭합니다.

  4. 공급업체 목록에서 Google을 선택합니다.

  5. 웹 SDK 구성 아래에서 이전 섹션에서 얻은 클라이언트 ID 및 보안 비밀번호를 입력합니다.

  6. 저장을 클릭합니다.

이미 Google 인증을 사용 중인 경우 대신 클라이언트 ID를 사용할 수 있습니다. 이렇게 해도 기존 사용자에게는 영향이 없습니다.

  1. Google Cloud 콘솔에서 Identity Platform 공급업체 페이지로 이동합니다.
    ID 공급업체 페이지로 이동

  2. Identity Platform 멀티 테넌시를 사용하는 경우 IAP 리소스와 관련된 테넌트를 선택합니다.

  3. 공급업체 목록에서 Google을 찾아 수정을 클릭합니다.

  4. 허용된 클라이언트 ID에서 추가를 클릭합니다.

  5. 이전 섹션에서 얻은 클라이언트 ID를 입력합니다.

  6. 저장을 클릭합니다.

Google 토큰을 Identity Platform 토큰으로 바꾸기

Google을 통해 처음 인증하면 Identity Platform이 Google ID 토큰을 반환합니다. 그런 다음 signInWithIdp를 호출하여 Identity Platform 토큰으로 교환할 수 있습니다.

Node.js

import * as firebase from 'firebase/app';
import 'firebase/auth';

const config = {
  apiKey: '...',
};
firebase.initializeApp(config);
const cred = firebase.auth.GoogleAuthProvider.credential(google_oidc_id_token);
firebase.auth().signInWithCredential(cred)
  .then((userCredential) => {
    return userCredential.user.getIdToken();
  })
  .then((gcipIdToken) => {
    // This token can now be used to access the resource.
  })
  .catch((error) => {
    // Error occurred.
  });

Python

SIGN_IN_WITH_IDP_API = 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp'

def exchange_google_id_token_for_gcip_id_token(api_key, tenant_id, google_open_id_connect_token):
  url = SIGN_IN_WITH_IDP_API + '?key=' + api_key
  data={'requestUri': 'http://localhost',
        'returnSecureToken': True,
        'postBody':'id_token=' + google_open_id_connect_token + '&providerId=google.com',
        'tenantId': tenant_id}
  resp = requests.post(url, data)
  res = resp.json()
  return res['idToken']

REST

요청:

POST https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=API-KEY

본문:

{
"postBody":"id_token=GOOGLE-ID-TOKEN&providerId=google.com"
"requestUri": "http://localhost",
"returnIdpCredential": true,
"returnSecureToken": true,
"tenantId": "TENANT-ID"
}

IAP로 리소스에 액세스하려면 승인 헤더에 Identity Platform ID 토큰을 포함합니다.

curl -H "Authorization: Bearer GCIP-ID-TOKEN" "https://example.appspot.com/api"

외부 ID는 IAM을 지원하지 않으므로 서비스 계정에 대한 액세스 권한을 부여하도록 앱의 액세스 제어를 수동으로 업데이트해야 합니다. 자세히 알아보려면 외부 ID를 위한 JWT를 참조하세요.