-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencryptionServiceProvider.js
50 lines (41 loc) · 1.33 KB
/
encryptionServiceProvider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const ServiceProvider = require('@ostro/support/serviceProvider');
const Encrypter = require('./encrypter')
const HashManager = require('./hashManager')
const MissingAppKeyException = require('./missingAppKeyException')
class EncryptionServiceProvider extends ServiceProvider {
register() {
this.registerEncrypter();
this.registerHash()
}
registerHash() {
this.$app.singleton('hash', function($app) {
return new HashManager($app);
});
this.$app.singleton('hash.driver', function($app) {
return $app['hash'].driver();
});
}
registerEncrypter() {
this.$app.singleton('encrypter', ($app) => {
let $config = $app.make('config').get('app');
return new Encrypter(this.parseKey($config), $config['cipher']);
});
}
parseKey($config) {
let $key = this.key($config)
let $prefix = 'base64:'
if (String.startsWith($key, $prefix)) {
$key = new Buffer(String.after($key, $prefix), 'base64');
$key = $key.toString('ascii')
}
return $key;
}
key($config) {
return tap($config['key'], function($key) {
if (!$key) {
throw new MissingAppKeyException;
}
});
}
}
module.exports = EncryptionServiceProvider