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 is 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.

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.

Setting Expectations With the Cone of Uncertainty

I’ve been doing a lot of planning and estimating at work the past couple weeks. The goal has been to figure out what project can get us the most bang for the buck while working around some other deadlines.

To make this happen, I’ve been going through a lot of requirements, writing a lot of stories, and doing a lot of estimating with story points. While the requirements gathering has been collaborative, the writing of stories and estimation has been a one man operation. Such is the nature of early start up life. At the end of this though, I need to provide an estimate of how long a feature will take to develop. Reaching into my bag of tricks, I’ve gone back to using the cone of uncertainty a lot.

Poorly drawn cone of uncertainty

The cone of uncertainty is a tool to provide error bars on your estimate based on where you are in the planning and implementation process. The key is to accept that your estimate is going to be wrong and to communicate an appropriate amount of uncertainty around it.

For example, during project inception the range of outcomes is very large. This makes sense because you don’t actually know a lot yet. There is probably an end business goal in mind, but nobody has started to look into the details yet. If you give a gut feel estimate of 4 weeks, the cone says that the range of uncertainty at this point is from 1-16 weeks. This range is absurd and not helpful to a product owner, but it helps communicate that even asking for an estimate at this point is unrealistic.

As you begin to gather and refine requirements, the cone narrows. The exact numbers of the curve (and even the categories) are pretty poorly defined. You’ll find varying numbers of categories and specificity, but in general you’ll find ranges like the curve above.

  • Inception – 0.25 – 4x
  • Elaboration – 0.5 – 2x
  • Construction – 0.8 – 1.25x

An Example

To make things more concrete, I just finished estimating a project that came out to 82 points. Our sustainable velocity over two week sprints is currently 30 points. We’re firmly in the elaboration part of the curve, which gives a multiplier of 0.5 – 2.0x. This gives us the following range

Lower range: 82 points * 0.5 / 30 points per sprint * 2 weeks per sprint = 2.73 weeks
Upper range: 82 points * 2 / 30 points per sprint * 2 weeks per sprint = 10.9 weeks

A range of of 2.73- 10.9 weeks is large, but I think it accurately reflects the reality of many software projects where implementation hasn’t even started yet. We’re bad at estimating and using a tool to communicate that can help.

Narrowing With Iterations Instead

In Agile Estimating and Planning, Mike Cohn suggests an alternative way to narrow your cone by using the number of completed iterations instead of stages like Inception, Elaboration, and Construction. In this model, the range of your estimate tightens with each completed sprint until you have completed 4 or more. Obviously, the usefulness of this technique can be limited if your project is too small to have that many iterations, but it can be a useful technique to fall back on for a medium sized project. I use 2 week sprints, so if the project is less then 8 weeks long, I won’t even finish 4 sprints.

Summary

Overall, the cone of uncertainty as presented here is not a formalized construct with a lot of data behind it. However, throughout my career, I have found it to be a useful tool to guide discussion and expectation with product owners. It’s an easy to understand concept that lends itself to quick explanation and provides a reasonable framework when you need an estimate for how long a project takes.