Blog | G5 Cyber Security

REST API Security: Authentication Guide

TL;DR

This guide shows you how to secure your REST API using common authentication methods like API keys and JSON Web Tokens (JWT). We’ll cover generating, validating tokens, and best practices for keeping your API safe.

1. Choose an Authentication Method

There are several ways to authenticate users accessing your REST API. Here are two popular options:

2. Implementing API Key Authentication

  1. Generate Unique Keys: Create a unique key for each user or application accessing your API. Use a strong random string generator.
  2. Store Keys Securely: Store keys in a database, encrypted if possible. Never hardcode them into your application.
  3. Pass Key in Header: Require the API key to be sent in an HTTP header (e.g., X-API-Key).
  4. Validate on Every Request: Check the validity of the API key with each incoming request.

Example validation code (Python/Flask):

from flask import Flask, request, jsonify

app = Flask(__name__)

API_KEYS = {"user1": "your_api_key_here"}

def authenticate():
    api_key = request.headers.get('X-API-Key')
    if api_key in API_KEYS:
        return True
    else:
        return False

@app.route('/data')
def get_data():
    if authenticate():
        return jsonify({"message": "Data accessed successfully"})
    else:
        return jsonify({"message": "Authentication failed"}), 401

3. Implementing JWT Authentication

  1. Install a JWT Library: Use a library like PyJWT (Python), jsonwebtoken (Node.js) or similar for your language.
  2. Create a Secret Key: Generate a strong, random secret key to sign the tokens. Keep this very secure.
  3. Generate Tokens on Login: When a user logs in successfully, create a JWT containing user information (e.g., user ID).
  4. Pass Token in Authorization Header: Require the token to be sent in the Authorization header using the Bearer scheme (e.g., Authorization: Bearer <token>).
  5. Validate Tokens on Every Request: Verify the token’s signature and expiration date before granting access.

Example JWT generation code (Python/Flask):

import jwt
import datetime

SECRET_KEY = "your_secret_key"

def generate_token(user_id):
    payload = {"user_id": user_id, "exp": datetime.datetime.utcnow() + datetime.timedelta(minutes=30)}
    token = jwt.encode(payload, SECRET_KEY, algorithm="HS256")
    return token

Example JWT validation code (Python/Flask):

import jwt

SECRET_KEY = "your_secret_key"

def validate_token(token):
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
        return payload["user_id"]
    except jwt.ExpiredSignatureError:
        return None  # Token expired
    except jwt.InvalidTokenError:
        return None  # Invalid token

4. Best Practices for Security

Exit mobile version