Download the PHP package bpjs/auth-client without Composer
On this page you can find all versions of the php package bpjs/auth-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package auth-client
🔐 Auth Service Client
Library autentikasi terpusat untuk microservices BPJS. Cukup install, tambah 1 baris middleware, beres!
📖 Daftar Isi
- Konsep
- Requirements
- Installasi
- Quick Start
- Cara Kerja
- Konfigurasi
- Penggunaan
- API Reference
- Error Handling
- Testing
- Troubleshooting
💡 Konsep
┌─────────────┐ │ User Service │ ← Satu-satunya tempat login & database user │ :8000 │ └──────┬───────┘ │ │ Token diverifikasi ke sini │ ┌──────┴──────────────────────────┐ │ │ │ ┌──────────┐ ┌──────────┐ │ │ │ HRM │ │ Ticket │ │ ← Service lain TIDAK perlu │ │ :8001 │ │ :8002 │ │ database user sendiri │ └──────────┘ └──────────┘ │ │ │ │ User login SEKALI, │ │ bisa akses SEMUA service │ └──────────────────────────────────┘
Keuntungan:
- ✅ Single Sign-On (SSO) - login sekali akses semua
- ✅ Tidak perlu database user di setiap service
- ✅ Tidak perlu sharing JWT secret key
- ✅ User management terpusat
- ✅ Logout di User Service langsung berpengaruh ke semua service
📋 Requirements
- PHP >= 8.1
- ext-curl
- ext-json
- Composer
🚀 Installasi
1. via composer
-
USER LOGIN (sekali) ┌─────────┐ POST /api/auth/login ┌──────────────┐ │ Browser │ ─────────────────────────────► │ User Service │ │ │ ◄───────────────────────────── │ :8000 │ └─────────┘ { token: "eyJ...", └──────────────┘ user: {...} }
- AKSES HRM
┌─────────┐ GET /api/payroll ┌──────────────┐
│ Browser │ Authorization: Bearer eyJ... │ HRM Service │
│ │ ────────────────────────────► │ :8001 │
└─────────┘ └──────┬───────┘
│
- VERIFIKASI TOKEN │
┌────────────────────────────────┘
│ POST /api/auth/verify-token
▼
┌──────────────┐
│ User Service │
│ :8000 │
└──────┬───────┘
│
- USER DATA │ ▼ ┌──────────────┐ │ HRM Service │ → $_SESSION['user'] │ :8001 │ → Controller └──────┬───────┘ │
- RESPONSE │ ▼ ┌─────────┐ │ Browser │ └─────────┘ Request 1: Token "xxx" → Cek cache: KOSONG → HTTP ke User Service (200ms) → Simpan cache (5 menit)
- VERIFIKASI TOKEN │
┌────────────────────────────────┘
│ POST /api/auth/verify-token
▼
┌──────────────┐
│ User Service │
│ :8000 │
└──────┬───────┘
│
Request 2: Token "xxx" (dalam 5 menit) → Cek cache: ADA! → Langsung return (< 1ms) → TIDAK perlu HTTP request!
Environment Variables Variable Required Default Deskripsi AUTH_SERVICE_URL Ya http://localhost:8000 URL User Service AUTH_APP_KEY Ya - App Key dari User Service AUTH_APP_SECRET Ya - App Secret dari User Service AUTH_TIMEOUT Tidak 30 Timeout HTTP request (detik) AUTH_MAX_RETRIES Tidak 3 Max retry kalau gagal AUTH_TOKEN_SOURCE Tidak bearer Sumber token: bearer, cookie, header, query AUTH_COOKIE_NAME Tidak token Nama cookie (jika pakai cookie) AUTH_HEADER_NAME Tidak X-Auth-Token Nama custom header AUTH_REDIRECT_URL Tidak /login URL redirect jika tidak authenticated
Konfigurasi Middleware
Penggunaan Middleware
Mengakses User Data di Controller
Manual Usage (Tanpa Middleware)
Login/Logout
Cek Permission & Menu
Login di Frontend
API Reference AuthServiceClient Constructor
Parameters:
$config - Array konfigurasi
base_url - URL User Service
app_key - Application key
app_secret - Application secret
timeout - HTTP timeout (default: 30)
max_retries - Max retry (default: 3)
$cache - Optional CacheManager instance
Methods verifyToken(string $token): ?array Verifikasi token ke User Service
login(string $username, string $password, array $options = []): array Login user
logout(string $token): bool Logout user
refreshToken(string $refreshToken): array Refresh token expired
getUserMenus(int $userId, ?string $appCode = null): array Ambil menu user
checkMenuAccess(int $userId, string $menuCode): bool Cek akses menu
hasPermission(int $userId, string $permission): bool Cek permission user
AuthMiddlewareClient
Exceptions Exception HTTP Code Deskripsi AuthenticationException 401 Auth gagal TokenExpiredException 401 Token expired NetworkException 500 Network error UnauthorizedException 403 Tidak diizinkan 🐛 Error Handling Contoh Lengkap Error Handling
Response Format Sukses (200)
Token Tidak Ditemukan (401)
Token Expired (401)
Forbidden (403)
Troubleshooting Error: "Token tidak ditemukan" Penyebab: Token tidak dikirim atau format salah
Solusi:
Changelog v1.0.0 (2024-01-01) ✅ Initial release
✅ Token verification
✅ Login/Logout
✅ Permission check
✅ Menu access
✅ Caching support
✅ Middleware ready
📄 License MIT License
👥 Contributors Team BPJS Development
All versions of auth-client with dependencies
ext-curl Version *
ext-json Version *