Most applications I write have some need for authentication and perhaps authorisation too. In fact, most apps most people write fall into that bracket. Here's the thing: Auth done well is a *big* chunk of work. And the minute you start thinking about that you almost invariably lose focus on the thing you actually want to build and ship.
So this Christmas I decided it was time to take a look into offloading that particular problem onto someone else. I knew there were third parties who provided Auth-As-A-Service - time to give them a whirl. On the recommendation of a friend, I made Auth0 my first port of call. Lest you be expecting a full breakdown of the various players in this space, let me stop you now; I liked Auth0 so much I strayed no further. Auth0 kicks AAAS. (I'm so sorry)
My criteria for "auth success" was this:
- I want to build a SPA, specifically a React SPA. Ideally, I shouldn't need a back end of my own at all
- I want to use TypeScript on my client.
But, for when I do implement a back end:
- I want that to be able to use the client side's Auth tokens to allow access to Auth routes on my server.
- I want to able to identify the user, given the token, to provide targeted data
- Oh, and I want to use .NET Core 2 for my server.
And in achieving all of the I want to add minimal code to my app. Not War and Peace. My code should remain focused on doing what it does.
I ended up with unqualified ticks for all my criteria, but it took some work to find out. I will say that Auth0 do travel the extra mile in terms of getting you up and running. When you create a new Client in Auth0 you're given the option to download a quick start using the technology of your choice.
This was a massive plus for me. I took the quickstart provided and ran with it to get me to the point of meeting my own criteria. You can use this boilerplate for your own ends. Herewith, a walkthrough:
Fork and clone the repo at this location: https://github.com/johnnyreilly/auth0-react-typescript-asp-net-core.
What have we got? 2 folders, ClientApp contains the React app, Web contains the ASP.NET Core app. Now we need to get setup with Auth0 and customise our config.
Here's how to get the app set up with Auth0; you're going to need to sign up for a (free) Auth0 account. Then login into Auth0 and go to the management portal.
- Create a Client with the name of your choice and use the Single Page Web Applications template.
- From the new Client Settings page take the Domain and Client ID and update the similarly named properties in the
appsettings.Production.jsonfiles with these settings.
- To the Allowed Callback URLs setting add the URLs:
http://localhost:3000/callback,http://localhost:5000/callback- the first of these faciliates running in Debug mode, the second in Production mode. If you were to deploy this you'd need to add other callback URLs in here too.
- Create an API with the name of your choice (I recommend the same as the Client to avoid confusion), an identifier which can be anything you like; I like to use the URL of my app but it's your call.
- From the new API Settings page take the Identifier and update the Audience property in the
appsettings.Production.jsonfiles with that value.
Build the client app with
yarn build in the
ClientApp folder. (Don't forget to
yarn install first.) Then, in the
dotnet run and open your browser to
Run the client app using webpack-dev-server using
yarn start in the
ClientApp folder. Fire up VS Code in the root of the repo and hit F5 to debug the server. Then open your browser to
When you fire up the app you're presented with "you are not logged in!" message and the option to login. Do it, it'll take you to the Auth0 "lock" screen where you can sign up / login. Once you do that you'll be asked to confirm access:
All this is powered by Auth0's auth0-js npm package. (Excellent type definition files are available from Definitely Typed; I'm using the @types/auth0-js package DT publishes.) Usage of which is super simple; it exposes an
authorize method that when called triggers the Auth0 lock screen. Once you've "okayed" you'll be taken back to the app which will use the
parseHash method to extract the access token that Auth0 has provided. Take a look at how our
authStore makes use of auth0-js: (don't be scared; it uses mobx - but you could use anything)
Once you're logged in the app offers you more in the way of navigation options. A "Profile" screen shows you the details your React app has retrieved from Auth0 about you. This is backed by the
client.userInfo method on
auth0-js. There's also a "Ping" screen which is where your React app talks to your ASP.NET Core server. The screenshot below illustrates the result of hitting the "Get Private Data" button:
The "Get Server to Retrieve Profile Data" button is interesting as it illustrates that the server can get access to your profile data as well. There's nothing insecure here; it gets the details using the access token retrieved from Auth0 by the ClientApp and passed to the server. It's the API we set up in Auth0 that is in play here. The app uses the Domain and the access token to talk to Auth0 like so:
We can also access the
sub claim, which uniquely identifies the user:
The reason our ASP.NET Core app works with Auth0 and that we have access to the access token here in the first place is because of our startup code:
We're pretty much done now; just one magic button to investigate: "Get Admin Data". If you presently try and access the admin data you'll get a
403 Forbidden. It's forbidden because that endpoint relies on the
"do:admin:thing" scope in our claims:
This wired up in our ASP.NET Core app like so:
The reason we're 403ing at present is because when our
requirement.Scope has the value of
"do:admin:thing" and our
scopes do not contain that value. To add it, go to your API in the Auth0 management console and add it:
Note that you can control how this scope is acquired using "Rules" in the Auth0 management portal.
You won't be able to access the admin endpoint yet because you're still rocking with the old access token; pre-newly-added scope. But when you next login to Auth0 you'll see a prompt like this:
Which demonstrates that you're being granted an extra scope. With your new shiny access token you can now access the oh-so-secret Admin endpoint.
I had some more questions about Auth0 as I'm still new to it myself. To see my question (and the very helpful answer!) go here: https://community.auth0.com/questions/13786/get-user-data-server-side-what-is-a-good-approach