sts.class.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. * Copyright 2010-2011 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. *
  18. *
  19. * This is the AWS Security Token Service (STS) API Reference. STS is a web service that enables you to request temporary, limited-privilege
  20. * credentials for users that you authenticate (federated users), or IAM users. This guide provides descriptions of the STS API as well as
  21. * links to related content in <a href="http://docs.amazonwebservices.com/IAM/latest/UserGuide/">Using AWS Identity and Access Management</a>.
  22. *
  23. * For more detailed information about using this service, go to <a
  24. * href="http://docs.amazonwebservices.com/IAM/latest/UserGuide/TokenBasedAuth.html">Granting Temporary Access to Your AWS Resources</a>, in
  25. * <i>Using AWS Identity and Access Management</i>.
  26. *
  27. * For specific information about setting up signatures and authorization through the API, go to <a
  28. * href="http://docs.amazonwebservices.com/IAM/latest/UserGuide/IAM_UsingQueryAPI.html">Making Query Requests</a> in <i>Using AWS Identity and
  29. * Access Management</i>.
  30. *
  31. * If you're new to AWS and need additional technical information about a specific AWS product, you can find the product's technical
  32. * documentation at <a href="http://aws.amazon.com/documentation/">http://aws.amazon.com/documentation/</a>.
  33. *
  34. * We will refer to Amazon AWS Security Token Service using the abbreviated form STS, and to Amazon Identity and Access Management using the
  35. * abbreviated form IAM. All copyrights and legal protections still apply.
  36. *
  37. * @version Tue Aug 23 12:52:18 PDT 2011
  38. * @license See the included NOTICE.md file for complete information.
  39. * @copyright See the included NOTICE.md file for complete information.
  40. * @link http://aws.amazon.com/sts/AWS Secure Token Service
  41. * @link http://aws.amazon.com/documentation/sts/AWS Secure Token Service documentation
  42. */
  43. class AmazonSTS extends CFRuntime
  44. {
  45. /*%******************************************************************************************%*/
  46. // CLASS CONSTANTS
  47. /**
  48. * Specify the default queue URL.
  49. */
  50. const DEFAULT_URL = 'sts.amazonaws.com';
  51. /*%******************************************************************************************%*/
  52. // CONSTRUCTOR
  53. /**
  54. * Constructs a new instance of <AmazonSTS>.
  55. *
  56. * @param string $key (Optional) Your Amazon API Key. If blank, it will look for the <code>AWS_KEY</code> constant.
  57. * @param string $secret_key (Optional) Your Amazon API Secret Key. If blank, it will look for the <code>AWS_SECRET_KEY</code> constant.
  58. * @return boolean false if no valid values are set, otherwise true.
  59. */
  60. public function __construct($key = null, $secret_key = null)
  61. {
  62. $this->api_version = '2011-06-15';
  63. $this->hostname = self::DEFAULT_URL;
  64. if (!$key && !defined('AWS_KEY'))
  65. {
  66. // @codeCoverageIgnoreStart
  67. throw new STS_Exception('No account key was passed into the constructor, nor was it set in the AWS_KEY constant.');
  68. // @codeCoverageIgnoreEnd
  69. }
  70. if (!$secret_key && !defined('AWS_SECRET_KEY'))
  71. {
  72. // @codeCoverageIgnoreStart
  73. throw new STS_Exception('No account secret was passed into the constructor, nor was it set in the AWS_SECRET_KEY constant.');
  74. // @codeCoverageIgnoreEnd
  75. }
  76. return parent::__construct($key, $secret_key);
  77. }
  78. /*%******************************************************************************************%*/
  79. // SERVICE METHODS
  80. /**
  81. *
  82. * The GetSessionToken action returns a set of temporary credentials for an AWS account or IAM User. The credentials consist of an Access Key
  83. * ID, a Secret Access Key, and a security token. These credentials are valid for the specified duration only. The session duration for IAM
  84. * users can be between one and 36 hours, with a default of 12 hours. The session duration for AWS account owners is restricted to one hour.
  85. *
  86. * For more information about using GetSessionToken to create temporary credentials, go to <a
  87. * href="http://docs.amazonwebservices.com/IAM/latest/UserGuide/CreatingSessionTokens.html">Creating Temporary Credentials to Enable Access for
  88. * IAM Users</a> in <i>Using AWS Identity and Access Management</i>.
  89. *
  90. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  91. * <li><code>DurationSeconds</code> - <code>integer</code> - Optional - The duration, in seconds, that the credentials should remain valid. Acceptable durations for IAM user sessions range from 3600s (one hour) to 129600s (36 hours), with 43200s (12 hours) as the default. Sessions for AWS account owners are restricted to a maximum of 3600s (one hour). </li>
  92. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  93. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  94. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  95. */
  96. public function get_session_token($opt = null)
  97. {
  98. if (!$opt) $opt = array();
  99. return $this->authenticate('GetSessionToken', $opt, $this->hostname);
  100. }
  101. /**
  102. *
  103. * The GetFederationToken action returns a set of temporary credentials for a federated user with the user name and policy specified in the
  104. * request. The credentials consist of an Access Key ID, a Secret Access Key, and a security token. The credentials are valid for the specified
  105. * duration, between one and 36 hours.
  106. *
  107. * The federated user who holds these credentials has any permissions allowed by the intersection of the specified policy and any resource or
  108. * user policies that apply to the caller of the GetFederationToken API, and any resource policies that apply to the federated user's ARN. For
  109. * more information about how token permissions work, see <a
  110. * href="http://docs.amazonwebservices.com/IAM/latest/UserGuide/TokenPermissions.html">Controlling Permissions in Temporary Credentials</a> in
  111. * <i>Using AWS Identity and Access Management</i>. For information about using GetFederationToken to create temporary credentials, see <a
  112. * href="http://docs.amazonwebservices.com/IAM/latest/UserGuide/CreatingFedTokens.html">Creating Temporary Credentials to Enable Access for
  113. * Federated Users</a> in <i>Using AWS Identity and Access Management</i>.
  114. *
  115. * @param string $name (Required) The name of the federated user associated with the credentials. For information about limitations on user names, go to Limitations on IAM Entities in <i>Using AWS Identity and Access Management</i>.
  116. * @param array $opt (Optional) An associative array of parameters that can have the following keys: <ul>
  117. * <li><code>Policy</code> - <code>string</code> - Optional - A policy specifying the permissions to associate with the credentials. The caller can delegate their own permissions by specifying a policy, and both policies will be checked when a service call is made. For more information about how permissions work in the context of temporary credentials, see Controlling Permissions in Temporary Credentials in <i>Using AWS Identity and Access Management</i>. </li>
  118. * <li><code>DurationSeconds</code> - <code>integer</code> - Optional - The duration, in seconds, that the session should last. Acceptable durations for federation sessions range from 3600s (one hour) to 129600s (36 hours), with 43200s (12 hours) as the default. </li>
  119. * <li><code>curlopts</code> - <code>array</code> - Optional - A set of values to pass directly into <code>curl_setopt()</code>, where the key is a pre-defined <code>CURLOPT_*</code> constant.</li>
  120. * <li><code>returnCurlHandle</code> - <code>boolean</code> - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.</li></ul>
  121. * @return CFResponse A <CFResponse> object containing a parsed HTTP response.
  122. */
  123. public function get_federation_token($name, $opt = null)
  124. {
  125. if (!$opt) $opt = array();
  126. $opt['Name'] = $name;
  127. return $this->authenticate('GetFederationToken', $opt, $this->hostname);
  128. }
  129. }
  130. /*%******************************************************************************************%*/
  131. // EXCEPTIONS
  132. /**
  133. * Default STS Exception.
  134. */
  135. class STS_Exception extends Exception {}