Grants, scopes, and consent
When the printer asks for access to Ava's photos, several decisions are made by different parties: what it asks for, whether it may ask at all, what Ava agrees to, and what the photo service finally issues. Grants, scopes and consent are the words for those pieces, and keeping them apart explains why the printer doesn't always get what it requested.
Understanding grants
In Following the messages, the printer received an authorization codeA short-lived, single-use value that the authorization server sends back to a client through the browser. The client swaps it for tokens in a direct request to the authorization server; APIs never accept it. through Ava's browser and presented it at the token endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect.. OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. has a name for what the printer presented. An authorization grant is a credential representing someone's authorization, which a client uses to obtain an access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in.. In the authorization code flowThe main OAuth flow: the browser visits the authorization server, where the person signs in and approves access, then carries an authorization code back to the client, which exchanges it for tokens directly. When the client runs on a server, the tokens never pass through the browser., the authorization code is that grant: it stands for Ava's approval until the printer trades it in.
A grant type names the method used at the tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. endpoint. The printer says which one it is using in the grant_type field of its token request, a form-encoded POST like those from Sending requests with curl:
POST /token HTTP/1.1
Host: auth.photos.example
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=SplxlOBeZQQYbYS6WxSbIA&redirect_uri=https%3A%2F%2Fprinter.example%2Foauth%2Fcallback&client_id=photo-printerThe value authorization_code tells the authorization serverIn OAuth, the service that handles a person's sign-in and approval and decides which tokens to issue to a client. It may belong to the same company as the API, but its job is separate. to expect a code and check it against the redirect URI and the client. Other grant types present something else. When the printer sends orders to its print lab on its own behalf, with no person involved, it can use the client credentials grant, which presents only the printer's own credentials. The token endpoint is the same; the grant type says what kind of evidence is arriving.
You may also hear a product call a saved application connection a "grant", as in "the printer has a grant to Ava's photos". That describes the continuing relationship the service records after Ava approves. When reading a protocolAn agreed set of rules for the messages two systems exchange and what each message means. HTTP, OAuth 2.0, OpenID Connect and SAML are all protocols. exchange, keep the two apart: the stored relationship lasts until Ava disconnects the printer, while the code presented in one token request is used once and gone.
Describing access with scopes
Giving another application limited access introduced a scope as a name for a kind of access. The photo service defines its own:
photos.read
photos.write
photos.deleteThe printer asks for scopes in the authorization request, as a space-separated list in its scope parameter. It needs to read photos to make a book, so it asks for photos.read. It has no reason to ask for photos.delete, and asking only for what the job needs is the first protection Ava gets: a smaller request is easier for her to judge and does less harm if the printer is ever compromised.
Those names are examples. Their meaning comes from the service that defines and enforces them, and another photo service could use different names or divide its capabilities differently. Adding photos.read to a request does not grant anything by itself. Before Ava sees anything, the authorization server checks the request against the printer's registration, the record it keeps for each client, which can limit the scopes that client may ask for at all. A request for a scope outside it is refused straight away, with no sign-in and no consent page, and the browser returns to the printer with an error:
HTTP/1.1 302 Found
Location: https://printer.example/oauth/callback?error=invalid_scope&state=af0ifjsldkjA scope can also leave important questions unanswered. Does photos.read mean every photo in Ava's library, only the album she picked, or photos others shared with her? The name alone does not say. The service needs its own rules, and sometimes more detail in the request, to express those limits, and the photo APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests. still checks each album as Protecting each request showed. Describing access with authorization details, in Advanced OAuth, covers one way to describe access more precisely.
Consent and granted access
Consent is Ava's agreement to the requested access. When a request passes the registration check, the photo service shows her a page naming the printer and what it asks for, and she chooses to allow or deny it.
Signing in and consenting are separate decisions. Signing in shows the photo service that she controls the account. Approving the connection authorizes the printer to receive access. Ava can sign in successfully and still say no, and then the printer gets an error instead of a code:
HTTP/1.1 302 Found
Location: https://printer.example/oauth/callback?error=access_denied&state=af0ifjsldkjA consent page does not need to appear every time. Once Ava has approved photos.read for the printer, the service can remember that approval, and the next request for the same access returns a code without asking again. A request for more, such as photos.read photos.write, brings the page back for the new part. An organization may also approve access for its people through administrative policy, and a client acting on its own behalf, like the printer sending orders to the print lab, involves no person to ask.
Even Ava's approval is not the last word. The printer's registration, organizational restrictions and the photo service's access policies can all limit what is issued after she agrees. The service might drop a scope from the token, or refuse to issue one at all.
Read request allowed
Requested: photos.read
User approves
Service policy permits it
Authorization server
User approved
Policy permits
Read access issued
Delete request refused
Requested: photos.delete
User approves
Service policy blocks it
Authorization server
User approved
Policy blocks it
No access issued
A new consent screen is not required in every OAuth interaction.
The token response says what was actually granted, in its own scope field:
HTTP/1.1 200 OK
Content-Type: application/json
{
"access_token": "mF_9.B5f-4.1JqM",
"token_type": "Bearer",
"expires_in": 3600,
"scope": "photos.read"
}Here the printer asked for photos.read photos.write, Ava approved both, and the service's policy issued only photos.read. The printer has to read that field and work with the access it received, not assume every requested scope was granted. Scopes and audiences and Enforcing access at an API follow that decision through to the API.
Check your understanding
Try these questions before moving on. If an answer isn't right, use the feedback and try again.
Try it in the Lab
- Request, approve, deny and narrow access with scopesChoose minimal photo access, compare registration with consent, and inspect the scope actually granted. Explore issuance policy in an optional extension.