Loading classes/UserHandler.php +5 −2 Original line number Diff line number Diff line Loading @@ -95,8 +95,11 @@ class UserHandler { //show information regarding the error curl_close($conn); error_log($response); http_response_code(500); die('Error: GMS response code: ' . $info['http_code'] . "\n"); $httpCode = $info['http_code']; if ($httpCode === 0) { throw new ServerErrorException('GMS service is unreachable'); } throw new ServerErrorException('Error: GMS response code: ' . $httpCode); } } Loading classes/datalayer/mysql/MySQLUserDAO.php +4 −4 Original line number Diff line number Diff line Loading @@ -37,8 +37,8 @@ class MySQLUserDAO extends BaseMySQLDAO implements UserDAO { $dbh = $this->getDBHandler(); $stmt = $dbh->prepare("INSERT INTO identity(`user_id`, `type`, `email`, `name`, `surname`, `institution`, `typed_id`, `eppn`)" . " VALUES(:user_id, :type, :email, :name, :surname, :institution, :typed_id, :eppn)"); $stmt = $dbh->prepare("INSERT INTO identity(`user_id`, `type`, `email`, `name`, `surname`, `institution`, `typed_id`, `eppn`, `last_login`)" . " VALUES(:user_id, :type, :email, :name, :surname, :institution, :typed_id, :eppn, NOW())"); $stmt->bindParam(':user_id', $userId); $stmt->bindParam(':type', $identity->type); Loading Loading @@ -288,8 +288,8 @@ class MySQLUserDAO extends BaseMySQLDAO implements UserDAO { $dbh = $this->getDBHandler(); $query = "UPDATE identity SET email = :email, name = :name, surname = :surname, institution = :institution" . " WHERE id = :id"; $query = "UPDATE identity SET email = :email, name = :name, surname = :surname, institution = :institution," . " last_login = NOW() WHERE id = :id"; $stmt = $dbh->prepare($query); $stmt->bindParam(':email', $identity->email); Loading classes/exceptions/ServerErrorException.php 0 → 100644 +13 −0 Original line number Diff line number Diff line <?php namespace RAP; class ServerErrorException extends \Exception { public $message; public function __construct($message) { $this->message = $message; } } classes/login/LoginHandler.php +49 −25 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ class LoginHandler { public function __construct(Locator $locator, string $identityType) { $this->locator = $locator; $this->identityType = $identityType; $this->locator->getSession()->setLoginIdentityType($identityType); } public function onIdentityDataReceived(string $typedId, \Closure $fillIdentityData): string { Loading @@ -22,6 +23,12 @@ class LoginHandler { $this->updateUser($user, $typedId, $fillIdentityData); } $session = $this->locator->getSession(); if ($session->getOAuth2RequestData() === null && $session->getAction() === 'join' && $session->getUser() !== null && $session->getUser()->id !== $user->id) { return $this->showConfirmJoin($user); } return $this->getAfterLoginRedirect($user); } Loading @@ -30,25 +37,16 @@ class LoginHandler { $session = $this->locator->getSession(); if ($session->getUser() !== null && $session->getAction() === 'join') { return $this->joinToPreviousUser($session->getUser(), $typedId, $fillIdentityData); $userToJoin = $this->getNewUser($typedId, $fillIdentityData); return $this->showConfirmJoin($userToJoin); } else { return $this->redirectToTOUCheck($typedId, $fillIdentityData); } } private function joinToPreviousUser(User $user, string $typedId, \Closure $fillIdentityData): string { $identity = new Identity($this->identityType); $identity->typedId = $typedId; $fillIdentityData($identity); $user->addIdentity($identity); $this->locator->getUserHandler()->saveUser($user); $this->locator->getSession()->setUser($user); return $this->getAfterLoginRedirect($user); private function showConfirmJoin(User $userToJoin): string { $this->locator->getSession()->setUserToJoin($userToJoin); return $this->locator->getBasePath() . '/confirm-join'; } /** Loading @@ -57,19 +55,25 @@ class LoginHandler { private function redirectToTOUCheck(string $typedId, \Closure $fillIdentityData): string { // Create new user $user = new \RAP\User(); $user = $this->getNewUser($typedId, $fillIdentityData); $this->locator->getSession()->setUser($user); return $this->locator->getBasePath() . '/tou-check'; } private function getNewUser(string $typedId, \Closure $fillIdentityData): User { $user = new User(); $identity = new Identity($this->identityType); $identity->typedId = $typedId; $fillIdentityData($identity); $user->addIdentity($identity); $this->locator->getSession()->setUser($user); return $this->locator->getBasePath() . '/tou-check'; return $user; } /** * Update user with fresh information received by IdP. Useful for keeping email address always updated. */ private function updateUser(User $user, string $typedId, \Closure $fillIdentityData): void { $identity = $user->getIdentityByTypedId($typedId); $fillIdentityData($identity); Loading @@ -93,11 +97,7 @@ class LoginHandler { $action = $session->getAction(); if ($action === 'join') { if ($session->getUser()->id !== $user->id) { $user = $this->locator->getUserHandler()->joinUsers($session->getUser(), $user); } // the join is completed $user = $this->joinTo($user); $action = 'account'; $session->setAction($action); } Loading @@ -112,4 +112,28 @@ class LoginHandler { throw new \Exception("Unable to find a proper redirect"); } private function joinTo(User $userToJoin): User { $session = $this->locator->getSession(); $user = $session->getUser(); if ($user === null) { return $userToJoin; } if ($userToJoin->id === null) { // New identity, not yet associated with an user: simply add it to // previously logged in user. $identity = $userToJoin->identities[0]; $user->addIdentity($identity); $this->locator->getUserHandler()->saveUser($user); } else if ($user->id !== $userToJoin->id) { $user = $this->locator->getUserHandler()->joinUsers($user, $userToJoin); } $session->setUserToJoin(null); return $user; } } classes/model/SessionData.php +20 −0 Original line number Diff line number Diff line Loading @@ -33,9 +33,11 @@ class SessionData { const KEY = "SessionData"; private $user; private $userToJoin; private $x509DataToRegister; private $oauth2RequestData; private $action; private $loginIdentityType; public function setUser(?User $user): void { $this->user = $user; Loading @@ -46,6 +48,24 @@ class SessionData { return $this->user; } public function setUserToJoin(?User $userToJoin): void { $this->userToJoin = $userToJoin; $this->save(); } public function getUserToJoin(): ?User { return $this->userToJoin; } public function setLoginIdentityType(string $loginIdentityType): void { $this->loginIdentityType = $loginIdentityType; $this->save(); } public function getLoginIdentityType(): ?string { return $this->loginIdentityType; } /** * Update the user data model stored into the session after the primary * identity has changed, in order to avoid reading again the user data from Loading Loading
classes/UserHandler.php +5 −2 Original line number Diff line number Diff line Loading @@ -95,8 +95,11 @@ class UserHandler { //show information regarding the error curl_close($conn); error_log($response); http_response_code(500); die('Error: GMS response code: ' . $info['http_code'] . "\n"); $httpCode = $info['http_code']; if ($httpCode === 0) { throw new ServerErrorException('GMS service is unreachable'); } throw new ServerErrorException('Error: GMS response code: ' . $httpCode); } } Loading
classes/datalayer/mysql/MySQLUserDAO.php +4 −4 Original line number Diff line number Diff line Loading @@ -37,8 +37,8 @@ class MySQLUserDAO extends BaseMySQLDAO implements UserDAO { $dbh = $this->getDBHandler(); $stmt = $dbh->prepare("INSERT INTO identity(`user_id`, `type`, `email`, `name`, `surname`, `institution`, `typed_id`, `eppn`)" . " VALUES(:user_id, :type, :email, :name, :surname, :institution, :typed_id, :eppn)"); $stmt = $dbh->prepare("INSERT INTO identity(`user_id`, `type`, `email`, `name`, `surname`, `institution`, `typed_id`, `eppn`, `last_login`)" . " VALUES(:user_id, :type, :email, :name, :surname, :institution, :typed_id, :eppn, NOW())"); $stmt->bindParam(':user_id', $userId); $stmt->bindParam(':type', $identity->type); Loading Loading @@ -288,8 +288,8 @@ class MySQLUserDAO extends BaseMySQLDAO implements UserDAO { $dbh = $this->getDBHandler(); $query = "UPDATE identity SET email = :email, name = :name, surname = :surname, institution = :institution" . " WHERE id = :id"; $query = "UPDATE identity SET email = :email, name = :name, surname = :surname, institution = :institution," . " last_login = NOW() WHERE id = :id"; $stmt = $dbh->prepare($query); $stmt->bindParam(':email', $identity->email); Loading
classes/exceptions/ServerErrorException.php 0 → 100644 +13 −0 Original line number Diff line number Diff line <?php namespace RAP; class ServerErrorException extends \Exception { public $message; public function __construct($message) { $this->message = $message; } }
classes/login/LoginHandler.php +49 −25 Original line number Diff line number Diff line Loading @@ -10,6 +10,7 @@ class LoginHandler { public function __construct(Locator $locator, string $identityType) { $this->locator = $locator; $this->identityType = $identityType; $this->locator->getSession()->setLoginIdentityType($identityType); } public function onIdentityDataReceived(string $typedId, \Closure $fillIdentityData): string { Loading @@ -22,6 +23,12 @@ class LoginHandler { $this->updateUser($user, $typedId, $fillIdentityData); } $session = $this->locator->getSession(); if ($session->getOAuth2RequestData() === null && $session->getAction() === 'join' && $session->getUser() !== null && $session->getUser()->id !== $user->id) { return $this->showConfirmJoin($user); } return $this->getAfterLoginRedirect($user); } Loading @@ -30,25 +37,16 @@ class LoginHandler { $session = $this->locator->getSession(); if ($session->getUser() !== null && $session->getAction() === 'join') { return $this->joinToPreviousUser($session->getUser(), $typedId, $fillIdentityData); $userToJoin = $this->getNewUser($typedId, $fillIdentityData); return $this->showConfirmJoin($userToJoin); } else { return $this->redirectToTOUCheck($typedId, $fillIdentityData); } } private function joinToPreviousUser(User $user, string $typedId, \Closure $fillIdentityData): string { $identity = new Identity($this->identityType); $identity->typedId = $typedId; $fillIdentityData($identity); $user->addIdentity($identity); $this->locator->getUserHandler()->saveUser($user); $this->locator->getSession()->setUser($user); return $this->getAfterLoginRedirect($user); private function showConfirmJoin(User $userToJoin): string { $this->locator->getSession()->setUserToJoin($userToJoin); return $this->locator->getBasePath() . '/confirm-join'; } /** Loading @@ -57,19 +55,25 @@ class LoginHandler { private function redirectToTOUCheck(string $typedId, \Closure $fillIdentityData): string { // Create new user $user = new \RAP\User(); $user = $this->getNewUser($typedId, $fillIdentityData); $this->locator->getSession()->setUser($user); return $this->locator->getBasePath() . '/tou-check'; } private function getNewUser(string $typedId, \Closure $fillIdentityData): User { $user = new User(); $identity = new Identity($this->identityType); $identity->typedId = $typedId; $fillIdentityData($identity); $user->addIdentity($identity); $this->locator->getSession()->setUser($user); return $this->locator->getBasePath() . '/tou-check'; return $user; } /** * Update user with fresh information received by IdP. Useful for keeping email address always updated. */ private function updateUser(User $user, string $typedId, \Closure $fillIdentityData): void { $identity = $user->getIdentityByTypedId($typedId); $fillIdentityData($identity); Loading @@ -93,11 +97,7 @@ class LoginHandler { $action = $session->getAction(); if ($action === 'join') { if ($session->getUser()->id !== $user->id) { $user = $this->locator->getUserHandler()->joinUsers($session->getUser(), $user); } // the join is completed $user = $this->joinTo($user); $action = 'account'; $session->setAction($action); } Loading @@ -112,4 +112,28 @@ class LoginHandler { throw new \Exception("Unable to find a proper redirect"); } private function joinTo(User $userToJoin): User { $session = $this->locator->getSession(); $user = $session->getUser(); if ($user === null) { return $userToJoin; } if ($userToJoin->id === null) { // New identity, not yet associated with an user: simply add it to // previously logged in user. $identity = $userToJoin->identities[0]; $user->addIdentity($identity); $this->locator->getUserHandler()->saveUser($user); } else if ($user->id !== $userToJoin->id) { $user = $this->locator->getUserHandler()->joinUsers($user, $userToJoin); } $session->setUserToJoin(null); return $user; } }
classes/model/SessionData.php +20 −0 Original line number Diff line number Diff line Loading @@ -33,9 +33,11 @@ class SessionData { const KEY = "SessionData"; private $user; private $userToJoin; private $x509DataToRegister; private $oauth2RequestData; private $action; private $loginIdentityType; public function setUser(?User $user): void { $this->user = $user; Loading @@ -46,6 +48,24 @@ class SessionData { return $this->user; } public function setUserToJoin(?User $userToJoin): void { $this->userToJoin = $userToJoin; $this->save(); } public function getUserToJoin(): ?User { return $this->userToJoin; } public function setLoginIdentityType(string $loginIdentityType): void { $this->loginIdentityType = $loginIdentityType; $this->save(); } public function getLoginIdentityType(): ?string { return $this->loginIdentityType; } /** * Update the user data model stored into the session after the primary * identity has changed, in order to avoid reading again the user data from Loading