/** * {@inheritDoc} */ public function authenticate(TokenInterface $token) { $ownerName = $token->getResourceOwnerName(); $oauthUtil = $this->container->get('glory_oauth.util.token2oauth'); $oauth = $oauthUtil->generate($token); $connect = $this->container->get('glory_oauth.connect'); if (!($user = $connect->getConnect($oauth))) { if ($this->container->getParameter('glory_oauth.auto_register')) { $user = $connect->connect($oauth); } else { $key = time(); $this->container->get('session')->set('glory_oauth.connect.oauth.' . $key, [$oauth->getOwner(), $oauth->getUsername()]); $url = $this->container->get('router')->generate('glory_oauth_register', ['key' => $key]); return new RedirectResponse($url); } } if (!$user instanceof UserInterface) { throw new BadCredentialsException(''); } try { $this->userChecker->checkPreAuth($user); $this->userChecker->checkPostAuth($user); } catch (BadCredentialsException $e) { if ($this->hideUserNotFoundExceptions) { throw new BadCredentialsException('Bad credentials', 0, $e); } throw $e; } $token = new OAuthToken($token->getRawToken(), $user->getRoles()); $token->setOwnerName($ownerName); $token->setUser($user); $token->setAuthenticated(true); return $token; }
/** * {@inheritDoc} */ protected function attemptAuthentication(Request $request) { $this->handleOAuthError($request); /* @var ResourceOwnerInterface $owner */ $owner = $this->owner; if (!$owner) { throw new AuthenticationException('No resource owner match the request.'); } if (!$owner->handles($request)) { throw new AuthenticationException('No oauth code in the request.'); } // If resource owner supports only one url authentication, call redirect if ($request->query->has('authenticated') && $owner->getOption('auth_with_one_url')) { $request->attributes->set('service', $owner->getName()); return new RedirectResponse(sprintf('%s?code=%s&authenticated=true', $this->httpUtils->generateUri($request, 'glory_oauth_connect'), $request->query->get('code'))); } $owner->isCsrfTokenValid($request->get('state')); $accessToken = $owner->getAccessToken($request, $this->httpUtils->createRequest($request, $this->getCheckPath($owner))->getUri()); $token = new OAuthToken($accessToken); $token->setOwnerName($owner->getName()); return $this->authenticationManager->authenticate($token); }