JWT scope claim compression using a bitmap

栏目: IT技术 · 发布时间: 4年前

内容简介:Today I’m proud to present you an idea to compress scope list into a bitmap where one bit represents one scope.The idea is quite simple. Instead of a huge string with all the scopes associated with the token, I associate each scope to a bit in a byte seque

JSON Web Tokens (JWT) are often used in stateless authentication flows. Thanks to the signature, the server does not need anything else to verify the token validity. The scope claim ( RFC8693 section 4.2 ) contains a space-separated list of scopes associated with the token . The server can use it to check the application permissions. Although this claim can quickly become heavy. The more scopes you have, the bigger your token is! But JWT are meant to be a compact token format

Today I’m proud to present you an idea to compress scope list into a bitmap where one bit represents one scope.

From space-separated list to bitmap

The idea is quite simple. Instead of a huge string with all the scopes associated with the token, I associate each scope to a bit in a byte sequence. Each bit tells us if a scope is associated (or not) to the token.

For instance:

0  1  1  0  1  0  0  1     1  0  1 (...)
scope_a ──┘  │  │  │  │  │  │  │     │  │  │
scope_b ─────┘  │  │  │  │  │  │     │  │  │
scope_c ────────┘  ┆  ┆  ┆  ┆  ┆     ┆  ┆  ┆
(...)                 ┆  ┆  ┆  ┆     ┆  ┆  ┆

In this example, scope_b and scope_c are enabled, not scope_a .

As you already know, byte-array is not a JSON data type. But it’s not a big deal, we can encode these bytes in Base 64 .

For a 3 bytes (max 24 scopes) claim, it would look like:

{
    (...)
    "b_scope": "NDIh"
}

Towards a new claim

I

don’t want to can’t override the current scope claim specification. Even if technically it would not break the specification (because we still store a string inside), this is not a good practice IMO. Furthermore, in some cases you might want to have both a traditional scope

claim and a bitmap scope claim.

That’s the reason why I decided to create a new claim: b_scope .

Where and how to define the scope list ?

Any resource provider is documenting the list of scopes available in its API. It’s defined either in a “human readable” documentation (text, html, pdf,…) or in a structured specification ( OAuth Authorization Server Metadata , OpenID Connect Discovery , OpenAPI,…).

For structured specifications my suggestion would be to keep the same ordered list and use the index of each scope as bit number. The scopes_supported field in OAuth Metadata is a JSON array. Order of elements is preserved. However the scopes field in OpenAPI is a map (JSON Object). We should not rely on the ordering of keys. Hopefuly the OpenAPI specification allows extensions . It means that we could define a x-scopes-order keyword which would list (JSON Array) the scopes with preserved ordering (like in OAuth Metadata).

Performances

Is it a good idea from size point of view ?

Let say that you have 42 scopes in your API ( I use Facebook API as example ). It would require 6 bytes to store them in the bitmap. Because of Base 64 encoding we have to do 4 * n / 3 (+ rounded up mult 4) = 8 bytes. And to be fair, I added 2 bytes in claim name (prefix b_ compared to scope claim) for a total of 10 bytes. The equivalent in size of scope list email name .

Is it a good idea from parsing (CPU load) point of view ?

Testing a byte sequence with binary operators is not CPU consuming. Compared to string manipulation and comparison it’s probably more performant.

Remaining questions

About bit numbering (endianness)

LSB vs MSB . Currently I don’t know what’s the best option. I guess the answer should come from the following criteria:

  • What’s the most efficient/performant ? To be tested in multiple web-oriented languages (JS, Python, Ruby, Rust, Swift,…)
  • What’s the most “natural” in OAuth/JWT ecosystem ?

How-to deprecate a scope ?

You might want to deprecate a scope but you can’t remove this scope from the list otherwise it would shift all other following scopes.

In this case you will loose a bit. But if your tokens are limited in time, you could replace the bit scope with another after a deprecation period (longer than the max token lifetime).

But… do you often deprecate a scope ?

And the “dynamic” scopes ?

Some people are definining “dynamic” scopes. For instance: pets.{id}.read . I don’t think it’s a good practice. My understanding of scopes is to grant access to an API feature, not to the resource itself. In other words, IMHO you should have only one scope pet.read which allows application to read a pet. Your resources (aka content) ACL should not be in the scope itself.

Btw this bitmap scope mechanism is compatible with existing standard scope claim.

Could we use this b_scope outside of JWT ?

Why not ? I didn’t analyze (yet) where and how we could use it but I guess it could be useful in some cases. However I don’t see a big benefit to use it in OAuth (token and authorize) endpoints. These endpoints are called punctually while JWT is sent in each API request.

Follow up

You might have questions or you just want to discuss about it. Please open issue in my blog repository or feel free to send me an email.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

创业时代

创业时代

付遥 / 中信出版社 / 2015-7 / 39.8元

香港人郭鑫年酷爱赛车,在驾车穿越隧道的时候,因为收发短信发生意外,他从被撞得破烂的车里爬出来时,兴奋地高喊:我有一个伟大的想法,手机上的对讲机,将要改变世界!他随即辞职来到北京,开始艰难的创业历程。 移动技术迅猛发展,正在颠覆互联网行业,郭鑫年误打误撞,对讲机用户数量急增,竟成为移动互联网的明星,他也因此置身于风口浪尖。三大互联网巨头为了抢夺手机入口大打出手,无不希望争夺这张通往未来移动市场......一起来看看 《创业时代》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具