Urgent.News

What's breaking now, across thousands of outlets.

Tech

Client API Key — Design (Lite) ออกแบบ API key service to service ให้เบาและยังถูกต้อง

ออกแบบ API key สำหรับการเรียกกัน service to service ให้เบาและยังถูกต้อง วันหนึ่งจะมีอีกทีมมาขอเรียก service ของคุณ และคุณต้องรู้ให้ได้ว่าใครเป็นคนยิงเข้ามา ตัวเลือกที่คนส่วนใหญ่หยิบมีสองขั้ว — ยัดค่าสุ่ม uuid/hash ไว้ใน env var แล้วเทียบสตริง ซึ่งใช้ได้จริงจนถึงวันที่ key หลุด หรือลาก OAuth2 มาทั้งชุด ซึ่งซับซ้อนกว่าสิ่งที่กำลังจะปกป้องหลายเท่า โพสต์นี้คือทางกลาง TL;DR ตารางเดียว 6 คอลัมน์…

The design for a lightweight API key service aims to provide a simple and secure solution for service-to-service communication. The key focuses on six columns and ten lines of functions, making it easy for anyone to implement. The main goal is to ensure that only authorized services can access the API without revealing the key to end-users.

The lite design does not include scopes, cache, CLI, or unique constraints on the consumer column. This simplification aims to keep the implementation lightweight and efficient. The key hash is stored in an environment variable, while the key itself remains secret. The revoke process checks the timestamp in the query rather than the code, ensuring accurate and secure revocation.

The primary design decisions revolve around not storing the key directly, revoking keys using timestamps, and avoiding the inclusion of UNIQUE constraints on the consumer column. By not storing the key, unauthorized access is minimized. Timestamp-based revocation allows for efficient revocation checks in queries. Excluding UNIQUE constraints reduces complexity and enables key rotation when necessary.

The CREATE TABLE statement outlines the six columns: ID, consumer, key_hash, revoked_at, description, and created_at. The key_hash is stored as a SHA256 hexadecimal value, making authentication secure without requiring bcrypt. The revocation timestamp is stored to track when a key was revoked, providing transparency and accountability.

To revoke a key, update the last_used_at timestamp in the query. This ensures that revoked keys are easily identifiable and can be revoked promptly. The UNIQUE constraint on the consumer column is optional and can be added later if needed. Key rotation is recommended to maintain security, but it involves dropping and recreating the table. When a key is revoked, a new key must be deployed, and consumers must switch to the new key. This process ensures that revoked keys are no longer valid while maintaining minimal downtime.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Shopify or WooCommerce? How I Chose My First E-Commerce Platform

When I first started thinking about opening an e-commerce store, my first question was simple: Shopify or WooCommerce? At first, Shopify looked like the obvious choice.

  • Shopify offers simplicity with included hosting and minimal technical concerns.
  • WooCommerce provides control over hosting, plugins, and custom integrations.
  • Decision hinges on preference for simplicity vs. control in managing an e-commerce store.

More from Saturday 29 August →