Combining the Default Policy in ASP.NET Core

Over the course of my career it seems that every ASP.NET (Core or otherwise) project I’ve inherited ignores the built-in policy-based authorization support. The codebase I inherited at Husmus is no exception. But this time I have the power to fix it!

"Fix all the things" meme

Policy-based authorization allows you to define a default policy. This policy will be applied to any endpoint that has the [Authorize] attribute, but no explicit policy.

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddAuthorization(options =>
    {
        options.DefaultPolicy = new AuthorizationPolicyBuilder()
            .RequireClaim("AccountActivated")
            .Build();
    });

    ...
}

For example, the default policy will be applied to Index, but not to New.

// Uses the default policy
[Authorize]
public IActionResult Index()
{
    return Ok();
}

// Only users who satisfy this policy
// are authorized. The default policy
// is not applied.
[Authorize("CanCreateWidget")]
public IActionResult New()
{
    return Ok();
}

That’s not what we want here though. We want CanCreateWidget and the default policy to both be applied. We want to build on top of the default policy so that the user is required to have activated their account and be able to create a widget. One ugly way around this to add multiple [Authorize] attributes. The framework will treat these as a logical AND, requiring each one to be satisfied.

// The user must satisfy both the default policy
// and CanCreateWidget
[Authorize]
[Authorize("CanCreateWidget")]
public IActionResult New()
{
    return Ok();
}

This duplication will quickly get out of control though. Every time we want the default policy to be applied in addition to another policy, we’ll need to add multiple [Authorize] attributes to the controller and/or action. A better alternative is using AuthorizationPolicyBuilder.Combine. Combine lets us combine one policy into another. We can use that to combine our default policy into out CanCreateWidget policy. Now, any changes we make to our default policy will always be applied to CanCreateWidget without duplication.

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddAuthorization(options =>
    {
        options.DefaultPolicy = new AuthorizationPolicyBuilder()
            .RequireClaim("AccountActivated")
            .Build();

        options.AddPolicy("CanCreateWidget", policyBuilder =>
        {
            policyBuilder
                .Combine(options.DefaultPolicy)
                ... setup your policy
                .Build();
        });
    });

    ...
}

I’m pretty happy with how this works now. Our default policy at Husmus is moderately complicated and we almost always want it applied when adding a more specific policy. Combine allows us to do that and still keep our policy configuration centralized in one location.

Using validateStatus in Axios

This past week I was working with an API endpoint where a 404 response is expected for invalid user input. I didn’t want Axios to treat this as an error. It had been awhile since I last needed to do this, but reading through the request configuration docs I was reminded of validateStatus. By default, it will return true for all 2xx and 3xx responses.

validateStatus: (status) => {
  return status >= 200 && status < 300; // default
},

In this case, I want the promise to resolve for 404 responses as well.

validateStatus: (status) => {
  return (status >= 200 && status < 300) || status == 404;
},

And that’s it. When the user puts in invalid input, the promise will still resolve. This isn’t something you often want to do, but it’s good to keep in mind depending on what a 404 actually means for your use case.

Custom Model Binders in ASP.NET Core 6

This past week I was integrating with a third party service that passes back boolean values in the query string as Yes/No. The built-in ASP.NET Core 6 model binding can handle true/false or 1/0, but not Yes/No. Let’s look at how to make our own custom model binder for this simple use case and how to unit test it.

Implementing IModelBinder

The official documentation provides a good overview of custom model binding. In this case, I implemented IModelBinder as follows

/// <summary>
/// Binds "Yes" or "No" (ignoring case), to true and false respectively.
/// Does not bind anything on other values.
/// </summary>
public class YesNoBooleanModelBinder : IModelBinder
{
    public Task BindModelAsync(ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
        {
            throw new ArgumentNullException(nameof(bindingContext));
        }

        var modelName = bindingContext.ModelName;

        var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);

        if (valueProviderResult == ValueProviderResult.None)
        {
            return Task.CompletedTask;
        }

        var value = valueProviderResult.FirstValue;
        if (string.Equals(value, "Yes", StringComparison.InvariantCultureIgnoreCase))
        {
            bindingContext.Result = ModelBindingResult.Success(true);
        }
        if (string.Equals(value, "No", StringComparison.InvariantCultureIgnoreCase))
        {
            bindingContext.Result = ModelBindingResult.Success(false);
        }

        return Task.CompletedTask;
    }
}

Even though my current use case is just for query strings, implementations of IModelBinder aren’t specific to where the data is coming from. The data is gathered from a variety of sources. By the time the custom IModelBinder executes, the data has already been gathered and added to the ModelBindingContext, along with the name being bound to.

How It Works

var modelName = bindingContext.ModelName;
var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);

if (valueProviderResult == ValueProviderResult.None)
{
    return Task.CompletedTask;
}

First, I pull the data out of the bindingContext by name and confirm that a match was found. If not, don’t set any result on the bindingContext and just return Task.CompletedTask.

var value = valueProviderResult.FirstValue;
if (string.Equals(value, "Yes", StringComparison.InvariantCultureIgnoreCase))
{
    bindingContext.Result = ModelBindingResult.Success(true);
}
else if (string.Equals(value, "No", StringComparison.InvariantCultureIgnoreCase))
{
    bindingContext.Result = ModelBindingResult.Success(false);
}

return Task.CompletedTask;

Next, pull out the first value in the result. Binding works with collections, but in this case there should only be a single value. Then make case-insensitive comparisons against the bound value looking for Yes/No and only set a result for Yes/No values. Anything else will not bind at all.

Usage

If your use case is better handled without an attribute, then you can implement IModelBinderProvider. This is a good idea if you always want the custom binder to be applied. In this case, I want to opt-in to the binding with an attribute.

[HttpGet]
public IActionResult Index([ModelBinder(BinderType = typeof(YesNoBooleanModelBinder))] bool isValid)
{
    ...
}

When executing the binder bindingContext.ModelName will be isValid.

Unit Testing

The next step was to figure out how to setup ModelBindingContext for unit tests. I ended up with this. Assertions are handled with FluentAssertions

public class YesNoBooleanModelBinderTests
{
    [Theory]
    [InlineData("Yes", true)]
    [InlineData("yes", true)]
    [InlineData("No", false)]
    [InlineData("no", false)]
    public async Task BindModelAsync_returns_success_with_with_expected_value(
        string modelValue, bool expectedResult)
    {
        // Arrange
        var modelBinder = new YesNoBooleanModelBinder();
        var bindingContext = BuildBindingContext(modelValue);

        // Act
        await modelBinder.BindModelAsync(bindingContext);

        // Assert
        bindingContext.Result.IsModelSet.Should().Be(true);
        var model = bindingContext.Result.Model as bool?;
        model.Value.Should().Be(expectedResult);
    }

    [Fact]
    public async Task BindModelAsync_does_not_bind_if_model_value_is_not_yes_or_no()
    {
        // Arrange
        var modelBinder = new YesNoBooleanModelBinder();
        var bindingContext = BuildBindingContext("invalid");

        // Act
        await modelBinder.BindModelAsync(bindingContext);

        // Assert
        bindingContext.Result.IsModelSet.Should().Be(false);
    }

    private ModelBindingContext BuildBindingContext(string modelValue)
    {
        const string ModelName = "test";
        var bindingContext = new DefaultModelBindingContext
        {
            ModelName = ModelName
        };

        var bindingSource = new BindingSource("", "", false, false);
        var queryCollection = new QueryCollection(new Dictionary<string, StringValues>
        {
            { ModelName, new StringValues(modelValue) }
        });
        bindingContext.ValueProvider = new QueryStringValueProvider(bindingSource, queryCollection, null);

        return bindingContext;
    }
}

The important bits are in BuildBindingContext. ModelBindingContext is abstract, but the framework provides a DefaultModelBindingContext that we can instantiate. The BindingSource is not relevant for our tests, but must be provided. The QueryCollection will feed into the QueryStringValueProvider that we pull values out of in the YesNoBooleanModelBinder implementation. In this case we’re saying the data came from a query string, but you could use another implementation of IValueProvider, such as RouteValueProvider. You can see an example of that https://stackoverflow.com/a/55387164/235145

100 Days of Morning Pages

Morning Pages are three pages of longhand, stream of consciousness writing, done first thing in the morning. *There is no wrong way to do Morning Pages*– they are not high art. They are not even “writing.” They are about anything and everything that crosses your mind– and they are for your eyes only. Morning Pages provoke, clarify, comfort, cajole, prioritize and synchronize the day at hand. Do not over-think Morning Pages: just put three pages of anything on the page…and then do three more pages tomorrow.

Julia Cameron (https://juliacameronlive.com/basic-tools/morning-pages/)

I’ve been journaling semi-regularly for years. On August 21 I sat down at my desk and wrote morning pages for the first time. I missed a couple of days, but after 100 entries, it’s a good time to reflect on the value of the habit for me.

Longhand vs. Typing

I wrote the first couple of months of entries longhand as Julia recommends. This took me about 30 minutes each day. However, after finishing my first notebook (about two months), I decided to try switching to typing for a bit. This goes against her recommendation but is working well for me. It’s an exercise in tradeoffs.  When writing longhand, I am slow enough that my internal monologue is almost always ahead of my hand movements. When typing, that isn’t always the case. It’s taken practice to get to the point where I don’t stop typing even when I don’t know what to type next. I resort to asking myself rhetorical questions more often about the fact that I don’t know what to type about next. This would occasionally happen when writing, but much more often when typing. In the time it takes to type out that rhetorical question, a new thread of thought usually appears that I can follow. And since I type so much faster than I write, I’m done in about half the time.

Even when writing in my journal, I would take pictures of each written entry and add them to Evernote. Now I just type directly into Evernote. I’ve done various forms of journaling with varying success for many years. Keeping everything centralized has value to me. When writing longhand, I missed being able to search for some random thought I know that I wrote down sometime in the past couple of weeks.

Overall, I’m pretty happy with the switch to typing. Taking only half the time and being able to search makes breaking the recommendation worth it to me.

Clearing My Head

For me, writing morning pages has not helped clear my head to any noticeable extent. It continues to be a jumbled mess. What it does do though is provide a routine to start my day with. Well, it tries to. The problem is that my mornings vary depending on if I’m taking the kids to school that day or not. The real solution here is to adjust my sleep schedule to get up a bit earlier, but I haven’t been willing to do that.

On mornings where I am not responsible for getting the kids ready and to school, the routine does help. I get ready for the day, make my tea, and head to my office. My morning pages often meander into what I want to get done at work for the day, helping to seed my task list. From years of experience, I know that I do my best work when I break problems down into small chunks. Days where my morning pages can transition directly into planning out my day are commonly my most productive.

Morning Pages as Therapy

This was not intended, but my notebook of longhand writing became a source of therapy. A couple of weeks after I started the habit, my dad was (not unexpectedly) diagnosed with terminal cancer. 23 days later he was dead. The journal morphed into a treasured possession containing many of my thoughts as he died in front of me. A couple of entries are filled with stream of consciousness writing for a eulogy. Many of those ideas made it into the one I gave. Events moved fast during those weeks. Having a record of my daily thoughts during that time is incredibly valuable to me.

Even if you’re not going through something that intense, stream of consciousness writing can be very valuable to help work out problems. It can be hard to turn off your filter, something I still struggle with. With regular practice though you can get better. Remember, these thoughts are for you and no one else.

Give It A Shot

Overall, I recommend giving morning pages a try, either written or longhand. Clear out some time at the beginning of each day, grab some tea, and dedicate yourself to the habit for at least thirty days.

Lessons Learned With Stripe Subscriptions

Stripe wordmark

So, you’ve got a new product or business idea and you think selling subscriptions is the way to go. After doing some research, you’ve decided that Stripe is pretty popular for handling payments, so you’re going to give it a shot.


First, that’s a good idea. Stripe is very popular for good reason. Their feature set is impressive and their API is excellent. They’ve clearly had a lot of very talented people put a lot of thought into a lot of different use cases.


Now for the bad news. Stripe having put all of that thought into different use cases is going to expose the fact that you haven’t done the same for your business yet.


We’re going to get through this though. My pain is your gain. I come with tales of pain and woe in the hope that you avoid some of the same.

Terminology

One of the most important things to understand about Stripe subscriptions is that your users will never pay for them. Rather, they will pay invoices that are generated from subscriptions at some point before the payment is due. This isn’t that important of a distinction for the very straightforward use case of somebody purchasing a subscription that becomes active immediately. As we’ll see though, it’s very important in other use cases.

Stripe subscriptions involve, at minimum, the following resources

  • Subscriptions
  • Subscription items
  • Invoices
  • Invoice items

Subscriptions have many invoice items. Invoices are generated from subscriptions and contain many invoice items. An invoice can be thought of as a snapshot of the subscription at a point in time. The contents of the subscription will change over time, but past invoices will not. When an invoice is created, you will have a brief window of one hour to modify it before it gets finalized.

Luckily, the Stripe documentation is often excellent. You’ll do yourself a huge favor by carefully reading their guide to how subscriptions work multiple times. The rest of this post is going to assume that you have made yourself moderately familiar with the basics of how subscriptions work in Stripe.

That being said…

How Are You Going to Sync With Your System?

Stripe provides an extensive set of events that you can subscribe to through webhooks. Deciding what webhooks you need to care about is important. At a minimum, you need to care about the invoice.paid event. This covers the happy path of your customer choosing a subscription plan and checking out. After their card (or other payment method) is successfully charged, you will receive the invoice.paid event. You handle this event to activate their subscription in your system.

But what do you do with the rest of the information from that event? Do you toss it aside? Persist it somewhere? As with just about anything in software engineering, it depends.

Do you need to show payment history to the user? If so, do you want to query the Stripe API every time or your own database?

Do you have any reporting requirements around revenue? Is that best handled through the Stripe dashboard or your own database?

If saving information in your own database, how much? Do you need everything on the invoice and each invoice item? Just a subset?

In my case, I’ve decided to persist a subset of this information in a relational database. We have some monthly reporting requirements that are easier to manage when we can query the Stripe data alongside non-Stripe data in our database. This does introduce overhead though and I have concerns about the number of Stripe events we’ll need to subscribe to over time. Time will tell if it was the correct choice.

Do You Offer Trials?

I mentioned above that on the happy path your customer will choose a subscription plan and immediately be charged. As soon as you decide to offer trials, you’re off this happy path.

In the happy path use case, you create a subscription through the Stripe API and an invoice is immediately generated. That invoice has a payment intent associated with it. If you specifically ask for the latest invoice and it’s payment intent by using expanded responses, you can get the ID for this payment intent in the response when creating the subscription. You can then use this payment intent when the user submits their payment information.

In trials, that is no longer true. When you create the subscription, you will not get a latest invoice back with the payment intent. Stripe will initially create an invoice for $0 that it will be immediately marked as paid for the trial. The first invoice for actual payment won’t be generated until an hour before the trial expires.

I haven’t actually implemented this yet, but, from what I can tell as of writing, this means you need to collect a payment method and associate that with the customer in Stripe. When the trial expires and the next invoice needs to be paid, you can then specify that payment method.

Do You Want To Start Subscriptions on a Future Date?

Suppose trials don’t make sense for your use case. For Husmus, this is true. We sell subscriptions for insurance. A standard use case is for a tenant to buy insurance that they want to become active when they move into their new home. Trials don’t make sense here.

Solving this is actually what I’m working on now, so I don’t have a full solution yet. It involves using subscription schedules though. The documentation has a guide for starting a subscription in the future. Given how trials work, it’s safe to assume (I can’t wait to eat these words) that the same rules for invoice generation apply. You will need to collect payment information in advance and then charge the user when the subscription actually starts.

Do You Want To Remind Users of Renewal?

Thankfully, this one is a bit easier to handle. In your account billing settings, you can set the number of days in advance to send an invoice.upcoming event. You will need to handle this webhook event and remind your users in whatever way is appropriate for your app.

Stripe renewal event settings screenshot

What Taxes Do You Need to Collect?

Are you assessing sales tax or VAT? Good news! Stripe will handle that for you. Anything else though, and you’ll want to start reading up on Stripe tax rates.

Husmus currently operates in the UK, which mean means we have to deal with IPT (Insurance Premium Tax). This is a tax that applies to all insurance products sold in the UK. Stripe doesn’t attempt to handle tax rates like this itself, so we need to define the tax ourselves and make sure it is added to all insurance products that the user is subscribed to. As long as it is attached to the subscription item, then when the invoice is generated it will be attached to the corresponding invoice item and users will be charged the correct tax.

Stripe supports up to five tax rates per line item, so go nuts. Chances are Stripe can handle the tax rates in your jurisdiction.

Are You Going To Sync Up Subscription Renewal Dates?

Another issue we’re dealing with is if we should sync up subscription renewal dates. Let’s say that you purchase a subscription for one insurance product on the 3rd of the month. Then you purchase another one two weeks later on the 17th. Should these subscriptions be treated independently or not? In our case, we haven’t definitively answered this question. There is a tension between the simplicity of a single subscription vs. most insurance being sold with annual subscriptions. Should the second subscription be independent or sync up with first? Or should it be dependent of if the insurance is for the same property? Your business may have similar questions.

If you decide to sync up to existing subscriptions, then you get into proration. Luckily, this is another thing that Stripe had good support for, but it’s yet another detail you will need to handle properly.

How Do You Handle Renewal Failures?

I haven’t implemented this yet. That’s a problem for future Brian when subscriptions start to renew. However, it is something on my mind. Once again, Stripe will send you an event when this happens, invoice.payment_failed in this case. Handle the event and prod your user to update their payment method.

Of course, that opens another issue. If you’ve been handling trials or subscriptions that start in the future, chances are that you already have a way for users to enter payment methods. In this case, they probably need to update their payment method. If you haven’t care about this yet, you need to now.

Summary

Stripe is complicated because payments are complicated. I’ve only scratched the surface on use cases for Stripe subscriptions. The good news is that if you have a simple subscription product, you can probably punt on a lot of these questions. You don’t have to have firm answers to all of these questions up front, but the more you can answer, the better your Stripe experience will be for both you and your customers.