/**
  * {@inheritDoc}
  */
 public function start(Request $request, AuthenticationException $authException = null)
 {
     // redirect to the login url if there are several resource owners
     if (null === $this->resourceOwner) {
         return $this->httpUtils->createRedirectResponse($request, $this->loginPath);
     }
     // otherwise start authentication
     $authorizationUrl = $this->resourceOwner->getAuthorizationUrl($this->httpUtils->createRequest($request, $this->checkPath)->getUri());
     return $this->httpUtils->createRedirectResponse($request, $authorizationUrl);
 }
 public function loadUserByUsername($username)
 {
     $user = $this->userRepository->findOneBy(['spotifyId' => $username]);
     if (!$user instanceof SpotifyUser) {
         return new SpotifyUser($username);
     }
     if ($user->getAccessTokenExpires() <= time() + 10) {
         $refreshResponse = $this->resourceOwner->refreshAccessToken($user->getRefreshToken());
         $user->setAccessToken($refreshResponse['access_token']);
         $user->setAccessTokenExpires(time() + $refreshResponse['expires_in']);
         $this->em->persist($user);
         $this->em->flush();
     }
     return $user;
 }
Esempio n. 3
0
 function it_should_create_new_user_when_none_was_found($userManager, FactoryInterface $customerFactory, FactoryInterface $userFactory, FactoryInterface $oauthFactory, RepositoryInterface $oauthRepository, CustomerInterface $customer, UserInterface $user, UserResponseInterface $response, ResourceOwnerInterface $resourceOwner, UserOAuthInterface $oauth)
 {
     $resourceOwner->getName()->willReturn('google');
     $response->getEmail()->willReturn(null);
     $response->getUsername()->willReturn('username');
     $response->getNickname()->willReturn('user');
     $response->getResourceOwner()->willReturn($resourceOwner);
     $response->getAccessToken()->willReturn('access_token');
     $oauthRepository->findOneBy(array('provider' => 'google', 'identifier' => 'username'))->willReturn(null);
     $oauthFactory->createNew()->willReturn($oauth);
     $userFactory->createNew()->willReturn($user);
     $customerFactory->createNew()->willReturn($customer);
     $oauth->setIdentifier('username');
     $oauth->setProvider('google');
     $oauth->setAccessToken('access_token');
     $user->setCustomer($customer)->shouldBeCalled();
     $user->getUsername()->willReturn(null);
     $user->setUsername('user')->shouldBeCalled();
     $user->setPlainPassword('2ff2dfe363')->shouldBeCalled();
     $user->setEnabled(true)->shouldBeCalled();
     $user->addOAuthAccount($oauth)->shouldBeCalled();
     $userManager->persist($user)->shouldBeCalled();
     $userManager->flush()->shouldBeCalled();
     $this->loadUserByOAuthUserResponse($response)->shouldReturn($user);
 }
Esempio n. 4
0
 /**
  * @param Request                $request
  * @param ResourceOwnerInterface $resourceOwner
  *
  * @return string
  */
 public function getServiceAuthUrl(Request $request, ResourceOwnerInterface $resourceOwner)
 {
     if ($resourceOwner->getOption('auth_with_one_url')) {
         $redirectUrl = $this->httpUtils->generateUri($request, $this->ownerMap->getResourceOwnerCheckPath($resourceOwner->getName())) . '?authenticated=true';
     } else {
         $request->attributes->set('service', $resourceOwner->getName());
         $redirectUrl = $this->httpUtils->generateUri($request, 'hwi_oauth_connect_service');
     }
     return $redirectUrl;
 }
Esempio n. 5
0
 /**
  * @param Request                $request
  * @param ResourceOwnerInterface $resourceOwner
  *
  * @return string
  */
 public function getServiceAuthUrl(Request $request, ResourceOwnerInterface $resourceOwner)
 {
     if ($resourceOwner->getOption('auth_with_one_url')) {
         return $this->httpUtils->generateUri($request, $this->getResourceOwnerCheckPath($resourceOwner->getName()));
     }
     $request->attributes->set('service', $resourceOwner->getName());
     return $this->httpUtils->generateUri($request, 'hwi_oauth_connect_service');
 }
 /**
  * Key to for fetching or saving a token.
  *
  * @param ResourceOwnerInterface $resourceOwner
  * @param mixed $tokenId
  *
  * @return string
  */
 protected function generateKey(ResourceOwnerInterface $resourceOwner, $tokenId)
 {
     return implode('.', array('_hwi_oauth.request_token', $resourceOwner->getName(), $resourceOwner->getOption('client_id'), $tokenId));
 }
 /**
  * Key to for fetching or saving a token.
  *
  * @param ResourceOwnerInterface $resourceOwner
  * @param string                 $key
  * @param string                 $type
  *
  * @return string
  */
 protected function generateKey(ResourceOwnerInterface $resourceOwner, $key, $type)
 {
     return sprintf('_hwi_oauth.%s.%s.%s.%s', $resourceOwner->getName(), $resourceOwner->getOption('client_id'), $type, $key);
 }