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