saml = $saml; $this->users = $users; $this->tokens = $tokens; } /** * @inheritDoc */ public function attempt(Presenter $presenter, array $credentials): void { $email = $credentials['email'] ?? null; $password = $credentials['password'] ?? null; if (!$email || !$password) { throw new AuthenticationException(); } $user = $this->users->findByEmail(strtolower($email)); if (!$user || !$user->getPassword() || !$user->getPassword()->matches($password)) { throw new AuthenticationException(); } $token = $this->tokens->createLoginToken($user->getId()); $response = new ResponseModel($user, $token); $presenter->present($response); } /** * @inheritDoc */ public function handToSaml(array $options = []): string { return $this->saml->login($options); } /** * @inheritDoc */ public function handleSamlLogin(Presenter $presenter): void { $samlUser = $this->saml->handleLogin(); if (!$samlUser) { throw new UserCreationException(); } // First check to see if the user exists in the database. $user = $this->users->findByEmail($samlUser->getEmail()); // If the user does not exist, create them. if (!$user) { $user = $this->users->create(new User( 0, $samlUser->getFirstName(), $samlUser->getLastName(), $samlUser->getDisplayName(), $samlUser->getEmail(), $samlUser->getEmplid() )); } else { $user = $this->users->update($user->getId(), new User( $user->getId(), $samlUser->getFirstName(), $samlUser->getLastName(), $samlUser->getDisplayName(), $samlUser->getEmail(), $samlUser->getEmplid(), $user->getPassword(), $user->getDoorcode(), $user->getExpiresAt(), $user->getCreatedAt(), $user->getUpdatedAt() )); } if (!$user) { throw new UserCreationException(); } $token = $this->tokens->createLoginToken($user->getId()); $response = new ResponseModel($user, $token); $presenter->present($response); } /** * @inheritDoc */ public function samlLogout(?string $token): string { if ($token) { $this->tokens->invalidateToken($token); } return $this->saml->logout(); } }