<?php

/**
 * OpenKM, Open Knowledge Management System S.L.  (http://www.openkm.com)
 * Copyright (c) 2006-2018
 *
 * No bytes were intentionally harmed during the development of this application.
 *
 * This program is commercial software; you can use it under the terms of the
 * EULA - OpenKM SDK End User License Agreement as published by OpenKM Knowledge Management System S.L.
 *
 * 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
 * EULA - OpenKM SDK End User License Agreement for more details:
 * http://docs.openkm.com/kcenter/view/licenses/eula-openkm-sdk-end-user-license-agreement.html
 */

namespace openkm\impl;

use openkm\bean\GrantedRole;
use openkm\bean\GrantedUser;
use openkm\bean\ProfileSimple;
use openkm\definition\BaseAuth;
use openkm\bean\ChangeSecurity;
use openkm\util\UriHelper;
use openkm\util\FormatUtils;
use Httpful\Request;
use Httpful\Mime;
use openkm\util\Status;
use openkm\util\TypeException;
use openkm\exception\PrincipalAdapterException;
use openkm\exception\AccessDeniedException;
use openkm\exception\PathNotFoundException;
use openkm\exception\RepositoryException;
use openkm\exception\DatabaseException;
use Httpful\Exception\ConnectionErrorException;
use openkm\exception\UnknowException;
use openkm\exception\WebServiceException;

/**
 * AuthImpl
 *
 * @author sochoa
 */
class AuthImpl extends ClientImpl implements BaseAuth {

    /**
     * Simulate first login process 
     * 
     * @throws ConnectionErrorException
     * @throws AccessDeniedException
     * @throws UnknowException
     */
    public function login($username, $password, $expiration = 1, $restrictIp = false) {
        try {
            $uri = UriHelper::getUri($this->host, UriHelper::AUTH_GET_LOGIN);
            $params = [];
            $params['username'] = $username;
            $params['password'] = $password;
            $params['expiration'] = $expiration;
            $params['restrictIp'] = $restrictIp;
            $client = Request::post($uri);
            $client->body(json_encode($params), Mime::JSON);
            $client->expects(Mime::JSON);
            $response = $client->send();            
            if ($response->code == Status::OK) {                
                return $response->headers["authorization"];
            } else {
                if ($response->code == Status::UNAUTHORIZED) {
                    throw new AccessDeniedException($response->body, $response->code);
                } else if ($response->code == Status::INTERNAL_SERVER_ERROR) {
                    $position = strpos($response->body, ':');
                    if ($position) {
                        $error = substr($response->body, 0, $position);
                        $msg = substr($response->body, $position + 1, strlen($response->body));
                        throw new UnknowException($error . ': ' . $msg);
                    } else {
                        throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                    }
                } else {
                    throw new UnknowException("HTTP error code " . $response->code . ": " . $response->body);
                }
            }
        } catch (ConnectionErrorException $cee) {
            throw new ConnectionErrorException('ConnectionErrorException: ' . $cee->getMessage());
        }
    }

    public function getSessionId() {
        
    }

    public function logout() {
        
    }
}

?>
