Skip to content

Authentication and authorization: who you are, and what you may do

A request to GET /orders raises two different questions for the service that receives it. The first is who is calling. The second is whether that caller may do this. They are easy to run together because a login screen and an error page sit next to each other, and they fail in different ways.

OWASP defines the first as the process of verifying that an individual, entity or website is who or what it claims to be: authentication. Its Authorization Cheat Sheet defines the second as verifying that a requested action or service is approved for a specific entity: authorization. It is distinct from authentication, which only verifies an identity and says nothing about what that identity may do.

Mental model. Authentication is showing your pass at the door. Authorization is whether the pass opens this room. A valid pass opens only the rooms that someone decided it should open, and a perfect door check does nothing for a room whose lock is missing.

The order matters. The service cannot decide what a caller may do before it knows who the caller is, so authentication comes first. But the two do not happen equally often. A person proves who they are once, at sign-in, and the proof is turned into a session or a token. The permission question comes back with every request: OWASP’s guidance is to validate the permissions on every request, whatever its source, because an attacker needs to find only one request where the check was missed.

Try it: the first request, message by message

Section titled “Try it: the first request, message by message”

The flow below follows a first request from a client to a service, with a session cookie as the variant that opens. Press Play, or Step to advance one message at a time, and read the headings: TCP, TLS, then authentication, then authorization.

TLS, then who you are, then what you may do

80 ms
80 ms
10 ms
10 ms
Compare

The service checks the password, keeps the session in its own memory and gives the client a cookie that names it. Each later request carries the cookie and the service looks the session up. One connection, two requests.

340 ms
  1. TCP handshake40 ms
    SYN
    Client to Service: SYN. Arrives at 40 ms.
  2. 80 ms
    SYN-ACK
    Service to Client: SYN-ACK. Arrives at 80 ms.
  3. TLS 1.3 handshake120 ms
    ACK + ClientHello
    Client to Service: ACK + ClientHello. Arrives at 120 ms.
  4. 160 ms
    ServerHello … Finished
    Service to Client: ServerHello … Finished. Arrives at 160 ms.
  5. Authentication: proving who you are200 ms
    Finished + POST /login (username, password)
    Client to Service: Finished + POST /login (username, password). Arrives at 200 ms.Processing: 10 ms
  6. 250 ms
    200, Set-Cookie: session=…
    Service to Client: 200, Set-Cookie: session=…. Arrives at 250 ms.
  7. Authorization: the request and the check290 ms
    GET /orders, Cookie: session=…
    Client to Service: GET /orders, Cookie: session=…. Arrives at 290 ms.Processing: 10 ms
  8. 340 ms
    200 + orders
    Service to Client: 200 + orders. Arrives at 340 ms.

Time until the first response arrives: 340 ms4 round trips on the wire · 20 ms of processing

Model notes

Assumptions

  • Each message takes half the round-trip time of the link it crosses. Messages are never lost, never queue and never overlap, and each leaves when the one before it has arrived, once its receiver has finished any processing.
  • TLS 1.3 over TCP with a full handshake is used for every new connection, as in the connection-setup flow: one round trip for TCP, one for TLS, and the client’s first request travels with its Finished message. One processing time stands for everything a server does with a message: checking a password, creating or looking up a session, issuing a token, verifying a signature, checking a permission.
  • The first request only. The authorization server is a different host from the service, so the two token variants open a connection to each. The cookie variant signs in at the service itself.

Simplifications

  • A single request with the credentials stands for the whole sign-in. A browser sign-in with the OAuth authorization code flow adds redirects and the time the person takes to type, and neither is drawn. The token request is the token endpoint of RFC 6749, which must be reached over TLS.
  • The service already holds the key that checks the signature of a JWT, so it verifies the token without any message. Fetching that key the first time, and again when it is rotated, costs a call to the authorization server that is not drawn. A JWT stays valid until it expires; the model does not show how to revoke one earlier.
  • The introspection call goes over a connection that is already open and has no cache: every request pays it. A service can keep the answer for a short time, which removes the call for the requests that follow.
  • The session is stored in the memory of the service, so finding it costs no message. A session store shared by several instances adds a call to the store on every request.
  • Refresh tokens, certificate validation time, protection against cross-site request forgery, the size of the credentials on the wire and the requests that follow the first one are left out. Later requests over the same connection cost one round trip, plus the introspection call for an opaque token.

Parameter values are illustrative. They are not benchmarks of any real product.

With the starting settings the Session cookie variant has 8 messages, and the first response arrives after 340 ms. Four of those round trips are on the wire. The TCP handshake takes 80 ms and the TLS 1.3 handshake another 80 ms, which the TLS handshakes lesson counted. The sign-in request and its answer take 90 ms: one round trip plus the 10 ms the server spends checking the password. The request for the orders and the check that goes with it take 90 ms too.

The secure channel comes before both questions. RFC 8446 gives TLS its three properties: the server is always authenticated and the client optionally, the data is visible only to the two ends, and it cannot be modified without detection. That is why the password in the sign-in request is sent after the handshake and not before. OWASP’s Authentication Cheat Sheet says the same of the application: the login page, and every page after it, must be reached only over TLS.

The flow draws only the first request. A second request on the same connection costs one round trip. The sign-in does not repeat, and only the check does.

After authentication, the service holds some proof of who the caller is: a session identifier in a cookie, or a token. RFC 6265 describes the cookie mechanism: the server sets a Set-Cookie header, and the browser sends the cookie back with later requests. The next lesson compares what a service does with a cookie and with two kinds of token, and what each costs.

Whichever proof it is, the authorization decision needs more than the identity. It needs what the identity may do. That may be a role, a list of scopes carried by a token, or a rule about the specific object. RFC 6749 defines a scope as a list of space-delimited, case-sensitive strings that describe the range of access, and lets the authorization server grant a narrower scope than the client asked for.

When a check fails, the response says which question failed. RFC 6750, which describes bearer tokens, gives the error for a token that is expired, revoked, malformed or invalid for another reason, invalid_token, and says the resource should answer with HTTP 401. For a request that needs higher privileges than the token provides, it gives insufficient_scope, with HTTP 403.

Failure Question that failed Status in RFC 6750
Token expired, revoked or malformed Who are you? 401
Token valid, scope too small May you do this? 403

A 401 says the service could not establish who is calling. A 403 says it knows, and the answer is no. Keeping them apart helps the client: a 401 means getting a new token may help, and a 403 means a new token with the same rights will not.

A signed-in user can still read someone else’s order

Section titled “A signed-in user can still read someone else’s order”

Authentication does not protect an object. OWASP’s Authorization Cheat Sheet describes a request for /transactions?acct_id=1234, and asks what happens if the user changes acct_id to another number. If the service returns the other account’s transactions, it has an authorization bypass through a user-controlled key, which OWASP calls an insecure direct object reference. The user was authenticated the whole time.

The cheat sheet’s answers are a short list that applies far beyond this example.

  • Check every request for the specific object. Having access to one order does not mean having access to every order.
  • Deny by default. Access is refused unless a rule grants it, so a new endpoint or a new feature starts closed.
  • Enforce on the server. A check in the browser, or in the user interface, is not a control.
  • Apply least privileges. Give each caller the minimum access its job needs. The lesson on secrets and least privilege comes back to this.

Obscure identifiers do not replace the check. OWASP says that masking or randomising ids to make them hard to guess is generally not sufficient by itself, and that the focus should be on controlling access to the underlying objects.

What happens if the service only checks at the front door?

Section titled “What happens if the service only checks at the front door?”

Say a gateway in front of ten services checks the token and the scope, and the services behind it trust whatever the gateway lets through. The gateway answers the question “who is calling, and may they use this API”. It cannot answer “may they read order 523”, because it does not know who owns the order. The services still need an object-level check, and a request that reaches a service some other way, for instance from another service inside the network, passes no check at all.

OWASP’s guidance fits this: the check may be done server-side, at a gateway or in a serverless function, but it must happen for every request and not only the ones that come through the front. A gateway is a good place for the coarse questions. The decision about a particular object belongs where the object is.

  • Authentication answers who is calling. Authorization answers whether they may do this. A control does one of them, and a design needs both.
  • Authentication happens at sign-in and its result travels with each request. The authorization check is made again on every request, for the specific object.
  • In the flow, the first request with a session cookie has 8 messages and the first response arrives after 340 ms: 80 ms of TCP, 80 ms of TLS, 90 ms for the sign-in and 90 ms for the request and its check.
  • RFC 6750 suggests 401 for a token that is expired, revoked or invalid, and 403 for a valid token whose scope is too small.
  • A signed-in user changing an id in the address is an authorization failure, and it is prevented by a check on the object, deny by default, and enforcement on the server.

Next: sessions and tokens, and what checking each of them costs.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.