You are here

function _oauth2_proxy_login in OAuth2 Login 7

Same name and namespace in other branches
  1. 7.2 oauth2_login.module \_oauth2_proxy_login()

Function for: oauth2/proxy/login.

1 call to _oauth2_proxy_login()
oauth2_proxy_callback in ./oauth2_login.module
Callback function for the menu oauth2/proxy. The parameter $action can be: login|logout|refresh

File

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

Code

function _oauth2_proxy_login() {
  if (user_is_anonymous()) {
    $query = [
      'proxy' => 'true',
    ] + $_GET;
    unset($query['q']);
    drupal_goto('user/oauth2_login', [
      'query' => $query,
    ]);
  }
  else {

    // Get the token.
    $server_url = variable_get('oauth2_login_oauth2_server', '');
    $client_id = variable_get('oauth2_login_client_id', '');
    $client_secret = variable_get('oauth2_login_client_secret', '');
    $token_endpoint = $server_url . '/oauth2/token';
    $authorization_endpoint = $server_url . '/oauth2/authorize';
    $redirect_uri = url('oauth2/authorized', [
      'absolute' => TRUE,
    ]);
    $auth_flow = 'server-side';
    $oauth2 = new OAuth2\Client([
      'token_endpoint' => $token_endpoint,
      'auth_flow' => $auth_flow,
      'client_id' => $client_id,
      'client_secret' => $client_secret,
      'redirect_uri' => $redirect_uri,
      'authorization_endpoint' => $authorization_endpoint,
      'scope' => 'user_profile',
    ]);
    $oauth2
      ->getAccessToken();
    $id = md5($token_endpoint . $client_id . $auth_flow);
    $token = oauth2_client_get_token($id);

    // Return the token to the mobile app.
    $json_token = json_encode($token);
    print "<script>opener.postMessage({$json_token}, '*');</script>";
    drupal_exit();
  }
}