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: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253:
<?php
namespace MvcCore\Ext\Auths\Basics\User;
trait Auth {
protected static $sessionIdentity = NULL;
protected static $sessionAuthorization = NULL;
private static $_sessionClass = NULL;
public static function SetUpUserBySession () {
$sessionIdentity = static::GetSessionIdentity();
$sessionAuthorization = static::GetSessionAuthorization();
$userNameStr = \MvcCore\Ext\Auths\Basics\IUser::SESSION_USERNAME_KEY;
$authorizedStr = \MvcCore\Ext\Auths\Basics\IUser::SESSION_AUTHORIZED_KEY;
if (
isset($sessionIdentity->{$userNameStr}) &&
isset($sessionAuthorization->{$authorizedStr}) &&
$sessionAuthorization->{$authorizedStr}
) {
$user = static::GetByUserName($sessionIdentity->{$userNameStr});
$user->passwordHash = NULL;
if (is_array($user->initialValues) && array_key_exists('passwordHash', $user->initialValues))
$user->initialValues['passwordHash'] = NULL;
return $user;
}
return NULL;
}
public static function LogIn ($userName = '', $password = '') {
$user = static::GetByUserName($userName);
if ($user) {
if (\PHP_VERSION_ID >= 50500){
$authenticated = password_verify($password, $user->passwordHash);
} else {
$hashedPassword = static::EncodePasswordToHash($password);
$authenticated = static::hashEquals($user->passwordHash, $hashedPassword);
}
if ($authenticated) {
$sessionIdentity = static::GetSessionIdentity();
$sessionAuthorization = static::GetSessionAuthorization();
$userNameStr = \MvcCore\Ext\Auths\Basics\IUser::SESSION_USERNAME_KEY;
$authorizedStr = \MvcCore\Ext\Auths\Basics\IUser::SESSION_AUTHORIZED_KEY;
$sessionIdentity->{$userNameStr} = $user->userName;
$sessionAuthorization->{$authorizedStr} = TRUE;
$user->passwordHash = NULL;
if (is_array($user->initialValues) && array_key_exists('passwordHash', $user->initialValues))
$user->initialValues['passwordHash'] = NULL;
return $user;
}
}
return NULL;
}
public static function LogOut ($destroyWholeSession = FALSE) {
$sessionIdentity = static::GetSessionIdentity();
$sessionAuthorization = static::GetSessionAuthorization();
if ($destroyWholeSession) {
$sessionIdentity->Destroy();
$sessionAuthorization->Destroy();
} else {
$authorizedStr = \MvcCore\Ext\Auths\Basics\IUser::SESSION_AUTHORIZED_KEY;
$sessionAuthorization->{$authorizedStr} = FALSE;
}
}
public static function EncodePasswordToHash ($password = '', $options = []) {
if (!isset($options['salt'])) {
$configuredSalt = \MvcCore\Ext\Auths\Basic::GetInstance()->GetPasswordHashSalt();
if ($configuredSalt !== NULL) {
$options['salt'] = $configuredSalt;
} else {
throw new \InvalidArgumentException(
'['.get_class().'] No option `salt` given by second argument `$options`'
." or no salt configured by `\MvcCore\Ext\Auth::GetInstance()->SetPasswordHashSalt('...')`."
);
}
}
if (isset($options['cost']) && ($options['cost'] < 4 || $options['cost'] > 31))
throw new \InvalidArgumentException(
'['.get_class().'] `cost` option has to be from `4` to `31`, `' . $options['cost'] . '` given.'
);
if (\PHP_VERSION_ID >= 50500) {
if (\PHP_VERSION_ID >= 80000 && isset($options['salt']))
unset($options['salt']);
$result = @password_hash($password, PASSWORD_BCRYPT, $options);
} else {
$cost = isset($options['cost']) ? $options['cost'] : 10;
$hashPrefix = sprintf("$2y$%02d$", $cost);
$hash = $hashPrefix . $options['salt'] . '$';
$result = crypt($password, $hash);
}
if (!$result || strlen($result) < 60)
throw new \RuntimeException(
'['.get_class().'] Hash computed by `password_hash()` is invalid. Try a little bit longer salt.'
);
return $result;
}
public static function GetSessionIdentity () {
if (static::$sessionIdentity === NULL) {
$sessionClass = self::$_sessionClass ?: (
self::$_sessionClass = \MvcCore\Application::GetInstance()->GetSessionClass()
);
static::$sessionIdentity = $sessionClass::GetNamespace('MvcCore\Ext\Auths\Identity');
static::$sessionIdentity->SetExpirationSeconds(
\MvcCore\Ext\Auths\Basic::GetInstance()->GetExpirationIdentity()
);
}
return static::$sessionIdentity;
}
public static function SetSessionIdentity (\MvcCore\ISession $sessionIdentity) {
return static::$sessionIdentity = $sessionIdentity;
}
public static function GetSessionAuthorization () {
if (static::$sessionAuthorization === NULL) {
$sessionClass = self::$_sessionClass ?: (
self::$_sessionClass = \MvcCore\Application::GetInstance()->GetSessionClass()
);
static::$sessionAuthorization = $sessionClass::GetNamespace('MvcCore\Ext\Auths\Authorization');
static::$sessionAuthorization->SetExpirationSeconds(
\MvcCore\Ext\Auths\Basic::GetInstance()->GetExpirationAuthorization()
);
}
return static::$sessionAuthorization;
}
public static function SetSessionAuthorization (\MvcCore\ISession $sessionAuthorization) {
return static::$sessionAuthorization = $sessionAuthorization;
}
protected static function hashEquals ($hash1, $hash2) {
if (function_exists('hash_equals')) {
return hash_equals($hash1, $hash2);
} else {
if (strlen($hash1) != strlen($hash2)) {
return FALSE;
} else {
$res = $hash1 ^ $hash2;
$ret = 0;
for ($i = strlen($res) - 1; $i >= 0; $i--)
$ret |= ord($res[$i]);
return !$ret;
}
}
}
}