OAuth 2.0 SSO

Tích hợp Single Sign-On với Google, Microsoft, Keycloak, Auth0, Okta — tự động tạo tài khoản và phân quyền qua identity provider

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ợ

ProviderLoạiGhi chú
GoogleBuilt-inĐơn giản nhất
Auth0CustomSaaS identity platform
KeycloakCustomSelf-hosted, enterprise
Microsoft Azure ADCustomMicrosoft 365
OktaCustomEnterprise SSO
FacebookCustomSocial login
GitHubCustomDeveloper-friendly
AppleCustomiOS ecosystem

Cấu hình: Google OAuth

Bước 1: Tạo OAuth Client trên Google Cloud

  1. Vào Google Cloud ConsoleAPIs & Services > Credentials
  2. Create Credentials > OAuth client ID
  3. Application type: Web application
  4. Authorized redirect URIs:
    https://your-domain.com/login/oauth2/code/google
  5. Lưu Client IDClient 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
DOMAINLấy domain của email làm tenant name (user@acme.com → tenant acme.com)
EMAILDùng toàn bộ email làm tenant name
CUSTOMDù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