AuthenticationController.php in Janrain Registration 8
File
src/Controller/AuthenticationController.php
View source
<?php
namespace Drupal\janrain_capture\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\user\Entity\User;
use Drupal\janrain_capture\JanrainCaptureApi;
use Drupal\janrain_capture\JanrainMarkupBuilder;
use GuzzleHttp\Psr7\Uri;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use function GuzzleHttp\Psr7\parse_query;
class AuthenticationController extends ControllerBase {
protected $captureApi;
protected $markupBuilder;
public function __construct(JanrainCaptureApi $capture_api, JanrainMarkupBuilder $markup_builder) {
$this->captureApi = $capture_api;
$this->markupBuilder = $markup_builder;
}
public static function create(ContainerInterface $container) : self {
return new static($container
->get('janrain_capture.capture_api'), $container
->get('janrain_capture.markup_builder'));
}
public function forgot() {
return $this->markupBuilder
->getScreenRenderArray('forgot');
}
public function editProfile() {
$access_token = $this->captureApi
->getAccessToken();
$return = $this->markupBuilder
->getScreenRenderArray('edit-profile');
$return['janrain_capture_edit_js'] = [
'#markup' => '<script>var access_token = "' . $access_token . '";</script>',
'#allowed_tags' => [
'script',
],
'#cache' => [
'contexts' => [
'user',
],
'max-age' => 60,
],
];
return $return;
}
public function viewProfile(Request $request) {
if (!$request->query
->get('uuid')) {
$user = User::load(\Drupal::currentUser()
->id());
$uuid = $user
->uuid();
return new RedirectResponse(Url::fromRoute('janrain_capture.view_profile', [
'uuid' => $uuid,
])
->toString(), RedirectResponse::HTTP_MOVED_PERMANENTLY);
}
$current_janrain_uuid = $this->captureApi
->getUserProfile()
->getUuid();
if ($current_janrain_uuid == $_GET['uuid']) {
return $this->markupBuilder
->getScreenRenderArray('public-profile');
}
else {
throw new \InvalidArgumentException('An invalid uuid is given.');
}
}
public function logout() {
user_logout();
return new RedirectResponse(Url::fromRoute('<front>', [], [
'absolute' => TRUE,
])
->toString(), RedirectResponse::HTTP_MOVED_PERMANENTLY);
}
public function login(Request $request) {
$response_class = Response::class;
$one_time_login_link = FALSE;
if ($request->query
->get('url_type') === 'forgot') {
return $this
->forgot();
}
$destination_url = $this
->getDestinationUrl($request)
->setAbsolute()
->toString();
try {
$this->captureApi
->authenticate($this
->getAuthorizationCode($request), $request
->getUri());
if ($one_time_login_link) {
$this
->messenger()
->addStatus('You have been successfully logged in via one-time login link.');
}
$module_handler = \Drupal::moduleHandler();
$module_handler
->alter('janrain_capture_auth_destination', $destination_url, $this->captureApi
->getUserProfile(), $this->captureApi
->getCurrentUser());
} catch (\Throwable $e) {
$this
->messenger()
->addError($e
->getMessage());
}
return new $response_class($destination_url);
}
protected function getAuthorizationCode(Request $request) : string {
if (!$request->query
->has('code')) {
throw new BadRequestHttpException($this
->t('Malformed request. Authorization code is missing.'));
}
$code = $request->query
->get('code');
$request->query
->remove('code');
$request
->overrideGlobals();
return $code;
}
protected function getDestinationUrl(Request $request) : Url {
if ($request->server
->has('HTTP_REFERER')) {
$request_uri = new Uri($request
->getUri());
$referer_uri = new Uri($request->server
->get('HTTP_REFERER'));
if ($referer_uri
->getAuthority() === $request_uri
->getAuthority()) {
return Url::fromUserInput($referer_uri
->getPath(), [
'query' => parse_query($referer_uri
->getQuery()),
]);
}
}
return Url::fromRoute('<front>');
}
}