@@ -8,118 +8,133 @@ const authModel = require('./models/auth');
8
8
const settingModel = require ( './models/setting' ) ;
9
9
const debug_mode = process . env . NODE_ENV !== 'production' || ! ! process . env . DEBUG ;
10
10
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...' ) ;
15
21
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 = { } ;
19
25
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
+ }
26
33
}
27
- }
28
34
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 ( ) ;
32
38
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
+ } ;
37
43
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 ) ;
45
51
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' ) ;
48
60
}
49
- } ) ;
50
61
51
- } else {
52
- // JWT key pair exists
53
- if ( debug_mode ) {
54
- logger . debug ( 'JWT Keypair already exists' ) ;
62
+ resolve ( ) ;
55
63
}
64
+ } ) ;
65
+ } ;
56
66
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
63
74
. query ( )
64
75
. select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
65
76
. where ( 'is_deleted' , 0 )
66
77
. 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' ) ;
71
82
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
+ } ;
80
91
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 ( {
97
106
user_id : user . id ,
98
107
visibility : 'all' ,
99
108
proxy_hosts : 'manage' ,
100
109
redirection_hosts : 'manage' ,
101
110
dead_hosts : 'manage' ,
102
111
streams : 'manage' ,
103
112
access_lists : 'manage' ,
104
- certificates : 'manage'
113
+ certificates : 'manage' ,
105
114
} ) ;
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
+ } ;
116
125
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 = ( ) => {
118
132
return settingModel
119
133
. query ( )
120
- . select ( userModel . raw ( 'COUNT(`id`) as `count`' ) )
134
+ . select ( settingModel . raw ( 'COUNT(`id`) as `count`' ) )
135
+ . where ( { id : 'default-site' } )
121
136
. first ( )
122
- . then ( ( row ) => {
137
+ . then ( ( row ) => {
123
138
if ( ! row . count ) {
124
139
settingModel
125
140
. query ( )
@@ -128,22 +143,20 @@ function setupDefaultSettings() {
128
143
name : 'Default Site' ,
129
144
description : 'What to show when Nginx is hit with an unknown Host' ,
130
145
value : 'congratulations' ,
131
- meta : { }
132
- } ) . then ( ( ) => {
146
+ meta : { } ,
147
+ } )
148
+ . then ( ( ) => {
133
149
logger . info ( 'Default settings added' ) ;
134
150
} ) ;
135
- } if ( debug_mode ) {
151
+ }
152
+ if ( debug_mode ) {
136
153
logger . debug ( 'Default setting setup not required' ) ;
137
154
}
138
155
} ) ;
139
- }
156
+ } ;
140
157
141
158
module . 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 ) ;
149
162
} ;
0 commit comments