Skip to content

Commit

Permalink
feat(token): encrypt tokens before sending
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 29, 2018
1 parent ae0a4fd commit fef3d2f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions app/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions app/config/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
appKey: '16charslongtoken',
logger: {
transport: 'console',
console: {
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@
"devDependencies": {
"@adonisjs/ace": "^5.0.1",
"@adonisjs/fold": "^4.0.7",
"@adonisjs/framework": "^5.0.5",
"@adonisjs/framework": "^5.0.6",
"@adonisjs/lucid": "^5.0.3",
"@adonisjs/sink": "^1.0.16",
"@adonisjs/validator": "^5.0.3",
"coveralls": "^3.0.0",
"cz-conventional-changelog": "^2.1.0",
"japa": "^1.0.5",
"japa": "^1.0.6",
"japa-cli": "^1.0.1",
"nyc": "^11.6.0",
"sqlite3": "^4.0.0",
"standard": "^10.0.3"
"standard": "^11.0.1"
},
"dependencies": {
"@adonisjs/generic-exceptions": "^2.0.0",
Expand Down
3 changes: 2 additions & 1 deletion providers/PersonaProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class PersonaProvider extends ServiceProvider {
const Config = app.use('Adonis/Src/Config')
const Event = app.use('Adonis/Src/Event')
const Hash = app.use('Adonis/Src/Hash')
const Encryption = app.use('Adonis/Src/Encryption')
const Validator = app.use('Adonis/Addons/Validator')
const Persona = require('../src/Persona')

return new Persona(Config, Validator, Event, Hash)
return new Persona(Config, Validator, Event, Encryption, Hash)
})

this.app.alias('Adonis/Addons/Persona', 'Persona')
Expand Down
6 changes: 4 additions & 2 deletions src/Persona.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class InvalidTokenException extends GE.LogicalException {
* @param {Object} Hash
*/
class Persona {
constructor (Config, Validator, Event, Hash) {
constructor (Config, Validator, Event, Encryption, Hash) {
this.config = Config.merge('persona', {
uids: ['email'],
email: 'email',
Expand All @@ -56,6 +56,8 @@ class Persona {
this.Hash = Hash
this.Event = Event
this.Validator = Validator

this._encrypter = Encryption.getInstance({ hmac: false })
this._model = null
}

Expand Down Expand Up @@ -184,7 +186,7 @@ class Persona {
return row.token
}

const token = randtoken.generate(16)
const token = this._encrypter.encrypt(randtoken.generate(16))
await user.tokens().create({ type, token })
return token
}
Expand Down
2 changes: 1 addition & 1 deletion test/persona.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test.group('Persona', (group) => {
})

group.beforeEach(async () => {
this.persona = new Persona(use('Config'), use('Validator'), use('Event'), use('Hash'))
this.persona = new Persona(use('Config'), use('Validator'), use('Event'), use('Encryption'), use('Hash'))
await use('Database').beginGlobalTransaction()
})

Expand Down

0 comments on commit fef3d2f

Please sign in to comment.