OAuth 2.0 / SSO
VieLang IoT hỗ trợ OAuth 2.0 để đăng nhập qua identity provider (Google, Microsoft Azure AD, Auth0, Keycloak, Okta, Facebook, GitHub, Apple). Người dùng có thể đăng nhập bằng tài khoản tổ chức mà không cần tạo mật khẩu riêng.
Provider được hỗ trợ
| Provider | Loại | Ghi chú |
|---|---|---|
| Built-in | Đơn giản nhất | |
| Auth0 | Custom | SaaS identity platform |
| Keycloak | Custom | Self-hosted, enterprise |
| Microsoft Azure AD | Custom | Microsoft 365 |
| Okta | Custom | Enterprise SSO |
| Custom | Social login | |
| GitHub | Custom | Developer-friendly |
| Apple | Custom | iOS ecosystem |
Cấu hình: Google OAuth
Bước 1: Tạo OAuth Client trên Google Cloud
- Vào Google Cloud Console → APIs & Services > Credentials
- Create Credentials > OAuth client ID
- Application type: Web application
- Authorized redirect URIs:
https://your-domain.com/login/oauth2/code/google - Lưu Client ID và Client Secret
Bước 2: Cấu hình Domain
System Settings > Security > OAuth 2.0 > Domains tab:
Domain: your-domain.com
Bước 3: Tạo OAuth Client
Title: Google
Provider: Google
Client ID: xxxx.apps.googleusercontent.com
Client Secret: your-secret
Scope: openid email profile
Mapper Configuration:
Tenant name strategy: CUSTOM
Tenant name pattern: %{email} ← mỗi email = một tenant
(hoặc DOMAIN để dùng domain email làm tenant)
Allow user creation: ✓
Cấu hình: Microsoft Azure AD
Endpoint configuration:
Authorization URI: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize
Access Token URI: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
User Info URI: https://graph.microsoft.com/oidc/userinfo
JWK Set URI: https://login.microsoftonline.com/{tenant}/discovery/v2.0/keys
Scope: openid email profile
Cấu hình: Keycloak
Authorization URI: https://keycloak.example.com/auth/realms/{realm}/protocol/openid-connect/auth
Access Token URI: https://keycloak.example.com/auth/realms/{realm}/protocol/openid-connect/token
User Info URI: https://keycloak.example.com/auth/realms/{realm}/protocol/openid-connect/userinfo
JWK Set URI: https://keycloak.example.com/auth/realms/{realm}/protocol/openid-connect/certs
Client Auth Method: POST (client credentials trong body)
Scope: openid email profile roles
Tenant Provisioning Strategies
| Strategy | Ý nghĩa |
|---|---|
DOMAIN | Lấy domain của email làm tenant name (user@acme.com → tenant acme.com) |
EMAIL | Dùng toàn bộ email làm tenant name |
CUSTOM | Dùng pattern tùy chỉnh: %{email}, %{sub}, %{given_name} |
Custom pattern examples:
%{email} → user@acme.com
%{hd} → acme.com (hosted domain từ Google)
Company_%{given_name} → Company_John
Customer Provisioning
Ngoài Tenant, có thể tự động gán user vào Customer:
Customer name: ${%{email}}
Create new customer if not exists: ✓
Luồng đăng nhập OAuth
1. User click "Login with Google"
2. Redirect → Google login page
3. Google trả về authorization code
4. VieLang exchange code lấy access token
5. VieLang gọi User Info endpoint
6. Lấy email, tên từ user info
7. Map → Tenant + User theo cấu hình
8. Tự tạo Tenant/User nếu chưa có
9. User đăng nhập thành công
Custom Mapper
Dùng custom mapper khi cần logic phức tạp (role mapping, conditional tenant assignment):
Custom mapper URL: https://my-server.com/oauth2/mapper
VieLang gọi POST đến URL này với user info, nhận về:
{
"tenantName": "acme",
"customerName": "Engineering Team",
"email": "john@acme.com",
"firstName": "John",
"lastName": "Doe"
}Bảo mật
- Redirect URI phải khớp chính xác với cấu hình trên provider
- Không bao giờ expose Client Secret
- Dùng HTTPS cho production
- Cân nhắc giới hạn domain được phép đăng nhập