Monday, 30 March 2020

Building a Multi-tenant Microsoft 365 App Part 3: Integrate with payment provider

So this is the final part of the thinking out loud type of a guide to build a multi-tenant Microsoft 365 App, if your app is not free you can provide either a one-off license , per user license or per tenant license, I'm not going to go through all the options in details.

In the old world when I used to publish apps on Office SellerDashboard now AppSource via developer dashboard then now Partner centre. There is always two types of apps or actually three, from the store perspective 2 types (paid and free). Most ISVs will use the free option on Microsoft store and they implement their own license model which allow them to decouple it from Microsoft's existing model also they can offer different types of licenses etc..

For me I used to piggyback on Microsoft's licensing model. Why ? firstly I was building mainly free apps and putting it out there for people to use and I only have a single Outlook Add-in I have made paid for just doing a simple market test back in 2015, so I used Microsoft licensing model where I rely on Microsoft licensing etc.

now with Microsoft changing the model as in my humble opinion most ISVs already using their own license model and the current model is relatively a major overhead or maybe some other reason but this is my opinion. So relying on Microsoft to handle your M365 app is not an option, so you better off listing the app as Free and have your own integration with a payment gateway.

My intention was to use a provider that fits both recurring payments (subscription based model) and one off. and most importantly because I'm a very lazy developer something that requires minimum effort.

so I decided to use Stripe and Stripe checkout for the following reasons:

  1. Easy setup , I'm not going through how to setup products and plans, there are heaps of documentation on Stripe website that could provide more detailed and better explanation 
  2. Minimum code (JS mostly, you can even copy the code from Stripe dashboard) 
in less than 10 minutes you can have a stripe checkout integrated to your application, but is it that easy ? the answer is not really when the user click checkout it doesn't guarantee that the payments is correctly processed you have two options here:

  1.  activate your premium features asap, and when the payment is not confirmed remind the user and try again later (which is great allowing user to test your app and maybe pay later)
  2. Switch off your premium features until a successful payment is being processed.
So how can I link the user M365 tenant to the newly created stripe subscription, maybe i need to know if another user within the same tenant has purchased a tenant wide subscription. Stripe checkout offers minimal functionality to customise the checkout data passed, but you can pass tenant ID or user ID if you are offering per user subscription as clientReferenceId as below

the main question is : how to make sure that you know when Stripe has processed payment or not in this scenario ?

Stripe Web-hooks to the rescue 

You can register a set of strip web-hooks to make sure that you listen to Stripe events, in my case I have created an Azure Function (Http triggered) that queue Stripe requests in a queue that have another Azure Function that process the requests, the following is a snapshot listing the events I subscribed to

You might need to subscribe to other set of events, read the documentation carefully but this is what I've selected 

Why 2 Azure Functions?
You might need to sequentially process some of the events prior to the others and because you don't control the order that stripe call your registered webhook it's better to pass them all via a single pipeline.

In my case I was storing stripe events data in a table which lists subscription status and another one for invoices, these two tables are in the "common" storage account , you can decide to move them to specific storage account but you won't be able to completely wipe storage account data without archiving these.

I got side tracked, so I needed to process the CheckoutCompleted event first as it's the only event has the "ClientReferenceID" that allows me to later locate the Tenant and enable/disable the premium features.

in this case I rely on a record of the subscription to be created if I can't find it I'll simply throw exception that makes Azure function retry for 5 times, during this time I would be hopeful that the CheckoutCompleted event is processed.

Is that the best way. Absolutely not but it works and it's super easy the code is below.

Another Thing to Consider if you decided to put these in the common area make sure that you are not breaching any data sovereignty issues.

as you can see the code above ensure that a subscription record has been created first then when invoice processed successfully you can locate your tenant/user object using the clientReferenceID and update it's subscription tier and payment status.

Another thing you need to worry about when your user choosing to change tiers and then you will have to cancel their subscription and create another one, you can choose to continue the billing cycle using the old subscription or prorate the fee to the exact day of switching.

I've been distracted for the past couple of months due to reasons we all know, I hope wherever you are to be safe and I hope we get out of this stronger.

Be Safe

Saturday, 22 February 2020

Building a Multi-tenant Microsoft 365 App Part 2: Data Partitioning

In this part we will discuss how we can partition different tenant data, in general you should have 2 types of data:
  1. Common Data that should live in a common data storage for this you can use a common database/Azure Storage account, example of this data is Tenant Configuration, admin data like invoicing and subscription status in case you've decided to make your app paid or freemium.
  2. Tenant Specific Data, In this case you can either go with logical segregation (using single azure storage account and use partitioning within the table) However, I'm always in favour of having a physical partitioning. This allows admin to choose data location upon registration. Your tenant registration will not only create some data configuration items in the common data store but it will also attempt to create a separate data store for the newly registered tenant. This can be achieved by adding a message to a storage queue which triggers an Azure Function which will provision the necessary artefact for each tenant.
The main two questions/concerns would be 
  1. Where shall I store access to these various storage accounts? and  
  2. How can i distinguish between tenant requests if I will be using the same services? (physical segregation for the API layer is an overkill and not necessary in my opinion)
The answers for the above questions are pretty simple. First you will need to store the keys for each tenant in a key store like Azure key Vault. For the second one we are very fortunate that tenantId is passed as part of the user claims and you can easily extract it from the claims using the below line of code. 
You can use the tenantId to locate your tenant record , get the tenant storage account key from Azure Key Vault then once you get a reference to the tenant storage account you can do the operation you desire.

Very Important: Don't rely on the client app to send the tenant Id , always get it from the user claims 

In the following parts I'll address some of the concerns that I've faced and how I overcome them, I'm keen to hear your thoughts some of the ideas are pretty basic and I'm open for more sophisticated approaches:
  1. What you shall do when a user decided to delete the registration?  The answer is do you want to keep the organisation data for a while so the admin can restore the data specially if the deletion was by mistake.
  2. What shall I do for data backup and storage account tiers? You can include the storage tier in your tenant registration tiers for example if you offer a free/Basic tier you can use LRS for a higher tiers you can GRS option.
  3. How can my client App access my tenant blob files?  the need for this might arise if you store some static files for each tenant blob and you need you client app to access these files (custom logos/custom images etc..) one easy way is to use a shared access signature another way is to allow your Azure AD to control access to your blob storage.
  4. How can I stop users from abusing the shared storage?  as far as I know and until the time I've last checked (couple of weeks back) I know that there is no storage quota limit per storage account or per blob container. What I was successful to do is to get used capacity per storage account using Azure monitor client this is of course a delayed result but you can get the updated value then store it in your tenant record and always check if the each tenant still have available quota or not. Of course your decision will be delayed as it rely on Azure monitor metric but better than nothing.

In  a nutshell this is a very basic multi-tenant application that uses the simplest form of storage (Azure Storage Account) to segregate each tenant data in a separate physical container if your app is more complex you can use other storage options like cosmos db.

In the Next post I'll go through the simplest method to hook up a payment gateway like stripe to your application.

Monday, 13 January 2020

Building a Multi-tenant Microsoft 365 App Part 1: Authorisation & Authentication

In this series , I'll talk about building a standalone web app that connects to some of your Microsoft 365 workloads, as an example we will assume that we are building a Mutli-tenant app that connects to Azure AD, SharePoint and Planner.

In this post I'll focus on one fundamental part which is authorisation&authentication. From an architectural standpoint we don't have a lot of options to assess specially if we are looking to use some of the existing platform capabilities
  1. Use Azure AD as your main Authentication/Authorisation provider , to get more details about how please read my detailed previous post(  in which I explained how to use Azure AD to not only connect to existing Microsoft Graph APIs but I also used it to protected a custom API I've built.
  2. The second important thing to consider is permission scope elevation. In other words, when requesting permissions you should start with least privileged set. This is not only a good user experience but it will allow you to  provide partial functionality to normal users even if your full app functionality requires an admin consent. An example, if your app is trying to get users images, it will require Read.Users.All scope which needs an admin consent. If you try to request this scope the app will keep showing the users that it requires admin consent which will make it not usable at all. 
    The solution to this problem could be very simply, don't initially request the scopes that requires admin consent until an admin "configures" your app. The trick would be how you can make sure that your app is configured without risking requesting some write permission to the directory which will make the admin very reluctant to try out your app.  You can do this by creating a configuration record in your app metadata (you can store this configuration anywhere Cosmos DB or even Azure Storage Table), This configuration record can be created as part of multi-step process in which you can ask the admin manually to consent to the add and maybe throwing a button to ease the process. You can simply assume that the admin has configured the app correctly so when your app loads app it checks if there is configuration record it requests the full set of scopes.
  3. The other challenge is how your app would tell whether the current user is an admin or not. This indeed is a tricky one, if you rely on user claims and given that the user could be a member of a lot of groups. Group associations won't appear as part of the token claims . You will require to query user groups maybe multiple times to just check whether the current user is and admin or not. 
    In my case I used a very simplistic approach. I used Azure AD app Roles, I've created a single App Role called "Admin" and I've asked the Microsoft 365 admin to add himself and maybe other people to this group and by adding the attribute  [Authorize(Roles = "Admin")] I can make sure that this endpoint is only available for users with Admin roles.  The drawback of this approach is you have to use direct user/role associations unless the organisation has P1 or P2 Azure AD license.
  4. Using Client-Side Authentication Library like MSAL will definitely make your life easier to implement authentication including login, token acquisition and token refresh. If you are using MSAL within a single page app it might be a bit challenging, if you want to read more about some issues with msal and SPA you can get an idea reading this github issue  

This would be a wrap for this post, in part 2 we will discuss how to do logically or physically partition tenant data along with some challenges that comes with an easy to provision cloud storage vs. associated consumption cost.