1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
<?php
/**
* Created by PhpStorm.
* Copyright: Bertschi AG, 2015
* User: jbaumann
* File: Authenticate.php
* Date: 23.12.2015
* Time: 08:40
*/
namespace JRA\Config;
use JRA\Exceptions\AuthenticationConfigException;
use JRA\Exceptions\TokenConfigException;
use JRA\Exceptions\UserCredentialsConfigException;
class Authenticate
{
/**
* The auth method id for header token authentication.
*/
const AUTH_METHOD_HEADER_TOKEN = 100;
/**
* The auth method id for user credentials authentication.
*/
const AUTH_METHOD_USER_CREDENTIALS = 101;
/**
* The auth method if for token authentication.
*/
const AUTH_METHOD_TOKEN = 102;
/**
* The auto login id if the api should automatically login.
*/
const AUTH_AUTO_LOGIN_ON = 200;
/**
* The auto login id if the api should not automatically login.
*/
const AUTH_AUTO_LOGIN_OFF = 201;
/**
* The token which should be used for header token authentication or session token authentication.
*
* @var string
*/
private $_token;
/**
* The username which should be used for credentials authentication.
*
* @var string
*/
private $_username;
/**
* The password which should be used for credentials authentication.
*
* @var string
*/
private $_password;
/**
* The return session from the cAPI.
*
* @var string
*/
private $_session;
/**
* The id of the chosen authentication option.
*
* @var int
*/
private $_authMethod;
/**
* The id if the auto login is turned on or turned off.
*
* @var int
*/
private $_autoLogin;
/**
* Authenticate constructor.
*
* @param int $pAuthMethod
* @param int $pAutoLogin
*/
public function __construct($pAuthMethod, $pAutoLogin)
{
$this->_authMethod = $pAuthMethod;
$this->_autoLogin = $pAutoLogin;
}
/**
* Sets the token for an token required authentication method.
*
* @param string $pToken The token which should be set.
* @throws TokenConfigException If the authentication method is not set to an token required authentication method an exception will be thrown.
*/
public function setToken($pToken)
{
if ($this->_authMethod === Authenticate::AUTH_METHOD_HEADER_TOKEN || $this->_authMethod === Authenticate::AUTH_METHOD_TOKEN) {
$this->_token = $pToken;
} else {
throw new TokenConfigException('Token authentication is not enabled.', TokenConfigException::TOKEN_CONFIG_EXCEPTION_NOT_ENABLED);
}
}
/**
* Sets the user credentials for an user credentials required authentication method.
*
* @param string $pUsername The username which should be used for authentication.
* @param string $pPassword The password which should be used for authentication.
* @throws UserCredentialsConfigException If the authentication method is not set to an user credentials required authentication method an exception will be thrown.
*/
public function setCredentials($pUsername, $pPassword)
{
if ($this->_authMethod === Authenticate::AUTH_METHOD_USER_CREDENTIALS) {
$this->_username = $pUsername;
$this->_password = $pPassword;
} else {
throw new UserCredentialsConfigException('User credentials authentication is not enabled.', UserCredentialsConfigException::USER_CREDENTIALS_CONFIG_EXCEPTION_NOT_ENABLED);
}
}
/**
* Returns the specified authentication method.
*
* @return int The authentication method.
*/
public function getAuthenticationMethod()
{
return $this->_authMethod;
}
/**
* Checks if auto login is on or off.
*
* @return bool True if auto login is on, false otherwise.
*/
public function isAutoLoginOn()
{
return ($this->_autoLogin === Authenticate::AUTH_AUTO_LOGIN_ON) ? true : false;
}
/**
* Returns the authentication credentials, based on the specified authentication method and if they are set before.
*
* @return array|string The user credentials or the token.
* @throws AuthenticationConfigException Throws an exception if the authentication method is not set.
* @throws TokenConfigException Throws an exception if the token is not specified and a token required authentication method is specified.
* @throws UserCredentialsConfigException Throws an exception if the user credentials are not specified and a user credentials required authentication method is specified.
*/
public function getAuthenticationCredentials()
{
if ($this->_authMethod === Authenticate::AUTH_METHOD_USER_CREDENTIALS) {
if ($this->_username === null && $this->_password === null) {
throw new UserCredentialsConfigException('User credentials not set.', UserCredentialsConfigException::USER_CREDENTIALS_CONFIG_EXCEPTION_USER_CREDENTIALS_NOT_SET);
} else {
return [
'username' => $this->_username,
'password' => $this->_password
];
}
} elseif ($this->_authMethod === Authenticate::AUTH_METHOD_TOKEN) {
if ($this->_token === null) {
throw new TokenConfigException('Token is not set for session token authentication.', TokenConfigException::TOKEN_CONFIG_EXCEPTION_TOKEN_NOT_SET);
} else {
return $this->_token;
}
} elseif ($this->_authMethod === Authenticate::AUTH_METHOD_HEADER_TOKEN) {
if ($this->_token === null) {
throw new TokenConfigException('Token is not set for header token authentication', TokenConfigException::TOKEN_CONFIG_EXCEPTION_TOKEN_NOT_SET);
} else {
return $this->_token;
}
} else {
throw new AuthenticationConfigException('Authentication method not set.', AuthenticationConfigException::AUTHENTICATION_CONFIG_EXCEPTION_METHOD_NOT_SET);
}
}
/**
* Sets the current session of the Joomla.
*
* @param string $pSession
*/
public function setSession($pSession)
{
$this->_session = $pSession;
}
/**
* Returns the current session of the Joomla.
*
* @return string
*/
public function getSession()
{
return $this->_session;
}
}