އޮތެންޓިކޭޝަން
އެސްއެމްއެސްބީއޭޓީ ޗެޓްހަބް އެޕީއައި އިން ބޭނުންކުރަނީ ކުންފުނީގެ ޓޯކަންތަކާއި އޮޕަރޭޓަރ ޓޯކަންތަކާއެކު ދެ ލެވެލްގެ ޖޭޑަބްލިޔުޓީ ޓޯކަން އަށް ބިނާކޮށް އޮތެންޓިކޭޝަން ސިސްޓަމެކެވެ.
އޮތެންޓިކޭޝަން ފްލޯ
graph TD
A[Login Credentials] --> B[Get Company Token]
B --> C[Use Company Token]
C --> D[Get Operator Token]
D --> E[Use Operator Token]
E --> F[Validate Token]
ކުންފުނީގެ ޓޯކަން
ކުންފުނީގެ ޓޯކަންތަކުގެ ސަބަބުން ޗެޓްހަބް އެޕީއައި އަށް އޯގަނައިޒޭޝަން ލެވެލްގެ އެކްސެސް ލިބެ އެވެ.
ކުންފުނީގެ ޓޯކަން ހޯދުން
އެންޑްޕޮއިންޓް: POST /api/company/get-token
ރިކުއެސްޓް:
curl -X POST https://chatapi.smsbat.com/api/company/get-token \
-H "Content-Type: application/json" \
-d '{
"login": "your-company-login",
"password": "your-company-password"
}'
ރިކުއެސްޓް ބޮޑީ:
{
"login": "string",
"password": "string"
}
ޖަވާބު:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
އެ ޖަވާބަކީ ޖޭޑަބްލިޔުޓީ ޓޯކަން ސްޓްރިންގެކެވެ.
ކުންފުނީގެ ޓޯކަން ބޭނުންކުރުން
ދެ ގޮތުގެ ތެރެއިން އެއް ގޮތެއް ބޭނުންކޮށްގެން އެޕީއައި ރިކުއެސްޓްތަކުގައި ކުންފުނީގެ ޓޯކަން ހިމަނާށެވެ:
ގޮތް 1: އޮތޯރިޒޭޝަން ހެޑަރ (ރަނގަޅު)
curl -X GET https://chatapi.smsbat.com/api/company/organization \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
ގޮތް 2: އެކްސް-އޮތޮރައިޒޭޝަން-ކީ ހެޑަރ
curl -X GET https://chatapi.smsbat.com/api/company/organization \
-H "X-Authorization-Key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
އޮޕަރޭޓަރ ޓޯކަން
އޮޕަރޭޓަރ ޓޯކަންސް އިން އޯގަނައިޒޭޝަނެއްގެ ތެރޭގައި ވަކިވަކި އޮޕަރޭޓަރުންނަށް ޔޫޒަރ ސްޕެސިފިކް އެކްސެސް ލިބިގެންދެއެވެ.
އޮޕަރޭޓަރ ޓޯކަން ހޯދުން
އެންޑްޕޮއިންޓް: POST /api/operator/get-token
ރިކުއެސްޓް:
curl -X POST https://chatapi.smsbat.com/api/operator/get-token \
-H "Authorization: Bearer {company-token}" \
-H "Content-Type: application/json" \
-d '{
"id": 123,
"expiresAt": "2025-12-31T23:59:59Z"
}'
ރިކުއެސްޓް ބޮޑީ:
{
"id": 0,
"expiresAt": "2025-01-20T14:33:34.147Z"
}
ޕެރެމިޓަރސް:
| ޕެރެމިޓަރ | ވައްތަރު | ބޭނުންވާ | ތަފްސީލު |
|---|---|---|---|
އައިޑީ | އިންޓިޖަރ | އާދެ | އޮޕަރޭޓަރ އައިޑީ |
މުއްދަތު ހަމަވާ ވަގުތު | ސްޓްރިންގް (އައިއެސްއޯ 8601) | އާދެ | ޓޯކަން މުއްދަތު ހަމަވާ ތާރީޚާއި ގަޑި (މިހާރުން ފެށިގެން ގިނަވެގެން 24 ގަޑިއިރު) |
މުހިންމު: ޓޯކަންގެ އެންމެ ދިގު މުއްދަތަކީ 24 ގަޑިއިރު އެވެ. ކުރިއަށް އޮތްތަނުގައި expiresAt ޕެރެމިޓަރ 24 ގަޑިއިރަށްވުރެ ގިނައިރު ނުވާނެއެވެ.
ޖަވާބު:
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvcGVyYXRvcl9pZCI6MTIzLCJleHAiOjE3Mzc0MTI3OTl9.example_signature"
އޮޕަރޭޓަރ ޓޯކަން ބޭނުންކުރުން
އެޕީއައި ރިކުއެސްޓްތަކުގައި އޮޕަރޭޓަރ ޓޯކަން ހިމަނާށެވެ:
curl -X GET https://chatapi.smsbat.com/api/operator \
-H "Authorization: Bearer {operator-token}"
ޓޯކަން ވެލިޑޭޝަން
ޓޯކަން ބޭނުން ކުރުމުގެ ކުރިން އަދިވެސް ސައްހަކަން ޔަގީންކުރުން.
އެންޑްޕޮއިންޓް: POST /api/operator/validate-token
ރިކުއެސްޓް:
curl -X POST https://chatapi.smsbat.com/api/operator/validate-token \
-H "Authorization: Bearer {company-token}" \
-H "Content-Type: application/json" \
-d '{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
}'
ރިކުއެސްޓް ބޮޑީ:
{
"token": "string"
}
ޖަވާބު (ސައްހަ ޓޯކަން):
{
"isValid": true,
"operatorId": 123,
"clientId": 0,
"expiresAt": "2025-12-31T23:59:59Z",
"error": null
}
ޖަވާބު (ސައްހަ ނޫން ޓޯކަން):
{
"isValid": false,
"error": "Invalid token"
}
ޓޯކަން މުއްދަތު ހަމަވުން
ކުންފުނީގެ ޓޯކަންސް
- އެޕީއައިގައި ސާފުކޮށް މުއްދަތު ހަމަނުވުން
- ޓޯކަން ލައިފްސައިކަލް ޕޮލިސީތަކަށް އެކައުންޓް މެނޭޖަރާ ގުޅުން
- ސެކިއުރިޓީއަށްޓަކައި ވަކިވަކިން ޓޯކަންތައް އެނބުރުވުން
އޮޕަރޭޓަރ ޓޯކަންސް
- ޓޯކަން ރިކުއެސްޓް ކުރާއިރު މުއްދަތު ހަމަވުން ސެޓްކުރުން (
expiresAtޕެރެމިޓަރ) - ބޭނުން ކުރުމުގެ ކުރިން ޓޯކަންތައް ސައްހަކުރުން
- މުއްދަތު ހަމަވުމުގެ ކުރިން އާ ޓޯކަން ހޯދުމަށް އެދި ހުށަހެޅުން
ތަންފީޒުކުރުމުގެ މިސާލުތައް
ޕައިތަން
import requests
from datetime import datetime, timedelta
class ChatHubAuth:
def __init__(self, base_url):
self.base_url = base_url
self.company_token = None
self.operator_tokens = {}
def get_company_token(self, login, password):
"""Get company authentication token"""
response = requests.post(
f"{self.base_url}/api/company/get-token",
json={"login": login, "password": password}
)
response.raise_for_status()
self.company_token = response.json()
return self.company_token
def get_operator_token(self, operator_id, expires_days=30):
"""Get operator token with expiration"""
expires_at = (
datetime.utcnow() + timedelta(days=expires_days)
).isoformat() + "Z"
response = requests.post(
f"{self.base_url}/api/operator/get-token",
headers={"Authorization": f"Bearer {self.company_token}"},
json={"id": operator_id, "expiresAt": expires_at}
)
response.raise_for_status()
token = response.json()
self.operator_tokens[operator_id] = token
return token
def validate_token(self, token):
"""Validate if token is still valid"""
response = requests.post(
f"{self.base_url}/api/operator/validate-token",
headers={"Authorization": f"Bearer {self.company_token}"},
json={"token": token}
)
response.raise_for_status()
return response.json()
# Usage
auth = ChatHubAuth("https://chatapi.smsbat.com")
# Get company token
company_token = auth.get_company_token("login", "password")
# Get operator token
operator_token = auth.get_operator_token(operator_id=123, expires_days=30)
# Validate token
is_valid = auth.validate_token(operator_token)
print(f"Token valid: {is_valid['valid']}")
ޖާވާސްކްރިޕްޓް (Node.js)
const axios = require('axios');
class ChatHubAuth {
constructor(baseUrl) {
this.baseUrl = baseUrl;
this.companyToken = null;
this.operatorTokens = {};
}
async getCompanyToken(login, password) {
const response = await axios.post(
`${this.baseUrl}/api/company/get-token`,
{ login, password }
);
this.companyToken = response.data;
return this.companyToken;
}
async getOperatorToken(operatorId, expiresDays = 30) {
const expiresAt = new Date(
Date.now() + expiresDays * 24 * 60 * 60 * 1000
).toISOString();
const response = await axios.post(
`${this.baseUrl}/api/operator/get-token`,
{ id: operatorId, expiresAt },
{
headers: {
Authorization: `Bearer ${this.companyToken}`
}
}
);
const token = response.data;
this.operatorTokens[operatorId] = token;
return token;
}
async validateToken(token) {
const response = await axios.post(
`${this.baseUrl}/api/operator/validate-token`,
{ token },
{
headers: {
Authorization: `Bearer ${this.companyToken}`
}
}
);
return response.data;
}
}
// Usage
const auth = new ChatHubAuth('https://chatapi.smsbat.com');
async function authenticate() {
// Get company token
const companyToken = await auth.getCompanyToken('login', 'password');
// Get operator token
const operatorToken = await auth.getOperatorToken(123, 30);
// Validate token
const validation = await auth.validateToken(operatorToken);
console.log('Token valid:', validation.isValid);
}
authenticate();
ޕީއެޗްޕީ
<?php
class ChatHubAuth {
private $baseUrl;
private $companyToken;
private $operatorTokens = [];
public function __construct($baseUrl) {
$this->baseUrl = $baseUrl;
}
public function getCompanyToken($login, $password) {
$ch = curl_init($this->baseUrl . '/api/company/get-token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'login' => $login,
'password' => $password
]));
$response = curl_exec($ch);
curl_close($ch);
$this->companyToken = json_decode($response);
return $this->companyToken;
}
public function getOperatorToken($operatorId, $expiresDays = 30) {
$expiresAt = date(
'Y-m-d\TH:i:s\Z',
time() + ($expiresDays * 24 * 60 * 60)
);
$ch = curl_init($this->baseUrl . '/api/operator/get-token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->companyToken
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'id' => $operatorId,
'expiresAt' => $expiresAt
]));
$response = curl_exec($ch);
curl_close($ch);
$token = json_decode($response);
$this->operatorTokens[$operatorId] = $token;
return $token;
}
public function validateToken($token) {
$ch = curl_init($this->baseUrl . '/api/operator/validate-token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->companyToken
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'token' => $token
]));
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
}
// Usage
$auth = new ChatHubAuth('https://chatapi.smsbat.com');
// Get company token
$companyToken = $auth->getCompanyToken('login', 'password');
// Get operator token
$operatorToken = $auth->getOperatorToken(123, 30);
// Validate token
$validation = $auth->validateToken($operatorToken);
echo "Token valid: " . ($validation['isValid'] ? 'Yes' : 'No');
އެންމެ ރަނގަޅު އުސޫލުތައް
ޓޯކަން ސްޓޯރޭޖް
- ✅ ޓޯކަންތައް ރައްކާތެރިކަމާއެކު ރައްކާކުރުން (އެންކްރިޕްޓް ކުރެވިފައިވާ ޑޭޓާބޭސް، ސިއްރުތައް މެނޭޖަރ)
- ✅ ދުވަހަކުވެސް ޓޯކަންސް ވަރޝަން ކޮންޓްރޯލަށް ކޮމިޓް ނުކުރުން
- ✅ ކްރެޑިޝަންތަކަށް އެންވަޔަރަންމަންޓް ވެރިއޭބަލްސް ބޭނުންކުރުން
- ❌ ޓޯކަންތައް ސާދާ ލިޔުމެއްގައި ރައްކާ ނުކުރުން
- ❌ ކްލައިންޓް ސައިޑް ކޯޑުގައި ޓޯކަންތައް ހާމަ ނުކުރުން
ޓޯކަން ރޮޓޭޝަން
- ކުންފުނީގެ ޓޯކަންތައް ވަކިވަކިން (ކޮންމެ 3-6 މަހަކުން) އެނބުރުވުން
- އޮޕަރޭޓަރ ޓޯކަންތަކަށް އެކަށީގެންވާ މުއްދަތު ހަމަވުން (7-30 ދުވަސް) ކަނޑައެޅުން
- މުއްދަތު ހަމަވުމުގެ ކުރިން އޮޓޮމެޓިކް ޓޯކަން ރިފްރެޝް ތަންފީޒުކުރުން
- އޮޕަރޭޓަރުން ފުރާއިރު ޓޯކަން ބާތިލްކުރުން
އެރަރ ހެންޑްލިންގ
async function authenticateWithRetry(login, password, retries = 3) {
for (let i = 0; i < retries; i++) {
try {
return await getCompanyToken(login, password);
} catch (error) {
if (error.response?.status === 401) {
throw new Error('Invalid credentials');
}
if (i === retries - 1) throw error;
// Wait before retry
await new Promise(resolve =>
setTimeout(resolve, Math.pow(2, i) * 1000)
);
}
}
}
ޓޯކަން ވެލިޑޭޝަން
ކްރިޓިކަލް އޮޕަރޭޝަންތަކުގެ ކުރިން އަބަދުވެސް ޓޯކަންތައް ސައްހަކުރުން:
async function performSecureOperation(token, operation) {
// Validate token first
const validation = await validateToken(token);
if (!validation.isValid) {
throw new Error('Token expired or invalid');
}
// Proceed with operation
return await operation();
}
ސަލާމަތަށް ވިސްނަންޖެހޭ ކަންތައްތައް
އެޗްޓީޓީޕީއެސް އެކަނި
އޮތެންޓިކޭޝަން ރިކުއެސްޓް ފޮނުވާއިރު އަބަދުވެސް އެޗްޓީޓީޕީއެސް ބޭނުންކުރުން:
// ✅ Correct
const baseUrl = 'https://chatapi.smsbat.com';
// ❌ Wrong - never use HTTP for authentication
const baseUrl = 'http://api.chathub.smsbat.com';
ޓޯކަން ސްކޯޕް
ކޮންމެ އޮޕަރޭޝަނަކަށް އެކަށީގެންވާ ޓޯކަން ބޭނުންކުރުން:
- ކުންފުނީ ޓޯކަން: އޯގަނައިޒޭޝަން މެނޭޖްމަންޓް، އޮޕަރޭޓަރ އުފެއްދުން
- އޮޕަރޭޓަރ ޓޯކަން: ޗެޓް އޮޕަރޭޝަންސް، މެސެޖް ހެންޑްލިންގ
ރޭޓް ލިމިޓްކުރުން
އޮތެންޓިކޭޝަން ރިކުއެސްޓްތަކަށް ރޭޓް ލިމިޓިންގ ތަންފީޒުކުރުން:
class RateLimitedAuth {
constructor() {
this.lastRequest = 0;
this.minInterval = 1000; // 1 second between requests
}
async getToken(login, password) {
const now = Date.now();
const timeSinceLastRequest = now - this.lastRequest;
if (timeSinceLastRequest < this.minInterval) {
await new Promise(resolve =>
setTimeout(resolve, this.minInterval - timeSinceLastRequest)
);
}
this.lastRequest = Date.now();
return await makeAuthRequest(login, password);
}
}
މައްސަލަ ހައްލުކުރުން
401 ހުއްދަ ނެތް
- ކްރެޑެންޝިއަލްސް ރަނގަޅުކަން ޔަގީންކުރުން
- ޗެކް ޓޯކަން މުއްދަތު ހަމަނުވާކަން
- ރިކުއެސްޓް ހެޑަރސްގައި ޓޯކަން ހިމެނޭކަން ޔަޤީންކުރުން
- ޓޯކަން ފޯމެޓް ސައްހަކުރުން
403 ޙަރާމް
- ޓޯކަންގައި ބޭނުންވާ ހުއްދަތައް ހުރިކަން ޔަގީންކުރުން
- ރަނގަޅު ޓޯކަން ޓައިޕް ބޭނުންކުރާތޯ ބެލުން (ކުންފުނި އާއި އޮޕަރޭޓަރ)
- ޓޯކަން ބާތިލްކޮށްފައި ނުވާކަން ޔަޤީންކުރުން
ޓޯކަން މުއްދަތު ހަމަވެއްޖެ
- އާ ޓޯކަން ހޯދުމަށް އެދުމެވެ
- އޮޓޮމެޓިކް ޓޯކަން ރިފްރެޝް ތަންފީޒުކުރުން
- މުއްދަތު ހަމަވާންޖެހޭ އެކަށީގެންވާ ވަގުތުތައް ކަނޑައެޅުން
ދެން އަޅަންޖެހޭ ފިޔަވަޅުތައް
- އޮގަނައިޒޭޝަންތައް - އޯގަނައިޒޭޝަންތައް ބެލެހެއްޓުން
- އޮޕަރޭޓަރުން - އޮޕަރޭޓަރުންނާއެކު މަސައްކަތްކުރުން
- ވިޖެޓް އިންޓަގްރޭޝަން - ޗެޓް ވިޖެޓް އިންޓަގްރޭޓްކުރުން