openid_connect_rest-8.x-1.0-rc2/openid_connect_rest.module
openid_connect_rest.module
<?php
/**
* @file
* A REST API for Drupal OpenID Connect module.
*/
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\openid_connect_rest\Entity\AuthorizationMapping;
/**
* Implements hook_openid_connect_post_authorize().
*/
function openid_connect_rest_openid_connect_post_authorize($tokens, $account, $userinfo, $plugin_id) {
if (!empty($userinfo['sub'])) {
$currentRequest = \Drupal::request();
$state_token = $currentRequest->query->get('state');
$authorization_code = $currentRequest->query->get('code');
if (!empty($authorization_code) && !empty($state_token)) {
$rest_authentication_mapping = AuthorizationMapping::create([
'id' => \Drupal::service('uuid')->generate(),
'authorization_code' => $authorization_code,
'state_token' => $state_token,
'user_sub' => $userinfo['sub'],
'expires' => ((new DrupalDateTime())->format('U')) + 1800,
]);
try {
$rest_authentication_mapping->save();
return TRUE;
}
catch (Exception $e) {
return FALSE;
}
}
}
return FALSE;
}
/**
* Implements hook_theme().
*/
function openid_connect_rest_theme() {
$theme['openid_connect_rest_authenticate_page'] = [
'variables' => [
'message' => NULL,
'authenticated' => NULL,
],
'template' => 'openid_connect_rest_authenticate',
];
return $theme;
}
