Mit der Google Cloud Identity Platform REST API können Sie die OAuth-Identitätsanbieterkonfiguration (IdP) eines Firebase-Projekts programmatisch verwalten. Mit dieser API können Sie die Identitätsanbieter konfigurieren, die Sie unterstützen möchten, und die aktuellen OAuth-Konfigurationen Ihres Projekts aktualisieren, aktivieren und deaktivieren.
Autorisierung abrufen
Bevor Sie die REST API aufrufen können, benötigen Sie ein OAuth 2.0-Zugriffstoken, das Ihnen Bearbeitungszugriff auf Ihr Firebase-Projekt gewährt. So rufen Sie beispielsweise ein Zugriffstoken mit einem Dienstkonto in Node.js ab:
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);
}
Neue OAuth-Identitätsanbieterkonfiguration hinzufügen
Wenn Sie eine neue OAuth-Identitätsanbieterkonfiguration (IdP) hinzufügen möchten, senden Sie die neue Konfiguration per POST an den Endpunkt projects.defaultSupportedIdpConfigs.
Sie müssen die ID des Identitätsanbieters sowie Ihre Client-ID und Ihren Clientschlüssel angeben, die Sie normalerweise auf der Entwicklerwebsite des Anbieters finden. Hier sind die von Firebase unterstützten Identitätsanbieter und ihre IDs:
| Anbieter | IdP-ID |
|---|---|
| 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 |
Beispiel mit 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);
Wenn der Aufruf erfolgreich ist, wird die neu erstellte Konfiguration zurückgegeben. Beispiel:
{
name: 'projects/your-numerical-project-id/defaultSupportedIdpConfigs/facebook.com',
enabled: true,
clientId: 'your-facebook-client-id',
clientSecret: 'your-facebook-client-secret'
}
Wenn Sie versuchen, einen Identitätsanbieter zu konfigurieren, der bereits für Ihr Projekt konfiguriert wurde, gibt der Aufruf den HTTP-Fehler 409 zurück. In diesem Fall können Sie stattdessen die Konfiguration aktualisieren, wie unten beschrieben.
Konfiguration eines OAuth-Identitätsanbieters aktualisieren
Wenn Sie einen OAuth-Identitätsanbieter aktivieren oder deaktivieren oder die Clientkonfiguration Ihres Projekts aktualisieren möchten, rufen Sie zuerst die aktuelle Konfiguration des Anbieters ab. Senden Sie dazu eine GET-Anfrage an den Endpunkt projects.defaultSupportedIdpConfigs.
Nehmen Sie dann die gewünschten Änderungen an der Konfiguration vor und senden Sie die neue Konfiguration mit PATCH an den Endpunkt projects.defaultSupportedIdpConfigs.
Beispiel mit 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);
Wenn Sie versuchen, die Konfiguration eines Identitätsanbieters zu aktualisieren, den Sie noch nie für Ihr Projekt konfiguriert haben, geben die Aufrufe den HTTP-Fehler 404 zurück. Konfigurieren Sie stattdessen einen neuen Identitätsanbieter, wie im vorherigen Abschnitt beschrieben.