GeoNode社交帐户

允许GeoNode登录社交帐户(Facebook和LinkedIn)

基本概念和对象

为了协调本地账号和远程社交账号之间的各种认证流程,对整个用户注册和认证代码库进行了重构。

主要变化:

  • geonode-user-accounts 已经退役,不再使用了。此应用程序只能管理本地帐户;

  • django-allauth 已集成为地理节点的依赖项。它为管理本地和远程用户帐户提供了解决方案。它现在用于处理大多数用户注册和认证流;

  • django-invitations 还集成为geonode的依赖项,用于管理对新用户的邀请。此功能以前由geonode用户帐户提供;

  • django-allauth 已进行扩展,以提供以下附加功能:

    • 当电子邮件用于连接到社交账户时,自动向用户注册电子邮件;

    • 自动从用户的社交账户中提取信息,并使用这些信息来增强用户在geonode上的个人资料字段。这是以可插拔的方式实现的,允许自定义安装将其配置为其他提供程序;

    • 允许新用户登录之前,允许员工批准新注册。此功能以前由geonode-user-account提供。

  • 现在,用户配置文件中有额外的部分用于管理连接的社交帐户和电子邮件帐户

    ../../_images/001_screenshot.png
  • 正确配置后,登录和注册页面现在可以使用社交帐户登录

    ../../_images/002_screenshot.png

安装

  • 安装新的 allauth 插件并删除任何旧的依赖项

    pip install -r requirements.txt --upgrade
    pip install -e . --upgrade --no-cache
    pip uninstall geonode-user-accounts -y
    pip uninstall django-user-accounts -y
    
  • 确保Django模型已更新,并且模板已更新到 static 文件夹

    DJANGO_SETTINGS_MODULE=geonode.local_settings python -W ignore manage.py makemigrations
    DJANGO_SETTINGS_MODULE=geonode.local_settings python -W ignore manage.py migrate
    DJANGO_SETTINGS_MODULE=geonode.local_settings python -W ignore manage.py collectstatic --noinput
    
  • 确保在中启用了社交提供商 settings

    # prevent signing up by default
    ACCOUNT_OPEN_SIGNUP = True
    ACCOUNT_EMAIL_REQUIRED = True
    ACCOUNT_EMAIL_VERIFICATION = 'optional'
    ACCOUNT_EMAIL_CONFIRMATION_EMAIL = True
    ACCOUNT_EMAIL_CONFIRMATION_REQUIRED = True
    ACCOUNT_CONFIRM_EMAIL_ON_GET = True
    ACCOUNT_APPROVAL_REQUIRED = True
    
    SOCIALACCOUNT_ADAPTER = 'geonode.people.adapters.SocialAccountAdapter'
    
    SOCIALACCOUNT_AUTO_SIGNUP = False
    
    INSTALLED_APPS += (
        'allauth.socialaccount.providers.linkedin_oauth2',
        'allauth.socialaccount.providers.facebook',
    )
    
    SOCIALACCOUNT_PROVIDERS = {
        'linkedin_oauth2': {
            'SCOPE': [
                'r_emailaddress',
                'r_basicprofile',
            ],
            'PROFILE_FIELDS': [
                'emailAddress',
                'firstName',
                'headline',
                'id',
                'industry',
                'lastName',
                'pictureUrl',
                'positions',
                'publicProfileUrl',
                'location',
                'specialties',
                'summary',
            ]
        },
        'facebook': {
            'METHOD': 'oauth2',
            'SCOPE': [
                'email',
                'public_profile',
            ],
            'FIELDS': [
                'id',
                'email',
                'name',
                'first_name',
                'last_name',
                'verified',
                'locale',
                'timezone',
                'link',
                'gender',
            ]
        },
    }
    
    # Comment out this in case you wont to diable Social login
    SOCIALACCOUNT_PROFILE_EXTRACTORS = {
        "facebook": "geonode.people.profileextractors.FacebookExtractor",
        "linkedin_oauth2": "geonode.people.profileextractors.LinkedInExtractor",
    }
    

配置

  1. 转到GeoNode/Django Admin Dashboard并添加您要配置的社交应用:

admin/socialaccount/socialapp/

转到管理部分
  • LinkedIn

LinkedIn
  • Facebook

Facebook

警告

请确保添加要启用的站点。

用法

您需要首先在您的社交提供商上创建和配置OAuth2应用程序。

这将需要一个Persoanl或业务帐户,该帐户可以访问 developers LinkedIn和Facebook的部分,并创建和配置新的 Applications

GeoNode用户将无法查看该帐户。这仅在生成OAuth2时需要 Client IDClient Secret 授权密钥。

在接下来的部分中,我们将详细了解如何为LinkedIn和Facebook配置它们。

LinkedIn应用程序

(档号:http://django-allauth.readthedocs.io/en/latest/providers.html)

  1. 转到https://www.linkedin.com/developer/apps并选择 Create Application

LinkedIn设备
  1. 创建新公司

../../_images/005_socialaouth.png
  1. 填写信息

注解

标志必须有精确的正方形尺寸

../../_images/006_socialaouth.png
  1. 选择以下选项 Default Application Permissions

警告

请确保选择 r_basicprofiler_emailaddress 应用程序权限。

../../_images/007_socialaouth.png
  1. 添加OAuth 2.0授权重定向URL:

    http://geonode.geo-solutions.it/account/linkedin_oauth2/login/callback/
    http://geonode.geo-solutions.it/account/linkedin/login/callback/
    
../../_images/008_socialaouth.png
  1. 保存

../../_images/009_socialaouth.png
  1. 请注意 Authentication Keys

../../_images/010_socialaouth.png
  1. 转到GeoNode/Django管理、社交应用程序,然后选择LinkedIn One

(/admin/socialaccount/socialapp/ )

../../_images/011_socialaouth.png
  1. 剪切并粘贴 Client IDClient Secret 关于相关字段的几个问题

../../_images/012_socialaouth.png
  1. 保存

Facebook应用程序

(编号:http://django-allauth.readthedocs.io/en/latest/providers.html)(编号:https://www.webforefront.com/django/setupdjangosocialauthentication.html)

  1. 转到https://developers.facebook.com/apps并添加一个 New Application

../../_images/013_socialaouth.png
  1. 创建 App ID 然后转到 Dashboard

../../_images/014_socialaouth.png ../../_images/015_socialaouth.png
  1. 请注意 Authentication Keys

../../_images/016_socialaouth.png
  1. 转到GeoNode/Django管理、社交应用程序,然后选择LinkedIn One

(/admin/socialaccount/socialapp/ )

../../_images/017_socialaouth.png
  1. 剪切并粘贴 App IDSecret Key 关于相关字段的几个问题

    ClientID      <--> App Id
    Client Secret <--> Secret Key
    
../../_images/018_socialaouth.png
  1. 保存

  2. 返回Facebook应用程序 Dashboard 并选择 Settings

../../_images/019_socialaouth.png
  1. 添加您的应用程序域

../../_images/020_socialaouth.png
  1. 单击 Add Platform

../../_images/021_socialaouth.png
  1. 选择 Web Site

../../_images/022_socialaouth.png
  1. 添加 URL

../../_images/023_socialaouth.png
  1. 和保存

../../_images/024_socialaouth.png
  1. Add Product

../../_images/025_socialaouth.png
  1. 选择 Facebook Login

../../_images/026_socialaouth.png
  1. 选择 Web

../../_images/027_socialaouth.png
  1. Settings

../../_images/028_socialaouth.png
  1. 一定要确保 Allow client OAuthAccess via OAuth Web 已启用

../../_images/029_socialaouth.png
  1. 添加有效的 redirect URIs

    http://geonode.geo-solutions.it/account/facebook/login/callback/
    http://geondoe.geo-solutions.it/account/login/
    
../../_images/030_socialaouth.png
  1. 保存

../../_images/031_socialaouth.png

使用GeoNode上的现有帐户登录

如果要使现有用户帐户能够通过社交应用登录,则需要将其关联到社交帐户。

通常这只能由当前用户完成,因为此操作需要对其社交帐户进行身份验证。

为此,您需要转到User Profile Settings(用户配置文件设置

../../_images/032_socialaouth.png

点击“已连接的社交账户”

../../_images/033_socialaouth.png

并将它们真正连接起来

../../_images/034_socialaouth.png ../../_images/035_socialaouth.png