Nandeep Mali
Nandeep Mali

Reputation: 4456

Firebase Authentication in a Chrome Extension Background Page

How would I authenticate with Firebase in a chrome extension? I need to specify the allowed domain list in the Forge. Chrome domain for the extension is just a big hash-like string.

I did read this: authClient.login problems

But the hashed based domain of a chrome extension is not being accepted in the Firebase forge. Is there another way to go about it? Currently am just reading the cookie firebaseSessionKey to just assume that I am logged in. But surely that can't be as secure as letting Firebase validate this session key.

Upvotes: 5

Views: 3021

Answers (2)

RIdotCOM
RIdotCOM

Reputation: 61

This will work using Google Plus Login Flow which I believe is the only one that allows cross authentication so the scopes are Google Plus Login.

"www[dot]googleapis[dot]com/auth/plus.login"

So what is happening here is you will get the access_token from the extension which you will be sending to firebase with the request using authwihtoauthtoken specifying google as a provider along with the access_token acquired from chrome.identity.getAuthToken()!

https://www.firebase.com/docs/web/api/firebase/authwithoauthtoken.html

Now the fact is that this access token could be issued by any other app, so we need to make sure that it is valid and has been issued for our app, basically we need to know there isn't man in the middle trying to access our database.

This verification is being made by the firebase.

They will check if this token belongs to the same application as the token has been issued to.

So you will need to create another set of credentials under the same application in the google developers console as for your extension. We will be basically doing the same thing as if we were to do it for our webpage but we will be inserting this new set of credentials to firebase's google oAuth in their security section.

They will do this check for us there. They will verify with google if the token is issued to the same app.

That's it.

Background Information.

https://developers.google.com/identity/protocols/OAuth2UserAgent#validatetoken

Use case

Sending ID tokens with requests that need to be authenticated. For example, if you need to pass data to your server and you want to ensure that particular data came from a specific user.

When to verify the access All tokens need to be verified on your server unless you know that they came directly from Google. Any token that you receive from your client apps must be verified.

Google has a tutorial how to do this for python found at:

"github[dot]com/googleplus/gplus-verifytoken-python"

So basically what is happening here is; instead you doing to verification from on your server, firebase does this verification for you when you enter the CLIENT_ID and APP_SECRET into the firebase and enable the Google Authentication.

The way to do this correctly is a combination or same style of verifying to whom the client_secret was issued. Chrome will give you a access_token and then this access_token will be checked on the firebase's backend.

Upvotes: 2

Anant
Anant

Reputation: 7428

As Rob points out, authentication cannot work in an environment that does not enforce origin restrictions. The fundamental problem here is that any authentication provider (Facebook, Twitter, Persona, or your own service) cannot issue an identity to a browser - i.e. it is meaningless to use Facebook to login to your browser (or extension).

The F1 add-on for Firefox ran into a similar problem (http://f1.mozillamessaging.com/) - where you would authorize F1 to post on twitter/facebook on your behalf. The extension had a website to along with it, from where you would serve the login page and proceed as you would normally in a web page. You'll need some code to communicate between the web page and your extension, chrome provides the tools necessary.

I would recommend the same approach - create a web page on a real domain (Github pages is awesome for this) to go along with your extension. This means your extension can't work offline, but neither can your login or writing to Firebase!

Upvotes: 8

Related Questions