You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
oauth-account/Security/Core/User/OAuthUserProvider.php

154 lines
5.1 KiB

7 years ago
<?php
/**
* Authserver, an OAuth2-based single-signon authentication provider written in PHP.
*
* Copyright (C) $today.date Lars Vierbergen
*
* his program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace vierbergenlars\AuthserverOAuthAccountBundle\Security\Core\User;
use App\Entity\User;
use App\Security\User\UserProvider;
use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\User\UserInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface;
7 years ago
use HWI\Bundle\OAuthBundle\Connect\AccountConnectorInterface;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
use vierbergenlars\AuthserverExternalAccountBundle\Entity\ExternalUser;
use vierbergenlars\AuthserverOAuthAccountBundle\Entity\TemporaryUser;
7 years ago
class OAuthUserProvider extends UserProvider implements OAuthAwareUserProviderInterface, AccountConnectorInterface
{
7 years ago
/**
*
7 years ago
* @var ManagerRegistry
*/
private $registry;
/**
*
* @var boolean
*/
private $allowRegistrations;
public function __construct(ManagerRegistry $registry, $allowRegistrations)
7 years ago
{
parent::__construct($registry);
$this->registry = $registry;
$this->allowRegistrations = $allowRegistrations;
7 years ago
}
/**
* Loads the user by a given UserResponseInterface object.
*
* @param UserResponseInterface $response
*
* @return UserInterface
*
* @throws AccountNotLinkedException if the provider is not linked to an account
*/
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
try {
return $this->getExternalAccount($response)->getUser();
} catch (AccountNotLinkedException $ex) {
if (!$this->allowRegistrations) {
throw $ex;
}
$user = new TemporaryUser();
$externalUser = $this->createExternalUser($response);
$user->setExternalUser($externalUser);
if ($response->getEmail())
$user->setEmail($response->getEmail());
return $user;
}
}
private function createExternalUser(UserResponseInterface $response)
{
$externalUser = new ExternalUser();
$externalUser->setProvider('oauth_' . $response->getResourceOwner()
->getName());
$externalUser->setProviderRef($response->getUsername());
$externalUser->setProviderFriendlyName($response->getRealName());
return $externalUser;
7 years ago
}
public function connect(UserInterface $user, UserResponseInterface $response)
{
if (!$user instanceof User)
throw new \UnexpectedValueException('User must be instance of ' . User::class . ', got ' . get_class($user));
7 years ago
try {
$externalUser = $this->getExternalAccount($response);
$this->disconnect($externalUser);
} catch (AccountNotLinkedException $ex) {
7 years ago
// do nothing
}
$externalUser = $this->createExternalUser($response);
7 years ago
$externalUser->setUser($user);
$this->getManager()->persist($externalUser);
$this->getManager()->flush();
}
public function disconnect(ExternalUser $externalUser)
{
$this->getManager()->remove($externalUser);
$this->getManager()->flush();
}
/**
*
7 years ago
* @return \Doctrine\Common\Persistence\ObjectRepository
*/
private function getRepo()
{
return $this->getManager()->getRepository('AuthserverExternalAccountBundle:ExternalUser');
}
/**
*
7 years ago
* @return \Doctrine\Common\Persistence\ObjectManager|null
*/
private function getManager()
{
return $this->registry->getManagerForClass('AuthserverExternalAccountBundle:ExternalUser');
}
/**
*
7 years ago
* @param UserResponseInterface $response
* @return ExternalUser
*/
private function getExternalAccount(UserResponseInterface $response)
{
$repo = $this->getRepo();
$externalAccount = $repo->findOneBy([
'provider' => 'oauth_' . $response->getResourceOwner()
->getName(),
7 years ago
'provider_ref' => $response->getUsername()
]);
if (!$externalAccount) {
7 years ago
throw new AccountNotLinkedException(sprintf('No external account registered for provider "%s", ref: "%s"', $response->getResourceOwner()->getName(), $response->getUsername()));
}
return $externalAccount;
}
}