You are here

function oauth2_login_hybridauth_user_login in OAuth2 Login 7.2

Same name and namespace in other branches
  1. 7 oauth2_login.module \oauth2_login_hybridauth_user_login()

Implements hook_hybridauth_user_login().

Invoked when a user has logged in through HybridAuth.

Parameters

object $account: User account object.

array $data: HybridAuth identity data.

File

./oauth2_login.module, line 233
Provides OAuth2 Login functionality.

Code

function oauth2_login_hybridauth_user_login($account, $data) {
  $hybridauth = hybridauth_get_instance();
  if (!$hybridauth or !is_object($hybridauth)) {
    return;
  }
  try {
    $adapter = $hybridauth
      ->getAdapter('DrupalOAuth2');
  } catch (Exception $e) {
    return;
  }
  $oauth2 = $adapter->adapter;
  $token = [
    'access_token' => $oauth2->api->access_token,
    'refresh_token' => $oauth2->api->refresh_token,
    'expires_in' => $oauth2->api->access_token_expires_in,
    'expiration_time' => $oauth2->api->access_token_expires_at,
    'scope' => $oauth2->scope,
  ];
  if (isset($_GET['proxy']) and $_GET['proxy'] == 'true') {
    $json_token = json_encode($token);
    print "<script>opener.postMessage({$json_token}, '*');</script>";
    drupal_exit();
  }
  else {
    $token_endpoint = $oauth2->api->token_url;
    $client_id = $oauth2->api->client_id;
    $auth_flow = 'server-side';
    $id = md5($token_endpoint . $client_id . $auth_flow);
    oauth2_client_set_token($id, $token);
  }
}