function gauth_login_gauth_google_response in Google Auth 7
Same name and namespace in other branches
- 7.2 gauth_login/gauth_login.module \gauth_login_gauth_google_response()
Implementation of hook_gauth_google_response().
File
- gauth_login/
gauth_login.module, line 42 - Google Auth Api for drupal.
Code
function gauth_login_gauth_google_response() {
if (isset($_GET['state'])) {
$state = json_decode($_GET['state'], TRUE);
if (isset($state['src']) && $state['src'] != 'gauth_login') {
// Handle response only if the request was from gauth_login.
return;
}
if ($state['hash'] != $_SESSION['gauth_login_state']['hash']) {
drupal_set_message(t('Invalid state parameter'), 'error');
drupal_access_denied();
return;
}
$redirect_url = isset($state['destination']) ? $state['destination'] : '';
if (isset($_GET['code'])) {
$client_id = variable_get('gauth_login_client_id');
$client_secret = variable_get('gauth_login_client_secret');
$api_key = variable_get('gauth_login_developer_key');
$client = new Google_Client();
$client
->setApplicationName("Google OAuth2");
$client
->setClientId($client_id);
$client
->setClientSecret($client_secret);
$client
->setRedirectUri(gauth_callback_url());
$client
->setDeveloperKey($api_key);
$client
->setApprovalPrompt('force');
$scopes = gauth_google_services_scopes('oauth2');
$client
->addScope($scopes);
$client
->fetchAccessTokenWithAuthCode($_GET['code']);
$account['access_token'] = json_encode($client
->getAccessToken());
$client = new Google_Client();
$client
->setApplicationName("Google OAuth2");
$client
->setClientId($client_id);
$client
->setClientSecret($client_secret);
$client
->setRedirectUri(gauth_callback_url());
$client
->setDeveloperKey($api_key);
$client
->setApprovalPrompt('force');
$client
->setAccessToken($account['access_token']);
$scopes = gauth_google_services_scopes('oauth2');
$client
->addScope($scopes);
$oauth = new Google_Service_Oauth2($client);
$info = $oauth->userinfo
->get();
if ($uid = gauth_login_load_google_id($info['id'])) {
$form_state['uid'] = $uid;
user_login_submit(array(), $form_state);
}
else {
$account['client_id'] = variable_get('gauth_login_client_id');
$account['client_secret'] = variable_get('gauth_login_client_secret');
$account['developer_key'] = variable_get('gauth_login_developer_key');
$account['services'] = 'oauth2';
$account['is_authenticated'] = TRUE;
if (!($new_user = gauth_login_find_existing_user($info))) {
if (variable_get('gauth_login_create_user', TRUE)) {
$user = new stdClass();
$user->mail = $info['email'];
$user->name = user_load_by_name($info['name']) ? $info['name'] . time() : $info['name'];
$user->is_new = TRUE;
$user->status = 1;
$new_user = user_save($user);
}
else {
drupal_set_message(t(variable_get('gauth_login_create_user_not_allowed_message', 'Can not find a user with this email. Did you use other google account while registering?')));
drupal_goto('user/login');
}
}
$form_state['uid'] = $new_user->uid;
user_login_submit(array(), $form_state);
global $user;
$token = drupal_hash_base64(drupal_random_bytes(55));
$_SESSION['pass_reset_' . $user->uid] = $token;
if (variable_get('gauth_login_prom_message', TRUE)) {
drupal_set_message(t("Click <a href='!url' target=_blank>here</a> to set password", array(
'!url' => url('user/' . $user->uid . '/edit', array(
'query' => array(
'pass-reset-token' => $token,
),
)),
)), 'warning');
}
$gauth_login = array(
'google_id' => $info['id'],
'uid' => $user->uid,
);
drupal_write_record('gauth_login', $gauth_login);
$account['name'] = 'Gauth Login ' . $user->uid;
$account['uid'] = $user->uid;
drupal_write_record('gauth_accounts', $account);
}
}
drupal_goto($redirect_url);
}
}