Skip to content

Commit 0a36e43

Browse files
Merge pull request #9 from FreshMail/feature-add-authorization-by-bearer-token
Add authorization by bearer token usage
2 parents 883ebfa + 825b325 commit 0a36e43

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

src/FreshMail/RestApi.php

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ class RestApi
2020
/**
2121
* @var string
2222
*/
23-
private $strApiSecret;
23+
private $apiSecret;
2424

2525
/**
2626
* @var string
2727
*/
28-
private $strApiKey;
28+
private $apiKey;
29+
30+
/**
31+
* @var string
32+
*/
33+
private $token;
2934

3035
private $response = null;
3136
private $rawResponse = null;
@@ -98,8 +103,8 @@ public function getHttpCode()
98103
*/
99104
public function setApiKey($apiKey)
100105
{
101-
$this->strApiKey = $apiKey;
102-
106+
$this->apiKey = $apiKey;
107+
$this->token = '';
103108
return $this;
104109
}
105110

@@ -112,8 +117,20 @@ public function setApiKey($apiKey)
112117
*/
113118
public function setApiSecret($apiSecret)
114119
{
115-
$this->strApiSecret = $apiSecret;
120+
$this->apiSecret = $apiSecret;
121+
$this->token = '';
122+
return $this;
123+
}
116124

125+
/**
126+
* @param $token
127+
* @return $this
128+
*/
129+
public function setToken($token)
130+
{
131+
$this->token = $token;
132+
$this->apiKey = '';
133+
$this->apiSecret = '';
117134
return $this;
118135
}
119136

@@ -141,11 +158,14 @@ public function doRequest($strUrl, $arrParams = array(), $returnRawResponse = fa
141158
$strPostData = http_build_query($arrParams);
142159
}
143160

144-
$apiSignature = sha1($this->strApiKey . '/' . self::PREFIX . $strUrl . $strPostData . $this->strApiSecret);
145-
146161
$headers = array();
147-
$headers[] = 'X-Rest-ApiKey: ' . $this->strApiKey;
148-
$headers[] = 'X-Rest-ApiSign: ' . $apiSignature;
162+
if ($this->token) {
163+
$headers[] = sprintf('Authorization: Bearer %s', $this->token);
164+
} elseif ($this->apiKey) {
165+
$apiSignature = sha1($this->apiKey . '/' . self::PREFIX . $strUrl . $strPostData . $this->apiSecret);
166+
$headers[] = sprintf('X-Rest-ApiKey: %s', $this->apiKey);
167+
$headers[] = sprintf('X-Rest-ApiSign: %s', $apiSignature);
168+
}
149169

150170
if (!empty($this->contentType)) {
151171
$headers[] = 'Content-Type: ' . $this->contentType;
@@ -206,20 +226,4 @@ private function getResponseFromHeaders($cUrl)
206226
$this->response = json_decode(substr($this->rawResponse, $headerSize), true);
207227
}
208228
}
209-
210-
/**
211-
* Calculate request signature.
212-
*
213-
* @param string $apiKey
214-
* @param string $address
215-
* @param string $getData
216-
* @param string $postData
217-
* @param string $apiSecret
218-
*
219-
* @return string
220-
*/
221-
private function calculateSignature($apiKey, $address, $getData, $postData, $apiSecret)
222-
{
223-
return sha1($apiKey . $address . $getData . $postData . $apiSecret);
224-
}
225229
}

0 commit comments

Comments
 (0)