Skip to content

Commit

Permalink
adds authrep function with OAuth method (#46)
Browse files Browse the repository at this point in the history
Adds authrep function with OAuth method
  • Loading branch information
avilatusell committed Mar 17, 2017
1 parent 755c856 commit 69b6ab0
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 22 deletions.
69 changes: 52 additions & 17 deletions README.md
Expand Up @@ -98,11 +98,48 @@ client.authrep_with_user_key({ "service_id": "your service id", "user_key": "you
*/
```


If you use `OAuth` as authentication mode, this would be the equivalent to the examples above:

```javascript
var Client = require('3scale').Client;

//Create a Client with a given host and port when connecting to an on-premise instance of the 3scale platform:
client = new Client({host: "service_management_api.example.com", port: 80});

/* or create a Client with default host and port. This will comunicate with the 3scale platform SaaS default server:
client = new Client();
*/

client.oauth_authrep({ service_token: "your service token", service_id: "your service id", app_id: "your Client id", usage: { "hits": 1 } }, function(response){
console.log(response);
});


/* If you don't use service_token in the method, you'll be expected to specify a provider_key parameter in the Client instance, which is deprecated in favor of using service_token in the method.
Create a Client with a given host and port:
client = new Client("your provider key",{host: "service_management_api.example.com", port: 80});
or
Create a Client with default host and port.This will comunicate with the 3scale platform SaaS default server:
client = new Client("your provider key");
client.oauth_authrep({ service_id: "your service id", app_id: "your Client id" , usage: { "hits": 1 } }, function(response){
console.log(response);
});
*/
```

### Authorize and Report

You can alternatively use the **authorize** and **report** methods to do the same in two separate calls.
Note that the **report** method supports sending the usage for multiple transactions in a single call.


If you use the authentication mode with `app_id` and `app_key pair:
```javascript
var Client = require('3scale').Client;

Expand Down Expand Up @@ -197,22 +234,7 @@ client.authorize_with_user_key({ service_id: "your service id", user_key: "your
*/
```

Note that the **report** method supports sending the usage for multiple transactions in a single call.

```javascript
var trans = [
{ service_token: "your service token", app_id: "your application id", usage: {"hits": 1} },
{ service_token: "your service token", app_id: "your application id", usage: {"hits": 1000} }
]

client.report("your service id", trans, function(response){
console.log(response);
});
```

### OAuth

If you set OAuth as the authentication pattern for your API in 3scale, you will need to take the separate **authorize** and **report** approach (i.e. there is no **authrep** for OAuth).
For `OAuth` as the authentication mode:

```javascript
var Client = require('3scale').Client;
Expand All @@ -224,7 +246,7 @@ client = new Client({host: "service_management_api.example.com", port: 80});
client = new Client();
*/

client.oauth_authorize({ service_token: "your service token", service_id: "your service id", app_id: "your application id" }, function(response){
client.oauth_authorize({ service_token: "your service token", service_id: "your service id", app_id: "your Client Id" }, function(response){
if (response.is_success()) {
var trans = [{ service_token: "your service token", app_id: "your application id", usage: {"hits": 3} }];
client.report("your service id", trans, function (response) {
Expand Down Expand Up @@ -260,6 +282,19 @@ client.oauth_authorize({ "service_id": "your service id", "app_id": "your applic
*/
```

Note that the **report** method supports sending the usage for multiple transactions in a single call.

```javascript
var trans = [
{ service_token: "your service token", app_id: "your application id", usage: {"hits": 1} },
{ service_token: "your service token", app_id: "your application id", usage: {"hits": 1000} }
]

client.report("your service id", trans, function(response){
console.log(response);
});
```

## To test

To run tests: `npm test` or `vows test/* --spec` from the root directory of the project.
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "3scale",
"description": "Client for 3Scale Networks API",
"version": "0.7.4",
"version": "0.7.5",
"homepage": "http://www.3scale.net",
"repository": {
"type": "git",
Expand Down
39 changes: 39 additions & 0 deletions src/client.coffee
Expand Up @@ -238,6 +238,45 @@ module.exports = class Client
this._do_request_response req_opts, callback, "Client::authrep_with_user_key"



###
Authorize and Report in a single call with OAuth
------------------------------------------------
Parameters:
options is a Hash object with the following fields:
service_token Required if you didn't use provider_key to ceate the Client instance
app_id Required. app_id is your Client ID.
service_id Required (from November 2016)
callback {Function} Is the callback function that receives the Response object which includes `is_success` method to determine the status of the response
Example using provider_key:
client.oauth_authrep {service_id: '1234567890987', app_id: '87654321'}, (response) ->
if response.is_success
# All Ok
else
sys.puts "#{response.error_message} with code: #{response.error_code}"
Example using service_token:
client.authrep_oauth {service_token: '12sdtsdr23454sdfsdf', service_id: '1234567890987', app_id: '87654321'}, (response) ->
if response.is_success
# All Ok
else
sys.puts "#{response.error_message} with code: #{response.error_code}"
###

oauth_authrep: (options, callback) ->
this._verify_app_id_exists options

this._ensure_hits_exist_in options

query = this._build_query_string options
req_opts = this._prepare_request "/transactions/oauth_authrep.xml", query

this._do_request_response req_opts, callback, "Client::oauth_authrep"



###
Report transaction(s)
---------------------
Expand Down
50 changes: 46 additions & 4 deletions test/client_test.coffee
Expand Up @@ -54,6 +54,9 @@ describe 'Basic test for the 3Scale::Client', ->
client = new Client('1234abcd')
assert.equal typeof client.authrep_with_user_key, 'function'

it 'should have an oauth_authrep method with app_id', ->
client = new Client('1234abcd')
assert.equal typeof client.oauth_authrep, 'function'



Expand Down Expand Up @@ -127,7 +130,6 @@ describe 'Basic test for the 3Scale::Client', ->
client = new Client()
assert.throws (() -> client.oauth_authorize({}, () ->)), 'missing app_id'


it 'should call the callback with a error response if app_id was wrong', (done) ->
nock('https://su1.3scale.net')
.get('/transactions/oauth_authorize.xml')
Expand All @@ -143,6 +145,49 @@ describe 'Basic test for the 3Scale::Client', ->
nock.cleanAll()


describe 'The oauth_authrep method', ->
it 'should throw an exception if oauth_authrep called without :app_id', ->
client = new Client()
assert.throws (() -> client.oauth_authrep({}, () ->)), 'missing app_id'

it 'should call the callback with a 200 response if app_id was ok', (done) ->
nock('https://su1.3scale.net')
.get('/transactions/oauth_authrep.xml')
.query({
service_id: 1234567890987,
app_id: "foo",
"usage[hits]":1,
provider_key: "1234abcd"
})
.reply(200, '<status><authorized>true</authorized><plan>Basic</plan></status>')

client = new Client '1234abcd'
client.oauth_authrep { service_id: '1234567890987', app_id: 'foo',usage: { hits: 1 }}, (response) ->
assert.equal response.is_success(), true
assert.equal response.status_code, 200
done()

it 'should call the callback with a error response if app_id was wrong', (done) ->
nock('https://su1.3scale.net')
.get('/transactions/oauth_authrep.xml')
.query({
service_id: 1234567890987,
app_id: "ERROR",
"usage[hits]":1,
provider_key: "1234abcd"
})
.reply(403, '<error code="application_not_found">application with id="ERROR" was not found</error>')

client = new Client '1234abcd'
client.oauth_authrep {service_id: '1234567890987', app_id: 'ERROR'}, (response) ->
assert.equal response.is_success(), false
assert.equal response.status_code, 403
done()

after ->
nock.cleanAll()


describe 'Request headers in authrep calls', ->
it 'should throw an exception if authrep called without :app_id', ->
client = new Client()
Expand All @@ -162,13 +207,10 @@ describe 'Basic test for the 3Scale::Client', ->
client.authorize { service_id: '1234567890987', app_id: 'foo' }, (response) ->
assert match.isDone()
done()

after ->
nock.cleanAll()




describe 'Request headers in authrep_with_user_key calls', ->
it 'should throw an exception if is called without :user_key', ->
client = new Client()
Expand Down

0 comments on commit 69b6ab0

Please sign in to comment.