Giter Site home page Giter Site logo

damilolaadegunwa / firebaseauthwebapisample Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sergeitemkin/firebaseauthwebapisample

0.0 1.0 0.0 59 KB

Authenticating Asp.Net Core WebApi via Firebase Jwt tokens.

C# 41.35% TypeScript 45.77% JavaScript 10.61% HTML 1.82% CSS 0.45%

firebaseauthwebapisample's Introduction

Sample Angular 2 SPA using Firebase Authentication with a .Net Core WebApi

There are two projects rolled into the solution here. One is a SPA client that calls an api endpoint, and the other is a WebApi that serves data via the aforementioned endpoint.

Client (Angular 2 SPA)

Mostly generated via the angular-cli. The actual firebase authentication sample is in src/app/app.component.ts. The sample Api call would normally happen via an Angular Service, but I was lazy so it's kinda bundled into the component. This part is pretty straight forward:

  1. Create your Firebase config (you really only need the authDomain setting and your api key for this)
    var config = {
      apiKey: "AIzaSyD1JWKae5SRLMbVwWdRz2YXu6z-jMzWNJU",
      authDomain: "testing-6bf89.firebaseapp.com"
    };
  1. If you're allowing 3rd party authentication providers, call firebase.auth().getRedirectResult() first to check if this is a returning redirect response from said providers. If there's still no user, then you can redirect to the provider, or display an authentication form, etc.

  2. Once you get a user, use the Dd property on the object to get the Id Token. This is a token containing various claims for the user, including the user's Firebase uid. Pop the Id Token into http://jwt.io to see the claims if you're curious.

Check out Firebase docs and related links under Authentication for various ways to authenticate.

Things to look out for:

  • You'll want to use lock down your Api key to only be used by your web app at: https://console.developers.google.com/apis/credentials?project={YOUR_FIREBASE_PROJECTID}. Set up your key to use Http referrers, and enter your apps url.
  • You'll want to store the bearer token you get from firebase.auth() in session storage or a javascript cookie (depending on how secure you wanna be). Else the user will be required to log in everytime they refresh the page. You do get some form of single sign-on when you use 3rd party authentication providers like Google, but if you're using email/password, there's no server component to store the cookie to remember who is logged in.
  • You'll want to write an interceptor (I think the angular 2 equivalent is to extend Http service) to display the login form if 401s are returned
  • You'll want to write something that'll append the Authentication header to all your api requests

WebApi (.Net Core)

Simple, nearly blank, .Net Core app with one controller to return the user's claims.

  1. Setup CORS policy (else Authorize attribute will throw errors)

  2. Setup Authenticaton Middleware (else Authorize attribute will throw errors). This middleware will read the Jwt token and stuff the claims into the User claims principal.

  3. Add the [Authorize] attribute to your controllers or use a filter

Things to look out for:

  • Make sure the middleware is added to the pipeline in the right order. Authentication middleware has to go first or the rest of the middleware won't be able to make use of it and Authorize attributes on routes will fail.
  • Restrict CORS policy to at least the origin of your web app
  • Some of the claim names in the Api User object won't match the claim names in the original Jwt. This is because of the silly claim map in JwtSecurityTokenHandler. In .Net not core you could clear the claims via JwtSecurityTokenHandler.InboundClaimTypeMap.Clear(); call in Startup.cs, however, I didn't have time to figure out how to do that in Core.

firebaseauthwebapisample's People

Contributors

sergeitemkin avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.