Firebase প্রমাণীকরণের জন্য প্রোগ্রাম্যাটিকভাবে OAuth পরিচয় প্রদানকারী কনফিগার করুন

আপনি একটি Firebase প্রকল্পের OAuth পরিচয় প্রদানকারী (IdP) কনফিগারেশন প্রোগ্রাম্যাটিকভাবে পরিচালনা করতে Google Cloud Identity Platform REST API ব্যবহার করতে পারেন। এই API এর মাধ্যমে, আপনি যে পরিচয় প্রদানকারীকে সমর্থন করতে চান তা কনফিগার করতে পারেন এবং আপনার প্রকল্পের বর্তমান 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 পরিচয় প্রদানকারী কনফিগারেশন যোগ করুন

একটি নতুন OAuth পরিচয় প্রদানকারী (IdP) কনফিগারেশন যোগ করতে, নতুন কনফিগারেশনটি projects.defaultSupportedIdpConfigs এন্ডপয়েন্টে পোস্ট করুন।

আপনাকে পরিচয় প্রদানকারীর আইডি এবং আপনার ক্লায়েন্ট আইডি এবং ক্লায়েন্টের গোপনীয়তা উল্লেখ করতে হবে, যা আপনি সাধারণত প্রদানকারীর বিকাশকারী সাইট থেকে পান। ফায়ারবেস সমর্থন করে এবং তাদের আইডিগুলি এখানে পরিচয় সরবরাহকারী রয়েছে:

প্রদানকারী আইডিপি আইডি
আপেল apple.com
অ্যাপল গেম সেন্টার gc.apple.com
ফেসবুক facebook.com
গিটহাব github.com
গুগল google.com
গুগল প্লে গেমস playgames.google.com
লিঙ্কডইন linkedin.com
মাইক্রোসফট microsoft.com
টুইটার twitter.com
ইয়াহু 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'
}

If you try to configure an identity provider that has already been configured for your project, the call returns HTTP error 409. In this situation, you can update the configuration instead, as described below.

একটি OAuth পরিচয় প্রদানকারী কনফিগারেশন আপডেট করুন

To enable or disable an OAuth identity provider, or update your project's client configuration, first get the provider's current configuration by making a GET request to the the projects.defaultSupportedIdpConfigs endpoint. তারপর, কনফিগারেশনে আপনি যে পরিবর্তনগুলি চান তা করুন এবং নতুন কনফিগারেশনটিকে 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);

If you try to update the configuration of an identity provider you've never configured for your project, the calls will return HTTP error 404. Instead, configure a new identity provider as shown in the previous section .