OAuth2 Scopes and Consent
You'll learn how to define granular permissions using OAuth2 scopes and implement user consent flows to protect user data.
What Are OAuth2 Scopes?
Scopes are permission labels that define exactly what an access token is allowed to do. Instead of granting an application complete access to a user's account, scopes let you request only the specific permissions you need.
Think of scopes like keys to different rooms in a building. An employee might have a key to their office and the break room, but not the server room. Similarly, a third-party calendar app might have the read:calendar scope but not delete:email.
Designing Granular Scopes
Good scope design follows the principle of least privilege:
- Read vs Write: Separate
read:profilefromwrite:profile - Resource-based:
orders:read,orders:create,orders:delete - Hierarchical:
admin:allmight imply all other scopes - Descriptive: Use clear names like
photos:uploadnot justphotos
Common scope examples: openid, profile, email, read:posts, write:messages
The User Consent Flow
When an application requests scopes, the authorization server presents a consent screen to the user. This screen explains what permissions are being requested in human-readable language:
- Application redirects user to authorization server with requested scopes
- User sees: "AppName wants to: View your profile, Read your calendar"
- User clicks "Allow" or "Deny"
- If allowed, the issued access token is limited to those approved scopes
The consent flow ensures users make informed decisions about data sharing—they're not blindly trusting applications.
Key Takeaway: OAuth2 scopes enable fine-grained access control by defining specific permissions, while consent flows ensure users explicitly authorize what data third-party applications can access, implementing both security and user transparency.