简体   繁体   English

使用SAML的Dropbox和Django SSO

[英]Dropbox and Django SSO using SAML

Summary 摘要
I am looking to use Dropbox SSO functionality by using the authentication from a Django site. 我希望通过使用来自Django站点的身份验证来使用Dropbox SSO功能。 Note that I'm not looking to use SAML as a backend for my Django site. 请注意,我希望将SAML用作Django站点的后端。

Resources 资源资源
1) Dropbox Custom SSO help page: https://www.dropbox.com/en/help/1921#custom 1)Dropbox自定义SSO帮助页面: https : //www.dropbox.com/en/help/1921#custom
2) Creating a SAML response: https://robinelvin.wordpress.com/2009/09/04/saml-with-django/ 2)创建SAML响应: https : //robinelvin.wordpress.com/2009/09/04/saml-with-django/
3) Struggled to find any examples from Google of people doing this kind of SSO. 3)努力从Google中找到从事此类SSO的任何示例。 Lots of links about people using SAML as a Django backend. 关于使用SAML作为Django后端的人们的很多链接。

Question
In the dropbox admin settings I can add my X509 certificate and the login link. 在保管箱管理员设置中,我可以添加X509证书和登录链接。 This means that when you try to login into Dropbox using SSO it nicely forwards you to my Django site's login page using a GET request with a SAMLRequest in the querystring. 这意味着,当您尝试使用SSO登录到Dropbox时,它会使用带有查询字符串中的SAMLRequest的GET请求将您很好地转发到我的Django站点的登录页面。

However, my understanding is that I now need to, once the user is authenticated on the Django site, fire a POST request back to Dropbox at their SAML login link with a SAMLResponse in the post data. 但是,据我了解,我现在需要在用户在Django站点上通过身份验证之后,在其SAML登录链接中使用POST数据中的SAMLResponse将POST请求发送回Dropbox。 Using the second resource above I believe I can create the SAMLResponse xml but I am unsure how to redirect the user to the dropbox SAML login link with the SAML data from my Django view. 使用上面的第二个资源,我相信可以创建SAMLResponse xml,但是我不确定如何使用Django视图中的SAML数据将用户重定向到保管箱SAML登录链接。

Any help much appreciated. 任何帮助,不胜感激。

Managed to get the functionality I needed using django-saml2-idp https://github.com/peopledoc/django-saml2-idp 使用django-saml2-idp设法获得所需的功能https://github.com/peopledoc/django-saml2-idp

Good documentation on installing here: https://github.com/peopledoc/django-saml2-idp/blob/master/doc/INSTALL.txt 有关在此处安装的良好文档: https : //github.com/peopledoc/django-saml2-idp/blob/master/doc/INSTALL.txt

Settings in the Dropbox Admin console required the X509 certificate and then the login url set to: https://****.com/idp/login Dropbox管理控制台中的设置需要X509证书,然后将登录URL设置为: https://****.com/idp/login

Note that I had issues installing the M2Crypto dependency so used an Ubuntu package via: 请注意,我在安装M2Crypto依赖项时遇到问题,因此通过以下方式使用了Ubuntu软件包:
sudo apt-get install python-m2crypto

Additionally I'm using Django 1.9.6 so needed to make overrides to the views.py , urls.py , and registry.py files to make them compatible (various import statements needed updating and the urls changed to the new list format rather than using patterns). 另外,我使用的是Django 1.9.6,因此需要对views.pyurls.pyregistry.py文件进行覆盖以使其兼容(各种import语句需要更新,并且url更改为新的列表格式,而不是使用模式)。

Created a Dropbox Processor as follows: 创建了一个Dropbox处理器,如下所示:

import base64
import zlib
from saml2idp import base
from saml2idp.xml_render import _get_assertion_xml

def get_assertion_dropbox_xml(parameters, signed=False):
    return _get_assertion_xml(ASSERTION_DROPBOX, parameters, signed)

ASSERTION_DROPBOX = (
    '<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" '
            'ID="${ASSERTION_ID}" '
            'IssueInstant="${ISSUE_INSTANT}" '
            'Version="2.0">'
        '<saml:Issuer>${ISSUER}</saml:Issuer>'
        '${ASSERTION_SIGNATURE}'
        '${SUBJECT_STATEMENT}'
        '<saml:Conditions NotBefore="${NOT_BEFORE}" NotOnOrAfter="${NOT_ON_OR_AFTER}">'
            '<saml:AudienceRestriction>'
                '<saml:Audience>${AUDIENCE}</saml:Audience>'
            '</saml:AudienceRestriction>'
        '</saml:Conditions>'
        '<saml:AuthnStatement AuthnInstant="${AUTH_INSTANT}"'
            '>'
            '<saml:AuthnContext>'
                '<saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef>'
            '</saml:AuthnContext>'
        '</saml:AuthnStatement>'
        '${ATTRIBUTE_STATEMENT}'
    '</saml:Assertion>'
)


class Processor(base.Processor):

    def _decode_request(self):
        """
        Decodes _request_xml from _saml_request.
        """
        self._request_xml = zlib.decompress(base64.b64decode(self._saml_request), -15)

    def _format_assertion(self):
        self._assertion_xml = get_assertion_dropbox_xml(self._assertion_params, signed=False)

Which you register in your settings.py file as follows: 您在settings.py文件中注册的内容,如下所示:

SAML2IDP_CONFIG = {
    'autosubmit': True,
    'certificate_file': '/****/certificate.pem',
    'private_key_file': '/****/private-key.pem',
    'issuer': 'https://www.****.com',
    'signing': True,
}

sampleSpConfig = {
    'acs_url': 'https://www.dropbox.com/saml_login',
    'processor': 'dropbox.Processor',
}

SAML2IDP_REMOTES = {
    'sample': sampleSpConfig,
}

Works like a dream. 像梦一样工作。 Hope this helps somebody out there. 希望这对有人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM