Skip to content

Commit

Permalink
Created infrastructure objects and functions to generate api access t…
Browse files Browse the repository at this point in the history
…okens. Also added method to AccountRepository to find or create a user
  • Loading branch information
vgheri committed Aug 22, 2013
1 parent 1c17cc7 commit 4722383
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 1 deletion.
39 changes: 39 additions & 0 deletions infrastructure/apiAccessToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Created with JetBrains WebStorm.
* User: valerio
* Date: 20/08/13
* Time: 19.13
* To change this template use File | Settings | File Templates.
*/

var Random = require('../utils/random')
var moment = require('moment');
/*
Date.prototype.addHours = function(h) {
this.setTime(this.getTime() + (h*60*60*1000));
return this;
}
*/

var ApiAccessToken = function(userId, application) {
this.accessToken = Random.generateApiAccessToken();
this.issuedAt = moment();
this.expirationDate = moment().add('h', 24);
this.application = application;
this.user = userId;
};

module.exports = ApiAccessToken;


/*
Continua creando un handler per lo use case del login/logout
Crea un endpoint per il login via app mobili che accetta in ingresso
il nome dell'app (android/web) e il facebook access token.
Dopo di che l'api fa una chiamata a facebook per verificare che il token sia
valido e farsi restituire un oggetto che modella il profilo utente associato.
Poi questa funziona deve creare un oggetto user profile così come definito da Passport.js.
Poi invoca una funziona wrapper che accetta come parametri il nome dell'app, lo user profile e il fb access token e che
poi chiama findOrCreateUser, genera api access token e salva api, fb tokens e che ritorna all'utilizzatore l'api
access token.
*/
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dependencies": {
"express": "3.x",
"mongoose": "3.5.x",
"winston": "0.6.x"
"winston": "0.6.x",
"moment": "2.1.x"
},
"devDependencies": {
"supertest": "0.5.x",
Expand Down
25 changes: 25 additions & 0 deletions repositories/accountRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function AccountRepository() {
this.findAccountByUsername = findAccountByUsername;
this.updateAccount = updateAccount;
this.disableAccount = disableAccount;
this.findOrCreateUser = findOrCreateUser;
}

function findAccountById(id) {
Expand Down Expand Up @@ -162,4 +163,28 @@ function disableAccount(username) {
return deferred.promise;
}

// Attempt to find an existing account by username, and if it cannot find it, it creates it
// userProfile is of type UserProfile from Passport.js. See http://passportjs.org/guide/profile/
function findOrCreateUser(userProfile) {
var deferred = Q.defer();
var email = userProfile.emails[0];
this.findAccountByUsername(email)
.then(function(account) {
if (account && account.username && account.username !== '') {
deferred.resolve(account); // Found!
}
else {
// Let's create the account
this.createAccount(email, '', userProfile.givenName, userProfile.familyName)
.then(function(account) {
deferred.resolve(account);
});
}
})
.fail(function(err) {
deferred.reject(err);
});
return deferred.promise;
}

module.exports = AccountRepository;
34 changes: 34 additions & 0 deletions test/ApiAccessToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Created with JetBrains WebStorm.
* User: valerio
* Date: 22/08/13
* Time: 12.38
* Unit tests for object ApiAccessToken.
*/
var should = require("should");
var ApiAccessToken = require("../infrastructure/apiAccessToken");
var assert = require("assert");
var moment = require("moment");

describe('ApiAccessToken', function() {
var apiToken;
before(function(done) {
apiToken = new ApiAccessToken('vgheri@test.com', 'Mocha');
done();
});
it('should have a non empty property accessToken', function() {
apiToken.should.not.have.property('accessToken', '');
apiToken.should.not.have.property('accessToken', null);
});
it('should have property expiration date set to 24 hours later than issued at date', function() {
var expirationDate = moment(apiToken.expirationDate);
var issuedAt = moment(apiToken.issuedAt);
expirationDate.diff(issuedAt, 'h').should.equal(24);
});
it('should have correctly set the user property', function() {
apiToken.should.have.property('user', 'vgheri@test.com');
});
it('should have correctly set the application name property', function() {
apiToken.should.have.property('application', 'Mocha');
});
});
24 changes: 24 additions & 0 deletions utils/Random.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Created with JetBrains WebStorm.
* User: valerio
* Date: 20/08/13
* Time: 19.35
* To change this template use File | Settings | File Templates.
*/

var Random = {
generateApiAccessToken : generateToken
}

function generateToken()
{
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

for( var i=0; i < 128; i++ ) {
text += possible.charAt(Math.floor(Math.random() * possible.length));
}
return text;
}

module.exports = Random;

0 comments on commit 4722383

Please sign in to comment.