Blog | G5 Cyber Security

SDK API Authentication

TL;DR

This guide shows how to securely authenticate requests from your Software Development Kit (SDK) to a private API using API keys and tokens. We’ll cover key generation, secure storage within the SDK, and including authentication headers in each request.

1. Generate API Keys

Your API needs unique keys for each application or user using your SDK. Don’t use a single key for everything! Here’s how to generate them (example using Python):

import uuid

def generate_api_key():
    return str(uuid.uuid4())

# Example usage:
new_key = generate_api_key()
print(f"New API Key: {new_key}")

Store these keys securely in your database, associated with the application or user that owns them. Never commit API keys directly into source control.

2. Secure Storage within the SDK

The SDK needs to store the API key safely on the user’s device/system. The best approach depends on the platform:

Important: Never hardcode API keys into your SDK code.

3. Authentication Header

Each request from the SDK to your API must include an authentication header. The most common approach is using the Authorization header with a bearer token:

Here’s an example using JavaScript (fetch API):

async function makeApiRequest(endpoint, data) {
  const apiKey = await getApiKeyFromSecureStorage(); // Function to retrieve from Keychain/Keystore etc.
  const headers = new Headers({
    'Authorization': `Bearer ${apiKey}`,
    'Content-Type': 'application/json'
  });

  const response = await fetch(endpoint, {
    method: 'POST',
    headers: headers,
    body: JSON.stringify(data)
  });

  return response.json();
}

4. API-Side Verification

Your API must verify the authenticity of each request:

Example using Python/Flask:

from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/api/data', methods=['POST'])
  auth_header = request.headers.get('Authorization')
  if not auth_header:
    return jsonify({'error': 'Missing Authorization header'}), 401

token = auth_header.split(' ')[1]

  # Check if token exists in database and is valid (replace with your actual logic)
  if not validate_api_key(token):
    return jsonify({'error': 'Invalid API key'}), 401

  # Process the request...
  return jsonify({'message': 'Data processed successfully'})

5. Token Expiration & Refresh (Optional)

For increased security, consider using short-lived tokens and a refresh token mechanism:

This adds complexity but significantly improves security by limiting the impact of compromised tokens.

Exit mobile version