@@ -8,118 +8,133 @@ const authModel = require('./models/auth');
88const settingModel = require ( './models/setting' ) ;
99const debug_mode = process . env . NODE_ENV !== 'production' || ! ! process . env . DEBUG ;
1010
11- function setupJwt ( resolve , reject ) {
12- // Now go and check if the jwt gpg keys have been created and if not, create them
13- if ( ! config . has ( 'jwt' ) || ! config . has ( 'jwt.key' ) || ! config . has ( 'jwt.pub' ) ) {
14- logger . info ( 'Creating a new JWT key pair...' ) ;
11+ /**
12+ * Creates a new JWT RSA Keypair if not alread set on the config
13+ *
14+ * @returns {Promise }
15+ */
16+ const setupJwt = ( ) => {
17+ return new Promise ( ( resolve , reject ) => {
18+ // Now go and check if the jwt gpg keys have been created and if not, create them
19+ if ( ! config . has ( 'jwt' ) || ! config . has ( 'jwt.key' ) || ! config . has ( 'jwt.pub' ) ) {
20+ logger . info ( 'Creating a new JWT key pair...' ) ;
1521
16- // jwt keys are not configured properly
17- const filename = config . util . getEnv ( 'NODE_CONFIG_DIR' ) + '/' + ( config . util . getEnv ( 'NODE_ENV' ) || 'default' ) + '.json' ;
18- let config_data = { } ;
22+ // jwt keys are not configured properly
23+ const filename = config . util . getEnv ( 'NODE_CONFIG_DIR' ) + '/' + ( config . util . getEnv ( 'NODE_ENV' ) || 'default' ) + '.json' ;
24+ let config_data = { } ;
1925
20- try {
21- config_data = require ( filename ) ;
22- } catch ( err ) {
23- // do nothing
24- if ( debug_mode ) {
25- logger . debug ( filename + ' config file could not be required' ) ;
26+ try {
27+ config_data = require ( filename ) ;
28+ } catch ( err ) {
29+ // do nothing
30+ if ( debug_mode ) {
31+ logger . debug ( filename + ' config file could not be required' ) ;
32+ }
2633 }
27- }
2834
29- // Now create the keys and save them in the config.
30- let key = new NodeRSA ( { b : 2048 } ) ;
31- key . generateKeyPair ( ) ;
35+ // Now create the keys and save them in the config.
36+ let key = new NodeRSA ( { b : 2048 } ) ;
37+ key . generateKeyPair ( ) ;
3238
33- config_data . jwt = {
34- key : key . exportKey ( 'private' ) . toString ( ) ,
35- pub : key . exportKey ( 'public' ) . toString ( )
36- } ;
39+ config_data . jwt = {
40+ key : key . exportKey ( 'private' ) . toString ( ) ,
41+ pub : key . exportKey ( 'public' ) . toString ( ) ,
42+ } ;
3743
38- // Write config
39- fs . writeFile ( filename , JSON . stringify ( config_data , null , 2 ) , ( err ) => {
40- if ( err ) {
41- logger . error ( 'Could not write JWT key pair to config file: ' + filename ) ;
42- reject ( err ) ;
43- } else {
44- logger . info ( 'Wrote JWT key pair to config file: ' + filename ) ;
44+ // Write config
45+ fs . writeFile ( filename , JSON . stringify ( config_data , null , 2 ) , ( err ) => {
46+ if ( err ) {
47+ logger . error ( 'Could not write JWT key pair to config file: ' + filename ) ;
48+ reject ( err ) ;
49+ } else {
50+ logger . info ( 'Wrote JWT key pair to config file: ' + filename ) ;
4551
46- logger . warn ( 'Restarting interface to apply new configuration' ) ;
47- process . exit ( 0 ) ;
52+ logger . warn ( 'Restarting interface to apply new configuration' ) ;
53+ process . exit ( 0 ) ;
54+ }
55+ } ) ;
56+ } else {
57+ // JWT key pair exists
58+ if ( debug_mode ) {
59+ logger . debug ( 'JWT Keypair already exists' ) ;
4860 }
49- } ) ;
5061
51- } else {
52- // JWT key pair exists
53- if ( debug_mode ) {
54- logger . debug ( 'JWT Keypair already exists' ) ;
62+ resolve ( ) ;
5563 }
64+ } ) ;
65+ } ;
5666
57- resolve ( ) ;
58- }
59- }
60-
61- function setupDefaultUser ( ) {
62- ( userModel
67+ /**
68+ * Creates a default admin users if one doesn't already exist in the database
69+ *
70+ * @returns {Promise }
71+ */
72+ const setupDefaultUser = ( ) => {
73+ return userModel
6374 . query ( )
6475 . select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
6576 . where ( 'is_deleted' , 0 )
6677 . first ( )
67- ) . then ( ( row ) => {
68- if ( ! row . count ) {
69- // Create a new user and set password
70- logger . info ( 'Creating a new user: admin@example.com with password: changeme' ) ;
78+ . then ( ( row ) => {
79+ if ( ! row . count ) {
80+ // Create a new user and set password
81+ logger . info ( 'Creating a new user: admin@example.com with password: changeme' ) ;
7182
72- let data = {
73- is_deleted : 0 ,
74- email : 'admin@example.com' ,
75- name : 'Administrator' ,
76- nickname : 'Admin' ,
77- avatar : '' ,
78- roles : [ 'admin' ]
79- } ;
83+ let data = {
84+ is_deleted : 0 ,
85+ email : 'admin@example.com' ,
86+ name : 'Administrator' ,
87+ nickname : 'Admin' ,
88+ avatar : '' ,
89+ roles : [ 'admin' ] ,
90+ } ;
8091
81- return userModel
82- . query ( )
83- . insertAndFetch ( data )
84- . then ( ( user ) => {
85- return authModel
86- . query ( )
87- . insert ( {
88- user_id : user . id ,
89- type : 'password' ,
90- secret : 'changeme' ,
91- meta : { }
92- } )
93- . then ( ( ) => {
94- return userPermissionModel
95- . query ( )
96- . insert ( {
92+ return userModel
93+ . query ( )
94+ . insertAndFetch ( data )
95+ . then ( ( user ) => {
96+ return authModel
97+ . query ( )
98+ . insert ( {
99+ user_id : user . id ,
100+ type : 'password' ,
101+ secret : 'changeme' ,
102+ meta : { } ,
103+ } )
104+ . then ( ( ) => {
105+ return userPermissionModel . query ( ) . insert ( {
97106 user_id : user . id ,
98107 visibility : 'all' ,
99108 proxy_hosts : 'manage' ,
100109 redirection_hosts : 'manage' ,
101110 dead_hosts : 'manage' ,
102111 streams : 'manage' ,
103112 access_lists : 'manage' ,
104- certificates : 'manage'
113+ certificates : 'manage' ,
105114 } ) ;
106- } ) ;
107- } )
108- . then ( ( ) => {
109- logger . info ( 'Initial admin setup completed' ) ;
110- } ) ;
111- } else if ( debug_mode ) {
112- logger . debug ( 'Admin user setup not required' ) ;
113- }
114- } ) ;
115- }
115+ } ) ;
116+ } )
117+ . then ( ( ) => {
118+ logger . info ( 'Initial admin setup completed' ) ;
119+ } ) ;
120+ } else if ( debug_mode ) {
121+ logger . debug ( 'Admin user setup not required' ) ;
122+ }
123+ } ) ;
124+ } ;
116125
117- function setupDefaultSettings ( ) {
126+ /**
127+ * Creates default settings if they don't already exist in the database
128+ *
129+ * @returns {Promise }
130+ */
131+ const setupDefaultSettings = ( ) => {
118132 return settingModel
119133 . query ( )
120- . select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
134+ . select ( settingModel . raw ( 'COUNT(`id`) as `count`' ) )
135+ . where ( { id : 'default-site' } )
121136 . first ( )
122- . then ( ( row ) => {
137+ . then ( ( row ) => {
123138 if ( ! row . count ) {
124139 settingModel
125140 . query ( )
@@ -128,22 +143,20 @@ function setupDefaultSettings() {
128143 name : 'Default Site' ,
129144 description : 'What to show when Nginx is hit with an unknown Host' ,
130145 value : 'congratulations' ,
131- meta : { }
132- } ) . then ( ( ) => {
146+ meta : { } ,
147+ } )
148+ . then ( ( ) => {
133149 logger . info ( 'Default settings added' ) ;
134150 } ) ;
135- } if ( debug_mode ) {
151+ }
152+ if ( debug_mode ) {
136153 logger . debug ( 'Default setting setup not required' ) ;
137154 }
138155 } ) ;
139- }
156+ } ;
140157
141158module . exports = function ( ) {
142- return new Promise ( ( resolve , reject ) => {
143- return setupJwt ( resolve , reject ) ;
144- } ) . then ( ( ) => {
145- return setupDefaultUser ( ) ;
146- } ) . then ( ( ) => {
147- return setupDefaultSettings ( ) ;
148- } ) ;
159+ return setupJwt ( )
160+ . then ( setupDefaultUser )
161+ . then ( setupDefaultSettings ) ;
149162} ;
0 commit comments