You are here

public function OAuthSignatureMethod_HMAC::__construct in OAuth 1.0 7.4

Same name and namespace in other branches
  1. 6.3 includes/OAuthSignatureMethod_HMAC.inc \OAuthSignatureMethod_HMAC::__construct()
  2. 7.3 includes/OAuthSignatureMethod_HMAC.inc \OAuthSignatureMethod_HMAC::__construct()

Create a HMAC oauth signature method object using the (or one of the) specified algorithm implementations.

Parameters

mixed $algo: Pass the name of a algorithm supported by hash_hmac() or an array of names when you have several candidate algorithms that may be used. The first algorithm int the array that is supported on the system will be used.

Throws

Exception A exception is thrown when none of the provided algorithms are supported by the system.

File

includes/OAuthSignatureMethod_HMAC.inc, line 22

Class

OAuthSignatureMethod_HMAC
A generic signature method implementation that leverages hash_hmac() for increased flexibility.

Code

public function __construct($algo) {
  $algos = hash_algos();

  // Create a single-element array from strings to simplify the logic of
  // support checking and failure handling.
  if (is_string($algo)) {
    $algo = array(
      $algo,
    );
  }

  // Find a supported algorithm among the candidates
  foreach ($algo as $a) {
    if (in_array(strtolower($a), $algos)) {
      $this->algo = strtolower($a);
      continue;
    }
  }

  // Throw a exception if no matching algorithm can be found
  if (empty($this->algo)) {
    throw new OAuthException(t('None of the suggested hash algorithms (@cand) were ' . 'supported by the server. Try one of the following: !algos.', array(
      '@cand' => join($algo, ', '),
      '!algos' => join($algos, ', '),
    )));
  }
}