Authentication and Authorization
Lesson opener
In one line: Authentication says who is calling. Authorization says what that caller may do.
Why it matters: Northline put the warehouse API key in the webshop's public page script. Anyone who loaded the page could read supplier bank accounts. The key was a password published to the browser.
Before this lesson: JSON and data exchange.
What you will be able to do
- Distinguish an API key, a bearer token, and an OAuth grant.
- Choose client credentials for a system and authorization code for a person.
- Apply least privilege to one integration identity.
- Keep secrets out of URLs, source control, and browsers.
Prerequisites
Lessons 1 to 3. You do not need to register an OAuth provider.
Keys, tokens, OAuth, and RBAC
Authentication answers "who is this caller?" Authorization answers "may this caller perform this operation?"
An API key is a shared secret. Northline sends it in the Authorization header, not in the query string. Query strings are commonly written to access logs and browser history. A key in a browser script is public. Rotate a key that has been copied into a ticket or a chat. Store it in the platform's secret store, not in the git repository.
A bearer token is a credential the caller presents in the Authorization: Bearer <token> header, as RFC 6750 (https://www.rfc-editor.org/rfc/rfc6750) describes. Whoever holds the token can use it until it expires or is revoked. Treat it like a key.
OAuth 2.0 (RFC 6749, https://www.rfc-editor.org/rfc/rfc6749) is a way to issue those tokens. Two grants cover this course:
- Authorization code (section 4.1) when a person is present. The person signs in at the identity provider. The client secret stays on a server, not in the browser.
- Client credentials (section 4.4) when one system calls another with no person at the keyboard. The client id and secret identify the integration, not an employee.
This course does not claim that any law forces you to use OAuth. Use the grant the system of record actually offers. If a vendor documents only API keys, follow that document and still apply least privilege. Do not invent a token lifetime. Read the expiry the provider returns.
RBAC (role-based access control) maps a caller to roles, and roles to operations. It is authorization, and it runs after authentication. A valid token with the wrong role is 403, not 401.
Least privilege means the webshop identity may read order_block and may not read bank_account. A separate procurement identity may read supplier bank details. One shared "admin" key for every integration fails this test.
def decide(authenticated, role, operation):
if not authenticated:
return 401
allowed = {
"webshop": {"read_order_block"},
"procurement": {"read_order_block", "read_bank_account"},
}
if operation in allowed.get(role, set()):
return 200
return 403
assert decide(False, "webshop", "read_order_block") == 401
assert decide(True, "webshop", "read_bank_account") == 403
assert decide(True, "procurement", "read_bank_account") == 200Teacher walkthrough: two identities
Illustrative. No real secret is printed here.
- The webshop server holds client id
northline-webin a secret store. It requests a client-credentials token from the ERP identity provider. - It calls
GET /accounts/C-1044withAuthorization: Bearer <token>. - The ERP checks the token, maps the client to role
webshop, and returnsorder_blockonly. - A request for the bank field with that token returns 403.
- Procurement's identity, role
procurement, may read the bank field. - Validation: the token is absent from the web page source, from git, and from the URL.
A second workplace case
A developer pastes a personal bearer token into a shared notebook so a colleague can "try the API." The token belongs to a person who can change payment terms. The colleague's notebook is now that person. The correction is a client-credentials identity for the job, with only the needed role, and revocation of the pasted token.
Failure example: a key in the query string
Symptom: Access logs contain ?api_key=.... A log export is emailed to a vendor. The vendor can call the API.
Diagnosis: The secret was placed where logs and history keep a copy. The root cause is treating the URL as a private channel.
Correction: Send the key or token only in the Authorization header. Purge the logged URLs according to your log-retention practice. Rotate the key. Do not commit the new key.
Validation: A fresh log line for a successful call contains the path and the status, and does not contain the credential.
Guided practice: choose the grant
Scenario: A night job copies item quantities from the warehouse API into the analytics store. No person is at a keyboard. The job must not create purchase orders.
Hints:
- Who is the caller?
- Which OAuth grant fits a system?
- Which operations does the role include?
Complete solution:
The caller is the night job, so client credentials fit. Authorization code is for a person. The role may read_on_hand and may not create_purchase_order. A token that fails to parse is 401. A valid token that asks to create a purchase order is 403. The client secret lives in the secret store. Validation: one read succeeds, one create is 403, and the secret is not in the job's source file.
Independent practice
Describe the identity for one integration you know, or for Northline's webshop.
Acceptance criteria:
- You say whether the caller is a person or a system, and which grant or key style matches.
- You name the role and at least one allowed and one forbidden operation.
- You say where the secret is stored, and you do not paste a real secret.
- You state the status for "no credential" and for "wrong role."
- You do not claim a law requires a particular product.
Common mistakes
- Putting API keys in frontend code or query strings.
- Using one administrator key for every integration.
- Treating a valid login as permission to call every operation.
- Sharing a person's token to represent a system.
Quick check
- What is the difference between authentication and authorization?
- Which OAuth grant fits a person, and which fits a system?
- Where does this course put the API key?
- A webshop token asks for a bank account and the role forbids it. Which status?
- Why is least privilege more than "use HTTPS"?
Answers:
- Authentication identifies the caller. Authorization decides the operations that caller may perform.
- Authorization code for a person. Client credentials for a system.
- In the
Authorizationheader, with the secret kept in a secret store, not in git or a browser script. - 403. The caller is authenticated and not allowed.
- HTTPS protects the message in transit. Least privilege limits what a stolen or misused identity can do.
Lesson summary
Prove who is calling, then allow only the operations that identity needs. Keys and bearer tokens are secrets. OAuth's authorization code and client credentials cover the person and the system. Nothing in this lesson is a universal legal mandate.
Bridge to the next lesson
The next lesson builds a small HTTP API in Python with FastAPI 0.141.1: an endpoint, validation, an error status, and the generated documentation page.