Skip to content
certlogin.php 2.6 KiB
Newer Older
Sonia Zorba's avatar
Sonia Zorba committed
<?php

/* ----------------------------------------------------------------------------
 *               INAF - National Institute for Astrophysics
 *               IRA  - Radioastronomical Institute - Bologna
 *               OATS - Astronomical Observatory - Trieste
 * ----------------------------------------------------------------------------
 *
 * Copyright (C) 2016 Istituto Nazionale di Astrofisica
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License Version 3 as published by the
 * Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */


include '../../include/init.php';
startSession();

function saveUserFromX509Data($x509Data) {

    global $session;

    $user = new RAP\User();

    $identity = new RAP\Identity(RAP\Identity::X509);
    $identity->email = $x509Data->email;
    $identity->name = $x509Data->name;
    $identity->surname = $x509Data->surname;
    $identity->typedId = $x509Data->serialNumber;
    $identity->institution = $x509Data->institution;

    $user->addIdentity($identity);

    RAP\UserHandler::saveUser($user);

    $session->x509DataToRegister = null;

    return $user;
}

if ($session->x509DataToRegister !== null && $session->x509DataToRegister->name !== null) {

    $user = saveUserFromX509Data($session->x509DataToRegister);
} else {

    if (isset($_SERVER['SSL_CLIENT_VERIFY']) && isset($_SERVER['SSL_CLIENT_V_REMAIN']) &&
            $_SERVER['SSL_CLIENT_VERIFY'] === 'SUCCESS' && $_SERVER['SSL_CLIENT_V_REMAIN'] > 0) {

        $x509Data = RAP\X509Data::parse($_SERVER);

        $user = RAP\UserHandler::findUserByIdentity(RAP\Identity::X509, $x509Data->serialNumber);

        if ($user === null) {

            if ($x509Data->name === null) {
                $session->x509DataToRegister = $x509Data;
                $session->save();
                header('Location: ' . $BASE_PATH . '/x509-name-surname');
                die();
            } else {
                $user = saveUserFromX509Data($x509Data);
            }
        }
    } else {
        http_response_code(500);
        die("Unable to verify client certificate");
    }
}

RAP\CallbackHandler::manageLoginRedirect($user);