SaveHandler
1 month ago
SessionAuth.php
4 months ago
SessionFingerprint.php
2 weeks ago
SessionInitializer.php
1 year ago
SessionNamespace.php
1 year ago
SessionInitializer.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Session; |
| 10 | |
| 11 | use Exception; |
| 12 | use Piwik\Access; |
| 13 | use Piwik\Auth as AuthInterface; |
| 14 | use Piwik\AuthResult; |
| 15 | use Piwik\Piwik; |
| 16 | use Piwik\Session; |
| 17 | /** |
| 18 | * Initializes authenticated sessions using an Auth implementation. |
| 19 | */ |
| 20 | class SessionInitializer |
| 21 | { |
| 22 | /** |
| 23 | * Authenticates the user and, if successful, initializes an authenticated session. |
| 24 | * |
| 25 | * @param \Piwik\Auth $auth The Auth implementation to use. |
| 26 | * @throws Exception If authentication fails or the user is not allowed to login for some reason. |
| 27 | */ |
| 28 | public function initSession(AuthInterface $auth) |
| 29 | { |
| 30 | $this->regenerateSessionId(); |
| 31 | $authResult = $this->doAuthenticateSession($auth); |
| 32 | if (!$authResult->wasAuthenticationSuccessful()) { |
| 33 | Piwik::postEvent('Login.authenticate.failed', array($auth->getLogin())); |
| 34 | $this->processFailedSession(); |
| 35 | } else { |
| 36 | Piwik::postEvent('Login.authenticate.successful', array($auth->getLogin())); |
| 37 | $this->processSuccessfulSession($authResult); |
| 38 | } |
| 39 | } |
| 40 | /** |
| 41 | * Authenticates the user. |
| 42 | * |
| 43 | * Derived classes can override this method to customize authentication logic or impose |
| 44 | * extra requirements on the user trying to login. |
| 45 | * |
| 46 | * @param AuthInterface $auth The Auth implementation to use when authenticating. |
| 47 | * @return AuthResult |
| 48 | */ |
| 49 | protected function doAuthenticateSession(AuthInterface $auth) |
| 50 | { |
| 51 | Piwik::postEvent('Login.authenticate', array($auth->getLogin())); |
| 52 | return $auth->authenticate(); |
| 53 | } |
| 54 | /** |
| 55 | * Executed when the session could not authenticate. |
| 56 | * |
| 57 | * @throws Exception always. |
| 58 | */ |
| 59 | protected function processFailedSession() |
| 60 | { |
| 61 | throw new Exception(Piwik::translate('Login_LoginPasswordNotCorrect')); |
| 62 | } |
| 63 | /** |
| 64 | * Executed when the session was successfully authenticated. |
| 65 | * |
| 66 | * @param AuthResult $authResult The successful authentication result. |
| 67 | */ |
| 68 | protected function processSuccessfulSession(AuthResult $authResult) |
| 69 | { |
| 70 | $sessionIdentifier = new \Piwik\Session\SessionFingerprint(); |
| 71 | $sessionIdentifier->initialize($authResult->getIdentity(), $authResult->getTokenAuth(), $this->isRemembered()); |
| 72 | // reload access |
| 73 | Access::getInstance()->reloadAccess(); |
| 74 | /** |
| 75 | * @ignore |
| 76 | */ |
| 77 | Piwik::postEvent('Login.authenticate.processSuccessfulSession.end', array($authResult->getIdentity())); |
| 78 | } |
| 79 | protected function regenerateSessionId() |
| 80 | { |
| 81 | Session::regenerateId(); |
| 82 | } |
| 83 | private function isRemembered() |
| 84 | { |
| 85 | $cookieParams = session_get_cookie_params(); |
| 86 | return $cookieParams['lifetime'] > 0; |
| 87 | } |
| 88 | } |
| 89 |