credentials.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * The <CFCredentials> class enables developers to easily switch between multiple sets of credentials.
  20. *
  21. * @version 2011.11.15
  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 CFCredentials
  27. {
  28. /**
  29. * The key used to specify the default credential set
  30. */
  31. const DEFAULT_KEY = '@default';
  32. /**
  33. * The key used to identify inherited credentials
  34. */
  35. const INHERIT_KEY = '@inherit';
  36. /**
  37. * Stores the credentials
  38. */
  39. protected static $credentials = array();
  40. /**
  41. * Prevents this class from being constructed
  42. */
  43. final private function __construct() {}
  44. /**
  45. * Stores the credentials for re-use.
  46. *
  47. * @param array $credential_sets (Required) The named credential sets that should be made available to the application.
  48. * @return void
  49. */
  50. public static function set(array $credential_sets)
  51. {
  52. // Make sure a default credential set is specified or can be inferred
  53. if (count($credential_sets) === 1)
  54. {
  55. $credential_sets[self::DEFAULT_KEY] = reset($credential_sets);
  56. }
  57. elseif (!isset($credential_sets[self::DEFAULT_KEY]))
  58. {
  59. throw new CFCredentials_Exception('If more than one credential set is provided, a default credential set (identified by the key "' . self::DEFAULT_KEY . '") must be specified.');
  60. }
  61. // Resolve any @inherit tags
  62. foreach ($credential_sets as $credential_name => &$credential_set)
  63. {
  64. if (is_array($credential_set))
  65. {
  66. foreach ($credential_set as $credential_key => &$credential_value)
  67. {
  68. if ($credential_key === self::INHERIT_KEY)
  69. {
  70. if (!isset($credential_sets[$credential_value]))
  71. {
  72. throw new CFCredentials_Exception('The credential set, "' . $credential_value . '", does not exist and cannot be inherited.');
  73. }
  74. $credential_set = array_merge($credential_sets[$credential_value], $credential_set);
  75. unset($credential_set[self::INHERIT_KEY]);
  76. }
  77. }
  78. }
  79. }
  80. // Normalize the value of the @default credential set
  81. $default = $credential_sets[self::DEFAULT_KEY];
  82. if (is_string($default))
  83. {
  84. if (!isset($credential_sets[$default]))
  85. {
  86. throw new CFCredentials_Exception('The credential set, "' . $default . '", does not exist and cannot be used as the default credential set.');
  87. }
  88. $credential_sets[self::DEFAULT_KEY] = $credential_sets[$default];
  89. }
  90. // Store the credentials
  91. self::$credentials = $credential_sets;
  92. }
  93. /**
  94. * Retrieves the requested credentials from the internal credential store.
  95. *
  96. * @param string $credential_set (Optional) The name of the credential set to retrieve. The default value is set in DEFAULT_KEY.
  97. * @return stdClass A stdClass object where the properties represent the keys that were provided.
  98. */
  99. public static function get($credential_name = self::DEFAULT_KEY)
  100. {
  101. // Make sure the credential set exists
  102. if (!isset(self::$credentials[$credential_name]))
  103. {
  104. throw new CFCredentials_Exception('The credential set, "' . $credential_name . '", does not exist and cannot be retrieved.');
  105. }
  106. // Return the credential set as an object
  107. return new CFCredential(self::$credentials[$credential_name]);
  108. }
  109. }
  110. class CFCredentials_Exception extends Exception {}