You are here

function oauth2_server_services_authenticate_call in OAuth2 Server 7

Services authentication "authenticate_call" callback.

Parameters

array $auth_settings: The auth settings provided by Services.

Return value

mixed A non-empty return value indicates an error has occurred.

Throws

\Exception If no server is configured yet authentication is required.

See also

_services_authenticate_user()

1 string reference to 'oauth2_server_services_authenticate_call'
oauth2_server_services_authentication_info in ./oauth2_server.module
Implements hook_services_authentication().

File

includes/oauth2_server.services_auth.inc, line 21
Provides an authentication method to Services.

Code

function oauth2_server_services_authenticate_call($auth_settings) {

  // No server specified: cannot proceed.
  if (empty($auth_settings['server'])) {
    watchdog('oauth2_server', 'No server configured for Services OAuth2 authentication.', array(), WATCHDOG_ERROR);
    return TRUE;
  }

  // Check access.
  $result = oauth2_server_check_access($auth_settings['server'], $auth_settings['scope']);
  if (is_array($result) && !empty($result['user_id'])) {

    // The check was successful. If it matches with a user account, then switch
    // the current user to it.
    global $user;
    $user = user_load($result['user_id']);
  }
  elseif ($result instanceof \OAuth2\Response) {

    // There was an authentication error, or the user did not attempt to
    // authenticate. If authentication is required for this resource, send an
    // error response and abort the request. Otherwise, move on without error.
    if (!empty($auth_settings['require_authentication'])) {
      oauth2_server_send_response($result);
    }
  }
}