Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
refactor options.apiKey to options.client.apiKey
Browse files Browse the repository at this point in the history
  • Loading branch information
typerandom committed Oct 27, 2015
1 parent 7c32245 commit fd87c43
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 70 deletions.
7 changes: 5 additions & 2 deletions lib/authc/OAuthBasicExchangeAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,23 @@ OAuthBasicExchangeAuthenticator.prototype.buildTokenResponse = function buildTok

OAuthBasicExchangeAuthenticator.prototype.buildAccesstoken = function buildAccesstoken(account) {
var self = this;

var now = nowEpochSeconds();

var _jwt = {
sub: self.id,
iss: self.application.href,
iat: now,
exp: now + self.ttl
};

var scope = self.scopeFactory(account,self.requestedScope);

if(scope){
// TODO v1.0.0 - remove string option, should be array only
_jwt.scope = Array.isArray(scope) ? scope.join(' ') : scope;
}
return self._token = jwt.encode(_jwt,self.application.dataStore.requestExecutor.options.apiKey.secret,'HS256');

return self._token = jwt.encode(_jwt, self.application.dataStore.requestExecutor.options.client.apiKey.secret, 'HS256');
};

module.exports = OAuthBasicExchangeAuthenticator;
6 changes: 3 additions & 3 deletions lib/authc/OauthAccessTokenAuthenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var utils = require('../utils');

var nowEpochSeconds = utils.nowEpochSeconds;

function getJwt(token,secret){
function getJwt(token, secret){
var jwtObject;
try{
jwtObject = jwt.decode(token, secret);
Expand Down Expand Up @@ -36,8 +36,8 @@ function validateJwt(jwtObject){
}


function OauthAccessTokenAuthenticator(application,token){
var jwtObject = getJwt(token,application.dataStore.requestExecutor.options.apiKey.secret);
function OauthAccessTokenAuthenticator(application, token){
var jwtObject = getJwt(token, application.dataStore.requestExecutor.options.client.apiKey.secret);

if(jwtObject instanceof Error){
return jwtObject;
Expand Down
9 changes: 5 additions & 4 deletions lib/jwt/jwt-authentication-result.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function JwtAuthenticationResult(application,data) {
if (!(this instanceof JwtAuthenticationResult)) {
return new JwtAuthenticationResult(application,data);
}else{

/*
Take all the properties of the data response, and put them
on this object - but convert underscores to camelcase cuz
Expand All @@ -25,16 +24,18 @@ function JwtAuthenticationResult(application,data) {
*/

this.application = application;
var apiKey = application.dataStore.requestExecutor.options.client.apiKey;

if(this.accessToken){
this.accessToken = nJwt.verify(this.accessToken,application.dataStore.requestExecutor.options.apiKey.secret);
this.accessToken = nJwt.verify(this.accessToken, apiKey.secret);
this.account = {
href: this.accessToken.body.sub
};
}
if(this.refreshToken){
this.refreshToken = nJwt.verify(this.refreshToken,application.dataStore.requestExecutor.options.apiKey.secret);
this.refreshToken = nJwt.verify(this.refreshToken, apiKey.secret);
}

return this;
}
}
Expand All @@ -50,4 +51,4 @@ JwtAuthenticationResult.prototype.getAccount = function getAccount(callback) {
this.application.dataStore.getResource(this.account.href, require('../resource/Account'), callback);
};

module.exports = JwtAuthenticationResult;
module.exports = JwtAuthenticationResult;
2 changes: 1 addition & 1 deletion lib/jwt/jwt-authenticator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ JwtAuthenticator.prototype.unauthenticated = function unauthenticated(){
JwtAuthenticator.prototype.authenticate = function authenticate(token,cb){
var self = this;

var secret = self.application.dataStore.requestExecutor.options.apiKey.secret;
var secret = self.application.dataStore.requestExecutor.options.client.apiKey.secret;

njwt.verify(token,secret,function(err,jwt){
if(err){
Expand Down
3 changes: 1 addition & 2 deletions lib/resource/ApiKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ApiKey.prototype._getDecryptedSecret = function _getDecryptedSecret(callback) {
var salt = self.apiKeyMetaData.encryptionKeySalt;
var iterations = self.apiKeyMetaData.encryptionKeyIterations;
var keyLengthBits = self.apiKeyMetaData.encryptionKeySize;
var password = new Buffer(self.dataStore.requestExecutor.options.apiKey.secret);
var password = new Buffer(self.dataStore.requestExecutor.options.client.apiKey.secret);
var encryptedSecret = new Buffer(self.secret,'base64');
var iv = encryptedSecret.slice(0,16);
var rawEncryptedValue = encryptedSecret.slice(16);
Expand All @@ -34,7 +34,6 @@ ApiKey.prototype._getDecryptedSecret = function _getDecryptedSecret(callback) {
}
callback(null,decrypted);
});

};

ApiKey.prototype._setApiKeyMetaData = function _setApiKeyMetaData(obj){
Expand Down
6 changes: 3 additions & 3 deletions lib/resource/Application.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Application.prototype.createIdSiteUrl = function createIdSiteUrl(_options) {
var options = typeof _options === "object" ? _options : {};
var p = url.parse(self.href);
var base = p.protocol + '//' + p.host;
var apiKey = self.dataStore.requestExecutor.options.apiKey;
var apiKey = self.dataStore.requestExecutor.options.client.apiKey;
var nonce = uuid();
var state = options.state || '';

Expand Down Expand Up @@ -87,8 +87,8 @@ Application.prototype.handleIdSiteCallback = function handleIdSiteCallback(respo

var params = (url.parse(responseUri,true).query) || {};
var token = params.jwtResponse || '';
var secret = self.dataStore.requestExecutor.options.apiKey.secret;
var apiKeyId = self.dataStore.requestExecutor.options.apiKey.id;
var secret = self.dataStore.requestExecutor.options.client.apiKey.secret;
var apiKeyId = self.dataStore.requestExecutor.options.client.apiKey.id;

var responseJwt = self._decodeJwt(token,secret);

Expand Down
2 changes: 1 addition & 1 deletion lib/resource/AuthenticationResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ AuthenticationResult.prototype.getJwt = function getJwt() {
iss: self.application.href,
sub: self.forApiKey ? self.forApiKey.id : self.account.href,
jti: utils.uuid()
},self.application.dataStore.requestExecutor.options.apiKey.secret)
},self.application.dataStore.requestExecutor.options.client.apiKey.secret)
.setExpiration(new Date().getTime() + (3600*1000));
};

Expand Down
6 changes: 3 additions & 3 deletions test/it/api_auth_it.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ describe('Application.authenticateApiRequest',function(){
before(function(done){

var decodedJwt = nJwt.verify(accessToken,
client._dataStore.requestExecutor.options.apiKey.secret,'HS256');
client._dataStore.requestExecutor.options.client.apiKey.secret,'HS256');
decodedJwt.body.scope += ' things-i-cant-have';
var tamperedToken = nJwt.create(decodedJwt.body,'not the same key','HS256').compact();
var requestObject = {
Expand Down Expand Up @@ -569,7 +569,7 @@ describe('Application.authenticateApiRequest',function(){
},function(err,value){
result = [err,value];
decodedAccessToken = nJwt.verify(result[1].tokenResponse.access_token,
client._dataStore.requestExecutor.options.apiKey.secret,'HS256');
client._dataStore.requestExecutor.options.client.apiKey.secret,'HS256');
done();
});
});
Expand Down Expand Up @@ -620,7 +620,7 @@ describe('Application.authenticateApiRequest',function(){
result = [err,value];
tokenResponse = value.tokenResponse;
decodedAccessToken = nJwt.verify(result[1].tokenResponse.access_token,
client._dataStore.requestExecutor.options.apiKey.secret,'HS256');
client._dataStore.requestExecutor.options.client.apiKey.secret,'HS256');
done();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/it/client_credential_auth_it.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Client Credential Authentication',function(){

it('should return an access token',function(done){
assert.isString(accessToken);
var secret = client._dataStore.requestExecutor.options.apiKey.secret;
var secret = client._dataStore.requestExecutor.options.client.apiKey.secret;
nJwt.verify(accessToken,secret,function(err,jwt){
if(err){ throw err; }
// The subject should be the account
Expand Down
2 changes: 1 addition & 1 deletion test/it/jwt_authenticator_it.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('JwtAuthenticator',function(){

expiredToken = nJwt.create(
{hello:'world'},
application.dataStore.requestExecutor.options.apiKey.secret
application.dataStore.requestExecutor.options.client.apiKey.secret
).setExpiration(new Date().getTime())
.compact();

Expand Down
2 changes: 1 addition & 1 deletion test/it/oauth_authenticator_it.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('OAuthAuthenticator',function(){

expiredToken = nJwt.create(
{hello:'world'},
application.dataStore.requestExecutor.options.apiKey.secret
application.dataStore.requestExecutor.options.client.apiKey.secret
).setExpiration(new Date().getTime())
.compact();

Expand Down
34 changes: 24 additions & 10 deletions test/sp.ds.datastore_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ describe('ds:', function () {

describe('when constructed', function () {
describe('and request executor not provided in config', function () {
var ds = new DataStore({apiKey: {id: 1, secret: 2}});
var ds = new DataStore({client: {apiKey: {id: 1, secret: 2}}});
it('should create instance of default RequestExecutor', function () {
ds.requestExecutor.should.be.an.instanceof(RequestExecutor);
});
});

describe('and request executor was provided in config', function () {
var reqExec = new RequestExecutor({apiKey: {id: 1, secret: 2}});
var reqExec = new RequestExecutor({client: {apiKey: {id: 1, secret: 2}}});
var ds = new DataStore({requestExecutor: reqExec});
it('should reuse provided request executor instance', function () {
ds.requestExecutor.should.be.equal(reqExec);
Expand All @@ -44,8 +44,12 @@ describe('ds:', function () {

describe('getResource()', function () {
var ds = new DataStore({
cacheOptions: { store: 'memory' },
apiKey: {id: 1, secret: 2}
cacheOptions: {
store: 'memory'
},
client{
apiKey: {id: 1, secret: 2}
}
});

describe('without required params', function () {
Expand Down Expand Up @@ -167,7 +171,9 @@ describe('ds:', function () {
tti: 60
}
},
apiKey: {id: 1, secret: 2}
client: {
apiKey: {id: 1, secret: 2}
}
});

var href = '/tenants/3' + random();
Expand Down Expand Up @@ -216,7 +222,9 @@ describe('ds:', function () {
tti: 60
}
},
apiKey: {id: 1, secret: 2}
client: {
apiKey: {id: 1, secret: 2}
}
});

var href = '/tenants/3' + random();
Expand Down Expand Up @@ -251,7 +259,9 @@ describe('ds:', function () {
tti: 60
}
},
apiKey: {id: 1, secret: 2}
client: {
apiKey: {id: 1, secret: 2}
}
});

var href = '/tenants/3' + random();
Expand Down Expand Up @@ -317,7 +327,9 @@ describe('ds:', function () {
ttl: 60,
tti: 60
},
apiKey: {id: 1, secret: 2}
client: {
apiKey: {id: 1, secret: 2}
}
});

var href = '/tenants/2' + random();
Expand Down Expand Up @@ -369,7 +381,9 @@ describe('ds:', function () {
ttl: 60,
tti: 60
},
apiKey: {id: 1, secret: 2}
client: {
apiKey: {id: 1, secret: 2}
}
});

var href = '/tenants/2' + random();
Expand Down Expand Up @@ -408,4 +422,4 @@ describe('ds:', function () {
});
});
});
});
});
6 changes: 3 additions & 3 deletions test/sp.ds.requestExecutor_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ describe('ds:', function () {
});
});
describe('create with required options', function () {
var reqExec = new RequestExecutor({apiKey: apiKey});
var reqExec = new RequestExecutor({client: {apiKey: apiKey}});
it('should instantiate request authenticator', function () {
reqExec.requestAuthenticator.should.be.ok;
});
it('should store options', function () {
reqExec.options.apiKey.should.be.equal(apiKey);
reqExec.options.client.apiKey.should.be.equal(apiKey);
});
it('should set headers user agent as stormpath-sdk', function () {
reqExec.options.headers['User-Agent'].should
Expand All @@ -40,7 +40,7 @@ describe('ds:', function () {

});
describe('call to execute', function () {
var reqExec = new RequestExecutor({apiKey: apiKey});
var reqExec = new RequestExecutor({client: {apiKey: apiKey} });

function exec(req, cb) {
return function () {
Expand Down
4 changes: 2 additions & 2 deletions test/sp.resource.accountStoreMapping_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var BASE_URL = u.BASE_URL;
describe('Resources: ', function () {
"use strict";
describe('Account Store Mapping resource', function () {
var dataStore = new DataStore({apiKey: {id: 1, secret: 2}});
var dataStore = new DataStore({client: {apiKey: {id: 1, secret: 2}}});

describe('get application', function () {
var asm, appData, app, app2, accountStoreMapping;
Expand Down Expand Up @@ -148,4 +148,4 @@ describe('Resources: ', function () {
});
});
});
});
});
12 changes: 7 additions & 5 deletions test/sp.resource.account_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@ describe('Resources: ', function () {
"use strict";
describe('Account resource class', function () {
var dataStore = new DataStore({
apiKey: {
id: 1,
// this secret will decrypt the api keys correctly
secret: '6b2c3912-4779-49c1-81e7-23c204f43d2d'
client: {
apiKey: {
id: 1,
// this secret will decrypt the api keys correctly
secret: '6b2c3912-4779-49c1-81e7-23c204f43d2d'
}
}
});

Expand Down Expand Up @@ -463,4 +465,4 @@ describe('Resources: ', function () {
});


});
});
Loading

0 comments on commit fd87c43

Please sign in to comment.