Page tree
Skip to end of metadata
Go to start of metadata

This article describes how to use ForgeRock AM as an SAML2 IdP for a python Django application (SP).

This article uses the library to integrate AM SAML2 authentication into a Django application. We will use as a sample Django application for this purpose.


  1. This article assumes CentOS 7. Some steps may vary depending on the OS used.
  2. Python 3 (3.6.8 for this example) installed. To install there are many tutorials available online, on example is [here] (
  3. A working instance of AM 6.5. Should work with older and newer versions of AM too.
  4. Add a local hosts file entry on the users laptop/desktop:

<VM ip address>

High level steps

  1. Deploy, run and test the sample application as-is in a python virtual environment
  2. Create SAML2 federation configuration in AM
  3. Configure the sample application, using django-saml2-auth library to use AM as an IdP

AM is deployed under and the django application is deployed under

Detailed steps

  1. Deploy, run and test the sample application as-is in a python virtual environment

    a. Create and activate a python virtual environment

    [forgerock@centosvm ~]$ mkdir django-saml2
    [forgerock@centosvm ~]$ cd django-saml2/
    [forgerock@centosvm django-saml2]$ python3.6 -m venv django-saml2-venv
    [forgerock@centosvm django-saml2]$ source django-saml2-venv/bin/activate
    (django-saml2-venv) [forgerock@centosvm django-saml2]$

    b. Installdjangoin the venv

    (django-saml2-venv) [forgerock@centosvm 2do1ist]$ pip3 installDjango==2.1.*
      Using cached
    Collecting pytz (fromDjango==2.1.*)
      Using cached
    Installing collected packages: pytz,Django
    Successfully installedDjango-2.1.9 pytz-2019.1
    (django-saml2-venv) [forgerock@centosvm 2do1ist]$

    c. Clone the sample appication

    (django-saml2-venv) [forgerock@centosvmdjango-saml2]$ git clone
    Cloning into '2do1ist'...

    d. Create DB (sqlite) schema

    (django-saml2-venv) [forgerock@centosvm 2do1ist]$ python3.6 makemigrations
    Migrations for 'todolist':
        - Create model Todo
    (django-saml2-venv) [forgerock@centosvm 2do1ist]$ python3.6 migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, sessions, todolist
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying auth.0009_alter_user_last_name_max_length... OK
      Applying sessions.0001_initial... OK
      Applying todolist.0001_initial... OK
    (django-saml2-venv) [forgerock@centosvm 2do1ist]$

    e. Run the appication

    (django-saml2-venv) [forgerock@centosvm 2do1ist]$ python3.6 runserver0.0.0.0:8000
    Performing system checks...
    System check identified no issues (0 silenced).
    June 10, 2019 - 22:55:15
    Djangoversion 2.1.9, using settings 'mysite.settings'
    Starting development server athttp://
    Quit the server with CONTROL-C.

    f. Access the application in your browser. Login screen is shown

    g. Create two users and login and create some sample to do items for both.
    usernames: testuser1 and testuser2
    passwords: s#cret123

  2. Create SAML2 federation configuration in AM

    a. We will use alpha realm for this exercise. Similar steps can be done on bravo realm if desired.
    h. Create a file, called todolist_sp.xml with the content:
    <?xml version="1.0"?>
    <md:EntityDescriptor xmlns:md="urn:oasis:names:tc:SAML:2.0:metadata"
    <md:SPSSODescriptor AuthnRequestsSigned="false" WantAssertionsSigned="false" protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
    <md:AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
    index="1" />


    b. Go to "Native consoles" -> "Access Management".
    c. Make sure you are working in alpha realm, on the alpha realm's dashboard, click SAML Applications
    d. On "Entity Providers" page, click "Add Entity Provider" -> "Hosted"
    e. Fill in the details:
    Entity ID: forgerockidcloud
    Identity Provider Meta Alias - Optional: idp

    Leave the other settings unchanged. Click "Create"
    f. "forgerockidcloud" entity page will be shown. Click "Assertion Processing" and in the "Attribute Map - optional" list, add the following:
    SAML Attribute = Local Attribute
    uid = userName
    sn = sn
    giveName = givenName
    mail = mail
    g. Click "Save changes"
    h. Click "Entity Providers" on the left and then "Add Entity Provider" -> "Remote"
    i. On "New Remote Entity Provider" page, drag the created XML file to the upload area and click "Create".
    j. A new remote SP should be created, as shown:
    k. Click on the newly created SP entity, then do to "Assertion Processing" tab and add the following entries in "Attribute Map - optional"
    SAML Attribute = Local Attribute
    uid = uid
    sn = sn
    giveName = givenName
    mail = mail

    l. Click "Save changes"
    m. Click "Applications" -> "Federation" -> "Circles of Trust" on the left menu, click "Add Circle of Trust"
    n. Name it "cot1" and click "Create"
    o. Add a description (optional) and add "forgerockidcloud|saml2" and "todolist|saml2" entities in the "Entity Providers" list, then click "Save Changes".

    m. Fetch the IdP metadata from AM from and save it in a file, for example: /home/forgerock/django-saml2/metadata.xml

  1. Configure the sample application, using django-saml2-auth library to use AM as an IdP

    a. Stop the django application if running and install django-saml2-auth in the virtual environment

    (django-saml2-venv) [forgerock@centosvm django-saml2]$ pip3 install django_saml2_auth
    Collecting django_saml2_auth
      Using cached
    Successfully installed .... long list of libs
    (django-saml2-venv) [forgerock@centosvm django-saml2]$

    b. Edit django-saml2/2do1ist/mysite/ and un-comment the lines starting with re_path, so it reads:

        # These are the SAML2 related URLs. You can change "^saml2_auth/" regex to
        # any path you want, like "^sso_auth/", "^sso_login/", etc. (required)
        re_path(r'^saml2_auth/', include('django_saml2_auth.urls')),

        # The following line will replace the default user login with SAML2 (optional)
        # If you want to specific the after-login-redirect-URL, use parameter "?next=/the/path/you/want"
        # with this view.
        re_path(r'^accounts/login/$', django_saml2_auth.views.signin),

        # The following line will replace the admin login with SAML2 (optional)
        # If you want to specific the after-login-redirect-URL, use parameter "?next=/the/path/you/want"
        # with this view.
        re_path(r'^admin/login/$', django_saml2_auth.views.signin)

    also un-comment the following line:

    import django_saml2_auth.views

    c. Edit 2do1ist/mysite/ and un-comment the SAML2_AUTH section at the very end, so it reads:

    SAML2_AUTH = {
        'METADATA_LOCAL_FILE_PATH': '/home/forgerock/django-saml2/metadata.xml',
        'DEFAULT_NEXT_URL': '/',
        'CREATE_USER': 'FALSE',
        'ATTRIBUTES_MAP': {
            'email': 'mail',
            'username': 'uid',
            'first_name': 'givenName',
            'last_name': 'sn',
        'ENTITY_ID': 'todolist',
        'USE_JWT': False,
        'FRONTEND_URL': '',

    d. Launch the application

    (django-saml2-venv) [forgerock@centosvm 2do1ist]$ python3.6 runserver
    Performing system checks...

    System check identified no issues (0 silenced).
    June 10, 2019 - 23:25:55
    Django version 2.1.9, using settings 'mysite.settings'
    Starting development server at
    Quit the server with CONTROL-C.

    e. Access the application in the browser again - you should be redirected to AM login page, with URL similar to:

    Login as testuser1 or testuser2 in AM. AM should send a POST to with SAMLResponse in the POST payload. You should be redirected to the application main page with the todo list for the user you logged in as.

    The decoded SAML assertion will look like:

    <samlp:Response Destination=""
        ID="s279fe23917842cdc4f50e63249db753daab02561f" InResponseTo="id-hZnmzTaHb2zFEqpUz"
        IssueInstant="2019-06-10T23:31:00Z" Version="2.0" xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
        <saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">djangoidp</saml:Issuer>
        <samlp:Status xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol">
            <samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"
        <saml:Assertion ID="s2d1a5ac225906d844234c47162029ca330ee07ad1" IssueInstant="2019-06-10T23:31:00Z"
            Version="2.0" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
            <ds:Signature xmlns:ds="">
                    <ds:CanonicalizationMethod Algorithm=""/>
                    <ds:SignatureMethod Algorithm=""/>
                    <ds:Reference URI="#s2d1a5ac225906d844234c47162029ca330ee07ad1">
                            <ds:Transform Algorithm=""/>
                            <ds:Transform Algorithm=""/>
                        <ds:DigestMethod Algorithm=""/>
                <saml:NameID Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient" NameQualifier="djangoidp"
                <saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer">
                    <saml:SubjectConfirmationData InResponseTo="id-hZnmzTaHb2zFEqpUz"
                        NotOnOrAfter="2019-06-10T23:41:00Z" Recipient=""/></saml:SubjectConfirmation>
            <saml:Conditions NotBefore="2019-06-10T23:21:00Z" NotOnOrAfter="2019-06-10T23:41:00Z">
            <saml:AuthnStatement AuthnInstant="2019-06-10T23:31:00Z"
                <saml:Attribute Name="uid">
                    <saml:AttributeValue xmlns:xs=""
                        xmlns:xsi="" xsi:type="xs:string">testuser1</saml:AttributeValue>
                <saml:Attribute Name="sn">
                    <saml:AttributeValue xmlns:xs=""
                        xmlns:xsi="" xsi:type="xs:string">user1</saml:AttributeValue>
                <saml:Attribute Name="mail">
                    <saml:AttributeValue xmlns:xs=""
                        xmlns:xsi="" xsi:type="xs:string"></saml:AttributeValue>
                <saml:Attribute Name="givenName">
                    <saml:AttributeValue xmlns:xs=""
                        xmlns:xsi="" xsi:type="xs:string">test1</saml:AttributeValue>