Search code examples
firebasefirebase-authenticationgoogle-cloud-functionsaccess-tokengoogle-smart-home

Action on google Smart home token exchange problem


I am getting stuck at a point.

Situation:

We use Firebase Authentication in Mobile App IOS/Android and Action On Google (Smart Home). along side we use Firebase Function to handle request.

Problem:

Account linking on Action on google is my problem. however my authentication URL works perfectly and able to send back data successfully. but when it calls token url we get the problem here.

As, I said we use Firebase Authentication, we don't know how to fetch Access token and Refresh token from Firebase to send back to Google Home.

Here is my token code below, i just send constant token right now, i have no idea how to obtain Access token, please help, i would be really thankful to you.

Token URL Firebase Function:

export const token = functions.https.onRequest(async (request, response) => {
  //admin.auth().getUser(response)
  const grantType = request.query.grant_type ?
    request.query.grant_type : request.body.grant_type;
  const secondsInDay = 86400; // 60 * 60 * 24
  const HTTP_STATUS_OK = 200;
  console.log(`Grant type ${grantType}`);


  const user = admin.auth().getUser(request.body.code);

  user.getIdToken(true).then(function(idToken) {

}).catch(function(error) {

});

  console.log(request.body.client_id);
  console.log('test');
  console.log(request.body.client_secret);


  let obj = {};
  if (grantType === 'authorization_code')  {

      obj = {
        token_type: 'bearer',
        access_token: 'jkljkhasdjhjhkdhasdsd',
        refresh_token: 'nkjcajkguiahdilawjd,bcjkbakdjhdlasd',
        expires_in: secondsInDay,
      };
  } else if (grantType === 'refresh_token') {


    obj = {
      token_type: 'bearer',
      access_token: 'njkhasiumnabkdukchaskjhkhad',
      expires_in: secondsInDay,
    };
  }
  response.status(HTTP_STATUS_OK)
    .json(obj);
});

Solution

  • we use Firebase Authentication, we don't know how to fetch Access token and Refresh token from Firebase to send back to Google Home.

    Firebase Authentication does not directly expose functionality to mint and validate OAuth tokens for users, it simply enables you to provide client-side user sign-in and validation. You will need to implement your own OAuth token management on top of Firebase Authentication.

    We have an example app that does something similar which you might find helpful, and a companion blog post that describes some of the implementation details. The example uses JWT to encode the Firebase Authentication user data so each request can determine how to map the Assistant intents to the Firebase uid value.