You are here

public function OAuth2ServerAuthentication::authenticate in RESTful 7.2

Authenticate the request by trying to match a user.

Parameters

RequestInterface $request: The request.

Return value

object The user object.

Overrides AuthenticationInterface::authenticate

File

src/Plugin/authentication/OAuth2ServerAuthentication.php, line 44

Class

OAuth2ServerAuthentication
Authentication support for oauth2_server.

Namespace

Drupal\restful\Plugin\authentication

Code

public function authenticate(RequestInterface $request) {
  $oauth2_info = $this
    ->getOAuth2Info($request);
  if (!$oauth2_info) {
    throw new ServerConfigurationException('The resource uses OAuth2 authentication but does not specify the OAuth2 server.');
  }
  $result = oauth2_server_check_access($oauth2_info['server'], $oauth2_info['scope']);
  if ($result instanceof \OAuth2\Response) {
    throw new UnauthorizedException($result
      ->getResponseBody(), $result
      ->getStatusCode());
  }
  elseif (empty($result['user_id'])) {
    return NULL;
  }
  return user_load($result['user_id']);
}