Get a Pentest and security assessment of your IT network.

Cyber Security

Secure Server Connections with App Credentials

TL;DR

This guide shows you how to set up secure connections between clients and your server using application credentials (like API keys). We’ll cover creating strong credentials, safe storage on the client-side, and best practices for sending them to your server.

Setting Up Secure Server Connections

  1. Create Strong Application Credentials
    • Use long, random strings. A good length is at least 32 characters.
    • Avoid using predictable information like your server name or date.
    • Generate credentials programmatically whenever possible. For example, in Python:
      import secrets
      api_key = secrets.token_hex(16)
      print(api_key)
    • Consider using different credentials for different clients or purposes (e.g., one key for a mobile app, another for a web application).
  2. Secure Storage on the Client-Side
    • Never hardcode credentials directly into your client code! This is a major security risk.
    • Mobile Apps (iOS/Android): Use platform-specific secure storage mechanisms:
      • iOS: Keychain
      • Android: Keystore System

      These systems encrypt the credentials and protect them from unauthorized access. Consult your mobile development documentation for specific implementation details.

    • Web Applications (JavaScript): This is more challenging, as JavaScript runs in a user’s browser. Avoid storing credentials locally if possible. If you must:
      • Use HTTP-only cookies with the Secure flag set to prevent access from JavaScript. This requires server-side control of cookie creation.
      • Consider using a token-based authentication system (e.g., JWT) and storing the token in memory only, refreshing it as needed.
  3. Sending Credentials to the Server
    • Always use HTTPS: This encrypts all communication between the client and server, protecting credentials from eavesdropping.
    • Authentication Headers: The most common approach is to send credentials in an Authorization header. Use a scheme like Bearer Token or API Key:
      Authorization: Bearer YOUR_API_KEY

      or

      Authorization: API-Key YOUR_API_KEY
    • POST Requests (for sensitive data): When sending credentials as part of a login or registration process, use the POST method with an encrypted body.
    • Avoid GET requests for credentials: Credentials should never be passed in the URL, as they can be logged in browser history and server logs.
  4. Server-Side Validation
    • Always validate credentials on the server-side: Never trust client-provided data.
    • Check that the credential exists, is active, and has the necessary permissions.
    • Implement rate limiting to prevent brute-force attacks.
      # Example (Python/Flask)
      from flask import Flask, request
      app = Flask(__name__)
      @app.route('/api/data')
      def get_data():
          api_key = request.headers.get('Authorization')
          if api_key != 'YOUR_API_KEY':
              return "Invalid API Key", 401
          # ... rest of your code
  5. Credential Rotation
    • Regularly rotate your application credentials. This limits the impact if a credential is compromised.
    • Implement a process for invalidating old credentials when they are rotated.
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