What a scope is
A scope is a name for a slice of access. When an app asks for a token, it lists the scopes it wants. The user may approve them, the server may trim them, and the token that comes back is limited to what was granted.
RFC 6749 section 3.3 defines the scope parameter: a list of scope tokens separated by spaces. The order does not matter. Each token is a case-sensitive string. The specification does not define any scope values itself; the server decides what each one means.
OpenID Connect Core 1.0 adds a few standard scopes on top. The most important is openid, which turns an OAuth request into an OpenID Connect request and makes the server issue an ID token.
The syntax rules
RFC 6749 allows a narrow set of characters in a scope token: any visible ASCII character except the space, the double quote and the backslash. Tokens are separated by single spaces.
- Valid:
openid,orders:read,https://api.example.com/orders.read. - Not valid as one scope:
orders read(that is two scopes), or a value with"or\. - A common mistake: commas between scopes.
openid,profileis a single, unknown scope.
In a URL the space is percent-encoded as %20. In a form body it may also appear as +. Both decode to the same space.
scope: openid profile email orders:read
in a URL: scope=openid%20profile%20email%20orders%3Aread
form body: scope=openid+profile+email+orders%3AreadThe standard OpenID Connect scopes
OpenID Connect Core section 5.4 defines scopes that ask for groups of user claims. Section 11 defines offline_access.
| Scope | What it asks for |
|---|---|
openid | Required for OpenID Connect. An ID token is issued. |
profile | name, family_name, given_name, middle_name, nickname, preferred_username, profile, picture, website, gender, birthdate, zoneinfo, locale, updated_at. |
email | email and email_verified. |
address | The address claim, a JSON object. |
phone | phone_number and phone_number_verified. |
offline_access | A refresh token that keeps working after the user’s session ends. |
Asking for a scope does not guarantee every claim. A user with no phone number on file gets no phone_number, whatever the scope says. The OIDC claims reference describes each claim.
What this tool does and does not do
The scope builder turns a set of choices into a correct scope string.
- It offers the standard OpenID Connect scopes and the common Keycloak client scopes as checkboxes.
- It takes your own scopes separated by spaces, commas or new lines.
- It removes repeats, turns commas into spaces and drops tokens with characters RFC 6749 does not allow.
- It shows the string plain, encoded for a URL and encoded for a form body.
- It lists what each chosen scope asks for.
It does not know which scopes your server defines or which ones a client may request. A scope that looks fine here can still be refused with invalid_scope.
How to use the builder
- Tick
openidif you want an ID token, then the claim scopes you need. - Tick any Keycloak client scopes you want to request explicitly.
- Type your own API scopes in the text box.
- Copy the scope string, or the URL or form encoding, into your request.
- Read the notes for anything that was removed or changed.
Choosing scopes for your app
Sign-in only
An app that only needs to know who the user is can ask for openid and maybe profile and email. Ask for no more than the screens actually show.
Calling your own API
Add API scopes such as orders:read. The API checks the scope claim in the access token before it serves a request. Keep read and write scopes separate, so a client that only reads cannot write.
Background work
A sync job or mobile app that must keep working while the user is away adds offline_access. Leave it out otherwise; long-lived tokens are a bigger loss if stolen.
Machine-to-machine
With client credentials there is no user, so openid and the profile scopes make no sense. Request only the API scopes the service needs, or none and rely on the client’s defaults.
How Keycloak handles scopes
Keycloak models scopes as client scopes. Each client scope bundles protocol mappers, which add claims to tokens, and can carry role scope mappings. A client scope is attached to a client as either default or optional.
- Default client scopes are applied to every token for that client, whether or not the request asks for them.
- Optional client scopes are applied only when they appear in the
scopeparameter. - In a new realm,
profile,email,roles,web-origins,acrandbasicare default;address,phone,offline_accessandmicroprofile-jwtare optional. rolesaddsrealm_accessandresource_access;web-originsaddsallowed-origins;basicaddssubandauth_time(Keycloak 25 and later).offline_accessneeds the user to have theoffline_accessrole, which new users get by default.
So in Keycloak, sending scope=openid alone often still returns profile and email claims, because those scopes are defaults. To make them opt-in, move them from default to optional on the client.
Seeing the result in Keycloak
Because default scopes are added silently, it can be hard to predict what a token will hold. Keycloak’s admin console has a preview for this. Open the client, go to Client scopes, then Evaluate. Pick a user and type the scope parameter you plan to send.
The page then lists the client scopes that would apply and shows a generated access token, ID token and UserInfo response for that user. It is the quickest way to check that a mapper adds a claim, or that moving a scope to optional really removes it.
Scope versus audience
A scope says what a token allows. It does not say which API the token is for. Two APIs could both define read, and a token with that scope would work at both.
The audience fixes that. The aud claim names the services that should accept the token, and each service must reject tokens meant for others. RFC 8707 adds a resource parameter so a client can ask for a token for a specific API by URL.
Use both together: scopes for the actions, audience for the target. Naming API scopes with a prefix, such as orders:read, also helps keep them apart.
Common errors and how to fix them
invalid_scope: the scope does not exist, is not attached to the client, or is not allowed. In Keycloak, add it to the client as an optional client scope.- Commas instead of spaces: the server sees one long unknown scope. Separate with spaces.
- No ID token:
openidis missing from the scope. - Claims you did not ask for: the scope is a default client scope in Keycloak.
Offline tokens not allowed for the user or client: the user lacks theoffline_accessrole, or the client cannot request it.
For error codes in general, see the OAuth error lookup.
Security notes
Scopes are the main tool for least privilege. RFC 9700 recommends restricting access tokens to the minimum privileges an app needs, and to particular resource servers where possible.
A scope in a request is only a wish. The token’s scope claim, or the scope field in the token response, says what was actually granted. APIs must check the token, never the original request.
Avoid one catch-all scope that grants everything. If it leaks, everything leaks with it.
Best practices
- Name API scopes by resource and action, such as
orders:readandorders:write. - Request scopes when the user needs the feature, not all at sign-in.
- Check the granted scope in the token response before you call an API.
- Keep
offline_accessfor real background work. - Document what each custom scope allows.