Introduction
Firebase is a mobile app development framework from Google and provides various services and seamless integration with google Cloud. Xamarin has provided a good example of integration of Firebase with Google IDP.
I will show how to extend the same with Facebook and even with LinkedIn. Firebase has few major authentication providers which are managed identity providers through firebase but so far Instagram, LinkedIn have not been included which are unmanaged identity providers and can be maintained by firebase with the use of custom firebase token.
Find the source code in https://github.com/amartyamandal/vindpro
Background
While I was investigating on various authentication mechanisms for one of my pet projects, I picked Firebase for some of my basic needs.
On the other hand, I am not good at Java and do not even want to think about it so Xamarin is a wonderful alternative for the developer who has already been infected by C# (notice I used the word “infected”) as well as Visual Studio 2017 (any version of studios -hands down -most beautiful & efficient IDE of the galaxy).
So, my objective is very simple - using Microsoft development products (Visual Studio/VSTS/C#/Xamarin) to develop a product and never using any additional Microsoft services (or limiting them to an insignificant amount) like Azure/Cognitive-API/API management etc. – no matter how appealing or easy to implement they are, whenever I need any additional services, I look for other alternatives like for the security and user management of my app I chose firebase.
Using the Code
So, to start with, you must have an account with firebase - it’s best to have an account with Google Cloud to get access to google cloud control and then to firebase.
Go ahead and create/import your Android app project and get “google-services.json” folks who do not know what it is you have lot to catch. Replace the Json file of your own!
My project has a sample “google-services.json” file copied from FirebaseAuthQuickStart
example - have a look at this URL to have the basic clear https://components.xamarin.com/gettingstarted/firebase-auth.
FirebaseAuthQuickStart
sample already has Google sign in implemented with firebase.
Firebase has few major authentication providers which are managed identity providers through firebase but so far Instagram, LinkedIn has not been included which are unmanaged identity providers and can be maintained by firebase with the use of custom firebase token.
When you have decided to use Firebase to manage app users and broker authentication, it’s better if we can manage unmanaged authentication providers too with firebase.
The source code will provide a best practice to follow while implementing Facebook sign in with firebase -implemented with Xamarin Facebook SDK and Firebase SDK. As well as a way to maintain LinkedIn users with Firebase with xamarin.auth
and Firebase SDK.
Both of the implementations have different flavors for managed authentication providers like Google and Facebook respective auth SDK will handle the heavy lifting- like in the case of Facebook -specific activity should implement IFacebookCallback
(Xamarin.Facebook
) and IOnCompleteListener
(Android.Gms.Tasks
) and then firebase SDK will do the rest it has to use “AuthStateChanged
” to check if a user logged in or not and handle the Facebook access token just like the following simple example once an authentication credential is created, it will make an entry to firebase table.
private void handleFacebookAccessToken(AccessToken accessToken)
{
AuthCredential credential = FacebookAuthProvider.GetCredential(accessToken.Token);
mAuth.SignInWithCredential(credential).AddOnCompleteListener(this, this);
}
But in case of LinkedIn, its simple oAuth2 implementation complexity lies in creating the custom firebase token.
I have kept the token creation process in a separate shared project with a single class “Firebasetoken
” - make sure you have shared project template installed in your Visual Studio.
Remember this is just an example of how to do it – do not include this code or implement this at client side. It involves a service account and other secrets which are potential security variabilities - this is supposed to be implemented at server end. Following are the steps to follow:
- In the Firebase console, click the setting icon which is top left, next to the project name, and click 'Permissions'.
- At the IAM and Admin page, click 'Service Accounts' on the left
- Click 'Create Service Account' at the top, enter a 'Service Account Name', select 'Project->Editor' in the Role selection, tick the 'Furnish a new private key' checkbox and select JSON.
- Click 'Create' and download the Service Account JSON file and keep it safe.
- Open the Service Account JSON file in a suitable text editor and put the values into
Firebasetoken
class
Remember to include BouncyCastle
reference. In fact, here is a screenshot of the references you require to build this project correctly:
Find the source code in github - you are free to download and extend.
Here are a few screenshots of how it will look for Android:
History
- 12th September, 2017: Initial version