Rana Maninder Singh
Lesson 4 of 12API & Integration Development

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

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:

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") == 200

Teacher walkthrough: two identities

Illustrative. No real secret is printed here.

  1. The webshop server holds client id northline-web in a secret store. It requests a client-credentials token from the ERP identity provider.
  2. It calls GET /accounts/C-1044 with Authorization: Bearer <token>.
  3. The ERP checks the token, maps the client to role webshop, and returns order_block only.
  4. A request for the bank field with that token returns 403.
  5. Procurement's identity, role procurement, may read the bank field.
  6. 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:

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:

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

  1. What is the difference between authentication and authorization?
  2. Which OAuth grant fits a person, and which fits a system?
  3. Where does this course put the API key?
  4. A webshop token asks for a bank account and the role forbids it. Which status?
  5. Why is least privilege more than "use HTTPS"?

Answers:

  1. Authentication identifies the caller. Authorization decides the operations that caller may perform.
  2. Authorization code for a person. Client credentials for a system.
  3. In the Authorization header, with the secret kept in a secret store, not in git or a browser script.
  4. 403. The caller is authenticated and not allowed.
  5. 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.