You are here

public function LTIToolProvider::authenticate in LTI Tool Provider 2.x

Same name and namespace in other branches
  1. 8 src/Authentication/Provider/LTIToolProvider.php \Drupal\lti_tool_provider\Authentication\Provider\LTIToolProvider::authenticate()

Authenticates the user.

Parameters

\Symfony\Component\HttpFoundation\Request|null $request: The request object.

Return value

\Drupal\Core\Session\AccountInterface|null AccountInterface - in case of a successful authentication. NULL - in case where authentication failed.

Overrides AuthenticationProviderInterface::authenticate

File

src/Authentication/Provider/LTIToolProvider.php, line 141

Class

LTIToolProvider
Oauth authentication provider for LTI Tool Provider.

Namespace

Drupal\lti_tool_provider\Authentication\Provider

Code

public function authenticate(Request $request) {
  try {
    $this->context = $request->request
      ->all();
    $event = new LtiToolProviderLaunchEvent($this->context);
    LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $event);
    if ($event
      ->isCancelled()) {
      throw new Exception($event
        ->getMessage());
    }
    $this->context = $event
      ->getContext();
    $this
      ->validateOauthRequest();
    $this
      ->provisionUser();
    $event = new LtiToolProviderAuthenticatedEvent($this->context, $this->user);
    LtiToolProviderEvent::dispatchEvent($this->eventDispatcher, $event);
    if ($event
      ->isCancelled()) {
      throw new Exception($event
        ->getMessage());
    }
    $this->context = $event
      ->getContext();
    $this->user = $event
      ->getUser();
    $this
      ->userLoginFinalize();
    $this->context['consumer_id'] = $this->consumerEntity
      ->id();
    $this->context['consumer_label'] = $this->consumerEntity
      ->label();
    $session = $request
      ->getSession();
    $session
      ->set('lti_tool_provider_context', $this->context);
    return $this->user;
  } catch (Exception $e) {
    $this->loggerFactory
      ->warning($e
      ->getMessage());
    $this
      ->sendLtiError($e
      ->getMessage());
    return NULL;
  }
}