| 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 Psr\Log\LogLevel; |
| 26: | use SimpleID\Module; |
| 27: | use SimpleID\ModuleManager; |
| 28: | use SimpleID\Crypt\SecurityToken; |
| 29: | use SimpleID\Util\Events\GenericStoppableEvent; |
| 30: | use SimpleID\Util\Forms\FormState; |
| 31: | use SimpleID\Util\Forms\FormBuildEvent; |
| 32: | use SimpleID\Util\Forms\FormSubmitEvent; |
| 33: | use SimpleID\Util\UI\Template; |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | class AuthModule extends Module { |
| 45: | |
| 46: | private $auth; |
| 47: | |
| 48: | static function init($f3) { |
| 49: | $f3->route('GET|POST /auth/login', 'SimpleID\Auth\AuthModule->login'); |
| 50: | $f3->route('GET|POST @auth_login: /auth/login/*', 'SimpleID\Auth\AuthModule->login'); |
| 51: | $f3->route('GET /auth/logout', 'SimpleID\Auth\AuthModule->logout'); |
| 52: | $f3->route('GET @auth_logout: /auth/logout/*', 'SimpleID\Auth\AuthModule->logout'); |
| 53: | } |
| 54: | |
| 55: | public function __construct() { |
| 56: | parent::__construct(); |
| 57: | $this->auth = AuthManager::instance(); |
| 58: | } |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public function beforeroute() { |
| 68: | $this->auth->initSession(); |
| 69: | $this->auth->initUser(false); |
| 70: | } |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | public function login($f3, $params) { |
| 81: | $dispatcher = \Events::instance(); |
| 82: | |
| 83: | $params['destination'] = (isset($params['*'])) ? $params['*'] : ''; |
| 84: | $this->f3->set('PARAMS.destination', $params['destination']); |
| 85: | |
| 86: | $token = new SecurityToken(); |
| 87: | |
| 88: | |
| 89: | $this->checkHttps('error', true); |
| 90: | |
| 91: | if (($this->f3->exists('POST.fs') === false)) { |
| 92: | $form_state = new FormState([ 'mode' => AuthManager::MODE_CREDENTIALS ]); |
| 93: | if (in_array($this->f3->get('GET.mode'), [ AuthManager::MODE_VERIFY, AuthManager::MODE_REENTER_CREDENTIALS ])) { |
| 94: | $form_state['mode'] = $this->f3->get('GET.mode'); |
| 95: | } |
| 96: | $this->loginForm($params, $form_state); |
| 97: | return; |
| 98: | } |
| 99: | |
| 100: | $form_state = FormState::decode($token->getPayload($this->f3->get('POST.fs'))); |
| 101: | if (count($form_state) == 0) $form_state['mode'] = AuthManager::MODE_CREDENTIALS; |
| 102: | $mode = $form_state['mode']; |
| 103: | if (!in_array($mode, [ AuthManager::MODE_CREDENTIALS, AuthManager::MODE_REENTER_CREDENTIALS, AuthManager::MODE_VERIFY ])) { |
| 104: | $this->f3->set('message', $this->f3->get('intl.core.auth.state_error')); |
| 105: | $this->loginForm($params, $form_state); |
| 106: | return; |
| 107: | } |
| 108: | |
| 109: | if ($this->f3->exists('POST.tk') === false) { |
| 110: | if ($params['destination']) { |
| 111: | |
| 112: | $this->f3->set('message', $this->f3->get('intl.core.auth.missing_tk')); |
| 113: | } |
| 114: | $this->loginForm($params, $form_state); |
| 115: | return; |
| 116: | } |
| 117: | |
| 118: | if (!$token->verify($this->f3->get('POST.tk'), 'login')) { |
| 119: | $this->logger->log(LogLevel::WARNING, 'Login attempt: Security token ' . $this->f3->get('POST.tk') . ' invalid.'); |
| 120: | $this->f3->set('message', $this->f3->get('intl.core.auth.state_error')); |
| 121: | $this->loginForm($params, $form_state); |
| 122: | return; |
| 123: | } |
| 124: | |
| 125: | if ($this->f3->exists('POST.op') && $this->f3->get('POST.op') == 'cancel') { |
| 126: | $cancel_event = new FormSubmitEvent($form_state, 'login_form_cancel'); |
| 127: | |
| 128: | $dispatcher->dispatch($cancel_event); |
| 129: | |
| 130: | |
| 131: | if (!$cancel_event->isPropagationStopped()) { |
| 132: | $this->fatalError($this->f3->get('intl.core.auth.cancelled'), 400); |
| 133: | } |
| 134: | return; |
| 135: | } |
| 136: | |
| 137: | |
| 138: | if (($mode == AuthManager::MODE_CREDENTIALS) && $this->auth->isLoggedIn()) $this->f3->reroute('/'); |
| 139: | |
| 140: | $validate_event = new FormSubmitEvent($form_state, 'login_form_validate'); |
| 141: | $dispatcher->dispatch($validate_event); |
| 142: | if (!$validate_event->isValid()) { |
| 143: | $this->f3->set('message', $validate_event->getMessages()); |
| 144: | $this->loginForm($params, $form_state); |
| 145: | return; |
| 146: | } |
| 147: | |
| 148: | $submit_event = new LoginFormSubmitEvent($form_state, 'login_form_submit'); |
| 149: | $dispatcher->dispatch($submit_event); |
| 150: | if (!$submit_event->isValid()) { |
| 151: | $this->f3->set('message', $submit_event->getMessages()); |
| 152: | $this->loginForm($params, $form_state); |
| 153: | return; |
| 154: | } |
| 155: | |
| 156: | if ($submit_event->isAuthSuccessful()) { |
| 157: | |
| 158: | |
| 159: | $test_user = $submit_event->getUser(); |
| 160: | if ($test_user != null) $form_state['uid'] = $test_user['uid']; |
| 161: | |
| 162: | $form_state['auth_level'] = $submit_event->getAuthLevel(); |
| 163: | $form_state['modules'] = $submit_event->getAuthModuleNames(); |
| 164: | } else { |
| 165: | $this->loginForm($params, $form_state); |
| 166: | return; |
| 167: | } |
| 168: | |
| 169: | if (!isset($form_state['uid'])) { |
| 170: | |
| 171: | $this->loginForm($params, $form_state); |
| 172: | return; |
| 173: | } |
| 174: | |
| 175: | if ($mode == AuthManager::MODE_CREDENTIALS) { |
| 176: | $form_state['mode'] = AuthManager::MODE_VERIFY; |
| 177: | $event = new FormBuildEvent($form_state, 'login_form_build'); |
| 178: | |
| 179: | $dispatcher->dispatch($event); |
| 180: | if (count($event->getBlocks()) > 0) { |
| 181: | $this->loginForm($params, $form_state); |
| 182: | return; |
| 183: | } |
| 184: | } |
| 185: | |
| 186: | $this->auth->login($submit_event, $form_state); |
| 187: | |
| 188: | $this->f3->reroute('/' . $params['destination']); |
| 189: | } |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | public function logout($f3, $params) { |
| 199: | $params['destination'] = (isset($params['*'])) ? $params['*'] : ''; |
| 200: | $this->f3->set('PARAMS.destination', $params['destination']); |
| 201: | |
| 202: | |
| 203: | $this->checkHttps('redirect', true); |
| 204: | |
| 205: | $this->auth->logout(); |
| 206: | |
| 207: | $event = new GenericStoppableEvent('post_logout'); |
| 208: | \Events::instance()->dispatch($event); |
| 209: | |
| 210: | if (!$event->isPropagationStopped()) { |
| 211: | if ($params['destination']) { |
| 212: | $this->f3->reroute('/' . $params['destination']); |
| 213: | } else { |
| 214: | $this->f3->set('message', $this->f3->get('intl.core.auth.logout_success')); |
| 215: | $this->loginForm($params); |
| 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: | public function loginForm($params = [ 'destination' => null ], $form_state = null) { |
| 243: | $tpl = Template::instance(); |
| 244: | $config = $this->f3->get('config'); |
| 245: | if ($form_state == null) $form_state = new FormState([ 'mode' => AuthManager::MODE_CREDENTIALS ]); |
| 246: | |
| 247: | |
| 248: | $this->checkHttps('redirect', true); |
| 249: | |
| 250: | |
| 251: | if (($form_state['mode'] == AuthManager::MODE_VERIFY) && isset($form_state['verify_forms'])) { |
| 252: | $forms = $form_state['verify_forms']; |
| 253: | unset($form_state['verify_forms']); |
| 254: | } else { |
| 255: | $event = new FormBuildEvent($form_state, 'login_form_build'); |
| 256: | \Events::instance()->dispatch($event); |
| 257: | $forms = $event->getBlocks(); |
| 258: | $tpl->mergeAttachments($event); |
| 259: | } |
| 260: | $this->f3->set('forms', $forms); |
| 261: | |
| 262: | |
| 263: | switch ($form_state['mode']) { |
| 264: | case AuthManager::MODE_REENTER_CREDENTIALS: |
| 265: | |
| 266: | $this->f3->set('uid', $form_state['uid']); |
| 267: | case AuthManager::MODE_CREDENTIALS: |
| 268: | $this->f3->set('submit_button', $this->f3->get('intl.common.login')); |
| 269: | $this->f3->set('title', $this->f3->get('intl.common.login')); |
| 270: | break; |
| 271: | case AuthManager::MODE_VERIFY: |
| 272: | if (count($forms) == 0) return; |
| 273: | $this->f3->set('submit_button', $this->f3->get('intl.common.verify')); |
| 274: | $this->f3->set('title', $this->f3->get('intl.common.verify')); |
| 275: | } |
| 276: | |
| 277: | if (isset($form_state['cancel'])) { |
| 278: | $this->f3->set('cancellable', true); |
| 279: | } |
| 280: | |
| 281: | |
| 282: | |
| 283: | $token = new SecurityToken(); |
| 284: | $this->f3->set('tk', $token->generate('login', SecurityToken::OPTION_NONCE)); |
| 285: | |
| 286: | $this->f3->set('fs', $token->generate($form_state->encode())); |
| 287: | if (isset($params['destination'])) $this->f3->set('destination', $params['destination']); |
| 288: | $this->f3->set('page_class', 'is-dialog-page'); |
| 289: | $this->f3->set('layout', 'auth_login.html'); |
| 290: | |
| 291: | header('X-Frame-Options: DENY'); |
| 292: | print $tpl->render('page.html'); |
| 293: | } |
| 294: | } |
| 295: | |
| 296: | ?> |