Garment.id
🇮🇩 Indonesian
  • 🇮🇩 Indonesian
  • 🇺🇸 English
Home
Core APITenant APISupport CenterChangelog
Home
Core APITenant APISupport CenterChangelog
Github
Instagram
|
🇮🇩 Indonesian
  • 🇮🇩 Indonesian
  • 🇺🇸 English
🇮🇩 Indonesian
  • 🇮🇩 Indonesian
  • 🇺🇸 English
  1. Home
  • Introduction
  • 01 - Project Structure
  • 02 - Context Boundaries
  • 03 - Central Auth Runtime
  • 04 - Central Billing
  • 05 - Tenant Runtime
  • 06 - FE Integration Contracts
  • 07 - Security, Middleware, and RLS
  • 08 - Events, Jobs, Listeners, Notifications, Observers
  • 09 - Testing and Quality Gates
  • 10 - Operations Runbook
  • 11 - Architecture Decisions (ADR Ringkas)
  • 12 - API Endpoint Catalog (v1)
  • 13 - Class Catalog by Context
  • 14 - Feature Development Playbook
  • 15 - Migration and Model Conventions
  • 16 - Service, Support, and Trait Contracts
  • 17 - Auth Business Scenarios and Decision Tree
  • 18 - Config Reference and Impact
  • 19 - Migration and Model Cookbook
  • 20 - Billing and Provisioning Business Flows
  • 21 - FE Error Handling Matrix
  • 22 - Incident Playbooks
  • 23 - Data Ownership Matrix
  • 24 - Documentation Governance and Changelog
  • 25 - Tenant Authorization Backend Guide
Home
Core APITenant APISupport CenterChangelog
Home
Core APITenant APISupport CenterChangelog
Github
Instagram
|
🇮🇩 Indonesian
  • 🇮🇩 Indonesian
  • 🇺🇸 English
🇮🇩 Indonesian
  • 🇮🇩 Indonesian
  • 🇺🇸 English
  1. Home

07 - Security, Middleware, and RLS

Dokumen ini menjelaskan secara detail security pipeline, tenant context, RLS, dan logging operasional.

Middleware Alias Registration#

Seluruh alias ada di bootstrap/app.php.
AliasClass
tenant.contextSetTenantContext
tenant.tokenValidateTenantToken
tenant.requiredRequireTenantContext
password.confirmedRequireRecentPasswordConfirmation
stepup.confirmedRequireRecentStepUpAuthentication
subscription.activeEnsureTenantSubscriptionActive
tenant.writeEnsureTenantWriteAccess
subscription.featureEnforceSubscriptionFeature
usage.trackTrackFeatureUsage

Full Request Security Pipeline#

Tenant Context Resolution Detail#

SetTenantContext (app/Http/Middleware/Tenant/Context/SetTenantContext.php) melakukan:
1.
Ambil user dari sanctum (jika ada).
2.
Validasi format X-Tenant-ID (UUID).
3.
Ambil tenant_id dari token sebagai source of truth.
4.
Jika header tenant ada, header wajib match tenant token.
5.
Bind object TenantContext ke container.
6.
Set postgres session config untuk RLS:
app.current_tenant_id
app.current_user_id
7.
Set config ke koneksi central.
8.
Resolve cluster_id tenant dari central tenant registry.
9.
Jika cluster valid, set config yang sama ke koneksi cluster tenant.

Token Validation Detail#

ValidateTenantToken (app/Http/Middleware/Tenant/Context/ValidateTenantToken.php) memastikan:
super admin bisa bypass tenant token strict check.
endpoint tenant-scoped wajib punya tenant_id pada token.
tenant pada token harus sama dengan tenant context request.
user harus punya akses tenant aktif (hasActiveTenantAccess).
Jika gagal, response FORBIDDEN dikembalikan dengan envelope API.

RLS-Driven Isolation#

Arsitektur ini mengandalkan kombinasi:
route middleware tenant
token ability
TenantContext
postgres session variable untuk RLS policy
Konsekuensi desain:
query tidak perlu selalu explicit where tenant_id = ....
isolasi data terjadi di DB policy level.
tetap disarankan explicit filter di query untuk intent clarity pada use case tertentu.

Structured Logging and Alerting#

ApiRequestLoggerMiddleware#

post-controller logger middleware.
tidak mengganggu response jika logging gagal.

StructuredApiLogger#

log 4xx jika config('api.logging.log_4xx') aktif.
log 5xx jika config('api.logging.log_5xx') aktif.
payload log terstruktur: request_id, path, status, duration, user_id, ip.
trigger operational alert untuk spike status kritikal tertentu.
Referensi class:
app/Http/Middleware/Shared/ApiRequestLoggerMiddleware.php
app/Logging/StructuredApiLogger.php
app/Support/Security/OperationalAlerter.php

Security-Related Class Index#

Middleware:
SetTenantContext
ValidateTenantToken
RequireTenantContext
RequireRecentPasswordConfirmation
RequireRecentStepUpAuthentication
Auth controllers:
UserSecurityController
UserSessionController
MeController
Auth services:
AuthorizationResolver
Diubah pada 2026-03-03 22:25:30
Sebelumnya
06 - FE Integration Contracts
Berikutnya
08 - Events, Jobs, Listeners, Notifications, Observers
Built with