Autentikasi API
Pelajari cara autentikasi dengan API ARSAKA PUGUH.
Metode Autentikasi
PUGUH mendukung dua metode autentikasi:
- API Keys: Untuk integrasi server-to-server (gunakan header
X-API-Key) - JWT Tokens: Untuk operasi konteks pengguna (gunakan header
Authorization: Bearer)
API Keys
Mendapatkan API Key
- Buka pengaturan profil Anda
- Klik "API Keys"
- Klik "Create New Key"
- Salin key (hanya ditampilkan sekali!)
Atau buat service account untuk penggunaan produksi.
Menggunakan API Keys
Sertakan key di header X-API-Key:
curl -X GET https://api-puguh.arsaka.io/api/v1/organizations \
-H "X-API-Key: YOUR_API_KEY" \
-H "X-Organization-ID: YOUR_ORGANIZATION_ID" Header yang Diperlukan
| Header | Wajib | Deskripsi |
|---|---|---|
X-API-Key | Ya | API key Anda |
X-Organization-ID | Ya | UUID organisasi Anda |
X-Application-ID | Terkadang | Diperlukan untuk operasi berskala aplikasi |
Content-Type | Untuk POST/PUT | Biasanya application/json |
JWT Tokens
Login
Autentikasi dengan email dan password untuk mendapatkan pasangan token JWT:
curl -X POST https://api-puguh.arsaka.io/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "user@example.com",
"password": "your_password"
}' Response (dikembalikan langsung, tanpa wrapper):
{
"access_token": "eyJhbGciOiJSUzI1NiIs...",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "dGhpcyBpcyBhIHJlZnJl..."
} JWT tokens ditandatangani dengan RS256 (asimetris). Anda dapat memverifikasi token secara lokal menggunakan public key dari endpoint JWKS:
curl -X GET https://api-puguh.arsaka.io/.well-known/jwks.json Register
Buat akun pengguna baru:
curl -X POST https://api-puguh.arsaka.io/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "newuser@example.com",
"password": "secure_password",
"full_name": "Jane Doe"
}' Response:
{
"user_id": "550e8400-e29b-41d4-a716-446655440000",
"email": "newuser@example.com",
"organization_id": "660e8400-e29b-41d4-a716-446655440000"
} Menggunakan JWT Tokens
Sertakan access token di header Authorization:
curl -X GET https://api-puguh.arsaka.io/api/v1/organizations \
-H "Authorization: Bearer eyJhbGciOiJSUzI1NiIs..." Memperbarui Token
Sebelum access token kedaluwarsa, gunakan refresh token untuk mendapatkan yang baru:
curl -X POST https://api-puguh.arsaka.io/api/v1/auth/refresh \
-H "Content-Type: application/json" \
-d '{
"refresh_token": "dGhpcyBpcyBhIHJlZnJl..."
}' Response:
{
"access_token": "eyJhbGciOiJSUzI1NiIs...",
"token_type": "Bearer",
"expires_in": 3600
} Autentikasi SDK
JavaScript / TypeScript
import { PuguhClient } from '@arsaka/puguh-sdk';
// Using API Key
const client = new PuguhClient({
apiKey: process.env.PUGUH_API_KEY,
organizationId: process.env.PUGUH_ORGANIZATION_ID,
});
// Using JWT
const client = new PuguhClient({
accessToken: userToken,
onTokenExpired: async () => {
const newToken = await refreshToken();
return newToken;
},
}); Python
from puguh_sdk import PuguhClient
# Using API Key
client = PuguhClient(
api_key=os.environ['PUGUH_API_KEY'],
organization_id=os.environ['PUGUH_ORGANIZATION_ID']
)
# Using JWT
client = PuguhClient(
access_token=user_token,
on_token_expired=refresh_token_callback
) Praktik Keamanan Terbaik
Keamanan API Key
- Jangan pernah commit key ke source control bash
# .gitignore .env *.key - Gunakan environment variables bash
export PUGUH_API_KEY=your_key_here - Rotasi key secara berkala
- Rotasi key produksi setiap kuartal
- Rotasi segera jika terkompromi
- Gunakan key terpisah per environment
- Key development
- Key staging
- Key production
Keamanan Token
- Simpan dengan aman
- Gunakan secure cookies (HttpOnly, Secure, SameSite)
- Jangan simpan di localStorage untuk aplikasi sensitif
- Masa berlaku singkat
- Access tokens: disarankan 1 jam
- Refresh tokens: 7-30 hari
- Revoke saat logout bash
curl -X POST https://api-puguh.arsaka.io/api/v1/auth/logout \ -H "Authorization: Bearer YOUR_TOKEN"
Response Error
PUGUH menggunakan kode status HTTP standar. Response error mengikuti format FastAPI:
401 Unauthorized
Kredensial tidak ada atau tidak valid:
{
"detail": "Invalid or expired token"
} 403 Forbidden
Kredensial valid tetapi tidak memiliki izin:
{
"detail": "Missing permission: organization.members.create"
} 422 Validation Error
Body request gagal validasi:
{
"detail": [
{
"loc": ["body", "email"],
"msg": "value is not a valid email address",
"type": "value_error.email"
}
]
} 429 Too Many Requests
{
"detail": "Rate limit exceeded. Retry after 60 seconds."
} Periksa header Retry-After untuk waktu tunggu dalam detik.
Rate Limiting
Panggilan API dibatasi per paket:
| Paket | Request/menit |
|---|---|
| Free | 60 |
| Pro | 300 |
| Business | 1.000 |
| Enterprise | Kustom |
Header rate limit disertakan di setiap response:
X-RateLimit-Limit: 300
X-RateLimit-Remaining: 299
X-RateLimit-Reset: 1640000000
Retry-After: 60 Menguji Autentikasi
Uji API Key
Verifikasi API key Anda berfungsi dengan memanggil endpoint health:
curl -X GET https://api-puguh.arsaka.io/api/v1/health \
-H "X-API-Key: YOUR_API_KEY" \
-H "X-Organization-ID: YOUR_ORGANIZATION_ID" Uji JWT Token
Login dan gunakan token yang dikembalikan untuk memanggil endpoint yang dilindungi:
# 1. Login to get a token
TOKEN=$(curl -s -X POST https://api-puguh.arsaka.io/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{"email": "user@example.com", "password": "your_password"}' \
| jq -r '.access_token')
# 2. Use the token
curl -X GET https://api-puguh.arsaka.io/api/v1/iam/users \
-H "Authorization: Bearer $TOKEN" Integrasi OAuth (Enterprise)
Untuk integrasi SSO:
URL Otorisasi
https://api-puguh.arsaka.io/oauth/authorize
?client_id=YOUR_CLIENT_ID
&redirect_uri=YOUR_REDIRECT_URI
&response_type=code
&scope=read:user read:resources
&state=RANDOM_STATE Tukar Code dengan Token
curl -X POST https://api-puguh.arsaka.io/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=authorization_code" \
-d "code=AUTHORIZATION_CODE" \
-d "client_id=YOUR_CLIENT_ID" \
-d "client_secret=YOUR_CLIENT_SECRET" \
-d "redirect_uri=YOUR_REDIRECT_URI"