Skip to content

Instantly share code, notes, and snippets.

@wiratama
Created October 2, 2019 06:46
Show Gist options
  • Save wiratama/a239791fe142089eb85321069ffc7d4c to your computer and use it in GitHub Desktop.
Save wiratama/a239791fe142089eb85321069ffc7d4c to your computer and use it in GitHub Desktop.
<?php
public function checkauth() {
$config = $this->config('snsso.settings');
$authCode = null;
$user = null;
if (isset($_GET['code'])) {
if ($_GET['code']) $authCode = $_GET['code'];
}
if ($authCode) {
$checkUrl = \Drupal::request()->getSchemeAndHttpHost() . Url::fromRoute('snsso.checkauth')->toString();
// Build token URL
$params = array(
'grant_type' => 'authorization_code',
'code' => $authCode,
'redirect_uri' => $checkUrl,
'client_id' => $config->get('client_credentials.client_id'),
'client_secret' => $config->get('client_credentials.client_secret'),
);
$tokenUrl = Url::fromUri($config->get('sso_server_endpoints.token_endpoint'), ['query' => $params])->toString();
// di CI ganti ini dengan curl
$client = \Drupal::httpClient();
$response = $client->request('POST', $tokenUrl,[
'http_errors' => false,
]);
// /. di CI ganti ini dengan curl
$statusCode = $response->getStatusCode();
if ($statusCode == 200) {
// get access token
$json = json_decode($response->getBody()->getContents(), true);
$accessToken = $json['access_token'];
if ($accessToken) {
// user info url
$userInfoUrl = Url::fromUri($config->get('sso_server_endpoints.userinfo_endpoint'))->toString();
$response = $client->request('GET', $userInfoUrl,[
'http_errors' => false,
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
],
]);
$statusCode = $response->getStatusCode();
if ($statusCode == 200) {
// get user info
$json = json_decode($response->getBody()->getContents(), true);
/** your code
*
* --- CODE ---
*
* end of your code */
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment