You are here

simple_oauth.module in Simple OAuth (OAuth2) & OpenID Connect 8

Contains simple_oauth.module..

File

simple_oauth.module
View source
<?php

/**
 * @file
 * Contains simple_oauth.module..
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function simple_oauth_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the simple_oauth module.
    case 'help.page.simple_oauth':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The OAuth 2.0 Authorization Framework: Bearer Token Usage') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_cron().
 */
function simple_oauth_cron() {

  /* @var \Drupal\Core\Entity\EntityManagerInterface $manager */
  $manager = \Drupal::service('entity.manager');
  $storage = $manager
    ->getStorage('access_token');
  $query = $storage
    ->getQuery();

  // We only delete access tokens when deleting their expired refresh tokens.
  $ids = $query
    ->condition('expire', REQUEST_TIME, '<')
    ->condition('resource', 'authentication')
    ->execute();
  if (!empty($ids)) {
    $refresh_tokens = $storage
      ->loadMultiple($ids);

    // Get the access tokens associated to this refresh token.
    $access_tokens = array_map(function ($refresh_token) {
      return $refresh_token
        ->get('access_token_id')->entity;
    }, $refresh_tokens);

    // Delete the access tokens.
    $storage
      ->delete(array_filter($access_tokens));

    // Delete the refresh tokens.
    $storage
      ->delete($refresh_tokens);
  }
}

/**
 * Implements hook_theme().
 */
function simple_oauth_theme($existing, $type, $theme, $path) {
  return [
    'access_token' => [
      'render element' => 'access_token',
      'file' => 'access_token.page.inc',
    ],
  ];
}

Functions

Namesort descending Description
simple_oauth_cron Implements hook_cron().
simple_oauth_help Implements hook_help().
simple_oauth_theme Implements hook_theme().