QA AND TEST AUTOMATION · PART 4 · API TESTING · BASICS · 39 OF 112

API Authentication for Testers: Keys, OAuth 2.0, JWT

Authentication and authorisation from a tester's side: API keys, basic auth, session cookies, bearer tokens and JWTs (decoded, with real ShopRung claims), OAuth 2.0 flows (authorization code with PKCE, client credentials), and the test cases every protected API needs: missing, tampered and expired tokens, wrong roles, and other users' data.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • 401 who are you
  • 403 may you
  • keys, cookies, bearer JWT
  • OAuth flows
  • tampered, expired, wrong role, second user

Notes

Two questions

There are two separate questions. Authentication asks: who are you? If the API cannot tell, it answers four oh one, unauthorized, despite the confusing name. Authorisation asks: may you do this? If you are known but not allowed, the answer is four oh three, forbidden, or sometimes 404, to hide that the thing even exists, which is what ShopRung does for other customers' orders. Test both questions, separately.

AuthN vs AuthZ
  • authentication (authN) who are you? wrong → 401 Unauthorized
  • authorisation (authZ) may you do THIS? wrong → 403 Forbidden (or 404 to hide that it exists: ShopRung ORD-7)
  • test both, separately

The options

APIs identify callers in a few common ways. An API key: a secret string sent in a header, usually for one application calling another. Basic authentication: a user name and password in a header, acceptable only over HTTPS. A session cookie: the browser logs in and the server remembers the session. A bearer token: an authorization header with the word bearer and a token, very often a JSON web token. And OAuth two, a standard way to obtain such tokens.

How APIs authenticate
  • API key: a secret string in a header, for apps and services
  • basic auth: user:password in a header (only over HTTPS)
  • session cookie: browser logs in, server remembers
  • bearer token: "Authorization: Bearer <token>", often a JWT
  • OAuth 2.0: a standard way to GET those tokens

ShopRung login

ShopRung uses bearer tokens. Post Alice's email and password to the login endpoint, and you get back a token and expires in, seven thousand two hundred seconds, which is two hours. From then on, every protected call carries an authorization header: the word bearer, a space, and the token. Get the cart with it: two hundred. Without it: four oh one, log in first. In tests, log in once per test or per user, through the API, never through the login page.

Get and use a token
POST /api/auth/login
{"email":"alice@shoprung.test",
 "password":"Alice123!"}
→ 200 {"token":"eyJhbGciOi...",
        "expiresIn":7200, ...}

GET /api/cart
Authorization: Bearer eyJhbGci...
→ 200 { "items": [...] }
no header → 401 unauthorized

Inside a JWT

A JSON web token has three parts separated by dots: a header, a payload and a signature, each base64 url encoded. Decode ShopRung's and the header says the algorithm, H S two five six. The payload holds claims: the subject, user one, Alice's email, her role, customer, when it was issued and when it expires. Anyone can decode this, so a token must never contain secrets. Its safety comes from the signature, which only the server can create.

Alice's token, decoded
eyJhbGciOiJIUzI1NiIs... .
eyJzdWIiOiIxIiwiZW1h... .
kH3n8...  (signature)

header  {"alg":"HS256",
         "typ":"JWT"}
payload {"sub":"1","email":
  "alice@shoprung.test","role":
  "CUSTOMER","iat":...,"exp":...}
readable by anyone: not secret!

Token tests

Every protected endpoint needs negative token tests. No header: four oh one. A garbage token: four oh one. Change one character of a real token: four oh one, the token is not valid, because the signature no longer matches. An expired token: four oh one, token expired. Edit the payload to make yourself an admin: the signature fails, four oh one. The answer must never be two hundred, and never a five hundred crash.

Real ShopRung answers
no header      401 unauthorized
"Bearer abc"   401 invalid_token
change 1 char  401 invalid_token
  "The token is not valid"
expired (>2h)  401 token_expired
user deleted   401 invalid_token
role edited in payload →
  signature fails → 401

never 200, never 500

Authorisation tests

Then authorisation. A customer tries to ship an order: four oh three, only an admin can ship. The admin makes the same call: two hundred. Bob, logged in, asks for one of Alice's orders: 404. Switch on ShopRung's order leak bug and Bob gets two hundred and Alice's order. That bug class, broken object level authorisation, tops the OWASP list of API security risks. Always test with two users: one must never see the other's data.

Roles and ownership
customer → POST /orders/7/ship
  403 "Only an admin can ship
       an order"
admin → same call → 200
Bob → GET /api/orders/<Alice's>
  404 "No order with id ..."

bug "order-leak": Bob gets 200
= broken object-level authZ
  (OWASP API Security #1)

OAuth 2.0

OAuth two is a standard for getting tokens. Two flows are worth knowing. Authorization code with PKCE: the user logs in on the provider's page, like sign in with Google, the app receives a short code and swaps it for tokens; used by web and mobile apps. Client credentials: one service calls another with a client id and secret and gets a token, with no user involved. In automated tests, use client credentials or a dedicated test user, not the login screen.

Two OAuth flows to know
  • authorization code + PKCE a user logs in on the provider's page, the app gets a code, swaps it for tokens (web and mobile apps)
  • client credentials service to service: client id + secret → token
  • tests: use client credentials or a test user, not the UI

Secrets in tests

Handle test credentials carefully too. Never commit passwords, keys or tokens to Git; keep them in CI secret variables or a local dot env file that Git ignores. Use dedicated test accounts with the least privilege they need. Mask tokens in logs and test reports, because reports are often shared widely. And if a secret leaks, rotate it. ShopRung's practice passwords are public on purpose; real ones never are.

Handling secrets
  • never commit passwords, keys or tokens to Git
  • CI secret variables or a local .env (git-ignored)
  • dedicated test accounts with least privilege
  • mask tokens in logs and reports
  • rotate anything that leaked

Common mistakes

Common gaps. Testing everything with a valid admin token, so authorisation bugs never show. Never using a second user. Logging in through the UI in every test, which is slow. Secrets in code or in reports. Never testing expiry. And treating 404 and four oh three as interchangeable: check which one your API is supposed to return, because the choice is often deliberate, as with ShopRung's privacy rule.

Avoid these
  • only testing with a valid admin token
  • never testing a second user
  • logging in via the UI in every test
  • secrets in code or reports
  • not testing expiry
  • treating 404 vs 403 as "close enough" without checking the agreed rule

Say it like this

In the interview: I separate authentication and authorisation. For authentication: missing, malformed, tampered and expired tokens must all get four oh one, never data and never a crash. For authorisation: each role can do only its own actions, otherwise four oh three, and a second user can never read or change the first user's data. I get tokens through the API, keep secrets in CI variables, and mask them in logs.

In the interview
  • "I separate authN and authZ.
  • AuthN: missing, malformed, tampered and expired tokens
  • all give 401. AuthZ: each role
  • can only do its actions (403), and a second user can never
  • read or change the first
  • user's data. I get tokens via the API, keep secrets in CI
  • variables, and mask logs."

Interview questions

Q1What is the difference between authentication and authorisation?

Authentication verifies who the caller is (failure: 401); authorisation decides whether that caller may perform the action (failure: 403, or 404 to hide existence).

Q2What is a JWT?

A JSON Web Token: header, payload (claims such as sub, role, exp) and signature, base64url-encoded and dot-separated. Anyone can read it; only the issuer can sign it.

Q3How would you test a JWT-protected API?

Valid token works; missing, malformed, tampered (one character changed or payload edited) and expired tokens return 401; wrong-role calls return 403; other users' data is never returned.

Q4What is the difference between 401 and 403?

401: not authenticated or invalid credentials. 403: authenticated but not allowed.

Q5What are the main OAuth 2.0 flows?

Authorization code with PKCE for user logins in web and mobile apps, and client credentials for service-to-service calls; refresh tokens renew access tokens.

Q6What is broken object-level authorisation?

An API that returns or changes an object (like an order) without checking it belongs to the caller; it is the top OWASP API security risk. Test with two users.

Q7How do you handle credentials in automated tests?

Keep them in CI secret variables or an ignored .env file, use dedicated least-privilege test accounts, mask tokens in logs and rotate anything leaked.

Q8Should tests log in through the UI?

Only the tests that check the login page itself. Others should get tokens or sessions through the API, which is faster and more reliable.