← All posts

Post 04 / Web security

Prove It Once: How Login, Sessions and Tokens Work

Why a website checks your identity at login, then trusts a session or token on later requests

A hotel guest shows ID at reception as the clerk hands over a key card, with a room corridor behind them

Authentication works by checking evidence of identity once, at login, and then issuing a credential that the client attaches to every later request. HTTP, the protocol of the web, is stateless: each request can be understood in isolation, with no memory of earlier ones. The credential therefore ties later requests to the login. This article follows that order: the evidence, the credential and the option of delegating the login to another party, with an analogy at the end.

Evidence at login

Login evidence comes in categories called factors, which are something the user knows (a password), something the user has (a phone or security key) and something the user is (a fingerprint or face). NIST’s digital identity guidelines use the same three categories and allow a biometric only together with a physical authenticator. Combining two categories is called multi-factor authentication, and it helps because the categories fail differently. A password can be guessed, reused across sites, or tricked out of a user remotely, whereas a device-based factor usually forces the attacker to obtain the device or to mount a more elaborate attack, so the attacker has to defeat two unrelated weaknesses in one attempt. The protection is partial. NIST classes passwords, one-time codes and out-of-band codes as not phishing-resistant because a fake login page can ask the user for the value and pass it on to the real site. A passkey, a public-key credential bound to one website, is designed to close that gap.

Passwords are verified without being stored in readable form. At registration the server computes a hash, a one-way function that maps any input to a fixed-length output, and stores only the result. A hash cannot be directly reversed to reveal the password, but someone with a stolen database can test password guesses against it. At login it hashes the submitted password and compares the two values. Servers also add a salt, a random value generated for each password and stored beside the hash, so that two users with the same password have different stored hashes and an attacker cannot reuse one table of precomputed hashes against every account. They also use a deliberately expensive hashing scheme, which limits how many guesses per second an attacker holding a stolen database can test. OWASP’s Password Storage Cheat Sheet names Argon2id as its first choice, with scrypt, PBKDF2 and bcrypt as alternatives for cases where Argon2id is unavailable, where FIPS-140 compliance is required, or where a legacy system already uses bcrypt.

A passkey is a stronger alternative, based on public-key cryptography, in which a key pair is generated for each website: a private key that is kept secret and a public key that can be shared safely. The private key stays on the user’s device and is unlocked locally with a fingerprint, face or PIN, and the server keeps only the public key. At login the server sends a random challenge and the device signs it, and the signed response includes the site’s origin (its scheme, domain and port), which the server checks along with the signature. Because the credential is scoped to the legitimate site, a look-alike phishing site has nothing usable to capture, and a stolen server database contains only public keys. Passkeys may be synchronised between a user’s devices by a platform provider, or kept bound to a single device such as a hardware security key.

The credential after login

After login, the server gives the client a credential to present on later requests. One common browser design uses a session, a server-side record that links a random identifier to the user. The browser stores the identifier in a cookie (a small value the browser returns automatically with every request to that site). In this design, whoever holds a valid copy of the identifier can use it, so the identifier is a bearer credential. If the identifier is generated by a cryptographically secure random number generator and is 128 bits long, there are 2^128, about 3.4 × 10^38, possible values, so guessing one is not a realistic attack. OWASP’s Session Management Cheat Sheet sets a floor of 64 bits of entropy (a measure of unpredictability) for session identifiers and recommends at least 128 bits for identifiers that an application creates itself. Logging out has to delete the server-side record, because removing the cookie from the browser does not stop a copied identifier from working.

APIs and mobile apps often send an access token in an Authorization: Bearer ... header. Like a session identifier, a bearer access token can be used by anyone holding a valid copy, so it must travel over TLS (the encryption layer behind HTTPS); a copy taken from a log or a compromised device works as well as the original. RFC 6750 also discourages placing the token in a URL, because URLs are routinely logged. The access token can be an opaque random string that the server looks up, much like a session identifier, or a JWT (JSON Web Token), which carries its claims inside the string, such as the user identifier and an expiry time exp. A JWT is signed so that the receiving server can verify it without a lookup, and with an asymmetric signature algorithm such as RS256 or ES256 that verification needs only the issuer’s public key.

Access tokens are given a limited lifetime, and providers choose different defaults. The Microsoft identity platform issues access tokens with a randomised lifetime of 60 to 90 minutes, whereas Auth0 defaults to 24 hours for API access tokens, and in both cases the operator can change the value. A refresh token, a longer-lived credential that can be exchanged only for new access tokens, lets the client obtain fresh ones without a new login.

Delegating the login

An application can hand the login to an identity provider, a service such as Google, Microsoft or a company directory that authenticates users and reports the result to other applications. The application then never sees the password, and the user can reuse one login across several applications, which is called single sign-on. One protocol for this is OpenID Connect, an identity layer on top of OAuth 2.0, the authorisation framework defined in RFC 6749. The application redirects the browser to the identity provider, the user authenticates there by any of the methods under “Evidence at login”, and the browser returns with a short-lived, single-use code that the application redeems directly with the provider. RFC 6749 recommends a maximum lifetime of 10 minutes for that code. After the application redeems the code, the provider returns an ID token, a JWT that tells the application who authenticated, and an access token for requests to protected resources. The application can then create its own session for subsequent requests.

An analogy: staying at a hotel

The whole process resembles checking in at a hotel. The front desk is the authentication step: you show identification, and perhaps a payment card as a second item, and the clerk checks that you match your booking. The clerk then gives you a key card, which corresponds to the session or token. The card does not contain your passport details, and the door locks do not ask who you are. Each lock only reads the card and checks that it is valid for that room and has not expired, which is what a server does with a credential on each request.

Several details of the process map onto the same picture. The card opens only your room and the gym, not the whole building, which corresponds to the limited permissions a token carries. It stops working at checkout time, which corresponds to expiry. The desk can cancel it earlier if you report it lost, as a server can invalidate a session identifier or opaque token by deleting its record. A self-contained JWT needs a separate revocation check for early cancellation; otherwise it can remain valid until it expires. Anyone who picks the card up from the lobby floor can enter your room, which is the bearer property and the reason a stolen token is a problem. A hotel that accepts a passport issued by your government, and does not issue its own identification, matches the arrangement described under “Delegating the login”, with the passport authority playing the identity provider. A hotel that asks you to return to the desk to confirm your identity before charging a large amount to the room corresponds to step-up authentication, in which a site asks for fresh or stronger evidence before a sensitive action, such as changing an email address, even though you are already logged in.

Sources

The pages in this list were consulted on 14 October 2026 to check the factual statements in this article. The hotel analogy is original to this article and has no source.

← Back to all postsBack to top ↑