policy.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License").
  6. * You may not use this file except in compliance with the License.
  7. * A copy of the License is located at
  8. *
  9. * http://aws.amazon.com/apache2.0
  10. *
  11. * or in the "license" file accompanying this file. This file is distributed
  12. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied. See the License for the specific language governing
  14. * permissions and limitations under the License.
  15. */
  16. /*%******************************************************************************************%*/
  17. // CLASS
  18. /**
  19. * Simplifies the process of signing JSON policy documents.
  20. *
  21. * @version 2011.04.25
  22. * @license See the included NOTICE.md file for more information.
  23. * @copyright See the included NOTICE.md file for more information.
  24. * @link http://aws.amazon.com/php/ PHP Developer Center
  25. */
  26. class CFPolicy
  27. {
  28. /**
  29. * Stores the object that contains the authentication credentials.
  30. */
  31. public $auth;
  32. /**
  33. * Stores the policy object that we're working with.
  34. */
  35. public $json_policy;
  36. /**
  37. * Constructs a new instance of this class.
  38. *
  39. * @param CFRuntime $auth (Required) An instance of any authenticated AWS object that is an instance of <CFRuntime> (e.g. <AmazonEC2>, <AmazonS3>).
  40. * @param string|array $policy (Required) The associative array representing the S3 policy to use, or a string of JSON content.
  41. * @return $this A reference to the current instance.
  42. * @link http://docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/index.html?HTTPPOSTForms.html S3 Policies
  43. * @link http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?AccessPolicyLanguage.html Access Policy Language
  44. */
  45. public function __construct($auth, $policy)
  46. {
  47. $this->auth = $auth;
  48. if (is_array($policy)) // We received an associative array...
  49. {
  50. $this->json_policy = json_encode($policy);
  51. }
  52. else // We received a valid, parseable JSON string...
  53. {
  54. $this->json_policy = json_encode(json_decode($policy, true));
  55. }
  56. return $this;
  57. }
  58. /**
  59. * Alternate approach to constructing a new instance. Supports chaining.
  60. *
  61. * @param CFRuntime $auth (Required) An instance of any authenticated AWS object that is an instance of <CFRuntime> (e.g. <AmazonEC2>, <AmazonS3>).
  62. * @param string|array $policy (Required) The associative array representing the S3 policy to use, or a string of JSON content.
  63. * @return $this A reference to the current instance.
  64. */
  65. public static function init($auth, $policy)
  66. {
  67. if (version_compare(PHP_VERSION, '5.3.0', '<'))
  68. {
  69. throw new Exception('PHP 5.3 or newer is required to instantiate a new class with CLASS::init().');
  70. }
  71. $self = get_called_class();
  72. return new $self($auth, $policy);
  73. }
  74. /**
  75. * Get the key from the authenticated instance.
  76. *
  77. * @return string The key from the authenticated instance.
  78. */
  79. public function get_key()
  80. {
  81. return $this->auth->key;
  82. }
  83. /**
  84. * Base64-encodes the JSON string.
  85. *
  86. * @return string The Base64-encoded version of the JSON string.
  87. */
  88. public function get_policy()
  89. {
  90. return base64_encode($this->json_policy);
  91. }
  92. /**
  93. * Gets the JSON string with the whitespace removed.
  94. *
  95. * @return string The JSON string without extraneous whitespace.
  96. */
  97. public function get_json()
  98. {
  99. return $this->json_policy;
  100. }
  101. /**
  102. * Gets the JSON string with the whitespace removed.
  103. *
  104. * @return string The Base64-encoded, signed JSON string.
  105. */
  106. public function get_policy_signature()
  107. {
  108. return base64_encode(hash_hmac('sha1', $this->get_policy(), $this->auth->secret_key));
  109. }
  110. /**
  111. * Decode a policy that was returned from the service.
  112. *
  113. * @param string $response (Required) The policy returned by AWS that you want to decode into an object.
  114. * @return string The Base64-encoded, signed JSON string.
  115. */
  116. public static function decode_policy($response)
  117. {
  118. return json_decode(urldecode($response), true);
  119. }
  120. }