| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | namespace SimpleID\Auth; |
| 24: | |
| 25: | use \Base; |
| 26: | use \Cache; |
| 27: | use \Prefab; |
| 28: | use Psr\Log\LogLevel; |
| 29: | use SimpleID\ModuleManager; |
| 30: | use SimpleID\Store\StoreManager; |
| 31: | use SimpleID\Crypt\Random; |
| 32: | use SimpleID\Crypt\OpaqueIdentifier; |
| 33: | use SimpleID\Util\Events\BaseDataCollectionEvent; |
| 34: | use SimpleID\Util\Forms\FormState; |
| 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: | class AuthManager extends Prefab { |
| 72: | const AUTH_LEVEL_SESSION = 0; |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | const AUTH_LEVEL_TOKEN = 1; |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | const AUTH_LEVEL_NON_INTERACTIVE = 2; |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | const AUTH_LEVEL_CREDENTIALS = 3; |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | const AUTH_LEVEL_REENTER_CREDENTIALS = 4; |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | const AUTH_LEVEL_VERIFIED = 5; |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | const MODE_IDENTIFY_USER = 0; |
| 115: | |
| 116: | |
| 117: | |
| 118: | const MODE_CREDENTIALS = self::AUTH_LEVEL_CREDENTIALS; |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | const MODE_REENTER_CREDENTIALS = self::AUTH_LEVEL_REENTER_CREDENTIALS; |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | const MODE_VERIFY = self::AUTH_LEVEL_VERIFIED; |
| 129: | |
| 130: | |
| 131: | static private $cookie_prefix = null; |
| 132: | |
| 133: | |
| 134: | protected $f3; |
| 135: | |
| 136: | |
| 137: | protected $cache; |
| 138: | |
| 139: | |
| 140: | protected $logger; |
| 141: | |
| 142: | |
| 143: | protected $mgr; |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | private $auth_info = []; |
| 152: | |
| 153: | |
| 154: | private $ua_login_state = null; |
| 155: | |
| 156: | public function __construct() { |
| 157: | $this->f3 = Base::instance(); |
| 158: | $this->cache = Cache::instance(); |
| 159: | $this->logger = $this->f3->get('logger'); |
| 160: | $this->mgr = ModuleManager::instance(); |
| 161: | } |
| 162: | |
| 163: | |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | public function initSession() { |
| 169: | $this->logger->log(LogLevel::DEBUG, 'SimpleID\Auth\AuthManager->initSession'); |
| 170: | |
| 171: | if (session_id() == '') { |
| 172: | |
| 173: | session_name($this->getCookieName('sess')); |
| 174: | session_start(); |
| 175: | $this->f3->sync('SESSION'); |
| 176: | } |
| 177: | } |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | public function initUser($allow_non_interactive = true) { |
| 191: | $this->logger->log(LogLevel::DEBUG, 'SimpleID\Auth\AuthManager->initUser'); |
| 192: | |
| 193: | if ($this->f3->exists('SESSION.auth') && ($this->cache->get(rawurlencode($this->f3->get('SESSION.auth.uid')) . '.login') == session_id())) { |
| 194: | $this->auth_info = $this->f3->get('SESSION.auth'); |
| 195: | |
| 196: | $store = StoreManager::instance(); |
| 197: | $user = $store->loadUser($this->auth_info['uid']); |
| 198: | $this->f3->set('user', $user); |
| 199: | } elseif ($allow_non_interactive) { |
| 200: | $event = new NonInteractiveAuthEvent(); |
| 201: | \Events::instance()->dispatch($event); |
| 202: | |
| 203: | if ($event->isAuthSuccessful()) { |
| 204: | $this->login($event); |
| 205: | return; |
| 206: | } |
| 207: | } |
| 208: | } |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | |
| 214: | |
| 215: | public function isLoggedIn() { |
| 216: | return (isset($this->auth_info['uid'])); |
| 217: | } |
| 218: | |
| 219: | |
| 220: | |
| 221: | |
| 222: | |
| 223: | |
| 224: | public function getUser() { |
| 225: | if ($this->isLoggedIn()) return $this->f3->get('user'); |
| 226: | return null; |
| 227: | } |
| 228: | |
| 229: | |
| 230: | |
| 231: | |
| 232: | |
| 233: | |
| 234: | public function getAuthLevel() { |
| 235: | return (isset($this->auth_info['level'])) ? $this->auth_info['level'] : null; |
| 236: | } |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | public function getAuthTime() { |
| 245: | return (isset($this->auth_info['time'])) ? $this->auth_info['time'] : null; |
| 246: | } |
| 247: | |
| 248: | |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | public function getACR() { |
| 255: | if (isset($this->auth_info['acr'])) { |
| 256: | return implode(' ', $this->auth_info['acr']); |
| 257: | } else { |
| 258: | return $this->f3->get('config.acr'); |
| 259: | } |
| 260: | } |
| 261: | |
| 262: | |
| 263: | |
| 264: | |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | public function login(AuthResultInterface $result, FormState $form_state = null) { |
| 276: | if ($form_state == null) $form_state = new FormState(); |
| 277: | |
| 278: | $store = StoreManager::instance(); |
| 279: | $user = $result->getUser(); |
| 280: | $level = $result->getAuthLevel(); |
| 281: | $modules = $result->getAuthModuleNames(); |
| 282: | $acr = $result->getACR(); |
| 283: | |
| 284: | if (($user == null) && isset($form_state['uid'])) { |
| 285: | $user = $store->loadUser($form_state['uid']); |
| 286: | } |
| 287: | |
| 288: | $this->f3->set('user', $user); |
| 289: | |
| 290: | $this->auth_info['uid'] = $user['uid']; |
| 291: | $this->auth_info['level'] = $level; |
| 292: | $this->auth_info['modules'] = $modules; |
| 293: | $this->auth_info['time'] = time(); |
| 294: | if (count($acr) > 0) $this->auth_info['acr'] = $acr; |
| 295: | |
| 296: | if ($level >= self::AUTH_LEVEL_NON_INTERACTIVE) { |
| 297: | $this->f3->set('SESSION.auth', $this->auth_info); |
| 298: | $this->cache->set(rawurlencode($user['uid']) . '.login', session_id()); |
| 299: | |
| 300: | $this->assignUALoginState(true); |
| 301: | } |
| 302: | if ($level > self::AUTH_LEVEL_NON_INTERACTIVE) |
| 303: | $this->logger->log(LogLevel::INFO, 'Login successful: ' . $user['uid']); |
| 304: | |
| 305: | $event = new LoginEvent($result, $form_state); |
| 306: | \Events::instance()->dispatch($event); |
| 307: | } |
| 308: | |
| 309: | |
| 310: | |
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: | public function onLoginEvent(LoginEvent $event) { |
| 316: | $store = StoreManager::instance(); |
| 317: | $user = $event->getUser(); |
| 318: | $level = $event->getAuthLevel(); |
| 319: | $form_state = $event->getFormState(); |
| 320: | $modules = $event->getAuthResult()->getAuthModuleNames(); |
| 321: | |
| 322: | if ($level > self::AUTH_LEVEL_NON_INTERACTIVE) { |
| 323: | if (!isset($form_state['auth_skip_activity'])) { |
| 324: | $activity = [ |
| 325: | 'type' => 'browser', |
| 326: | 'level' => $level, |
| 327: | 'modules' => $modules, |
| 328: | 'time' => $event->getTime()->getTimestamp(), |
| 329: | ]; |
| 330: | if ($event->getIP()) $activity['remote'] = $event->getIP(); |
| 331: | if ($event->getUserAgent()) $activity['ua'] = $event->getUserAgent(); |
| 332: | |
| 333: | $user->addActivity($this->assignUAID(), $activity); |
| 334: | $store->saveUser($user); |
| 335: | } |
| 336: | } |
| 337: | } |
| 338: | |
| 339: | |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | public function logout() { |
| 345: | $user = $this->getUser(); |
| 346: | |
| 347: | $event = new LogoutEvent($user); |
| 348: | \Events::instance()->dispatch($event); |
| 349: | |
| 350: | $this->cache->clear(rawurlencode($user['uid']) . '.login'); |
| 351: | $this->f3->clear('user'); |
| 352: | |
| 353: | session_unset(); |
| 354: | session_destroy(); |
| 355: | session_write_close(); |
| 356: | $this->f3->set('COOKIE.' . session_name(), ''); |
| 357: | session_start(); |
| 358: | |
| 359: | $this->assignUALoginState(true); |
| 360: | |
| 361: | $this->logger->log(LogLevel::INFO, 'Logout successful: ' . $user['uid']); |
| 362: | } |
| 363: | |
| 364: | |
| 365: | |
| 366: | |
| 367: | |
| 368: | |
| 369: | |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | |
| 379: | |
| 380: | |
| 381: | |
| 382: | public function assignUAID($reset = false) { |
| 383: | $name = 'COOKIE.' . $this->getCookieName('uaid'); |
| 384: | |
| 385: | if (($this->f3->exists($name) === true) && !$reset) { |
| 386: | $uaid = $this->f3->get($name); |
| 387: | } else { |
| 388: | $rand = new Random(); |
| 389: | $uaid = $rand->id(); |
| 390: | } |
| 391: | |
| 392: | $this->f3->set($name, $uaid, SIMPLEID_ETERNAL_TOKEN_EXPIRES_IN); |
| 393: | |
| 394: | return $uaid; |
| 395: | } |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 403: | |
| 404: | |
| 405: | |
| 406: | |
| 407: | |
| 408: | |
| 409: | |
| 410: | |
| 411: | |
| 412: | |
| 413: | |
| 414: | |
| 415: | public function assignUALoginState($reset = false) { |
| 416: | $name = $this->getCookieName('uals'); |
| 417: | if (($this->f3->exists('COOKIE.' . $name) === true) && !$reset) { |
| 418: | $this->ua_login_state = $this->f3->get('COOKIE.' . $name); |
| 419: | } else { |
| 420: | $rand = new Random(); |
| 421: | $opaque = new OpaqueIdentifier(); |
| 422: | |
| 423: | $this->ua_login_state = $opaque->generate($this->assignUAID() . ':' . $rand->id()); |
| 424: | |
| 425: | |
| 426: | |
| 427: | setcookie($this->getCookieName('uals'), $this->ua_login_state, 0, $this->f3->get('BASE'), '', true, false); |
| 428: | } |
| 429: | |
| 430: | return $this->ua_login_state; |
| 431: | } |
| 432: | |
| 433: | |
| 434: | |
| 435: | |
| 436: | |
| 437: | |
| 438: | |
| 439: | public function getCookieName($suffix) { |
| 440: | if (self::$cookie_prefix == NULL) { |
| 441: | $opaque = new OpaqueIdentifier(); |
| 442: | self::$cookie_prefix = substr($opaque->generate('cookie'), -9) . '_'; |
| 443: | } |
| 444: | return self::$cookie_prefix . $suffix; |
| 445: | } |
| 446: | |
| 447: | |
| 448: | |
| 449: | |
| 450: | public function toString() { |
| 451: | return print_r($this->auth_info, true); |
| 452: | } |
| 453: | } |
| 454: | |
| 455: | |
| 456: | ?> |
| 457: | |