CredentialsParser.php
4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Braintree;
/**
*
* CredentialsParser registry
*
* @package Braintree
* @subpackage Utility
*/
class CredentialsParser
{
private $_clientId;
private $_clientSecret;
private $_accessToken;
private $_environment;
private $_merchantId;
public function __construct($attribs)
{
foreach ($attribs as $kind => $value) {
if ($kind == 'clientId') {
$this->_clientId = $value;
}
if ($kind == 'clientSecret') {
$this->_clientSecret = $value;
}
if ($kind == 'accessToken') {
$this->_accessToken = $value;
}
}
$this->parse();
}
/**
*
* @access protected
* @static
* @var array valid environments, used for validation
*/
private static $_validEnvironments = [
'development',
'integration',
'sandbox',
'production',
'qa',
];
public function parse()
{
$environments = [];
if (!empty($this->_clientId)) {
$environments[] = ['clientId', $this->_parseClientCredential('clientId', $this->_clientId, 'client_id')];
}
if (!empty($this->_clientSecret)) {
$environments[] = ['clientSecret', $this->_parseClientCredential('clientSecret', $this->_clientSecret, 'client_secret')];
}
if (!empty($this->_accessToken)) {
$environments[] = ['accessToken', $this->_parseAccessToken()];
}
$checkEnv = $environments[0];
foreach ($environments as $env) {
if ($env[1] !== $checkEnv[1]) {
throw new Exception\Configuration(
'Mismatched credential environments: ' . $checkEnv[0] . ' environment is ' . $checkEnv[1] .
' and ' . $env[0] . ' environment is ' . $env[1]);
}
}
self::assertValidEnvironment($checkEnv[1]);
$this->_environment = $checkEnv[1];
}
public static function assertValidEnvironment($environment) {
if (!in_array($environment, self::$_validEnvironments)) {
throw new Exception\Configuration('"' .
$environment . '" is not a valid environment.');
}
}
private function _parseClientCredential($credentialType, $value, $expectedValuePrefix)
{
$explodedCredential = explode('$', $value);
if (sizeof($explodedCredential) != 3) {
throw new Exception\Configuration('Incorrect ' . $credentialType . ' format. Expected: type$environment$token');
}
$gotValuePrefix = $explodedCredential[0];
$environment = $explodedCredential[1];
$token = $explodedCredential[2];
if ($gotValuePrefix != $expectedValuePrefix) {
throw new Exception\Configuration('Value passed for ' . $credentialType . ' is not a ' . $credentialType);
}
return $environment;
}
private function _parseAccessToken()
{
$accessTokenExploded = explode('$', $this->_accessToken);
if (sizeof($accessTokenExploded) != 4) {
throw new Exception\Configuration('Incorrect accessToken syntax. Expected: type$environment$merchant_id$token');
}
$gotValuePrefix = $accessTokenExploded[0];
$environment = $accessTokenExploded[1];
$merchantId = $accessTokenExploded[2];
$token = $accessTokenExploded[3];
if ($gotValuePrefix != 'access_token') {
throw new Exception\Configuration('Value passed for accessToken is not an accessToken');
}
$this->_merchantId = $merchantId;
return $environment;
}
public function getClientId()
{
return $this->_clientId;
}
public function getClientSecret()
{
return $this->_clientSecret;
}
public function getAccessToken()
{
return $this->_accessToken;
}
public function getEnvironment()
{
return $this->_environment;
}
public function getMerchantId()
{
return $this->_merchantId;
}
}
class_alias('Braintree\CredentialsParser', 'Braintree_CredentialsParser');