Skip to content

Commit 7b4e2f8

Browse files
Vinay Bhaleraomikz
authored andcommitted
Support service token (#15)
* added service token support * fix failing test cases * README update * minor edits * README edits * added secure env variables for .travis.yml * revert to original params defined way * README edits * new authrep, tests and readme * add changelog.md * support tuple service token and service id in same function * [poc] send tuple as array * added class for tuple * readme edits * fix test cases * minor edits per mikz comments * revert travis changes * added changed section * update release version
1 parent 6527e82 commit 7b4e2f8

6 files changed

Lines changed: 336 additions & 128 deletions

File tree

.travis.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
language: php
22
php:
3-
# - 5.2 # does not have DateTime::getTimestamp()
4-
- 5.3
5-
- 5.4
6-
before_script:
7-
- export TEST_3SCALE_APP_KEYS=3e05c797ef193fee452b1ddf19defa74
8-
- export TEST_3SCALE_APP_IDS=75165984
9-
- export TEST_3SCALE_PROVIDER_KEY=05273bcb282d1d4faafffeb01e224db0
3+
- 5.3
4+
- 5.4
5+
env:
6+
global:
7+
- TEST_3SCALE_APP_IDS=4d4b20b9 TEST_3SCALE_APP_KEYS=ecce202ecc2eb8dc7a499c34a34d5987
8+
- secure: "N4m/jyamokMvS7IuUjDzzexp6hExqFx2b7TZfFsKKBUVvbmdt0OuE/lgh68pFiFgAWNUwqhU8f7QmE6Snz8adqndgw+gaPVbOmLmFSt4/MUW7VjIgRvINZjKJQ6Rw18NjJ3eAl91XxtDK3kkD9pKbcV6Vb7uR1g0khNGxbSdTlI="
9+
- secure: "BaSNNNJC62aLH9b1xrOryLDZBtiWLvUcOjKA9pXQq6u1wvUNBMr6cQHwV7MQZTHlxAhGh+fahC9c6VInVVp7SyH4gcMI2Feq2CkAHBIFN4umBZC5rVVAgjM48UOg+qp5cYyyKzUhvFqAhAgmKh0dvoB+hLrY0qfKQ4sufSRTmUk="
10+
- secure: "myAM1Jeqrm2AuOiz96g/3KgQtqAxdWNn/0JuTGyKU2bdORtTthrfWZ7L+Sx6NP1e/GsioJ9iuqp0Q4aEvoJ6JtQLJhAXeRSovnuNRvaVnPqAsI/E2530laQOuGw8qgVLr7Kq1BkfoIF2Hc7J74yu7+Tyog+uSA+hpIyadHbFem8="
1011
script: php test/all.php

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Change Log
2+
All notable changes to this project will be documented in this file.
3+
4+
##[2.7.0] - 2017-02-16
5+
### Added
6+
- Added support for (Service Tokens)[https://support.3scale.net/docs/accounts/tokens]
7+
8+
### Changed
9+
- The signature for `authrep` method has been changed from `authrep($appId, $appKey = null, $usage = null, $userId = null, $object = null, $no_body = null, $serviceId = null)` to `authrep($appId, $appKey = null, $credentials_or_service_id, $usage = null, $userId = null, $object = null, $no_body = null)`
10+
- The signature for 'authrep_with_user_key' method has been changed from `authrep_with_user_key($userKey, $usage = null, $userId = null, $object = null, $no_body = null, $serviceId = null)` to `authrep_with_user_key($userKey, $credentials_or_service_id, $usage = null, $userId = null, $object = null, $no_body = null)`
11+

README.markdown

Lines changed: 135 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
# Client for 3scale web service management system API [![Build Status](https://secure.travis-ci.org/3scale/3scale_ws_api_for_php.png?branch=master)](http://travis-ci.org/3scale/3scale_ws_api_for_php)
1+
# Client for 3scale Web Service Management API [![Build Status](https://secure.travis-ci.org/3scale/3scale_ws_api_for_php.png?branch=master)](http://travis-ci.org/3scale/3scale_ws_api_for_php)
22
3scale integration plugin for PHP applications. 3scale is an API Infrastructure service which handles API Keys, Rate Limiting, Analytics, Billing Payments and Developer Management. Includes a configurable API dashboard and developer portal CMS. More product stuff at http://www.3scale.net/, support information at http://support.3scale.net/.
33

4+
### Tutorials
5+
* Plugin Setup: https://support.3scale.net/docs/deployment-options/plugin-setup
6+
* Rate Limiting: https://support.3scale.net/docs/access-control/rate-limits
7+
* Analytics Setup: https://support.3scale.net/quickstarts/3scale-api-analytics
8+
49
## Installation
510

611
Download the source code from github: http://github.com/3scale/3scale_ws_api_for_php and place it somewhere accessible from your project.
@@ -10,106 +15,182 @@ Download the source code from github: http://github.com/3scale/3scale_ws_api_for
1015
Require the ThreeScaleClient.php file (assuming you placed the library somewhere within the
1116
include path):
1217

13-
require_once('lib/ThreeScaleClient.php')
18+
```php
19+
require_once('lib/ThreeScaleClient.php')
20+
```
1421

15-
Then, create an instance of the client, giving it your provider API key:
22+
Then create an instance of the client
23+
```php
24+
$client = new ThreeScaleClient();
25+
```
1626

17-
$client = new ThreeScaleClient("your provider key");
27+
> NOTE: unless you specify ```ThreeScaleClient();``` you will be expected to specify
28+
a `provider_key` parameter, which is deprecated in favor of Service Tokens:
29+
```php
30+
$client = new ThreeScaleClient("your provider key");
31+
```
1832

1933
Because the object is stateless, you can create just one and store it globally.
2034

21-
**NOTE: From November 2016 `service_id` is mandatory.**
35+
Then you can perform calls in the client:
36+
37+
```php
38+
$response = $client->authorize("app id", "app key", new ThreeScaleClientCredentials("service id", "service token"));
39+
```
40+
41+
```php
42+
$response = $client->report(array(array('app_id' => "app's id"],'usage' => array('hits' => 1))), new ThreeScaleClientCredentials("service id", "service token"));
43+
```
44+
45+
**NOTE:`service_id` is mandatory since November 2016, both when using service tokens and when using provider keys**
2246

2347
### Authorize
2448

2549
To authorize a particular application, call the `authorize` method passing it the
26-
application id and optionally the application key:
50+
`application id` and `service id` and optionally the application key:
51+
52+
```php
53+
$response = $client->authorize("app id", "app key", new ThreeScaleClientCredentials("service id", "service token"));
54+
```
55+
56+
If you had configured a (deprecated) provider key, you would instead use:
2757

28-
$response = $client->authorize("the app id", "the app key");
29-
or
30-
$response = $client->authorize("the app id", "the app key", "the service id");
58+
```php
59+
$response = $client->authorize("the app id", "the app key", "service id"));
60+
```
3161

3262
Then call the `isSuccess()` method on the returned object to see if the authorization was
3363
successful:
3464

35-
if ($response->isSuccess()) {
36-
// All fine, proceeed.
37-
} else {
38-
// Something's wrong with this app.
39-
}
65+
```php
66+
if ($response->isSuccess()) {
67+
// All fine, proceeed.
68+
} else {
69+
// Something's wrong with this app.
70+
}
71+
```
4072

4173
If both provider and app id are valid, the response object contains additional information about the status of the application:
4274

43-
// Returns the name of the plan the application is signed up to.
44-
$response->getPlan()
75+
```php
76+
//Returns the name of the plan the application is signed up to.
77+
$response->getPlan()
78+
```
4579

4680
If the plan has defined usage limits, the response contains details about the usage broken down by the metrics and usage limit periods.
4781

48-
// The usageReports array contains one element per each usage limit defined on the plan.
49-
$usageReports = $response->getUsageReports();
50-
$usageReport = $usageReports[0];
82+
```php
83+
// The usageReports array contains one element per each usage limit defined on the plan.
84+
$usageReports = $response->getUsageReports();
85+
$usageReport = $usageReports[0];
5186

52-
// The metric
53-
$usageReport->getMetric() // "hits"
87+
// The metric
88+
$usageReport->getMetric() // "hits"
5489

55-
// The period the limit applies to
56-
$usageReport->getPeriod() // "day"
57-
$usageReport->getPeriodStart() // 1272405600 (Unix timestamp for April 28, 2010, 00:00:00)
58-
$usageReport->getPeriodEnd() // 1272492000 (Unix timestamp for April 29, 2010, 00:00:00)
90+
// The period the limit applies to
91+
$usageReport->getPeriod() // "day"
92+
$usageReport->getPeriodStart() // 1272405600 (Unix timestamp for April 28, 2010, 00:00:00)
93+
$usageReport->getPeriodEnd() // 1272492000 (Unix timestamp for April 29, 2010, 00:00:00)
5994

60-
// The current value the application already consumed in the period
61-
$usageReport->getCurrentValue() // 8032
95+
// The current value the application already consumed in the period
96+
$usageReport->getCurrentValue() // 8032
6297

63-
// The maximal value allowed by the limit in the period
64-
$usageReport->getMaxValue() // 10000
65-
66-
// If the limit is exceeded, this will be true, otherwise false:
67-
$usageReport->isExceeded() // false
98+
// The maximal value allowed by the limit in the period
99+
$usageReport->getMaxValue() // 10000
100+
101+
// If the limit is exceeded, this will be true, otherwise false:
102+
$usageReport->isExceeded() // false
103+
```
68104

69105
If the authorization failed, the `getErrorCode()` returns system error code and `getErrorMessage()` human readable error description:
70106

71-
$response->getErrorCode() // "usage_limits_exceeded"
72-
$response->getErrorMessage() // "Usage limits are exceeded"
107+
```php
108+
$response->getErrorCode() // "usage_limits_exceeded"
109+
$response->getErrorMessage() // "Usage limits are exceeded"
110+
```
111+
112+
### Authrep
113+
114+
To authorize a particular application, call the `authrep` method passing it the
115+
`application id` and `service id` and optionally the application key:
116+
117+
```php
118+
$response = $client->authrep("app id", "app key", new ThreeScaleClientCredentials("service id", "service token"), array('hits' => 1));
119+
```
120+
121+
Then call the `isSuccess()` method on the returned object to see if the authorization was
122+
successful:
123+
124+
```php
125+
if ($response->isSuccess()) {
126+
// All fine, proceeed.
127+
} else {
128+
// Something's wrong with this app.
129+
}
130+
```
131+
132+
If both provider and app id are valid, the response object contains additional information about the status of the application:
133+
134+
```php
135+
//Returns the name of the plan the application is signed up to.
136+
$response->getPlan()
137+
```
138+
139+
You can also use other patterns such as `user_key` mode during the authrep call
140+
141+
```php
142+
$response = $client->authrep_with_user_key("app id", "app key", new ThreeScaleClientCredentials("service id", "service token"), array('hits' => 1));
143+
```
73144

74145
### Report
75146

76147
To report usage, use the `report` method. You can report multiple transaction at the same time:
77148

78-
$response = $client->report(array(
79-
array('app_id' => "first app's id", 'usage' => array('hits' => 1)),
80-
array('app_id' => "second app's id", 'usage' => array('hits' => 1))), "service id");
149+
```php
150+
$response = $client->report(array(
151+
array('app_id' => "first app's id",'usage' => array('hits' => 1)),
152+
array('app_id' => "second app's id", 'usage' => array('hits' => 1))), new ThreeScaleClientCredentials("service id", "service token"));
153+
```
81154

82-
The `"app_id"` and `"usage"` parameters are required. Additionaly, you can specify a timestamp
155+
The `"app_id"`, `"usage"` parameters are required alongiwth `service id` and `service token`. Additionaly, you can specify a timestamp
83156
of the transaction:
84157

85-
$response = $client->report(array(
86-
array('app_id' => "app's id",
87-
'usage' => array('hits' => 1),
88-
'timestamp' => mktime(12, 36, 0, 4, 28, 2010))));
158+
```php
159+
$response = $client->report(array(
160+
array('app_id' => "app's id",
161+
'usage' => array('hits' => 1),
162+
'timestamp' => mktime(12, 36, 0, 4, 28, 2010, new ThreeScaleClientCredentials("service id", "service token")));
163+
```
89164

90165
The timestamp can be either an unix timestamp (as integer) or a string. The string has to be in a
91166
format parseable by the [strtotime](http://php.net/manual/en/function.strtotime.php) function.
92167
For example:
93168

94-
"2010-04-28 12:38:33 +0200"
169+
```php
170+
"2010-04-28 12:38:33 +0200"
171+
```
95172

96173
If the timestamp is not in UTC, you have to specify a time offset. That's the "+0200"
97174
(two hours ahead of the Universal Coordinate Time) in the example above.
98175

99176
Then call the `isSuccess()` method on the returned response object to see if the report was
100177
successful:
101178

102-
if ($response->isSuccess()) {
103-
// All OK.
104-
} else {
105-
// There was an error.
106-
}
179+
```php
180+
if ($response->isSuccess()) {
181+
// All OK.
182+
} else {
183+
// There was an error.
184+
}
185+
```
107186

108187
In case of error, the `getErrorCode()` returns system error code and `getErrorMessage()`
109188
human readable error description:
110189

111-
$response->getErrorCode() // "provider_key_invalid"
112-
$response->getErrorMessage() // "provider key \"foo\" is invalid"
190+
```php
191+
$response->getErrorCode() // "provider_key_invalid"
192+
$response->getErrorMessage() // "provider key \"foo\" is invalid"
193+
```
113194

114195
## Plugin integration
115196

@@ -119,6 +200,10 @@ If you are interested in integrating the plugin with:
119200

120201
* [Symphony2](http://symfony.com/) check [tonivdv's 3scaleBundle](https://github.com/tonivdv/3scaleBundle). This is kindly maintained by [Adlogix](http://www.adlogix.eu) tech team.
121202

203+
## To Test
204+
205+
To run tests: `php test/all.php`
206+
122207
## Legal
123208

124209
Copyright (c) 2010 3scale networks S.L., released under the MIT license.

0 commit comments

Comments
 (0)