HTTPS and why tokens need it
40 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A login request crosses a station Wi-Fi router, an internet provider and many other machines before it reaches the server. On plain HTTP, each of them could read the password and token, or change the reply.
Plain HTTP: every byte is readable on the way
An eavesdropper is someone who reads network traffic that was not meant for them. On plain HTTP, which sends requests and responses as ordinary text, they see the URL, headers, cookies, bodies and tokens.
One login and one later request, exactly as an eavesdropper copies them from plain HTTP.
Example 1 · A login over plain HTTP, as an eavesdropper sees it
POST /api/v1/auth/login HTTP/1.1Host: shoplite.exampleContent-Type: application/json{"email": "asha.rao@example.com", "password": "Mango-Kite-Rain-2026"}HTTP/1.1 200 OKContent-Type: application/json{"accessToken": "7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c4", "tokenType": "Bearer", "expiresIn": 900}GET /api/v1/me/orders HTTP/1.1Host: shoplite.exampleAuthorization: Bearer 7f3a9c2e5b8d41f0a6c3e9b2d5f8a1c4Asha's email and password, in plain text The access token, usable by anyone for the next 15 minutes Every order she opens, with her phone number and address The chance to change any response before it reaches her
What this code does
- This is the exact text that travels when a client uses
http://instead ofhttps://. Nothing here is hidden: the password is in the body and the token is in the response and in every later request. - The eavesdropper does not need the password at all. Copying the
Authorizationline is enough to call/api/v1/me/ordersas Asha until the token expires.
Every machine on the path can read plain HTTP: the free Wi-Fi at a railway station, the hostel router, the internet provider and every router in between. They can also change the traffic, for example swap the UPI ID on a payment page, and neither side would notice. An attacker who sits in the middle and changes traffic is called a man-in-the-middle.
What TLS adds: encryption, integrity and identity
TLS (Transport Layer Security) is the protocol that wraps a connection in protection; HTTPS is HTTP inside TLS, usually on port 443. It gives encryption, integrity and server identity.
The same login over HTTPS: the eavesdropper now sees only encrypted records.
Example 2 · The same login over HTTPS, as an eavesdropper sees it
phone 10.0.0.23 -> 203.0.113.10 port 443server name: shoplite.example (from the DNS lookup and the TLS hello)TLS 1.3 handshake: client hello, server hello, certificate (encrypted)then only encrypted records: 17 03 03 01 2a 8e 4f c2 91 ... about 300 bytes, phone -> server 17 03 03 00 9b 3d 7a 05 ee ... about 160 bytes, server -> phone 17 03 03 00 c4 51 e9 2b 07 ... about 200 bytes, phone -> serverHidden the URL path, headers, cookies, token, email, password and every body Detected any change to a record on the way (the connection is dropped) Proved the server holds the private key for shoplite.example's certificate Visible which server, when, and roughly how many bytes
What this code does
- After the handshake the eavesdropper sees only encrypted records.
17 03 03is the standard start of every TLS data record; the bytes after it are ciphertext. The record sizes and the server name are still visible. - Even
/api/v1/me/ordersand the query string are inside the encrypted part. Only the IP address, port and server name stay outside, which is why the URL path is safe from the network but not from logs on the server.
The three guarantees in detail: encryption makes the path, headers, cookies, tokens and bodies unreadable on the way; integrity detects any change and drops the connection; server identity proves you are talking to the real owner of the domain name. Identity comes from a certificate: a small file that says "this public key belongs to shoplite.example", signed by a certificate authority (CA). Browsers and phones come with a list of trusted CAs, and many of them, such as Let's Encrypt, sign for free after the owner proves control of the domain.
TLS has limits. It does not hide which server you connect to, when, or roughly how much data moves; it does not protect data once it arrives; and it does not fix bugs in the app, such as a missing owner check.
The handshake and what the padlock means
The TLS handshake is the short conversation a client and server run before any HTTP is sent, to agree on settings, check the server's certificate and create a shared key. In TLS 1.3 it takes one round trip.
curl -v prints each step of a real TLS handshake. Follow the lines in order.
Example 3 · Reading a real TLS handshake with curl -v
curl -sv --http1.1 -o /dev/null https://example.com* Host example.com:443 was resolved. * Trying 172.66.147.243:443... * ALPN: curl offers http/1.1 * TLSv1.3 (OUT), TLS handshake, Client hello (1): * TLSv1.3 (IN), TLS handshake, Server hello (2): * TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8): * TLSv1.3 (IN), TLS handshake, Certificate (11): * TLSv1.3 (IN), TLS handshake, CERT verify (15): * TLSv1.3 (IN), TLS handshake, Finished (20): * TLSv1.3 (OUT), TLS handshake, Finished (20): * SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 / X25519MLKEM768 / id-ecPublicKey * ALPN: server accepted http/1.1 * Server certificate: * subject: CN=example.com * start date: Sep 26 22:49:11 2026 GMT * expire date: Dec 25 22:56:35 2026 GMT * issuer: C=US; O=SSL Corporation; CN=Cloudflare TLS Issuing ECC CA 3 * subjectAltName: "example.com" matches cert's "example.com" * SSL certificate verified via OpenSSL. * Established connection to example.com (172.66.147.243 port 443) from 172.17.0.2 port 44338 > GET / HTTP/1.1 > Host: example.com > User-Agent: curl/8.17.0 > Accept: */* > * Request completely sent off < HTTP/1.1 200 OK < Date: Fri, 16 Oct 2026 06:26:47 GMT < Content-Type: text/html; charset=utf-8 < Connection: keep-alive < Server: cloudflare < * Connection #0 to host example.com:443 left intact
What this code does
- Lines starting with
*are curl's notes on the connection. You can follow the handshake from the section above: client hello, server hello, the certificate,CERT verify(the server's signature) andFinished. TheSSL connection usingline shows the agreed TLS version and cipher;AES_256_GCMis the fast symmetric encryption used for the data. - The
Server certificateblock shows the checks: the certificate namesexample.com, it is in date, a CA signed it and curl verified it. Only then do the>request lines and<response lines appear, and all of them travel encrypted.-o /dev/nullthrows the page away so only this trace remains.
The handshake in four steps:
- Client hello: the client says which TLS versions and ciphers it supports and sends its half of a key exchange.
- Server hello and certificate: the server picks the options, sends its half of the key exchange and its certificate, and signs the handshake with its private key.
- Checks: the client verifies the certificate is signed by a trusted CA, is in date and names this host, and that the server's signature is valid.
- Shared key: both sides now hold the same fresh symmetric key that never crossed the network. Every HTTP request and response is encrypted with it.
The browser's padlock means two things only: the connection is encrypted, and the server proved it owns that exact domain name. It does not mean the site is honest: a fake site at shopIite.example (a capital I instead of an l) can get a valid certificate for its own name and show a padlock too.
Bearer tokens: whoever holds the token is you
A bearer token is an access token that works for whoever bears (carries) it. The server checks only that the token is valid, not who sends it or from which phone.
A token travels as plain text in one header line. Anyone who copies that line can send it.
Example 4 · A bearer token is just text in a header
curl -s https://httpbin.org/headers -H "Authorization: Bearer demo-7f3a9c2e"{
"headers": {
"Accept": "*/*",
"Authorization": "Bearer demo-7f3a9c2e",
"Host": "httpbin.org",
"User-Agent": "curl/8.8.0",
"X-Amzn-Trace-Id": "Root=1-6ad1c3c9-5ed5f7186345e84943035add"
}
}What this code does
httpbin.org/headersechoes back the headers it received. The server got the token as plain text in one header line; nothing in it says which phone or person sent it.- Anyone who copies
Bearer demo-7f3a9c2ecan send this exact line from any computer, and a real API would treat them as the token's owner. Here the token travelled over HTTPS, so nobody on the network could copy it on the way.
A bearer token is like a metro token: the gate opens for anyone who drops it in. So a leaked token is a stolen account until it expires. Tokens leak through plain HTTP, through URLs (query strings end up in browser history and server logs), through log files that print headers, through screenshots and through code pushed to GitHub.
The defences are simple habits. Send tokens only over HTTPS, only in the Authorization header, never in a URL. Keep access tokens short-lived, such as 15 minutes, and never write them to logs.
Find the bug
Find the leaking token
Each snippet can leak the access token. Tap the risky line, then check the fix.
The app asks for the learner's orders.
Round 1 of 3HTTPS in practice: load balancers, HSTS and localhost
HSTS (HTTP Strict Transport Security) is a response header that tells the browser: for the next N seconds, only ever use HTTPS for this site. It closes the gap where the very first request goes out as plain HTTP.
A plain HTTP request is redirected to HTTPS, and the HTTPS answer carries the HSTS header. Then comes the picture of where TLS ends in a real deployment.
Example 5 · Redirect to HTTPS, then HSTS
curl -s -o /dev/null -w "%{http_code} %{redirect_url}\n" http://github.comcurl -sI https://github.com | grep -i strict-transport-security301 https://github.com/ strict-transport-security: max-age=31536000; includeSubdomains; preload
What this code does
- Plain HTTP gets nothing but a
301redirect to the HTTPS address. A careful server never serves real pages or accepts logins on plain HTTP. - The HTTPS response carries HSTS:
max-age=31536000is 365 days,includeSubdomainscovers every sub-domain andpreloadasks browsers to ship the rule built in, so even the very first visit uses HTTPS.
Example 6 · Where TLS starts and ends in a deployment
Phone app / browser | HTTPS (TLS 1.3), certificate for shoplite.example vLoad balancer or reverse proxy <- TLS ends here (TLS termination) | private network: plain HTTP, or HTTPS again | adds X-Forwarded-Proto: https vShopLite API servers on port 8080 (three copies) | vMySQL databaseDeveloper laptop: http://localhost:8080 (never leaves the machine)Public internet always HTTPS; plain HTTP only redirects Inside the private network plain HTTP is common; HTTPS again is safer On your own laptop plain HTTP on localhost is fine for development
What this code does
- The load balancer holds the certificate and its private key, so the app servers do not each need one. It also spreads requests over the three copies of the API.
- The app must trust
X-Forwarded-Protoonly from its own load balancer. That header is how the app knows the original request was HTTPS, for example when it buildsLocationURLs.
In a real deployment TLS often ends at a load balancer or reverse proxy: a server in front of the app that decrypts every request and passes it to one of several app servers. This is called TLS termination. Behind it, traffic travels inside a private network, as plain HTTP or encrypted again, and the app learns the original scheme from a header such as X-Forwarded-Proto: https.
The usual setup: plain HTTP answers only with a redirect to HTTPS, and HTTPS responses carry the HSTS header. Plain http://localhost:8080 is fine while you develop, because the traffic never leaves your own computer; a real server reachable from the internet must use HTTPS, with no exceptions for "just a test".
Next step
Check your understanding
11 quick questions