Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gmetais committed Dec 9, 2014
1 parent 94b1cce commit ebeb331
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 15 deletions.
7 changes: 5 additions & 2 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var FtvenNodeAuthSdk = require('../index.js');

var params = require('./example-params.json');

// The example-params.json file looks like this:
/*
var params = {
"apiUrl": "",
Expand All @@ -14,10 +15,12 @@ var params = {

var sdk = new FtvenNodeAuthSdk(params.apiUrl, params.clientId, params.userId, params.userPwd);

sdk.call(params.verb, params.uri)
.then(function(httpResponse, body) {
sdk.call(params.verb, params.uri, null, {'X-Ftven-Debug': 1})
.then(function(body) {
console.log('success');
console.log(body);
})
.fail(function(error) {
console.log('fail');
console.log(error);
});
26 changes: 25 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var request = require('request');
var url = require('url');
var Q = require('q');
var debug = require('debug')('index');

var ClientToken = require('./lib/clientToken');
var UserToken = require('./lib/userToken');
Expand Down Expand Up @@ -30,17 +31,26 @@ var FtvenNodeAuthSdk = function(apiUrl, clientId, userId, userPwd) {
function mergeOptions(verb, uri, data, headers, options) {
var deferred = Q.defer();

debug('Merging options');

try {

options = options || {};
options.url = url.resolve(apiUrl, uri);
options.method = verb;
options.body = (data) ? JSON.stringify(data) : data;
if (options.method === 'POST' || options.method === 'PATCH' || options.method === 'PUT') {
options.body = (data) ? JSON.stringify(data) : data;
}
options.headers = headers || {};

debug('Merged options:');
debug(options);

deferred.resolve(options);

} catch(err) {
debug('Error while merging options');
debug(err);
deferred.reject(err);
}

Expand All @@ -49,10 +59,15 @@ var FtvenNodeAuthSdk = function(apiUrl, clientId, userId, userPwd) {


function addClientTokenHeader(options) {

debug('Checking client token');

if (!clientToken.hasToken() || !clientToken.isExpiredToken()) {

debug('Client token null or expired');

return clientToken.requestToken().then(function() {
debug('New client token seems good');
options.headers[clientToken.getKey()] = clientToken.getValue();
return options;
});
Expand Down Expand Up @@ -91,14 +106,23 @@ var FtvenNodeAuthSdk = function(apiUrl, clientId, userId, userPwd) {
function sendRequest(options) {
var deferred = Q.defer();

debug('Sending the request');
debug(options);

request(options, function(err, httpResponse, body) {
if (err) {
debug('Request failed');
debug(err);
deferred.reject(err);
} else {

try {
debug('Success');
debug(body);
deferred.resolve(JSON.parse(body));
} catch(e) {
debug('Request failed');
debug(e);
deferred.reject('Error request response is not a valid JSON format');
}
}
Expand Down
31 changes: 25 additions & 6 deletions lib/clientToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var request = require('request');
var printf = require('printf');
var Q = require('q');
var url = require('url');
var debug = require('debug')('clientToken');

var ClientToken = function(apiUrl, clientId) {
'use strict';
Expand All @@ -16,7 +17,7 @@ var ClientToken = function(apiUrl, clientId) {


this.getValue = function() {
return printf('id: %s, expire: %s, token: %s', tokenInfos.id, tokenInfos.expire, tokenInfos.token);
return printf('id: %s, expire: %s, token: %s', tokenInfos.id, tokenInfos.expireString, tokenInfos.token);
};


Expand All @@ -33,40 +34,55 @@ var ClientToken = function(apiUrl, clientId) {
this.requestToken = function() {
var deferred = Q.defer();

debug('Requesting the client token');

var headers = {};
headers[tokenKey] = printf('id: %s', clientId);

request({
var requestOptions = {
url: url.resolve(apiUrl, '/access_token'),
method: 'POST',
headers: headers
}, function(err, httpResponse, body) {
};

debug(requestOptions);

request(requestOptions, function(err, httpResponse, body) {

if (err) {

debug('Could not retrieve client token');
debug(err);
deferred.reject(err);

} else {

debug('Response from API code:%d', httpResponse.statusCode);
debug('Headers:');
debug(httpResponse.headers);

if (httpResponse.statusCode !== 204 && httpResponse.statusCode !== 201) {
if (body) {
try {
var data = JSON.parse(body);
if (data.code && data.message) {

debug(printf('Error %d %s (API Client Token)', data.code, data.message));
deferred.reject(printf('Error %d %s (API Client Token)', data.code, data.message));
return;
}
} catch(e) {}
}
debug(printf('Error %d (API Client Token)', httpResponse.statusCode));
deferred.reject(printf('Error %d (API Client Token)', httpResponse.statusCode));
return;
}

if (httpResponse.headers && httpResponse.headers[tokenKey]) {
if (httpResponse.headers && httpResponse.headers[tokenKey.toLowerCase()]) {

// Hey, the token is here!
saveToken(httpResponse.headers[tokenKey]);
saveToken(httpResponse.headers[tokenKey.toLowerCase()]);
debug('Client token is ok and saved');
deferred.resolve();

} else {
Expand All @@ -86,10 +102,13 @@ var ClientToken = function(apiUrl, clientId) {
throw "The passed access token is not valid (API Client Token)";
}

var expireString = data.match(/expire: ([^,]+)/)[1];

// Extract the data from the access token
tokenInfos = {
id: data.match(/id: ([^,]+)/)[1],
expire: new Date(data.match(/expire: ([^,]+)/)[1]),
expire: expireString,
expireString: expireString,
token: data.match(/token: ([^,]+)/)[1]
};
}
Expand Down
31 changes: 25 additions & 6 deletions lib/userToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var request = require('request');
var printf = require('printf');
var Q = require('q');
var url = require('url');
var debug = require('debug')('userToken');

var UserToken = function(apiUrl, clientToken, userId, userPwd) {
'use strict';
Expand All @@ -16,7 +17,7 @@ var UserToken = function(apiUrl, clientToken, userId, userPwd) {


this.getValue = function() {
return printf('id: %s, expire: %s, token: %s', tokenInfos.id, tokenInfos.expire, tokenInfos.token);
return printf('id: %s, expire: %s, token: %s', tokenInfos.id, tokenInfos.expireString, tokenInfos.token);
};


Expand All @@ -33,41 +34,56 @@ var UserToken = function(apiUrl, clientToken, userId, userPwd) {
this.requestToken = function() {
var deferred = Q.defer();

debug('Requesting the user token');

var headers = {};
headers[clientToken.getKey()] = clientToken.getValue();
headers[tokenKey] = printf('id: %s, password: %s', userId, userPwd);

request({
var requestOptions = {
url: url.resolve(apiUrl, '/user_token'),
method: 'POST',
headers: headers
}, function(err, httpResponse, body) {
};

debug(requestOptions);

request(requestOptions, function(err, httpResponse, body) {

if (err) {

debug('Could not retrieve user token');
debug(err);
deferred.reject(err);

} else {

debug('Response from API code:%d', httpResponse.statusCode);
debug('Headers:');
debug(httpResponse.headers);

if (httpResponse.statusCode !== 204 && httpResponse.statusCode !== 201) {
if (body) {
try {
var data = JSON.parse(body);
if (data.code && data.message) {

debug(printf('Error %d %s (API User Token)', data.code, data.message));
deferred.reject(printf('Error %d %s (API User Token)', data.code, data.message));
return;
}
} catch(e) {}
}
debug(printf('Error %d (API User Token)', httpResponse.statusCode));
deferred.reject(printf('Error %d (API User Token)', httpResponse.statusCode));
return;
}

if (httpResponse.headers && httpResponse.headers[tokenKey]) {
if (httpResponse.headers && httpResponse.headers[tokenKey.toLowerCase()]) {

// Hey, the token is here!
saveToken(httpResponse.headers[tokenKey]);
saveToken(httpResponse.headers[tokenKey.toLowerCase()]);
debug('User token is ok and saved');
deferred.resolve();

} else {
Expand All @@ -87,10 +103,13 @@ var UserToken = function(apiUrl, clientToken, userId, userPwd) {
throw "The passed access token is not valid (API User Token)";
}

var expireString = data.match(/expire: ([^,]+)/)[1];

// Extract the data from the access token
tokenInfos = {
id: data.match(/id: ([^,]+)/)[1],
expire: new Date(data.match(/expire: ([^,]+)/)[1]),
expire: new Date(expireString),
expireString: expireString,
token: data.match(/token: ([^,]+)/)[1]
};
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"homepage": "https://github.com/francetv/node-auth-sdk",
"dependencies": {
"debug": "^2.1.0",
"printf": "^0.2.0",
"q": "^1.1.2",
"request": "^2.49.0"
Expand Down

0 comments on commit ebeb331

Please sign in to comment.