Scope Best Practices

url
https://curity.io/resources/learn/scope-best-practices/

What are OAuth 2.0 Scopes?

OAuth 2.0 scopes are strings issued to access tokens. The Introduction to Scopes explains how APIs use scopes to restrict access to resources. A common way to get started with scopes is to use a combination of the type of resource and the access required on it:

Resource TypeAccess LevelScope Value
orderreadorder_read

OAuth standards documents do not define how you should use scopes but leaves that to designers of each system. This article explains how to manage scopes at scale, avoid common problems, and use some advanced scope techniques. For that, it uses an example to showcase the arguments.

Scopes Design

When you design scopes for real-world systems you set boundaries on where clients can use access tokens. To enable this, APIs should separate data both by business area and data sensitivity, so that you expose parts of your data to different clients. You should control which clients can gain access to which API privileges.

Illustration of APIs separating data both by business area and data sensitivity, so that different parts of the model can be exposed to different clients

Use Hierarchical Scopes

When data is hierarchical you can use hierarchical scopes, as in the below examples, where colon characters represent subresources:

ScopeGrants access to
orderFull information about orders.
order:itemInformation about items within an order.
order:paymentAccess to order payment details.
order:shipping:addressInformation about where to deliver orders.
order:shipping:statusInformation about the delivery status of orders.

Use Least-Privilege Scopes

Design clients so that they only have access to the data they need, and limit the scope to read-only access when write access is not needed. One possible convention is to make read-only access the default and then add a write suffix when higher privilege is needed:

ScopeAccess Granted
orderRead only access to full order information.
order:itemsRead only access to details about order items.
inventory:writeThe ability to create, change or delete an entire inventory item.
inventory:price:writeThe ability to change the price details for an inventory item.