🛡️ Authly SSO
Loyiha va xizmatlar uchun yagona Authly SSO (Single Sign-On / OAuth 2.0) integratsiya kutubxonasi.
✨ Imkoniyatlar
- Backend va Frontend domenlari ajratilgan:
AUTHLY_BACKEND_URL— Token olish, profil yuklash va barcha server-to-server API so'rovlari.AUTHLY_FRONTEND_URL— Foydalanuvchini brauzerda SSO login sahifasiga yo'naltirish havolasini yasash.
- Django & DRF tayyor integratsiyasi — 4 ta tayyor endpoint:
login-url/— Authly SSO ga yo'naltiruvchi login havolasini olish.callback/— OAuth2 code orqali tizimga kirish va tokenlar qaytarish.webhook/— Authly SSO dan avtomatik foydalanuvchi ma'lumotlarini yangilash.sync/— Foydalanuvchi profilini qo'lda Authly bilan sinxronlashtirish.
- Dinamik
redirect_uri— Har bir so'rovdaredirect_urini so'rov orqali uzatish mumkin (settings.py da ham belgilash mumkin). - To'liq ma'lumotlar sinxronizatsiyasi —
authly_id,first_name,last_name,email,phone/phone_number,avatar,birth_dateavtomatik to'ldiriladi. - HMAC SHA-256 Webhook xavfsizligi —
X-SSO-Signatureimzosini avtomatik tekshirish. - Avtomatik JWT —
djangorestframework-simplejwtmavjud bo'lsaaccessvarefreshtokenlar generatsiya qilinadi. - 500 xatoliksiz — Barcha xatoliklar (
AuthlySDKError,IntegrityError,DatabaseError,PermissionDeniedva h.k.) DRFResponsesifatida qaytariladi.
📦 O'rnatish
# Faqat Python (FastAPI, Flask, skriptlar uchun):
pip install authly-sso
# Django / DRF bilan:
pip install "authly-sso[django]"
⚙️ Sozlash
settings.py ga quyidagilarni qo'shing:
AUTHLY_BACKEND_URL = "https://api.authly.uz"
AUTHLY_FRONTEND_URL = "https://authly.uz"
AUTHLY_CLIENT_ID = "sizning-client-id"
AUTHLY_CLIENT_SECRET = "sizning-client-secret"
# Ixtiyoriy — frontend har so'rovda dinamik yuborsa kerak emas:
AUTHLY_REDIRECT_URI = "https://saytingiz.uz/api/v1/auth/authly/callback/"
# Webhook xavfsizligi uchun:
AUTHLY_WEBHOOK_SECRET = "sizning-webhook-secret"
# Ixtiyoriy (standart: 10 soniya):
AUTHLY_TIMEOUT = 10
🛠 Django / DRF da ishlatish
1. urls.py ga uling
from django.urls import path, include
urlpatterns = [
path("api/v1/auth/authly/", include("authly.urls")),
]
4 ta endpoint faollashadi:
| Endpoint | Method | Tavsif |
|---|---|---|
login-url/ |
GET |
Authly SSO login havolasini qaytaradi |
callback/ |
GET, POST |
OAuth2 code → foydalanuvchi & tokenlar |
webhook/ |
POST |
Authly dan avtomatik yangilash |
sync/ |
GET, POST |
Foydalanuvchi profilini qo'lda yangilash |
2. Login havolasini olish
GET /api/v1/auth/authly/login-url/?redirect_uri=https://saytingiz.uz/callback
Parametrlar (query string):
| Parametr | Majburiy | Tavsif |
|---|---|---|
redirect_uri |
Ha* | Callback URL. AUTHLY_REDIRECT_URI settings da belgilangan bo'lsa ixtiyoriy. |
scope |
Yo'q | Ruxsatlar doirasi (standart: read) |
state |
Yo'q | CSRF himoyasi yoki holat parametri |
Javob:
{
"data": {
"url": "https://authly.uz/login?client_id=...&redirect_uri=...&scope=read"
},
"error": null,
"success": true
}
3. Callback (tizimga kirish)
GET /api/v1/auth/authly/callback/?code=<CODE>&redirect_uri=<URI>
POST /api/v1/auth/authly/callback/
Content-Type: application/json
{"code": "...", "redirect_uri": "..."}
Parametrlar:
| Parametr | Tavsif |
|---|---|
code |
Authly SSO dan kelgan bir martalik kod |
redirect_uri |
Login havolasi yasashda ishlatilgan xuddi shu URL |
code_verifier |
PKCE ishlatilsa (ixtiyoriy) |
Muvaffaqiyatli javob:
{
"data": {
"is_new_user": false,
"user": {
"id": 1,
"authly_id": "c1f7a0b3-1234-4567-89ab-cdef01234567",
"username": "authly_c1f7a0b3-1234-4567-89ab-cdef01234567",
"full_name": "Ism Familiya",
"first_name": "Ism",
"last_name": "Familiya",
"email": "user@example.com",
"phone": "+998901234567",
"avatar": "https://api.authly.uz/media/users/avatar/2026/10/avatar.jpg",
"birth_date": "1995-05-15"
},
"access": "<LOCAL_JWT_ACCESS_TOKEN>",
"refresh": "<LOCAL_JWT_REFRESH_TOKEN>",
"authly_access_token": "<AUTHLY_OAUTH_ACCESS_TOKEN>"
},
"error": null,
"success": true
}
Muhim:
authly_access_token— bu Authly serveriga murojaat qilish uchun OAuth tokeni.accessvarefreshesa loyihangiz (lokal) JWT tokenlari. Frontendauthly_access_tokenni saqlashi va/sync/endpointiga shu tokenni yuborishi mumkin.
4. Profil sinxronizatsiyasi (Manual Sync)
Foydalanuvchi Authly portalida o'z ma'lumotlarini o'zgartirib qaytganda profilini yangilash:
POST /api/v1/auth/authly/sync/
Authorization: Bearer <LOCAL_JWT>
Content-Type: application/json
{"access_token": "<AUTHLY_OAUTH_ACCESS_TOKEN>"}
⚠️
access_tokensifatida lokal JWT emas, callback da kelganauthly_access_tokenyuboriladi.
Muvaffaqiyatli javob:
{
"data": {
"status": "success",
"updated_fields": ["first_name", "avatar"],
"user": {
"id": 1,
"authly_id": "c1f7a0b3-...",
"first_name": "Yangi Ism",
"last_name": "Familiya",
"email": "user@example.com",
"phone": "+998901234567",
"avatar": "https://api.authly.uz/media/users/avatar/2026/10/new_avatar.jpg",
"birth_date": "1995-05-15",
"region_info": { "id": "...", "name": "Toshkent shahri" },
"district_info": { "id": "...", "name": "Yunusobod tumani" }
}
},
"error": null,
"success": true
}
5. Webhook (Avtomatik sinxronizatsiya)
Authly SSO foydalanuvchi ma'lumotlari o'zgarganda webhook yuboradi. SDK:
X-SSO-Signature(HMAC SHA-256) imzosini tekshiradi.- Foydalanuvchini
authly_id→phone_number→emailketma-ketligida topadi. - Barcha o'zgargan maydonlarni avtomatik yangilaydi.
POST /api/v1/auth/authly/webhook/
X-SSO-Signature: sha256=<HMAC>
Content-Type: application/json
{
"event": "user.updated",
"timestamp": "2026-10-31T12:00:00.000Z",
"user": {
"id": "c1f7a0b3-...",
"authly_id": "c1f7a0b3-...",
"first_name": "Alisher",
"last_name": "Valiyev",
"phone": "+998901234567",
"email": "alisher@example.com",
"avatar": "https://api.authly.uz/media/users/avatar/2026/10/new_avatar.jpg",
"birth_date": "1998-05-15",
"region": { "id": "...", "name": "Toshkent shahri" },
"district": { "id": "...", "name": "Yunusobod tumani" }
}
}
🧩 Maxsus moslashtirish (Override)
Viloyat/Tuman bog'lash va bloklash tekshiruvi
from authly.views import AuthlyCallbackAPIView, AuthlyWebhookAPIView
from rest_framework.exceptions import PermissionDenied
from apps.users.models import Region, District
class CustomAuthlyCallbackView(AuthlyCallbackAPIView):
def get_or_create_user(self, user_data: dict):
user, created = super().get_or_create_user(user_data)
region_data = user_data.get("region_info") or user_data.get("region")
district_data = user_data.get("district_info") or user_data.get("district")
if region_data and hasattr(user, "region"):
user.region = Region.objects.filter(
name__iexact=region_data.get("name")
).first()
if district_data and hasattr(user, "district"):
user.district = District.objects.filter(
name__iexact=district_data.get("name")
).first()
user.save()
if getattr(user, "is_blocked", False):
raise PermissionDenied("Hisobingiz administrator tomonidan bloklangan.")
return user, created
class CustomAuthlyWebhookView(AuthlyWebhookAPIView):
def sync_user(self, user_data: dict):
user = super().sync_user(user_data)
# Qo'shimcha logika...
return user
Javob formatini o'zgartirish
class CustomAuthlyCallbackView(AuthlyCallbackAPIView):
def get_response_data(self, user, user_data, created, tokens, authly_access_token=None):
data = super().get_response_data(user, user_data, created, tokens, authly_access_token)
data["my_custom_field"] = "qiymat"
return data
🐍 Sof Python / FastAPI / Flask da ishlatish
from authly import AuthlyClient, AuthlySDKError
client = AuthlyClient(
backend_url="https://api.authly.uz",
frontend_url="https://authly.uz",
client_id="sizning-client-id",
client_secret="sizning-client-secret",
redirect_uri="https://saytingiz.uz/callback",
webhook_secret="sizning-webhook-secret",
)
# 1. Login havolasi:
url = client.get_authorization_url(scope="read", state="csrf_token")
# 2. Code → profil + authly access_token:
try:
profile, authly_access_token = client.authenticate_code(
code="authorization_code_here",
redirect_uri="https://saytingiz.uz/callback",
)
print(profile) # {"authly_id": "...", "first_name": "...", ...}
print(authly_access_token) # "eyJ..." — Authly OAuth tokeni
except AuthlySDKError as e:
print(f"Xatolik: {e}")
# 3. access_token bilan profil olish:
profile = client.get_user_profile(access_token=authly_access_token)
# 4. Webhook imzosini tekshirish:
is_valid = client.verify_webhook_signature(
payload_bytes=raw_body,
signature=request.headers.get("X-SSO-Signature"),
)
⚠️ Xatoliklar (Exceptions)
| Sinf | Sababi |
|---|---|
AuthlySDKError |
Barcha SDK xatoliklari uchun asosiy sinf |
AuthlyTokenExchangeError |
Kodni tokenga almashtirishdagi xatolik |
AuthlyProfileFetchError |
Profil olishdagi xatolik |
AuthlyWebhookSignatureError |
Webhook imzosi xatoligi |
📋 Jarayon diagrammasi
Frontend Backend (SDK) Authly Server
| | |
|-- GET /login-url/ ---> | |
| |-- get_authorization_url()
|<-- {"url": "https://authly.uz/login?..."} ----|
| | |
|-- redirect to Authly login page -------------> |
|<-- redirect back with ?code=<CODE> ----------- |
| | |
|-- POST /callback/ ---> | |
| {code, redirect_uri} |-- exchange_code_for_token() --> Authly
| |<-- {access_token, ...} <------- Authly
| |-- get_user_profile(access_token) -> Authly
| |<-- {authly_id, phone, ...} <--- Authly
| |-- find_or_create_user()
| |-- generate_jwt_tokens()
|<-- {user, access, refresh, authly_access_token}
📄 Litsenziya
MIT litsenziyasi ostida tarqatiladi. Savollar va takliflar uchun Authly jamoasiga murojaat qiling.
Metadata
Release files for authly-sso 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| authly_sso-0.4.0.tar.gz | 18.7 kB | Without provenance | Details for authly_sso-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| authly_sso-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for authly_sso-0.4.0-py3-none-any.whl |
Total release size: 34.9 kB
Release files / authly_sso-0.4.0.tar.gz
| Download URL | authly_sso-0.4.0.tar.gz |
|---|---|
| Size | 18.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
702db2d92e3a9b791532122055e671ed40e3c14b6e858d11230f1375c1fdc09f
|
|
BLAKE2b-256 checksum How to use checksums |
a3503c9c6f9573a6d5399659b7bf01dcf9ea5d1e84aeb1a717252653758e7ebc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / authly_sso-0.4.0-py3-none-any.whl
| Download URL | authly_sso-0.4.0-py3-none-any.whl |
|---|---|
| Size | 16.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5ba189e0e8e5b888f0ab3dfb6ad9fe23f881cd5ccfaa7cb8565ee7c242f9ca86
|
|
BLAKE2b-256 checksum How to use checksums |
e6c4da5708b4d85c8c201968cde7912b4789471728b7148672014b106b2f526f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for authly-sso 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.