Skip to content

مصادقة SSO وOAuth

تهيئة Google OAuth وGitHub OAuth وOIDC العام للمصادقة الآمنة في الإنتاج.

مصادقة SSO وOAuth

جديد في الإصدار v2.3.0

بشكل افتراضي، يستخدم Potato تسجيل دخول بسيط يعتمد على اسم المستخدم حيث يكتب المعلقون أي اسم مستخدم لتسجيل الدخول. هذا مناسب للتطوير المحلي والمشاريع الصغيرة، لكن عمليات النشر الإنتاجية تحتاج إلى مصادقة مناسبة لمنع الوصول غير المصرح به وضمان هوية المعلق والامتثال للمتطلبات المؤسسية.

يضيف Potato 2.3 دعماً لثلاث طرق مصادقة:

  1. Google OAuth -- تسجيل الدخول بحسابات Google، مع تقييد اختياري للنطاق
  2. GitHub OAuth -- تسجيل الدخول بحسابات GitHub، مع تقييد اختياري للمنظمة
  3. Generic OIDC -- الاتصال بأي مزود OpenID Connect (مثل Okta وAzure AD وAuth0 وKeycloak وغيرها)

يمكن دمج جميع الطرق مع تسجيل الدخول الحالي باسم المستخدم في Potato لإعداد الوضع المختلط.

Google OAuth

المتطلبات الأساسية

  1. أنشئ مشروعاً في Google Cloud Console
  2. فعّل واجهة برمجة تطبيقات "Google Identity"
  3. أنشئ بيانات اعتماد OAuth 2.0 (نوع تطبيق الويب)
  4. أضف عنوان URL لخادم Potato إلى "Authorized redirect URIs": https://your-server.com/auth/google/callback

الإعداد

yaml
authentication:
  method: google_oauth
 
  google_oauth:
    client_id: ${GOOGLE_CLIENT_ID}
    client_secret: ${GOOGLE_CLIENT_SECRET}
    redirect_uri: "https://your-server.com/auth/google/callback"
 
    # Optional: restrict to specific domain(s)
    allowed_domains:
      - "umich.edu"
      - "research-lab.org"
 
    # Optional: auto-register new users on first login
    auto_register: true
 
    # Optional: map Google profile fields to Potato user fields
    field_mapping:
      username: email            # use email as Potato username
      display_name: name         # show Google display name

تقييد النطاق

عند تعيين allowed_domains، يمكن فقط للمستخدمين الذين لديهم عناوين بريد إلكتروني من تلك النطاقات تسجيل الدخول. يرى الآخرون رسالة خطأ:

yaml
authentication:
  method: google_oauth
  google_oauth:
    client_id: ${GOOGLE_CLIENT_ID}
    client_secret: ${GOOGLE_CLIENT_SECRET}
    allowed_domains:
      - "umich.edu"
    domain_error_message: "This annotation task is restricted to University of Michigan accounts."

GitHub OAuth

المتطلبات الأساسية

  1. انتقل إلى GitHub Settings > Developer settings > OAuth Apps > New OAuth App
  2. عيّن "Authorization callback URL" إلى https://your-server.com/auth/github/callback
  3. سجّل Client ID الخاص بك وأنشئ Client Secret

الإعداد

yaml
authentication:
  method: github_oauth
 
  github_oauth:
    client_id: ${GITHUB_CLIENT_ID}
    client_secret: ${GITHUB_CLIENT_SECRET}
    redirect_uri: "https://your-server.com/auth/github/callback"
 
    # Optional: restrict to members of specific GitHub organizations
    allowed_organizations:
      - "my-research-lab"
      - "university-nlp-group"
 
    # Optional: restrict to specific teams within an organization
    allowed_teams:
      - "my-research-lab/annotators"
 
    # Scopes to request
    scopes:
      - "read:user"
      - "read:org"             # needed for organization restriction
 
    auto_register: true
 
    field_mapping:
      username: login            # GitHub username
      display_name: name

تقييد المنظمة

يمكن لمصادقة GitHub OAuth تقييد الوصول لأعضاء منظمات محددة. يتطلب هذا نطاق read:org:

yaml
authentication:
  method: github_oauth
  github_oauth:
    client_id: ${GITHUB_CLIENT_ID}
    client_secret: ${GITHUB_CLIENT_SECRET}
    allowed_organizations:
      - "my-research-lab"
    scopes:
      - "read:user"
      - "read:org"
    org_error_message: "You must be a member of the my-research-lab GitHub organization."

Generic OIDC

لمزودي SSO المؤسسية (مثل Okta وAzure AD وAuth0 وKeycloak وغيرها)، استخدم تكامل OpenID Connect العام.

الإعداد

yaml
authentication:
  method: oidc
 
  oidc:
    # Discovery URL (provider's .well-known endpoint)
    discovery_url: "https://accounts.example.com/.well-known/openid-configuration"
 
    # Or specify endpoints manually
    # authorization_endpoint: "https://accounts.example.com/authorize"
    # token_endpoint: "https://accounts.example.com/token"
    # userinfo_endpoint: "https://accounts.example.com/userinfo"
    # jwks_uri: "https://accounts.example.com/.well-known/jwks.json"
 
    client_id: ${OIDC_CLIENT_ID}
    client_secret: ${OIDC_CLIENT_SECRET}
    redirect_uri: "https://your-server.com/auth/oidc/callback"
 
    scopes:
      - "openid"
      - "profile"
      - "email"
 
    auto_register: true
 
    field_mapping:
      username: preferred_username
      display_name: name
      email: email

مثال Azure AD

yaml
authentication:
  method: oidc
 
  oidc:
    discovery_url: "https://login.microsoftonline.com/${AZURE_TENANT_ID}/v2.0/.well-known/openid-configuration"
    client_id: ${AZURE_CLIENT_ID}
    client_secret: ${AZURE_CLIENT_SECRET}
    redirect_uri: "https://your-server.com/auth/oidc/callback"
    scopes:
      - "openid"
      - "profile"
      - "email"
    auto_register: true
    field_mapping:
      username: preferred_username
      display_name: name

مثال Okta

yaml
authentication:
  method: oidc
 
  oidc:
    discovery_url: "https://your-org.okta.com/.well-known/openid-configuration"
    client_id: ${OKTA_CLIENT_ID}
    client_secret: ${OKTA_CLIENT_SECRET}
    redirect_uri: "https://your-server.com/auth/oidc/callback"
    scopes:
      - "openid"
      - "profile"
      - "email"
      - "groups"               # request group membership
    auto_register: true
 
    # Restrict to specific groups
    allowed_groups:
      - "annotation-team"
 
    field_mapping:
      username: preferred_username
      display_name: name
      groups: groups

مثال Keycloak

yaml
authentication:
  method: oidc
 
  oidc:
    discovery_url: "https://keycloak.example.com/realms/annotation/.well-known/openid-configuration"
    client_id: ${KEYCLOAK_CLIENT_ID}
    client_secret: ${KEYCLOAK_CLIENT_SECRET}
    redirect_uri: "https://your-server.com/auth/oidc/callback"
    scopes:
      - "openid"
      - "profile"
      - "email"
    auto_register: true
    field_mapping:
      username: preferred_username
      display_name: name

تقييد النطاق

تدعم جميع طرق OAuth تقييد النطاق بناءً على عناوين البريد الإلكتروني. هذا مفيد عندما تريد السماح بأي حساب من مؤسسة معينة:

yaml
authentication:
  method: google_oauth          # or github_oauth, oidc
 
  domain_restriction:
    enabled: true
    allowed_domains:
      - "umich.edu"
      - "stanford.edu"
    error_message: "Access is restricted to university accounts."

بالنسبة لمزودي OIDC الذين يتضمنون مطالبة email، يعمل تقييد النطاق تلقائياً. بالنسبة للمزودين الذين لا يتضمنون البريد الإلكتروني، قد تحتاج إلى طلب نطاق email بشكل صريح.

التسجيل التلقائي

بشكل افتراضي، يجب تسجيل المستخدمين مسبقاً في Potato قبل أن يتمكنوا من التعليق التوضيحي. مع auto_register: true، يتم إنشاء أي مستخدم يصادق بنجاح تلقائياً في Potato عند تسجيل الدخول الأول.

yaml
authentication:
  auto_register: true
  auto_register_role: annotator   # annotator or admin

للموافقة المسبقة على مستخدمين محددين مع حظر الآخرين:

yaml
authentication:
  auto_register: false
  allowed_users:
    - "researcher@umich.edu"
    - "student1@umich.edu"
    - "student2@umich.edu"
  user_not_found_message: "Your account has not been approved. Contact the project administrator."

الوضع المختلط

يمكنك تمكين طرق مصادقة متعددة في وقت واحد. تعرض صفحة تسجيل الدخول أزراراً لكل طريقة ممكّنة بالإضافة إلى حقل اسم مستخدم اختياري.

yaml
authentication:
  methods:
    - google_oauth
    - github_oauth
    - username                   # keep simple username login as fallback
 
  google_oauth:
    client_id: ${GOOGLE_CLIENT_ID}
    client_secret: ${GOOGLE_CLIENT_SECRET}
    redirect_uri: "https://your-server.com/auth/google/callback"
 
  github_oauth:
    client_id: ${GITHUB_CLIENT_ID}
    client_secret: ${GITHUB_CLIENT_SECRET}
    redirect_uri: "https://your-server.com/auth/github/callback"
 
  # Username login settings
  username:
    enabled: true
    require_password: true       # require a password for username login
    password_file: "auth/passwords.yaml"

الوضع المختلط مفيد أثناء الترحيل: قم بتمكين OAuth إلى جانب تسجيل الدخول باسم المستخدم، ثم قم بتعطيل تسجيل الدخول باسم المستخدم بمجرد أن يربط جميع المعلقين حسابات OAuth الخاصة بهم.

إدارة الجلسات

إعداد سلوك الجلسة للمستخدمين المصادق عليهم:

yaml
authentication:
  session:
    lifetime_hours: 24           # session duration
    refresh: true                # refresh session on activity
    cookie_secure: true          # require HTTPS for cookies
    cookie_samesite: "Lax"       # SameSite cookie attribute

مصادقة المسؤول

يمكن لحسابات المسؤول استخدام نفس تدفق OAuth أو طريقة مصادقة منفصلة:

yaml
authentication:
  method: google_oauth
  google_oauth:
    client_id: ${GOOGLE_CLIENT_ID}
    client_secret: ${GOOGLE_CLIENT_SECRET}
 
  admin:
    # Admins must match these emails
    admin_emails:
      - "pi@umich.edu"
      - "lead-ra@umich.edu"
 
    # Or use a separate API key for admin access
    api_key: ${ADMIN_API_KEY}

متطلبات HTTPS

يتطلب مزودو OAuth استخدام HTTPS لعناوين URI لإعادة التوجيه في الإنتاج. للتطوير المحلي، يمكنك استخدام HTTP مع localhost:

yaml
# Development (HTTP allowed)
authentication:
  google_oauth:
    redirect_uri: "http://localhost:8000/auth/google/callback"
 
# Production (HTTPS required)
authentication:
  google_oauth:
    redirect_uri: "https://annotation.example.com/auth/google/callback"

لعمليات النشر الإنتاجية خلف وكيل عكسي (nginx أو Caddy)، تأكد من أن الوكيل يمرر ترويسة X-Forwarded-Proto الصحيحة:

nginx
# nginx example
location / {
    proxy_pass http://localhost:8000;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
}

مثال كامل

إعداد مصادقة إنتاجية كامل مع Google OAuth وتقييد النطاق ومفتاح API للمسؤول:

yaml
task_name: "Annotation Project"
task_dir: "."
 
authentication:
  method: google_oauth
 
  google_oauth:
    client_id: ${GOOGLE_CLIENT_ID}
    client_secret: ${GOOGLE_CLIENT_SECRET}
    redirect_uri: "https://annotation.umich.edu/auth/google/callback"
    allowed_domains:
      - "umich.edu"
    auto_register: true
    field_mapping:
      username: email
      display_name: name
 
  session:
    lifetime_hours: 48
    cookie_secure: true
 
  admin:
    admin_emails:
      - "pi@umich.edu"
    api_key: ${ADMIN_API_KEY}
 
data_files:
  - "data/instances.jsonl"
 
annotation_schemes:
  - annotation_type: radio
    name: sentiment
    labels: [Positive, Neutral, Negative]
 
output_annotation_dir: "output/"
output_annotation_format: "jsonl"

قراءة إضافية

لمزيد من تفاصيل التنفيذ، راجع التوثيق المصدري.