PI Services

Le blog des collaborateurs de PI Services

Authentification SSO avec l'ADFS et Apache 2.5 (installé sur un linux)

Dans cet article nous allons apprendre comment permettre apache 2.4 (installée sur un linux) à communiquer avec l'ADFS pour faire de l'authentification SSO (Single Sign On)

Contexte 

Apache ne communique pas nativement avec l'ADFS. Pour permettre à des applications web qui tournent sur apache de faire de l'authentification SSO, il faudrait installer deux modules , auth_mellon et lasso , pour permettre la communication.  

Installation des modules nécessaires 

1. auth_mellon

« auth_mellon » est un module Apache qui permet l'authentification d'utilisateurs d'un site web avec un fournisseur d'identité (Identiy Provider — IdP) implémentant SAML 2.0. Il peut accorder des droits d'accès à des chemins et fournir des attributs à d'autres modules ou applications.

Téléchargement du module « auth_mellon » : https://packages.debian.org/fr/sid/libapache2-mod-auth-mellon (nom du paquet: libapache2-mod-auth-mellon) pour debian

Télécharger depuis https://packages.debian.org/fr/sid/libapache2-mod-auth-mellon le fichier *.deb pour l’installation du module « auth_mellon ».

Utiliser la commande suivante pour installer auth_mellon.

dpkg –i libapache2-mod-auth-mellon_0.9.1-1-bpo70+1_amd64.deb

Note: Installer toutes les dépendence liées à « auth_mellon » avant de continuer.

2. lasso

« lasso » est une bibliothèque de modules écrits en C avec des protocoles de communication pour des entités fédérées , SSO et autres produits identiques. Pour que lasso puisse fonctionner,il faudrait en plus de liblasso3, libxml2, XMLSec et OpenSSL. 

Lasso est disponible sur le site http://lasso.entrouvert.org/ en téléchargement en ajoutant la repository dans le fichier sources.list par exemple pour une debian.

deb http://deb.entrouvert.org wheezy main

Note: Installer toutes les dépendence liées à « lasso » avant de continuer.

Génération du fichier métadata *.xml, *.crt, *.key 

Pour la création d'une fédération (federation trust), l'ADFS nécessite le fichier de configuration du Service Provider (SP) qui est le serveur apache et ses applications. Ces fichiers sont créés par le script fournit ci-dessous: 

Le nom du script est mellon_create_metadata.sh et placer dans le dossier /etc/apache2/mellon

#!/usr/bin/env bash
set -e

PROG="$(basename "$0")"

printUsage() {
    echo "Usage: $PROG ENTITY-ID ENDPOINT-URL"
    echo ""
    echo "Example:"
    echo "  $PROG urn:someservice https://sp.mon-application.com/mellon"
    echo ""
}

if [ "$#" -lt 2 ]; then
    printUsage
    exit 1
fi

ENTITYID="$1"
if [ -z "$ENTITYID" ]; then
    echo "$PROG: An entity ID is required." >&2
    exit 1
fi

BASEURL="$2"
if [ -z "$BASEURL" ]; then
    echo "$PROG: The URL to the MellonEndpointPath is required." >&2
    exit 1
fi

if ! echo "$BASEURL" | grep -q '^https\?://'; then
    echo "$PROG: The URL must start with \"http://\" or \"https://\"." >&2
    exit 1
fi

HOST="$(echo "$BASEURL" | sed 's#^[a-z]*://\([^/]*\).*#\1#')"
BASEURL="$(echo "$BASEURL" | sed 's#/$##')"

OUTFILE="$(echo "$ENTITYID" | sed 's/[^0-9A-Za-z.]/_/g' | sed 's/__*/_/g')"
echo "Output files:"
echo "Private key:               $OUTFILE.key"
echo "Certificate:               $OUTFILE.cert"
echo "Metadata:                  $OUTFILE.xml"
echo "Host:                      $HOST"
echo
echo "Endpoints:"
echo "SingleLogoutService:       $BASEURL/logout"
echo "AssertionConsumerService:  $BASEURL/postResponse"
echo

# No files should not be readable by the rest of the world.
umask 0077

TEMPLATEFILE="$(mktemp -t mellon_create_sp.XXXXXXXXXX)"

cat >"$TEMPLATEFILE" <<EOF
RANDFILE           = /dev/urandom
[req]
default_bits       = 2048
default_keyfile    = privkey.pem
distinguished_name = req_distinguished_name
prompt             = no
policy             = policy_anything
[req_distinguished_name]
commonName         = "mon-application.com"
EOF

openssl req -utf8 -batch -config "$TEMPLATEFILE" -new -x509 -days 3652 -nodes -out "$OUTFILE.cert" -keyout "$OUTFILE.key" 2>/dev/null

rm -f "$TEMPLATEFILE"

CERT="$(grep -v '^-----' "$OUTFILE.cert")"

cat >"$OUTFILE.xml" <<EOF
<EntityDescriptor entityID="$ENTITYID" xmlns="urn:oasis:names:tc:SAML:2.0:metadata" xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
  <SPSSODescriptor protocolSupportEnumeration="urn:oasis:names:tc:SAML:2.0:protocol">
    <KeyDescriptor use="signing">
      <ds:KeyInfo xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
        <ds:X509Data>
          <ds:X509Certificate>$CERT</ds:X509Certificate>
        </ds:X509Data>
      </ds:KeyInfo>
    </KeyDescriptor>
    <SingleLogoutService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect" Location="$BASEURL/logout"/>
    <AssertionConsumerService Binding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Location="$BASEURL/postResponse" index="0"/>
  </SPSSODescriptor>
</EntityDescriptor>
EOF

umask 0777
chmod go+r "$OUTFILE.xml"
chmod go+r "$OUTFILE.cert"

Note : Changer le CommonName par le FQDN complet, exemple : mon-application.com.

le script se lance comme indique dans la commande ci-dessous: 

sh mellon_create.metadata.sh https://mon-application.com/url_application https://mon-application.com/mellon

L'exécution du script va générer 3 fichiers à envoyer à l'IDP pour la création d'une fédération:

  1. https_mon-application.com_url_application.cert
  2. https_mon-application.com_url_application.key
  3. https_mon-application.com_url_application.xml

Configuration du fichier auth_mellon.conf

Configurer le fichier auth_mellon.conf (etc/apache2/mod-available/auth_mellon.conf) comme dans l’exemple ci-dessous.

# MellonCacheSize sets the maximum number of sessions which can be active
# at once. When mod_auth_mellon reaches this limit, it will begin removing
# the least recently used sessions.
# Default: MellonCacheSize 100
#MellonCacheSize 100

# MellonPostDirectory is the full path of a directory where POST requests
# are saved during authentication. This directory must writeable by the
# Apache user. It should not be writeable (or readable) by other users.
MellonPostDirectory "/var/cache/apache2/mod_auth_mellon/"

<Location />
    # Add information from the mod_auth_mellon session to the request.
    MellonEnable "info"

    # Configure the SP metadata
    # This should be the files which were created when creating SP metadata.
    MellonSPPrivateKeyFile /etc/apache2/mellon/https_mon-application.com_url_application.key
    MellonSPCertFile /etc/apache2/mellon/https_mon-application.com_url_application.cert
    MellonSPMetadataFile /etc/apache2/mellon/https_mon-application.com_url_application.xml

    # IdP metadata. This should be the metadata file you got from the IdP.
    MellonIdPMetadataFile /etc/apache2/mellon/FederationMetadata.xml

    # The location all endpoints should be located under.
    # It is the URL to this location that is used as the second parameter to the metadata generation script.
    # This path is relative to the root of the web server.
    MellonEndpointPath /mellon
</Location>

# This is a location that will trigger authentication when requested.
<Location /sspi/sspi_test.php>
    # This location will trigger an authentication request to the IdP.
    MellonEnable "auth"
MellonSecureCookie On
</Location>
<Location /index.php>
    # This location will trigger an authentication request to the IdP.
    MellonEnable "auth"
MellonSecureCookie On
</Location>
<Location /index_prod.html>
    # This location will trigger an authentication request to the IdP.
    MellonEnable "auth"
MellonSecureCookie On
</Location>
<Location /autoconnect_mail.php>
    # This location will trigger an authentication request to the IdP.
    MellonEnable "auth"
MellonSecureCookie On
</Location>

MellonIdPMetadataFile = le fichier de fédération de l'IDP.

Il ne reste plus qu'à paramétrer l'application pour la connection et redirection automatique.

 

 

 

Ajouter un commentaire

Loading