Skip to content

feat: add tokenization API with format-preserving token workflows#53

Merged
allisson merged 2 commits intomainfrom
tokenization
Feb 18, 2026
Merged

feat: add tokenization API with format-preserving token workflows#53
allisson merged 2 commits intomainfrom
tokenization

Conversation

@allisson
Copy link
Owner

@allisson allisson commented Feb 18, 2026

Implements format-preserving tokenization service with UUID, numeric, luhn-preserving, and alphanumeric token formats. Supports deterministic and non-deterministic token generation, token lifecycle management (revoke, validate, expire), and dual database persistence (PostgreSQL/MySQL).

Major changes:

  • New tokenization domain layer with FormatType, Token, and TokenizationKey models
  • Tokenization use cases with metrics decorators for observability
  • Token generator factory with format-specific implementations (UUID, numeric, Luhn, alphanumeric)
  • MySQL and PostgreSQL repository implementations for tokenization persistence
  • HTTP handlers with DTO validation for key management and token operations
  • CLI commands: create-tokenization-key, rotate-tokenization-key, clean-expired-tokens
  • Database migrations: 000002_add_tokenization (up/down for both PostgreSQL and MySQL)

API endpoints:

  • POST /v1/tokenization/keys (create key with format + algorithm)
  • POST /v1/tokenization/keys/:name/rotate (version increment)
  • DELETE /v1/tokenization/keys/:id (soft delete)
  • POST /v1/tokenization/keys/:name/tokenize (generate token with optional TTL)
  • POST /v1/tokenization/detokenize (retrieve plaintext)
  • POST /v1/tokenization/validate (check token validity)
  • POST /v1/tokenization/revoke (mark token as revoked)

Capability mapping: encrypt (tokenize), decrypt (detokenize), read (validate), delete (revoke/delete key), write (create key), rotate (rotate key version).

Documentation updates:

  • Added docs/api/tokenization.md with endpoint contracts and security notes
  • Added docs/api/capability-matrix.md as canonical capability reference
  • Added docs/releases/v0.4.0.md with upgrade checklist and migration rollback guidance
  • Added docs/operations/policy-smoke-tests.md for capability verification runbook
  • Added docs/metadata.json for centralized release/API version tracking
  • Added docs/tools/check_docs_metadata.py consistency checker
  • Updated smoke test script/docs with tokenization round-trip validation
  • Expanded monitoring operations guide with tokenization metrics
  • Updated README with v0.4.0 feature highlights and pinned Docker image version
  • Cross-linked capability matrix from API endpoint docs and policy cookbook

CI/CD enhancements:

  • Added API/docs consistency guard for PRs (enforces doc updates for API changes)
  • Added docs metadata check validation in CI workflow
  • Integrated check_docs_metadata.py into make docs-lint

Testing:

  • Comprehensive test coverage for domain, service, use case, repository, and HTTP layers
  • Integration tests using real PostgreSQL and MySQL test containers
  • Handler tests with Gin test context and mock use cases
  • DTO validation tests for request/response contracts

Breaking changes: None (additive migration, new endpoints under /v1/tokenization).
Rollback note: Down migration available; safe to roll back before production tokenization usage.

Implements format-preserving tokenization service with UUID, numeric,
luhn-preserving, and alphanumeric token formats. Supports deterministic
and non-deterministic token generation, token lifecycle management
(revoke, validate, expire), and dual database persistence (PostgreSQL/MySQL).

Major changes:
- New tokenization domain layer with FormatType, Token, and TokenizationKey models
- Tokenization use cases with metrics decorators for observability
- Token generator factory with format-specific implementations (UUID, numeric, Luhn, alphanumeric)
- MySQL and PostgreSQL repository implementations for tokenization persistence
- HTTP handlers with DTO validation for key management and token operations
- CLI commands: create-tokenization-key, rotate-tokenization-key, clean-expired-tokens
- Database migrations: 000002_add_tokenization (up/down for both PostgreSQL and MySQL)

API endpoints:
- POST /v1/tokenization/keys (create key with format + algorithm)
- POST /v1/tokenization/keys/:name/rotate (version increment)
- DELETE /v1/tokenization/keys/:id (soft delete)
- POST /v1/tokenization/keys/:name/tokenize (generate token with optional TTL)
- POST /v1/tokenization/detokenize (retrieve plaintext)
- POST /v1/tokenization/validate (check token validity)
- POST /v1/tokenization/revoke (mark token as revoked)

Capability mapping: encrypt (tokenize), decrypt (detokenize), read (validate),
delete (revoke/delete key), write (create key), rotate (rotate key version).

Documentation updates:
- Added docs/api/tokenization.md with endpoint contracts and security notes
- Added docs/api/capability-matrix.md as canonical capability reference
- Added docs/releases/v0.4.0.md with upgrade checklist and migration rollback guidance
- Added docs/operations/policy-smoke-tests.md for capability verification runbook
- Added docs/metadata.json for centralized release/API version tracking
- Added docs/tools/check_docs_metadata.py consistency checker
- Updated smoke test script/docs with tokenization round-trip validation
- Expanded monitoring operations guide with tokenization metrics
- Updated README with v0.4.0 feature highlights and pinned Docker image version
- Cross-linked capability matrix from API endpoint docs and policy cookbook

CI/CD enhancements:
- Added API/docs consistency guard for PRs (enforces doc updates for API changes)
- Added docs metadata check validation in CI workflow
- Integrated check_docs_metadata.py into make docs-lint

Testing:
- Comprehensive test coverage for domain, service, use case, repository, and HTTP layers
- Integration tests using real PostgreSQL and MySQL test containers
- Handler tests with Gin test context and mock use cases
- DTO validation tests for request/response contracts

Breaking changes: None (additive migration, new endpoints under /v1/tokenization).
Rollback note: Down migration available; safe to roll back before production tokenization usage.
Addresses 12 gosec lint warnings by adding appropriate nolint directives
for false positives and refactoring one type conversion for clarity.

Changes by category:
Test fixture false positives (9 files, G101):
- Add `//nolint:gosec // test fixture data` to test database credentials
  and token hash strings in repository and use case tests
- Files: internal/app/di_test.go, internal/testutil/database.go,
  internal/tokenization/service/*_test.go,
  internal/auth/repository/*_token_repository_test.go,
  internal/auth/usecase/token_usecase_test.go

Domain model fields (4 files, G117):
- Add `//nolint:gosec` to legitimate Secret/ClientSecret fields used in
  authentication flows (hashed secrets, not plaintext credentials)
- Files: internal/auth/domain/client.go, internal/auth/domain/token.go,
  internal/auth/http/dto/request.go, internal/auth/http/dto/response.go

Integer conversion safety (2 files, G115):
- numeric_generator.go: Add nolint comment (conversion bounded 0-9 by design)
- rules.go: Replace `string(rune(p.MinLength+48))` with `strconv.Itoa(p.MinLength)`
  for clearer intent and import strconv package

HTTP test SSRF false positive (1 file, G704):
- Add `//nolint:gosec // controlled test environment with localhost URLs`
  to integration test HTTP client
- File: test/integration/api_test.go

All lint warnings are legitimate false positives or have been refactored
for improved clarity. No actual security vulnerabilities were present.
Verification: `make lint` now passes with 0 issues.
@allisson allisson merged commit 9db939e into main Feb 18, 2026
2 checks passed
@allisson allisson deleted the tokenization branch February 18, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments