Least privilege with less effort: Macaroon access tokens for OAuth
Tags: oauth, security, authorization
Published: 2020-09-10 06:27:26

---
title: Least privilege with less effort: Macaroon access tokens for OAuth
published: true
date: 2020-07-29 17:20:58 UTC
tags: oauth, security, authorization
canonical_url: 
---

## Part 1: Lightweight PoP

One of the major changes between OAuth 1 and OAuth 2 was the decision to drop the requirement that [requests had to be signed](https://oauth.net/core/1.0a/#signing_process) using a secret key associated with each access token. This signing process was notoriously hard to get right and dropping it from OAuth 2 enormously simplified the life of client and resource server developers. In OAuth 2, access tokens are pure bearer tokens, a bit like passwords: if you know the value of the token you can use it, without needing any other credentials. This is very easy for developers, but is definitely less secure. The rise of single-page apps (SPAs) and other browser-based clients leaves access tokens more exposed to theft or accidental leakage compared to traditional server-side clients.

## Drinking the PoP

Signed requests in OAuth 1 are an example of proof-of-possession (PoP) tokens rather than bearer tokens. If bearer tokens are like cash, PoP tokens are like [Chip-and-PIN](https://en.wikipedia.org/wiki/EMV). Even if you steal an OAuth 1 token you can’t use it without the associated secret key used to sign requests. The only problem was it turned out that even if you did have the secret key you often couldn’t use the token either, because it was just too hard to get request signing to work reliably. Some brave souls periodically try and revive this idea.

To get around the problems with request signing, various alternative schemes have been proposed. Brian Campbell gave a nice overview of the history of OAuth PoP schemes at Identiverse recently, called [The Burden of Proof](https://onlinexperiences.com/Launch/Event/ShowKey=96547). It’s a good talk and worth watching. The latest such scheme is [DPoP](https://tools.ietf.org/html/draft-fett-oauth-dpop-04), which tries to keep things as simple as possible: rather than signing the whole request, you just sign enough basic details of the request (the URI and HTTP method) to be able to stop the most likely attacks and rely on TLS to protect the content of the request.

DPoP is good, and for some use-cases I think it’s really good, but it still has some downsides:

- Although the signature process itself is simplified, the client still has to generate and manage this secret key securely, which adds complexity that many client developers will avoid unless they have to.
- The client, authorization server, and resource servers all have to know about DPoP. It’s quite hard to incrementally deploy.
- It relies on fresh public key signatures on every request. Public key crypto is slow and expensive, so you generally want to do it as little as possible. I could imagine a rollout of DPoP requiring a significant increase in front-line servers to handle the extra load.
- DPoP is based on JWS, which supports [a wide range of signature algorithms](https://www.iana.org/assignments/jose/jose.xhtml#web-signature-encryption-algorithms), with no guidance as to which one to use. This means the resource servers have to potentially support them all (including some really expensive ones like ES512), or risk rejecting some clients. Clients will also have to guess which algorithms the server supports when they generate the private key. In practice this means that DPoP will only be used in closed deployments, or else everyone will gravitate to a lowest common denominator option like 2048-bit RSA with PKCS#1 v1.5 padding.
- [Public](https://mailarchive.ietf.org/arch/msg/openpgp/5rnE9ZRN1AokBVj3VqblGlP63QE/) [key](https://en.wikipedia.org/wiki/ROCA_vulnerability) [signature](https://www.bleepingcomputer.com/news/security/some-yubikey-fips-keys-allow-attackers-to-reconstruct-private-keys/) [algorithms](https://toadstyle.org/cryptopals/62.txt) [are](https://medium.com/asecuritysite-when-bob-met-alice/not-playing-randomly-the-sony-ps3-and-bitcoin-crypto-hacks-c1fe92bea9bc) [fragile](https://research.kudelskisecurity.com/2017/10/04/defeating-eddsa-with-faults/). The point of a signature is that it can be verified by anybody, but in the case of an OAuth request the DPoP token only needs to be verified by exactly one party: the server that you’re sending the request to. There are much simpler and safer ways to achieve request authentication in this case.

## Taking the biscuit

OK, so if DPoP is still too complex, what would a better solution look like? The risk that PoP solutions are trying to prevent is an access token being stolen from one request and used to authorize completely different requests that the client didn’t intend. A different way to solve this problem would be to have individual access tokens for every request that just authorizes that one specific request and no others. For example, it could have an audience restriction limiting it to only be used for requests to one specific server, and a single scope that authorizes just the specific type of API call that is being made. The expiry time could be limited to just a few seconds in the future, and so on. In current OAuth implementations it’s pretty hard to restrict an access token to exactly one request, but you can get pretty close.

Of course, this all sounds like a nightmare. The client can’t keep going back to the user to authorize access tokens for every little request! 

It turns out there is a way to get lots of little access tokens for individual requests. It’s easy for developers, relatively simple to deploy, and cheap. (_Much_ cheaper than digital signatures). It’s also quite delicious.

![Chocolate coated macaroons](https://lh5.googleusercontent.com/-s1BGxwv8J2_P8JVvbMdzz_SqvLZaOnGD6jii2Gsz9adhtw-SA1H2zxod3ab-x8pkn3OaZ0UAcLJrRiMc_s4NXarrjrATJP3g8nBdZkLH-CSWeemEQ-FzTXNtIMwAtvmi_kzOhcF)

## Enter macaroons

Apart from being [a tasty treat](https://en.wikipedia.org/wiki/Macaroon), macaroons are also [a new token format](http://evancordell.com/2015/09/27/macaroons-101-contextual-confinement.html) invented by the [boffins at Google](https://research.google/pubs/pub41892/). At first glance, a macaroon is an [authenticated](https://en.wikipedia.org/wiki/Message_authentication_code) token a bit like a JWT using the HS256 algorithm. But the additional yummy goodness that macaroons add is the ability to add _caveats_. A caveat is a restriction on how the token can be used. For example, you might limit the scope of a token, or reduce its expiry time. They always reduce, never expand, the authority of the token. The really cool thing is that adding a caveat gives you a _new_ token, leaving the original token unchanged. It’s also really cheap: a few microseconds typically. This means you can have a single powerful access token and then _derive_ multiple more restricted access tokens from it. This lets us have our cake and eat it (sorry, my metaphors are all over the place today): you can get a single token approved by the user but then derive individual access tokens for every single API call you make, with just the perfect amount of privilege for that one call.

![A picture of macaroon access tokens, showing how new tokens can be derived with reduced scope, expiry, or audience.](https://lh4.googleusercontent.com/2xwnm3nJkz_vMnxvPg1JxjX-8TF4PNPOIyAa0TlvQxRDOy4hsmqDaDn0WxhoZiddSoHjTzXlDvuC7Lmfm3HuFDlmjdkZq48x2ALVQneHElnQykrWzC7VhbUcw1rKRyeZOfvADZUC)

This gives us the benefits of having lots of tiny individual access tokens for each request, but we can generate these tokens on the fly from the original token. This is what the original macaroons paper refers to as _contextual caveats_: specific details of the context in which an API call is taking place that we add to a token to restrict its use. That way, if the token does get stolen or misused, the scope of what you can do with it is severely limited by the caveats attached to it. The genuine client still has the original access token though, so they can still do whatever they like within the scope of the original grant.

If we restrict the tokens enough then they become pretty hard to misuse. We get much of the same benefits of a PoP approach but with much less complexity. Where you might have had code like the following to call an API:

```
fetch('[https://api.example.com/v1/kittens](https://api.example.com/v1/kittens)', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer ' + token
    },
    body: JSON.stringify(kittenDetails)
});
```

The new macaroon-enhanced version becomes something as simple as the following with an [appropriate library](http://macaroons.io/):

```
fetch('[https://api.example.com/v1/kittens](https://api.example.com/v1/kittens)', {
    method: 'POST',
    headers: {
        'Authorization': 'Bearer ' + **token.restrict({**  
**            aud: 'api.example.com',**  
**            exp: nowPlus5Seconds(),**  
**            scope: 'upload\_cute\_kitten\_picture'**  
**        })**
    },
    body: JSON.stringify(kittenDetails)
});
```

## Macaroon OAuth tokens in ForgeRock AM 7.0

At [ForgeRock](https://www.forgerock.com) we love OAuth and we also love macaroons. We think they make a great combination. OAuth benefits from the simple but flexible approach to _least privilege_ that macaroons enable, and macaroons benefit from the framework and tooling that exists around OAuth. I strongly believe that macaroons provide about 90% of the benefit of PoP for about 20% of the effort.

That’s why we’ve included support for issuing OAuth access and refresh tokens as macaroons in the upcoming 7.0 release of ForgeRock Access Management. So how does it work? Well, mostly you just go into the OAuth2 Provider Settings and turn it on:

![Image of toggle button to turn on "Use Macaroon Access and Refresh Tokens"](https://lh6.googleusercontent.com/-U18sgnh544URwiVYtsEi00sqXjUX687yHwjEeey_ZYdOo49IZ63q-3STz56aBYO6pbyD5uNP_gLO4vLDUtnJA15qQuDWvPLSdaPQOqg1IGzIlW_zGvOiAwMN85fkJn7l46QMAO1)

Now when you complete an OAuth flow you get issued with a macaroon access token and optional refresh token. They look just like normal access tokens, but are a little bit longer:

![Screenshot of Postman showing macaroon access and refresh tokens](https://lh3.googleusercontent.com/KEaxly2z3I5uc5u1U88pe4ecvHeqlevepeDntwo2K6h0cPwNXIorQAwx9kapVxRCXiSV2mVpfNgwTTIuOSDUiDhf8XuWSZFifF8HQNubsTBGIeOUBQUWFHwM4rhiXHTUwKywaxE6)

You can call the [standard token introspection endpoint](https://tools.ietf.org/html/rfc7662) to see the details of the token, just like you could with a normal access token:

![Screenshot showing JSON response from the OAuth token introspection endpoint, showing the scope and other token details.](https://lh4.googleusercontent.com/jUopMcDkP43pzpxE2d-g5M13lC1XKd-kG-37dqXreCAELtCy3KnBxbvQrtQNYybF_vfRumeqqFjKn32JoKw8LJSuLiHXys1Rw3QoNfVJucYavPzeGDCTrSLGGM0PljGMgfgpzSyz)

You can then add a bunch of caveats just like in the example earlier, to reduce the scope, expiry time, and audience and introspect it again to see the difference:

![The updated JSON response from the introspection endpoint, showing the reduced scope, expiry, and audience due to the caveats added to the token.](https://lh4.googleusercontent.com/1pxK4QVPOxCG9H-mdFrlug1IHkrJHrMR8XlsbBR806EZxr31d_MAd_fp4Jg2FxyI7RlsNUEyPXw9rzUlzkso3024unQuEjizhmrLBLyi_ctfFDV5zaUZKgteP9XLSsTNf3t72Lz-)

Repeating the token introspection request a few seconds later, the restricted access token has expired:

![Screenshot showing the active: false response](https://lh6.googleusercontent.com/DijzuCMCxX1Wnks6lezkFCYvu9Q3BR2NlSC3WrlN-hC7gAjgJ-gbUs4WRF9JIQ6ePFsM5dbj5A-rngtGAtpbumm1z38ql9WPiIg_I48RvzRQNe94mzLSvlOHRlA3lAFK4I58xc4j)

But the original access token is still valid. In fact, all the details of the original token in the database (AM’s Core Token Service) are exactly as they were when the token was first issued.

Everything is handled transparently through the token introspection endpoint, making it really easy to deploy this incrementally:

1. In step 1, you simply turn on macaroon access tokens at the Authorization Server. So long as your resource servers are using token introspection, everything can carry on as normal. Clients and resource servers need no changes.
2. In step 2, your clients start adding caveats to tokens using a macaroon library. Resource servers carry on introspecting the tokens, but now the introspection responses take into account the caveats on the tokens.
3. There is no step 3.

That’s a little taster of macaroon access tokens in ForgeRock AM 7.0 and why I think they will be a game changer. _Least privilege with less effort._ In part 2, I’ll show you some more exotic things you can do with macaroon access tokens using 3rd-party caveats.