يمكنك استخدام REST API في Google Cloud Identity Platform لإدارة إعدادات موفِّر هوية (IdP) OAuth في مشروع Firebase آليًا. باستخدام واجهة برمجة التطبيقات هذه، يمكنك ضبط موفّري الهوية الذين تريد إتاحتهم، وتعديل إعدادات OAuth الحالية لمشروعك وتفعيلها وإيقافها.
الحصول على إذن
قبل طلب البيانات من REST API، تحتاج إلى رمز دخول OAuth 2.0 يمنح إذن وصول بصفة محرّر إلى مشروع Firebase. على سبيل المثال، للحصول على رمز دخول باستخدام حساب خدمة في 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);
}
إضافة إعدادات جديدة لموفِّر هوية OAuth
لإضافة إعدادات جديدة لموفِّر هوية (IdP) يستخدم بروتوكول OAuth، أرسِل طلب POST بالإعدادات الجديدة إلى نقطة النهاية projects.defaultSupportedIdpConfigs.
عليك تحديد معرّف موفِّر الهوية ومعرّف العميل وسر العميل، والتي تحصل عليها عادةً من الموقع الإلكتروني لمطوّر البرامج لدى موفِّر الهوية. في ما يلي موفّرو الهوية الذين يتوافق معهم Firebase وأرقام تعريفهم:
| المزوّد | رقم تعريف موفِّر الهوية |
|---|---|
| Apple | apple.com |
| Apple Game Center | gc.apple.com |
facebook.com |
|
| GitHub | github.com |
google.com |
|
| ألعاب Google Play | playgames.google.com |
linkedin.com |
|
| Microsoft | microsoft.com |
twitter.com |
|
| Yahoo | yahoo.com |
على سبيل المثال، باستخدام 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);
إذا نجح الطلب، يتم عرض الإعدادات التي تم إنشاؤها حديثًا. على سبيل المثال:
{
name: 'projects/your-numerical-project-id/defaultSupportedIdpConfigs/facebook.com',
enabled: true,
clientId: 'your-facebook-client-id',
clientSecret: 'your-facebook-client-secret'
}
إذا حاولت ضبط موفِّر هوية سبق ضبطه لمشروعك، يعرض الطلب الخطأ 409 في بروتوكول HTTP. في هذه الحالة، يمكنك تحديث الإعدادات بدلاً من ذلك، كما هو موضح أدناه.
تعديل إعدادات موفِّر هوية OAuth
لتفعيل موفِّر هوية OAuth أو إيقافه، أو تعديل إعدادات العميل في مشروعك، عليك أولاً الحصول على الإعدادات الحالية للموفِّر من خلال إرسال طلب استرداد بيانات باستخدام GET إلى نقطة النهاية projects.defaultSupportedIdpConfigs.
بعد ذلك، يمكنك إجراء التغييرات التي تريدها على الإعدادات وتصحيح الإعدادات الجديدة
في نقطة النهاية projects.defaultSupportedIdpConfigs.
على سبيل المثال، باستخدام 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);
إذا حاولت تعديل إعدادات موفِّر هوية لم يسبق لك ضبطه لمشروعك، ستعرض الطلبات خطأ HTTP 404. بدلاً من ذلك، يمكنك إعداد موفِّر هوية جديد كما هو موضّح في القسم السابق.