BlueDog is a simple set of Azure Functions for user authentication. Serves as the basis for simple scalable back-end services where you only pay for what you use!
See my blog post: (Using Azure Functions and Document DB for Simple User Authentication).
This is a pre-alpha example of a simple user registration and authentication system using Azure Functions. It implements basic functions: register, login, getcurrentuser.
Please submit issues, suggestions and requests. Pull requests are even better!!
Simply create an Http Trigger function, then add the call:
#r "..\bin\BlueDog.dll"
using System.Net;
using System.Collections.Generic;
using BlueDog;
using BlueDog.DataProvider;
using BlueDog.Models;
// Register
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info($"C# HTTP trigger function processed a request. RequestUri={req.RequestUri}");
ServerConfiguration configuration = ServerConfiguration.FromAppSettings(log);
BlueDogAuth bluedog = new BlueDogAuth(configuration, new DocumentDBProvider(configuration));
return await bluedog.Register(req, log);
}
Database calls are abstracted behind an interface which helps unit testing. I use moq for this. Relatively easy to switch to a different kind of database by providing a different implementation.
Register registers a new user. Takes two parameters: email and password
Login logs in a user and returns a Json Web Token. Takes two parameters email and password
GetCurrentUser returns the currently logged in user. Takes one parameter jwt (the JWT returned from Login) This shows how to validate the JWT to make sure the user is logged on.
There are 4 additional methods: startpasswordrset, completepasswordreset, startemailchange, completeemailchange that are designed to be used in conjunction with a web server to provides pages and send emails. These methods just perform the logic. These will be detailed in a future blog post, but are included here.
*** Note: This code is pre-alpha quality. There has been some testing, but should not be put into production without more testing.
To test and build you will need the Azure Tools for Visual Studio:
https://blogs.msdn.microsoft.com/webdev/2016/12/01/visual-studio-tools-for-azure-functions/
- You will need to set up a Document DB account.
- Create a database on Document DB.
- Create a collection for users. 'UserCollection' is a reasonable name. :-)
- After building, you can upload to an the BlueDogSampleFunction to Azure Function.
- To run locally, change values in appsettings.json according to your DocumentDB account.
- To run on Azure Functions, add and change values in the Azure Function Configuration. Add all the keys that are in appsettings.json.
Set the BlueDogSampleFunction project as startup and run. This example will work with get or post. It is easier to test get w/ query parameters.
List of things to do:
- Much more testing
- Better setup documentation
- Better documentation in general.
- Changing email address and password. (Implemented but not tested)
- Controlling what gets returned from the GetCurrentUser call.
- Common UX patterns such as the onboarding process (see User Experience: Inviting users to your Android or iOS app Part 1 and Part 2)
- Support other kinds of databases.
- OAuth 2.0 and/or OpenId connect. Since this supports JWTs, it shouldn’t be too hard to add the extra steps that allow for OpenId connect.
- Let’s encrypt support. There is a Microsoft Web API plugin that can do this. and since Azure Functions use Web APIs, it isn’t too hard to get it hooked up.
Curtis Shipley