Bạn có thể sử dụng API REST của Google Cloud Identity Platform để quản lý cấu hình nhà cung cấp dịch vụ danh tính (IdP) OAuth của dự án Firebase theo phương thức lập trình. Với API này, bạn có thể định cấu hình các nhà cung cấp dịch vụ danh tính mà bạn muốn hỗ trợ, đồng thời cập nhật, bật và tắt cấu hình OAuth hiện tại của dự án.
Nhận uỷ quyền
Trước khi có thể gọi API REST, bạn cần có mã truy cập OAuth 2.0 cấp quyền truy cập của Người chỉnh sửa vào dự án Firebase của bạn. Ví dụ: để lấy mã truy cập bằng tài khoản dịch vụ trong Node.js:
const googleAuth = require('google-auth-library');
const SCOPES = ['https://www.googleapis.com/auth/cloud-platform'];
async function getAccessToken() {
const serviceAccount = require('/path/to/service_account_key.json');
const jwtClient = new googleAuth.JWT(
serviceAccount.client_email,
null,
serviceAccount.private_key,
SCOPES,
null
);
return jwtClient.authorize().then((tokens) => tokens.access_token);
}
Thêm cấu hình nhà cung cấp dịch vụ danh tính OAuth mới
Để thêm cấu hình nhà cung cấp dịch vụ danh tính (IdP) OAuth mới, hãy POST cấu hình mới vào điểm cuối projects.defaultSupportedIdpConfigs.
Bạn cần chỉ định mã nhận dạng của nhà cung cấp dịch vụ danh tính, mã ứng dụng khách và khoá bí mật của ứng dụng khách. Bạn thường lấy các thông tin này từ trang web dành cho nhà phát triển của nhà cung cấp. Sau đây là các nhà cung cấp dịch vụ danh tính mà Firebase hỗ trợ và mã nhận dạng của họ:
| Nhà cung cấp | Mã nhận dạng của IdP (nhà cung cấp danh tính) |
|---|---|
| Apple | apple.com |
| Apple Game Center | gc.apple.com |
facebook.com |
|
| GitHub | github.com |
google.com |
|
| Google Play Games | playgames.google.com |
linkedin.com |
|
| Microsoft | microsoft.com |
twitter.com |
|
| Yahoo | yahoo.com |
Ví dụ: sử dụng Node.js:
const fetch = require('node-fetch');
const GCIP_API_BASE = 'https://identitytoolkit.googleapis.com/v2';
async function addIdpConfig(projectId, accessToken, idpId, clientId, clientSecret) {
const uri = `${GCIP_API_BASE}/projects/${projectId}/defaultSupportedIdpConfigs?idpId=${idpId}`;
const options = {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify({
name: `projects/${projectId}/defaultSupportedIdpConfigs/${idpId}`,
enabled: true,
clientId: clientId,
clientSecret: clientSecret,
}),
};
return fetch(uri, options).then((response) => {
if (response.ok) {
return response.json();
} else if (response.status == 409) {
throw new Error('IdP configuration already exists. Update it instead.');
} else {
throw new Error('Server error.');
}
});
}
(async () => {
const projectId = 'your-firebase-project-id';
const accessToken = await getAccessToken();
const idpId = 'facebook.com';
const clientId = 'your-facebook-client-id';
const clientSecret = 'your-facebook-client-secret';
try {
await addIdpConfig(projectId, accessToken, idpId, clientId, clientSecret);
} catch (err) {
console.error(err.message);
}
})().catch(console.error);
Nếu lệnh gọi thành công, lệnh này sẽ trả về cấu hình mới tạo. Ví dụ:
{
name: 'projects/your-numerical-project-id/defaultSupportedIdpConfigs/facebook.com',
enabled: true,
clientId: 'your-facebook-client-id',
clientSecret: 'your-facebook-client-secret'
}
Nếu bạn cố gắng định cấu hình một nhà cung cấp dịch vụ danh tính đã được định cấu hình cho dự án của mình, thì lệnh gọi sẽ trả về lỗi HTTP 409. Trong trường hợp này, bạn có thể cập nhật cấu hình, như mô tả bên dưới.
Cập nhật cấu hình nhà cung cấp dịch vụ danh tính OAuth
Để bật hoặc tắt nhà cung cấp dịch vụ danh tính OAuth hoặc cập nhật cấu hình ứng dụng của dự án, trước tiên, hãy lấy cấu hình hiện tại của nhà cung cấp bằng cách gửi yêu cầu GET đến điểm cuối projects.defaultSupportedIdpConfigs.
Sau đó, hãy thực hiện các thay đổi bạn muốn đối với cấu hình và PATCH cấu hình mới cho điểm cuối projects.defaultSupportedIdpConfigs.
Ví dụ: sử dụng Node.js:
async function getIdpCfg(projectId, accessToken, idpId) {
const uri = `${GCIP_API_BASE}/projects/${projectId}/defaultSupportedIdpConfigs/${idpId}`;
const options = {
method: 'GET',
headers: {
'Authorization': `Bearer ${accessToken}`
},
};
return fetch(uri, options).then((response) => {
if (response.ok) {
return response.json();
} else if (response.status == 404) {
throw new Error('IdP configuration not found. First add the IdP'
+ ' configuration to your project.');
} else {
throw new Error('Server error.');
}
});
}
async function updateIdpConfig(accessToken, idpCfg) {
const uri = `${GCIP_API_BASE}/${idpCfg.name}`;
const options = {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${accessToken}`
},
body: JSON.stringify(idpCfg),
};
return fetch(uri, options).then((response) => {
if (response.ok) {
return response.json();
} else if (response.status == 404) {
throw new Error('IdP configuration not found. First add the IdP'
+ ' configuration to your project.');
} else {
throw new Error('Server error.');
}
});
}
(async () => {
const projectId = 'your-firebase-project-id';
const accessToken = await getAccessToken();
const idpId = 'facebook.com';
try {
// Get the IdP's current configuration.
const idpCfg = await getIdpCfg(projectId, accessToken, idpId);
// Update the configuration. (For example, disable the IdP.)
idpCfg.enabled = false;
await updateIdpConfig(accessToken, idpCfg);
} catch (err) {
console.error(err.message);
}
})().catch(console.error);
Nếu bạn cố gắng cập nhật cấu hình của một nhà cung cấp dịch vụ danh tính mà bạn chưa từng định cấu hình cho dự án của mình, thì các lệnh gọi sẽ trả về Lỗi HTTP 404. Thay vào đó, hãy định cấu hình một nhà cung cấp dịch vụ danh tính mới như trong phần trước.