Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  
   3  namespace IMSGlobal\LTI\OAuth;
   4  
   5  /**
   6   * Class to represent an %OAuth HMAC_SHA256 signature method
   7   *
   8   * @author  Stephen P Vickers <svickers@imsglobal.org>
   9   * @copyright  IMS Global Learning Consortium Inc
  10   * @date  2016
  11   * @version 2015-11-30
  12   * @license https://opensource.org/licenses/MIT The MIT License
  13   */
  14  /**
  15   * The HMAC-SHA256 signature method uses the HMAC-SHA256 signature algorithm as defined in [RFC6234]
  16   * where the Signature Base String is the text and the key is the concatenated values (each first
  17   * encoded per Parameter Encoding) of the Consumer Secret and Token Secret, separated by an '&'
  18   * character (ASCII code 38) even if empty.
  19   */
  20  class OAuthSignatureMethod_HMAC_SHA256 extends OAuthSignatureMethod {
  21  
  22      function get_name() {
  23          return "HMAC-SHA256";
  24      }
  25  
  26      public function build_signature($request, $consumer, $token) {
  27  
  28          $base_string = $request->get_signature_base_string();
  29          $request->base_string = $base_string;
  30  
  31          $key_parts = array(
  32            $consumer->secret,
  33            ($token) ? $token->secret : ""
  34          );
  35  
  36          $key_parts = OAuthUtil::urlencode_rfc3986($key_parts);
  37          $key = implode('&', $key_parts);
  38  
  39          return base64_encode(hash_hmac('sha256', $base_string, $key, true));
  40  
  41      }
  42  
  43  }