Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Exemple d’intégration

L’exemple suivant génère une page intégrant :

  • iframeresizer ;

  • une callback de redirection post soumission du formulaire sur la page elle-même ;

  • le positionnement de l'URL envoyée par email à la page elle-même.

Note

ne pas utiliser en production

Code Block
breakoutModewide
languagephp
<?php
function get_conf()
{
    $config = parse_ini_file("config.ini");
    return $config;
}

function getToken()
{
    $config = get_conf();
    $auth_url = $config["auth_url"];
    $auth_realm = $config["auth_realm"];
    $auth_client_id = $config["auth_client_id"];
    $auth_username = $config["auth_username"];
    $auth_password = $config["auth_password"];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $auth_url . "/realms/" . $auth_realm . "/protocol/openid-connect/token");
    curl_setopt($curl, CURLOPT_POSTFIELDS, "grant_type=password&client_id=" . $auth_client_id . "&username=" . urlencode($auth_username) . "&password=" . urlencode($auth_password));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);

    return json_decode($response)->access_token;
}

function getFormUrl()
{
    $uuid = array_key_exists("uuid", $_GET) ? $_GET["uuid"] : uniqid();
    $email = array_key_exists("email", $_GET) ? $_GET["email"] : "nobody@exemple.com";
    $token = getToken();

    $config = get_conf();
    $api_url = $config["api_url"];
    $organisation_id = $config["organisation_id"];
    $model_id_or_alias = $config["model_id_or_alias"];

    $host = isset($_SERVER['HTTP_X_FORWARDED_HOST']) ? $_SERVER['HTTP_X_FORWARDED_HOST'] : $_SERVER['HTTP_HOST'];
    $port = isset($_SERVER['HTTP_X_FORWARDED_PORT']) ? $_SERVER['HTTP_X_FORWARDED_PORT'] : "";
    $proto = isset($_SERVER['HTTPS']) ? "https" : "http";
    $proto = isset($_SERVER['HTTP_X_FORWARDED_PROTO']) ? $_SERVER['HTTP_X_FORWARDED_PROTO'] : $proto;
    $me = $proto . "://" . $host . ($port ? ":" . $port : "") . $_SERVER['DOCUMENT_URI'];

    $context = [
        "userid" => $uuid,
        "callback" => "?uuid=". $uuid . "&email=". $email,
        "country" => "FR",
        "language" => "fr",
        "optoutEmail" => $email,
        "receipt" => false,
        "iframe" => true,
        "iframeEventsTargetOrigin" => $me,
        "optoutEmailLink" => $me . "?uuid=" . $uuid . "&email=" . $email,
    ];

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $api_url . "/organisations/" . $organisation_id . "/consents/" . $model_id_or_alias . "/endpoint");
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($context));
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token", "Content-Type: application/json"));
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($curl);
    curl_close($curl);

    return json_decode($response)->endpoint;
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="utf-8">
    <title>Consent iFrame Integration Test</title>
</head>
<body>
<h2 style="text-align: center">Consent iFrame Integration Test</h2>
    <iframe src="<?php echo getFormUrl() ?>" width="100%" title="Consent iFrame Integration Test" id="consent" name="consent"></iframe>
    <script type="text/javascript" src="iframeResizer-4.0.4.js"></script>
    <script type="text/javascript">iFrameResize({log: false});</script>
    <script type="application/javascript">
        window.addEventListener("message", messageListener, false);
        function messageListener(event) {
            if (event.data.search(/consent-callback/) >= 0) {
                const urlback = event.data.replace(/.*consent-callback\/([^"]*).*/, '$1');
                window.location.assign(urlback);
            }
        }
    </script>
</body>
</html>

Code à déposer sur un serveur HTTP avec interpréteur PHP, ainsi que iframeResizer-4.0.4.js et un fichier config.ini de la forme suivante :

Code Block
breakoutModewide
[authentication]
auth_url = "https://auth.fairandsmart.com/auth";
auth_realm = "FairAndSmart";
auth_client_id = "fsorg";
auth_username = "mysecretidentity@example.com";
auth_password = "MySecretPassword";

[backend]
api_url = "https://core.fairandsmart.com/api";
organisation_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
model_id_or_alias = "YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY";

Par exemple sur http://mon-serveur/index.php?email=mon-email@mon-domaine :

Image RemovedImage Added