You are here

services_keyauth.module in Services 7

Same filename and directory in other branches
  1. 6.2 auth/services_keyauth/services_keyauth.module

@author Services Dev Team

Provides a key based validation system.

File

auth/services_keyauth/services_keyauth.module
View source
<?php

/**
 * @author Services Dev Team
 * @file
 *  Provides a key based validation system.
 */

/**
 * Implements hook_cron().
 *
 * Clear down old values from the nonce table.
 */
function services_keyauth_cron() {
  $expiry_time = REQUEST_TIME - variable_get('services_key_expiry', 30);
  db_delete('services_timestamp_nonce')
    ->condition('timestamp', $expiry_time)
    ->execute();
}

/**
 * This function is called to determine whether the current user has
 * access to a keys configuration.
 */
function services_keyauth_access() {
  return user_access('administer services') && variable_get('services_use_key', FALSE);
}

/**
 * Implements hook_authentication_info().
 *
 * @return array
 *  The configuration array for the authentication scheme
 */
function services_keyauth_authentication_info() {
  return array(
    '#file' => 'services_keyauth.inc',
    '#title' => t('Key authentication'),
    '#description' => t('The default key-based authentication'),
    'security_settings' => '_services_keyauth_security_settings',
    'security_settings_validate' => '_services_keyauth_security_settings_validate',
    'security_settings_submit' => '_services_keyauth_security_settings_submit',
    'alter_methods' => '_services_keyauth_alter_methods',
    'alter_browse_form' => '_services_keyauth_alter_browse_form',
    'alter_browse_form_submit' => '_services_keyauth_alter_browse_form_submit',
    'authenticate_call' => '_services_keyauth_authenticate_call',
  );
}

/**
 * Implements hook_menu().
 */
function services_keyauth_menu() {
  $items = array();
  $items['admin/build/services/keys'] = array(
    'title' => 'Keys',
    'description' => 'Manage application access to site services.',
    'page callback' => 'services_keyauth_admin_keys_list',
    'access callback' => 'services_keyauth_access',
    'type' => MENU_LOCAL_TASK,
    'file' => 'services_keyauth.admin.inc',
  );
  $items['admin/build/services/keys/%'] = array(
    'title' => 'Edit key',
    'access arguments' => array(
      'administer services',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'services_keyauth_admin_keys_form',
    ),
    'file' => 'services_keyauth.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/build/services/keys/%/delete'] = array(
    'access arguments' => array(
      'administer services',
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'services_keyauth_admin_keys_delete_confirm',
    ),
    'file' => 'services_keyauth.admin.inc',
    'type' => MENU_CALLBACK,
  );
  $items['admin/build/services/keys/list'] = array(
    'title' => 'List',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'access callback' => 'services_keyauth_access',
    'weight' => -10,
  );
  $items['admin/build/services/keys/add'] = array(
    'title' => 'Create key',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'services_keyauth_admin_keys_form',
    ),
    'access callback' => 'services_keyauth_access',
    'type' => MENU_LOCAL_TASK,
    'file' => 'services_keyauth.admin.inc',
  );
  return $items;
}
function services_get_hash($timestamp, $domain, $nonce, $method, $args) {
  $hash_parameters = array(
    $timestamp,
    $domain,
    $nonce,
    $method['#method'],
  );
  foreach ($method['#args'] as $key => $arg) {
    if ($arg['#signed'] == TRUE) {
      if (is_numeric($args[$key]) || !empty($args[$key])) {
        if (is_array($args[$key]) || is_object($args[$key])) {
          $hash_parameters[] = serialize($args[$key]);
        }
        else {
          $hash_parameters[] = $args[$key];
        }
      }
      else {
        $hash_parameters[] = '';
      }
    }
  }
  $api_key = db_query("SELECT kid FROM {services_keys} WHERE domain = :key", array(
    ':key' => $domain,
  ))
    ->fetchField('kid');
  return hash_hmac("sha256", implode(';', $hash_parameters), $api_key);
}
function services_keyauth_get_key($kid) {
  $keys = services_keyauth_get_keys();
  foreach ($keys as $key) {
    if ($key->kid == $kid) {
      return $key;
    }
  }
}
function services_keyauth_get_keys() {
  static $keys;
  if (!$keys) {
    $keys = array();
    $result = db_query("SELECT * FROM {services_keys}");
    while ($key = $result
      ->fetchObject()) {
      $keys[$key->kid] = $key;
    }
  }
  return $keys;
}

Functions

Namesort descending Description
services_get_hash
services_keyauth_access This function is called to determine whether the current user has access to a keys configuration.
services_keyauth_authentication_info Implements hook_authentication_info().
services_keyauth_cron Implements hook_cron().
services_keyauth_get_key
services_keyauth_get_keys
services_keyauth_menu Implements hook_menu().