Designing secure web API access is crucial for protecting both user data and application integrity
We outline two widely used authentication methods: Token-based Authentication and HMAC Authentication.
Token-based Authentication
1. The user submits their credentials through the client application.
2. Upon verifying the credentials, the Authentication Server issues a token. This token is a string of characters representing the user's session, usually with an expiration time.
3. The client appends this token to the HTTP header of subsequent requests to the Web Server.
4. The Web Server validates the token and grants access to the requested resources.
HMAC Authentication
1. The client requests an API key from the Authentication Server.
2. The Authentication Server provides an API key (private key) and a Public APP ID (public key).
3. The client creates an HMAC signature (hmac A) using attributes such as the Public APP ID, request URI, HTTP method, request content, timestamp, and a nonce.
4. The client sends a request with hmac A in the HTTP header to the Web Server.
5. Upon receiving the request, the Web Server generates its own HMAC signature (hmac B) using the same attributes and the stored API key.
6. The Web Server compares hmac A and hmac B. If they match, it means the request is authentic.
7. The server then provides the requested resource to the client.
In both methods, sensitive information like passwords and API keys should be transmitted securely, often over HTTPS. Token-based authentication is generally simpler to implement and is stateless, while HMAC provides additional security by ensuring that the message has not been tampered with in transit.
When securing APIs, do you implement your own auth or use third-party solutions?
–
Subscribe to our weekly newsletter to get a Free System Design PDF (158 pages):
bit.ly/496keA7
ALT
This diagram depicts two methods for designing secure web API access: Token-based and HMAC authentication. The upper half illustrates token-based authentication with four steps: the client enters a password, receives a token from the Authentication Server, sends a request with the token to the Web Server, and then gets the resource. The lower half details HMAC authentication in seven steps: the client requests and receives an API key, generates an HMAC signature, sends a request with the signature, while the Web Server generates its own HMAC signature for comparison and, upon matching, releases the resource. A box shows the HMAC signature generation algorithm using attributes like the public app ID, request URI, HTTP method, content, timestamp, nonce, and the API key to produce the HMAC signature.