Get a Pentest and security assessment of your IT network.

Cyber Security

Mutual WebService Authentication

TL;DR

Secure communication between two WebServices using API keys and a shared secret for verification. This guide covers key generation, exchange, and validation.

Step-by-step Guide

  1. Generate API Keys: Each service needs its own unique API key (a public identifier) and a shared secret (kept private). Don’t use passwords; generate random strings.
    • For example, using OpenSSL:
      openssl rand -base64 32

      This will create a 32-byte random string encoded in Base64.

  2. Key Exchange: Securely exchange the API keys between the two services.
    • Important: Never transmit secrets over unencrypted channels (like HTTP). Use HTTPS, SSH, or a secure configuration management system.
    • Consider using an out-of-band method for initial key distribution (e.g., phone call, in-person meeting) to avoid man-in-the-middle attacks during setup.
  3. Authentication Header: Define a standard header for authentication requests.
    • A common approach is using the X-API-Key header:
      X-API-Key: <service_api_key>
  4. Request Validation (Service A validating Service B): When Service A receives a request from Service B, it performs these checks.
    • Extract the API Key: Read the X-API-Key header from the incoming request.
    • Verify the Key: Check if the received API key matches the expected API key for Service B stored in its configuration.
    • Generate a Challenge (Optional but Recommended): Create a unique, random challenge string.
      openssl rand -base64 16
    • Sign the Challenge: Combine the API key and the challenge using your shared secret with a hashing algorithm (like HMAC-SHA256).
      echo -n '<api_key><challenge>' | openssl dgst -sha256 -hmac "<shared_secret>"
    • Receive and Verify the Signature: Service B signs the challenge with its shared secret and sends the signature back to Service A. Service A verifies this signature against the expected value calculated using its stored shared secret.
      echo -n '<api_key><challenge>' | openssl dgst -sha256 -hmac "<shared_secret>"
  5. Request Validation (Service B validating Service A): Repeat step 4, but with the roles reversed. Service B validates requests from Service A using its own API key and shared secret.
    • Ensure both services have a clear understanding of which service is initiating the request to use the correct keys.
  6. Error Handling: Implement robust error handling for authentication failures.
    • Return appropriate HTTP status codes (e.g., 401 Unauthorized) and informative error messages.
    • Log all authentication attempts, both successful and failed, for auditing purposes.
  7. Key Rotation: Regularly rotate API keys and shared secrets to minimize the impact of potential compromises.
    • Establish a key rotation schedule (e.g., every 90 days).
    • Implement a mechanism for gracefully transitioning to new keys without disrupting service operation.

Security Considerations

  • HTTPS: Always use HTTPS to encrypt communication between the services.
  • Shared Secret Protection: Protect the shared secret with utmost care. Store it securely (e.g., using a secrets management system like HashiCorp Vault).
  • Rate Limiting: Implement rate limiting to prevent brute-force attacks against the authentication endpoint.
  • Input Validation: Validate all input data to prevent injection vulnerabilities.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation