Firebase Messaging and Authentication Combined to Set Custom Token - javascript

PROBLEM: There are two Firebase services (Cloud Messaging and Authentication) we want to combine in our project. The goal is to set FCM token a value we already know, not the one generated by FirebaseInstanceId.getInstance().getToken(). Thus the same token is used to get authorized both on custom server and Firebase server. Moreover multiple application instances would have been reached with one token and would have accepted the same message.
RESEARCH:
Sending a Message to Multiple Registration Tokens,
Creating Custom Tokens.
QUESTION:
Are these services supposed to be combined?
Is FirebaseInstanceId.getInstance().getToken() equal to what FirebaseAuth.getInstance().createCustomToken(uid) generates?
Is it safe to set an FCM token from client (message redirected to unsupposed device if hacked)?

No. The FCM service and Auth service are different and standalone -- one could be used without the other.
No. The token generated by the getToken() is the token that should be used to target the corresponding device for push notifications, createCustomToken() generates a token for authentication.
The FCM token is generated by calling the FirebaseInstanceId service on the client side -- it's the common usage, so yeah, I'd say it's safe.

Related

Google Identity Refresh Token (Web)

Per https://developers.google.com/identity/sign-in/web/server-side-flow in order to get a refresh token, the client must grant offline access. However, I see no way to do that using the new API. What's the proper way to get a refreshed token using the Google button, so the user doesn't need to re-login every hour?
Authentication for sign-in has been separated from authorization for data access in he new API.
In the new API authentication does not require or use access or refresh tokens, instead a signed JWT ID token credential containing the user profile is shared after user consent. This greatly simplifies the level of effort and need to manage tokens during app sign-up or sign-in.
If you're performing authorization to access Google APIs and storing refresh tokens on your backend, then a server-to-server OAuth flow is appropriate.
Another clarification, user sign-in to your app and maintaining session state are separate concepts and should be handled separate from backend processes which may use an offline refresh token to perform actions through a Google API on behalf of the user while they are not logged in.

How to implement token based authentication in oauth2.0 process

Below is my understanding of the process of oauth2.0(using google as the oauth2.0 server)
my customer click 'login with google account' button on client side.
the browser redirect to google's login page.
my customer inputs it's credential and click 'login'.
if my customer succeeded in previous step,the browser will redirect to my server's url
(www.[myserver].com/auth/google/callback) with some extra query
data.
then my server will do some work to get some token from google and finally get my customer's information.
My question comes from the next step. I want to use token based authentication.Then I have to make my customer to store my own token in localStorage. I can't figure out how to achieve this in the 6th step(how to send a new token to client side and store it in localStorage?).
(I know that every thing will be easy if I am using cookie-based authentication. because I can just utilize 'set-cookie' in the 6th step, and the client side will be easily store the credential data in client side's cookie)
Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, installed, and client-side applications.
I figure out that I can use some type of template engine on my server side(just like discussed here). So I can render my template file using variable before send it to my customer.

How to authenticate user in browser using AWS Cognito?

I need to authenticate users in browser (not mobile app) using AWS Cognito with username/pass, not FB/google IdProviders.
There are a lot of docs but they seem to be separate blocks which either incomplete, do not fit the requirements or do not fit each others :(
I created Cognito User Pool, then Identity pool and tied the userPool to the idPool, then I stuck. Do not know which library to use and how to use it.
The closest I find are:
https://aws.amazon.com/sdk-for-browser/ but my experience is not enough to convert their FB samples to not-using FB
https://github.com/aws/aws-amplify but using this lib I'll have to study React/Angular from the very beginning (I'm not a front-end developer, sorry) and I have no clue how to convert their npm-based samples to front-end javascript (npm is for NodeJS thus back-end, isn't it?).
All I need is plain html form with username/pass, send the request to Cognito and a way to check during the next page load whether the password was correct. If it matters I will use AWS Lambda as back-end for processing future tasks.
How can I do it? Is there a tutorial/doc for my case?
Thank you.
You can use AWS Cognito UserPools Hosted UI for your use case. The simplest form of authentication is using the Implicit Grant.
For more information about setting up Hosted UI refer Add an App to Enable the Hosted Web UI.. This will create a UserPool where users can register them self (If you plan to restrict this, you will need to either add users using the AWS Web Console, Cognito UserPools or using their SDK)
The steps are as follows.
Set up Cognito Hosted UI and register your application domain. This will create the login/registration pages for you where each of this will have a unique URL. What you have to do is, if the user is not authenticated (Let's discuss how to detect it later), you need to redirect the user to the Login page.
In the Login URL, you also need to specify the redirect back URL to the application so that after a successful login, Cognito will redirect back the user to the application providing the token in a query string.
You can then access the id_token from inside the application and use it for querying the backend.
Since the id_token is a JWT token you can verify it at your Backend using the public key available at the Cognito token endpoint.
To implement the JWT verification, you can also refer Cognito JWT Token validator NodeJS module.
Note: If you need to keep the user's logged in for a longer time period (Than 1 hr), you might need to use the Code Grant flow which will return a Refresh Token, which could be used to retrieve new id_tokens programmatically.

Using node package outside of node app

I'm making a chrome extension that uses Firebase database. For their custom authentication I need to create a JWT token to have the user sign in.
I saw on their documentation that their is a function to generate these tokens in node using the function call:
var customToken = firebase.auth().createCustomToken(uid);
My issue is that my chrome extension is not a node app, so I don't have access to this function. I have the firebase auth API from their CDN but when I try using that function I get firebase.auth(...).createCustomToken is not a function.
I guess that the create JWT token function is just for node.js firebase users. Is there a way I can still get access to this token generator without my extension being a node app?
I'm new to this JWT stuff so really I'm just looking for an easy way to generate one of these tokens somehow.
Creating a custom token requires access to the service account of your Firebase project. This is a server-side secret and should never be present in the clients that access your project. If you give your clients the secret needed to mint a custom token, you're giving them full access to your project. You might as well not use authentication in that case.
The typical approach is to run a server that mints a custom token based on your app's needs and then use that token in your client (e.g. a chrome extension) to sign in.
Alternatively you can use one of the built-in authentication methods, such as email+password, Facebook, Google or even anonymous sign-in.

Authenticated users with both Android and web (JavaScript)

I try to sync data for a Cognito authenticated user using the new User Pool.
From what's suggested here:
"Note that the generate client secret box must be unchecked because the JavaScript SDK doesn't support apps that have a client secret."
On the other hand for Android only apps with a client secret are supported.
So I added 2 apps to the User Pool, one with a client secret and one without it.
However, in the Identity Pool I can only add one App Client ID as an authentication provider.
So I get: "com.amazonaws.services.cognitoidentity.model.NotAuthorizedException: Token is not from a supported provider of this identity pool." when trying to use an authenticated token for the Android app.
Am I doing it right, or am I missing something?
Thanks!
Figured it out.
It is possible to add multiple (up to 100) apps or "audiences" to an OIDC provider through the IAM as explained here
So I set up the OIDC and added it as the authentication provider to the identity pool.
Since you already worked out a solution, one additional thought:
On the other hand for Android only apps with a client secret are supported.
The Android SDK for Cognito User Pools should allow you to handle apps without a secret, you should be able to just set that value to null. Check out the sample app for more details, if you are interested.

Categories