forked from vgheri/ShopWithMe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created infrastructure objects and functions to generate api access t…
…okens. Also added method to AccountRepository to find or create a user
- Loading branch information
Showing
5 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |