REST API Authorization & Authentication (web + mobile)

REST API Authorization & Authentication (web + mobile)

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Decoupling Authorization and Authentication in Mobile and Web APIs

Building an API-centric service requires a fundamental shift in mindset compared to traditional website development. You are no longer just managing sessions for logged-in users; you are building a gateway where distinct identities—standard users and third-party developers—must be validated independently. The core challenge lies in separating Authorization (verifying the API key) from Authentication (verifying the user identity), a distinction often blurred by older protocols like HTTP Basic Auth.

In your architecture, you have three primary client types: the PHP backend, Android/iOS apps, and external developers. Each needs its own API key to access specific endpoints. If an admin deletes content, that action must be tied to a high privilege level, whereas standard users should only ever manipulate their own data. This requires a middleware layer that checks the Authorization header for a valid key before it ever attempts to resolve the user's identity or permissions.

The Authorization Layer: API Keys vs. OAuth

You asked whether to adopt OAuth/xAuth or implement a custom scheme like Amazon CloudFront's signature-based authentication. For modern REST APIs, OAuth 2.0 (specifically the Client Credentials grant type) is the industry standard for machine-to-machine communication. It fits your needs perfectly without the complexity of xAuth, which was designed specifically to bridge mobile apps and OAuth 2.0 in an older era when stateless tokens weren't common.

With OAuth 2.0 Client Credentials flow, you issue a specific token to every developer who registers their app. This token grants access to the API based on the app_id, not a personal user account. Your PHP backend and mobile apps can also act as clients in this flow if they hold a service account key.

If you were to follow Amazon's scheme, you would be generating cryptographic signatures for every request using a shared secret. While robust, this requires managing long-lived secrets on the client side (even on mobile) and is generally considered over-engineered unless you have strict compliance requirements that mandate it. OAuth 2.0 tokens are short-lived and revocable, offering better security posture for dynamic environments.

The Authentication Layer: JWTs for Statelessness

Once your middleware validates the API key and confirms the request isn't coming from a rogue developer, you must authenticate the actual human user if the endpoint requires it (e.g., fetching a user's profile).

The most robust approach here is to issue a JSON Web Token (JWT) upon successful login. This solves the "simultaneous login" problem naturally. When a user logs in on Android and later on the website:

  1. The mobile app receives a JWT and stores it securely (e.g., in the OS keychain or secure storage).
  2. The web browser stores the JWT in memory or HTTP-only cookies.
  3. Subsequent requests include this token in the Authorization: Bearer <token> header.

Because the JWT is self-contained, your server does not need to query a database to verify if a user is logged in. It simply decodes the signature. This means the state of "who is logged in" travels with the request, avoiding race conditions where one device might see a logout that hasn't propagated yet to another device's session cache.

Security Considerations: HTTPS and Token Expiry

You raised a valid concern regarding transmitting credentials over HTTPS followed by plain HTTP for subsequent requests. This is a critical architectural flaw. All traffic must remain encrypted. If you send the JWT over HTTP, any actor on the same network (like a public Wi-Fi hotspot) can perform a Man-in-the-Middle attack to intercept your session tokens or API keys.

The solution is strict enforcement of HTTPS across your entire domain. Modern browsers and mobile OSs often block mixed content automatically, but relying on that is not enough; your server configuration must reject non-SSL connections at the load balancer or reverse proxy level (e.g., Nginx terminating SSL before passing requests to your PHP app).

Regarding token management, never store sensitive data inside the JWT payload. Only include user identifiers (user_id, role). Store session metadata (like "last active") in a database if you need to support server-side invalidation of tokens immediately upon logout. For mobile apps, implement token refresh logic: when the JWT expires, the client should silently request a new one using a stored refresh token before showing the user an error screen.

Implementation Trade-offs

When integrating this with Laravel or similar frameworks, ensure your Auth guard is configured to validate the Bearer token against the signature algorithm (typically RS256 for production, HS256 for development) rather than checking it against a database session table. This keeps your API truly stateless and scalable.

Remember that userLevel logic should reside in your authorization policy, not the authentication layer. The auth layer answers "Who are you?" (via token), while the policy layer answers "What can you do?" (via scope or role claims). By keeping these concerns separate, you create a system where adding a new client type—say, an IoT device—only requires defining a new API key scope, without rewriting your user authentication logic.

Our network