Menyiapkan API SSO SAML Masuk

Halaman ini menjelaskan cara menyiapkan API Profil SSO SAML Masuk (inboundSamlSsoProfiles) dan Penetapan SSO Masuk (inboundSsoAssignments) Cloud Identity sebelum mengelola profil SAML atau mengonfigurasi penetapan single sign-on (SSO).

Sebelum memulai

Sebelum menggunakan Inbound SAML SSO API, Anda harus menyiapkan Cloud Identity atau Google Workspace untuk domain Anda. Untuk mengetahui petunjuknya, lihat Menyiapkan Cloud Identity.

Menginstal library klien Python

Untuk menginstal library klien Python dan library autentikasi Google, jalankan perintah berikut:

pip install --upgrade google-api-python-client google-auth \
  google-auth-oauthlib google-auth-httplib2

Untuk mengetahui informasi selengkapnya tentang cara menyiapkan lingkungan pengembangan Python, lihat Panduan Penyiapan Lingkungan Pengembangan Python.

Mengaktifkan API dan menyiapkan kredensial

  1. Login ke akun Google Cloud Anda. Jika Anda baru menggunakan Google Cloud, buat akun untuk mengevaluasi performa produk kami dalam skenario dunia nyata. Pelanggan baru juga mendapatkan kredit gratis senilai $300 untuk menjalankan, menguji, dan men-deploy workload.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Identity API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Cloud Identity API, if it is not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the API

Melakukan autentikasi dengan Inbound SAML SSO API

API Inbound SAML SSO Profiles dan Inbound SSO Assignments mengelola setelan autentikasi tingkat domain. Anda dapat mengautentikasi panggilan menggunakan salah satu metode berikut:

  • Akun layanan dengan delegasi tingkat domain (direkomendasikan untuk alur kerja otomatis dan layanan backend): Akun layanan meniru identitas administrator super Google Workspace atau Cloud Identity untuk memanggil API.
  • Kredensial OAuth 2.0 administrator (direkomendasikan untuk alat interaktif dan skrip CLI): Pengguna administrator melakukan autentikasi langsung melalui alur web OAuth 2.0 atau alur izin lokal.

Cakupan OAuth

API Profil SSO SAML Masuk dan Penetapan SSO Masuk mendukung cakupan OAuth berikut:

Cakupan Deskripsi
https://www.googleapis.com/auth/cloud-identity.inboundsso Akses baca dan tulis penuh untuk membuat, memperbarui, menghapus, mendapatkan, dan mencantumkan profil serta penetapan SSO masuk.
https://www.googleapis.com/auth/cloud-identity.inboundsso.readonly Akses hanya baca untuk mendapatkan dan mencantumkan profil dan penetapan SSO masuk.
https://www.googleapis.com/auth/cloud-identity Akses penuh ke semua resource Cloud Identity API.
https://www.googleapis.com/auth/cloud-platform Akses penuh ke semua resource dan API Google Cloud .

Melakukan autentikasi sebagai akun layanan dengan delegasi tingkat domain

Untuk memanggil Inbound SAML SSO API dari akun layanan, Anda harus memberikan delegasi tingkat domain kepada akun layanan di konsol Google Admin. Karena delegasi tingkat domain memungkinkan akun layanan meniru identitas pengguna administrator, tindakan akun layanan akan muncul di log audit sebagai telah dilakukan oleh administrator yang ditiru identitasnya.

Langkah 1: Delegasikan otoritas tingkat domain ke akun layanan

  1. Di konsol Google Cloud , salin OAuth 2 Client ID (string numerik client ID) akun layanan Anda dari halaman Service Accounts.
  2. Buka konsol Google Admin.
  3. Buka Menu utama > Keamanan > Kontrol data dan akses > Kontrol API.
  4. Di panel Domain wide delegation, klik Manage Domain Wide Delegation.
  5. Klik Tambahkan baru.
  6. Di kolom Client ID, masukkan client ID akun layanan.
  7. Di kolom OAuth scopes, masukkan cakupan yang diperlukan sebagai daftar yang dipisahkan koma:
    • Untuk akses baca dan tulis: https://www.googleapis.com/auth/cloud-identity.inboundsso
    • Untuk akses hanya baca: https://www.googleapis.com/auth/cloud-identity.inboundsso.readonly
  8. Klik Otorisasi.

Untuk mengetahui petunjuk mendetail, lihat Mengontrol akses API dengan delegasi tingkat domain.

Langkah 2: Lakukan inisialisasi kredensial dalam kode aplikasi

Saat Anda melakukan inisialisasi kredensial yang didelegasikan di aplikasi, tentukan alamat email administrator domain menggunakan parameter subject:

Python

from google.auth import iam
from google.auth.transport import requests
import google.auth
from google.oauth2 import service_account
from googleapiclient.discovery import build

# OAuth scope authorized for domain-wide delegation in the Admin console
SCOPES = ['https://www.googleapis.com/auth/cloud-identity.inboundsso']

# Administrator email address to impersonate
ADMIN_EMAIL = 'admin@example.com'

# Option A: Using a service account JSON key file
# credentials = service_account.Credentials.from_service_account_file(
#     'SERVICE_ACCOUNT_KEY_FILE.json',
#     scopes=SCOPES,
#     subject=ADMIN_EMAIL
# )

# Option B: Using Application Default Credentials (ADC) with IAM Signer
base_credentials, _ = google.auth.default(
    scopes=['https://www.googleapis.com/auth/iam']
)
request = requests.Request()
base_credentials.refresh(request)

signer = iam.Signer(
    request,
    base_credentials,
    base_credentials.service_account_email
)
delegated_credentials = service_account.Credentials(
    signer=signer,
    service_account_email=base_credentials.service_account_email,
    token_uri='https://accounts.google.com/o/oauth2/token',
    scopes=SCOPES,
    subject=ADMIN_EMAIL
)

# Build the Cloud Identity API client
service = build('cloudidentity', 'v1', credentials=delegated_credentials)

Langkah 3: Gunakan gcloud CLI dengan peniruan akun layanan

Untuk menguji atau memanggil Inbound SAML SSO API menggunakan gcloud CLI dengan Kredensial Default Aplikasi (ADC) dan peniruan akun layanan, jalankan perintah berikut:

gcloud auth application-default login \
  --impersonate-service-account=SERVICE_ACCOUNT_EMAIL \
  --scopes=https://www.googleapis.com/auth/iam,https://www.googleapis.com/auth/cloud-identity.inboundsso

Ganti kode berikut:

  • SERVICE_ACCOUNT_EMAIL: alamat email akun layanan yang telah mengonfigurasi delegasi di seluruh domain.

Melakukan autentikasi sebagai administrator dengan OAuth 2.0

Untuk mengautentikasi aplikasi atau skrip interaktif secara langsung sebagai administrator super tanpa akun layanan, gunakan kredensial pengguna OAuth 2.0 standar.

Untuk mengetahui informasi selengkapnya tentang cara mendapatkan kredensial OAuth 2.0 untuk aplikasi web atau desktop, lihat Menggunakan OAuth 2.0 untuk mengakses Google API.

Langkah berikutnya