-
Notifications
You must be signed in to change notification settings - Fork 0
Auth Implementation Notes
simitben edited this page Apr 11, 2026
·
1 revision
Provide implementation guidance for developers to build the App-based auth model in SimBiz 6 API V3 safely under PHP 7.3 + MariaDB 10.11.
- This page defines implementation notes for the upcoming auth hardening phase.
- Never store plaintext client secret.
- Generate secret with
random_bytes()and encode for transport. - Store only hash:
password_hash($secret . ':' . APP_AUTH_PEPPER, PASSWORD_BCRYPT, ['cost' => 12])
- Verify with:
password_verify($providedSecret . ':' . APP_AUTH_PEPPER, $secretHash)
- Keep only
secret_hint(masked last chars) for operator reference. - Show new secret only once at create/rotate response.
- Token type: opaque bearer token.
- Generation example (PHP 7.3 safe):
$raw = bin2hex(random_bytes(32));$accessToken = 'sbv3_' . $raw;
- Persist only hash:
$tokenHash = hash_hmac('sha256', $accessToken, APP_TOKEN_PEPPER);
- Store
token_prefixfor support troubleshooting (for example first 12 chars).
- Default TTL from
system_api3_config.TOKEN_TTL_SECONDS(suggested default: 3600). - Max TTL guard from
TOKEN_MAX_TTL_SECONDS. - Secret rotation default grace from
SECRET_GRACE_HOURS. - Rotation behavior:
- create new
ACTIVEsecret version - old
ACTIVE->GRACEuntilgrace_until - after grace, old secret ->
EXPIREDorREVOKED
- create new
- Admin may force immediate revoke (
grace_hours = 0).
-
SUSPENDEDapp:- deny new token issuance
- deny API calls for active tokens immediately
-
REVOKEDapp:- deny issuance and access
- revoke all active tokens immediately
- Token revoke must be enforced in middleware by DB check per request.
- Do not check permissions inside each controller action manually.
- Centralize in auth guard middleware:
- resolve route key
- resolve required permission
- verify grant in
system_api3_app_permission - fail fast with
403
- Resolve effective org from request:
- query:
organization_id/org_id,organization_code/org_code - body: same keys for create/update
- fallback: token org context, then app default org
- query:
- Validate resolved org exists in
system_api3_app_organizationand is active. - Reject unresolved org with
400; reject unauthorized org with403.
Log at minimum these events to system_api3_audit_log:
TOKEN_ISSUEDTOKEN_REVOKEDAPP_SUSPENDEDAPP_REACTIVATEDAPP_REVOKEDSECRET_ROTATEDPERMISSION_DENIEDORG_DENIED
Include context: app, token, route key, reason code, actor uid, IP, user-agent.
Observed V3 core files:
source-code/simbiz6/apiv3/Core/Router.phpsource-code/simbiz6/apiv3/Auth/TokenGuard.phpsource-code/simbiz6/api_v3.php
Recommended extension path:
- Extend router metadata to include
route_key(and optionally required permission code). - Replace current
TokenGuardwithAppTokenGuardthat validates:- token
- app status
- permission
- organization access
- Keep module route declarations in
modules/*/apiv3/module.phpto preserve module-first ownership. - Introduce auth admin endpoints in
modules/simantz/apiv3or dedicatedmodules/auth/apiv3.
- Use
POST /api/v3/auth/tokenas the only V3 token issuance endpoint. - Add migration script to bootstrap app records for existing integrations.
- Communicate cutoff date for legacy user-secret integrations.
- Disable legacy user-secret token issuance path.
- Secrets hashed with pepper and never logged.
- Tokens stored as hash only.
- Permission denial returns standardized
403errors. - Organization access checked for every protected endpoint.
- App suspend/revoke takes effect immediately.
- Audit log captures auth-critical events.
- Rotation and revoke tested with live-like data.
- Wiki and API spec updated in same change set.