firebase-arduino icon indicating copy to clipboard operation
firebase-arduino copied to clipboard

Implement support for token based auth

Open RICK0707 opened this issue 8 years ago • 44 comments

Since Database secret are now being deprecated would it be possible to use new token generation. Can it be done on ESP8266 ? -> Database secrets are currently deprecated and use a legacy Firebase token generator. Update your source code with the Firebase Admin SDK.

RICK0707 avatar Nov 07 '16 18:11 RICK0707

+1 for authentication with Firebase using password-based accounts

andreask1 avatar Nov 16 '16 20:11 andreask1

You may find it here http://stackoverflow.com/questions/37418372/firebase-where-is-my-account-secret-in-the-new-console

However, please develop this feature as database secret support is deprecated

gayanpathirage avatar Nov 28 '16 10:11 gayanpathirage

Hi, i have same issue. I can't use token to connect firebase database... Can i help me?

phamtankhai2202 avatar Dec 26 '16 06:12 phamtankhai2202

Have you any plans to implement Token based Auth ? The issue is open from Last 2.5 Months 👎

atiqsamtia avatar Jan 13 '17 13:01 atiqsamtia

Sorry for the late follow-up

It seems that the REST API only support OAuth2.0 based auth, see https://firebase.google.com/docs/reference/rest/database/user-auth

In order to support it in the library we would need to:

  • replace ?auth= with ?access_token
  • embed client_id, client_secret in the sketch
  • use a third-party tool (ex: oauth2playground) to initialily generate the access_token/refresh_token pair
  • embed the refresh_token in the sketch
  • implement the refresh logic in the library since access_token expire after 1 hour
POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded

client_id=...&
client_secret=...&
refresh_token=...&
grant_type=refresh_token

proppy avatar Jan 20 '17 05:01 proppy

Will the new token based authentication be implemented in the future release?

francois1144 avatar Apr 06 '17 11:04 francois1144

Just found https://github.com/yutter/ArduinoJWT, which could make it easier implement Service account support directly on the ESP.

proppy avatar Apr 09 '17 06:04 proppy

Can you please elaborate on how to use this? I'm new to this authorization thing and its very confusing. I'm afraid that once the database secrets are fully deprecated, my software won't work anymore, so I'd like to implement token authorization as soon as possible.

antonclaeys avatar Apr 26 '17 09:04 antonclaeys

Hi there, new to the rep and have been playing around with the library but also running into this authentication issue. Trying to set up a home control system that allows different people in my house to control ESP-based switches, but want to use user-based auth to limit access only to a subset of the database (e.g. those switches they're authorized to access). Seems well documented on the firebase side but this library can't handle authentication beyond the deprecated database secret. Very willing to help out with the issue but not sure where to start. :/ Cheers David

ghost avatar May 03 '17 17:05 ghost

@davido1992 thanks for reaching out, I think a proper solution for this would be to either use oauth2 end user credentials or a jwt service account.

Sadly it looks like Firebase Admin custom token don't really work with the REST API.

proppy avatar May 03 '17 17:05 proppy

@proppy, can you elaborate on how to use OAuth 2.0 on esp? Is it doable with a 3rd party library or something? I intend to make an authentication with esp on firebase and create an user there, the same way I'd do with an iOS app or Android app. Thanks.

fabricio-stein avatar May 09 '17 18:05 fabricio-stein

Does anybody have a working example of esp8266 connected to firebase?

antcosic avatar Aug 21 '17 19:08 antcosic

@proppy can you provide end user credentials example please?

beratuslu avatar Sep 10 '17 19:09 beratuslu

@beratuslu we would need to implement some refresh logic using the oauth2 endpoint: https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline

proppy avatar Sep 11 '17 00:09 proppy

Any news about this issue ?

rjga94 avatar Oct 04 '17 10:10 rjga94

@proppy please can you provide example...

beratuslu avatar Oct 06 '17 14:10 beratuslu

Hi @beratuslu,

As described in https://github.com/firebase/firebase-arduino/issues/224#issuecomment-273981906 I think we should modify https://github.com/firebase/firebase-arduino/blob/master/src/Firebase.cpp#L91 to refresh the get a new refresh token when it expires.

Something like this could work:

http_->setReuseConnection(true);
http_->begin('www.googleapis.com', '/oauth2/v4/token');
http_->sendRequest('POST', 'client_id=...&client_secret=...&refresh_token=...&grant_type=refresh_token');

And then insert the new access token in the Authorization header or in the query string (if the former doesn't work).

proppy avatar Oct 06 '17 15:10 proppy

@proppy Is the firebase-arduino library modified to use oauth2 instead and no secret key?

sergioldr avatar Mar 06 '18 16:03 sergioldr

@proppy what if we generate access token from service accounts using a custom cloud function? they are free to use to certain amount. it is not ideal solution since you still need to authenticate yourself to the cloud function in some way (which could be a shared secret given as parameter), but that should be a choice of the user of this library.

kotl avatar May 15 '18 03:05 kotl

@kotl I think we need something that the device can refresh.

Either by reusing the jwt Arduino integration from @gguuss Cloud IoT Core library or embededing end users credentials (refresh_token) on the device and implementing OAuth2 refresh: https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline

I'd be in favor of the former as it allow to give device a dedicated identity (service account)

proppy avatar May 15 '18 05:05 proppy

In order to support it in the library we would need to: -replace ?auth= with ?access_token -embed client_id, client_secret in the sketch -use a third-party tool (ex: oauth2playground) to initialily generate the access_token/refresh_token pair -embed the refresh_token in the sketch -implement the refresh logic in the library since access_token expire after 1 hour

I think the questions of creating Oauth tokens and using them should be kept separate. The suggested changes from @proppy would enable this project to use a provided token instead of a db secret.

How you get them does not matter for the implementation and can be determined at a later point.

eberlemartin avatar Jun 08 '18 14:06 eberlemartin

Any news on this topic?

WilkoV avatar Jun 22 '18 05:06 WilkoV

Any news about it? It's a core feature, with the lack of it even the simplest integration between arduino and firebase would not be possible. I'm praying for the guys who knows/can implement this feature = ]

gpsgui avatar Jun 22 '18 20:06 gpsgui

+1 this feature is a must-have!

Aar0nC avatar Jul 14 '18 23:07 Aar0nC

@proppy

I've started exploring how can we get there. Managed to get ID token and then authenticated REST request to RTDB, generated through NodeJS, but without using anything from Firebase Admin / Client SDK. This means that we just need to translate this code into Arduino C++ and use ArduinoJWT library and one https request to get idToken. This simulates generation of custom tokens on the server side and then using signInWithCustomToken on client side, but all done on the client instead. I still believe a better way would be to do this through a Cloud Function that accepts parameters like email/password for generating access token, but we can probably support both methods eventually since once you have id token, it works.

In this sample you will need to copy sample_data.js into data.js and provide appropriate fields. https://github.com/kotl/firebase-arduino/tree/AUTH/contrib/auth Run 'node auth_jwt.js > test.sh' and then 'sh test.sh' will retrieve data from RTDB.

Updated July 15: turns out, we don't need to create tokens if we decide to use only email/password auth. In a new sample I made: https://github.com/kotl/firebase-arduino/blob/AUTH/contrib/auth/auth_email.js A simple https request to identity toolkit is all you need to mint idToken.

kotl avatar Jul 15 '18 04:07 kotl

@kotl but we still to implement refresh for the token as well?

proppy avatar Aug 02 '18 18:08 proppy

@kotl

Hi, could you explain implementation of you method, where i should add this auth_email.js ? into the project or upload to Firebase ? Thanks in advance.

rasulovk avatar Aug 05 '18 03:08 rasulovk

I would love for this to work - is there anything I can do to help?

crashless avatar Oct 13 '18 17:10 crashless

Edited: After googling more about firebase and cloud function, I agreed with @kotl that using signInWithCustomToken will be the best solution, as we can get away with forgotten password or stupid password. IMHO, the authentication can be done from app/service/web, like many said, it doesn't matter once have the first token, but can be done possibly with these steps:

  1. App in Mobile/pc connect to the device, authenticate the user.
  2. App get the initial token (via createCustomToken(id)) or pick it up from the file as part of sketch
  3. App save initial token (and maybe device id) and also to ESP if wasn't there
  4. Esp connects to firebase with the initial token (and maybe device id)

Does anyone know how to get this library to work with a initial token for now? Has been playing around with @proppy 's suggestion but don't seem to work.

yhua537 avatar Dec 16 '18 11:12 yhua537

Any news :(

EgHubs avatar Dec 14 '20 15:12 EgHubs