7
7
*/
8
8
// Command line tool for npm start
9
9
"use strict"
10
- const packageJson = require ( 'package-json' ) ;
11
- const basicAuth = require ( 'basic-auth' ) ;
12
10
const path = require ( 'path' ) ;
13
11
const jsonFile = require ( 'json-file-plus' ) ;
14
12
const express = require ( 'express' ) ;
13
+ const parseDashboard = require ( './app' ) ;
15
14
16
15
const program = require ( 'commander' ) ;
17
16
program . option ( '--appId [appId]' , 'the app Id of the app you would like to manage.' ) ;
@@ -24,17 +23,6 @@ program.option('--allowInsecureHTTP [allowInsecureHTTP]', 'set this flag when yo
24
23
25
24
program . parse ( process . argv ) ;
26
25
27
- const currentVersionFeatures = require ( '../package.json' ) . parseDashboardFeatures ;
28
-
29
- var newFeaturesInLatestVersion = [ ] ;
30
- packageJson ( 'parse-dashboard' , 'latest' ) . then ( latestPackage => {
31
- if ( latestPackage . parseDashboardFeatures instanceof Array ) {
32
- newFeaturesInLatestVersion = latestPackage . parseDashboardFeatures . filter ( feature => {
33
- return currentVersionFeatures . indexOf ( feature ) === - 1 ;
34
- } ) ;
35
- }
36
- } ) ;
37
-
38
26
const port = program . port || process . env . PORT || 4040 ;
39
27
const allowInsecureHTTP = program . allowInsecureHTTP || process . env . PARSE_DASHBOARD_ALLOW_INSECURE_HTTP ;
40
28
@@ -103,75 +91,7 @@ p.then(config => {
103
91
104
92
const app = express ( ) ;
105
93
106
- // Serve public files.
107
- app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ;
108
-
109
- // Serve the configuration.
110
- app . get ( '/parse-dashboard-config.json' , function ( req , res ) {
111
- const response = {
112
- apps : config . data . apps ,
113
- newFeaturesInLatestVersion : newFeaturesInLatestVersion ,
114
- } ;
115
- const users = config . data . users ;
116
-
117
- let auth = null ;
118
- //If they provide auth when their config has no users, ignore the auth
119
- if ( users ) {
120
- auth = basicAuth ( req ) ;
121
- }
122
-
123
- //Based on advice from Doug Wilson here:
124
- //https://github.com/expressjs/express/issues/2518
125
- const requestIsLocal =
126
- req . connection . remoteAddress === '127.0.0.1' ||
127
- req . connection . remoteAddress === '::ffff:127.0.0.1' ||
128
- req . connection . remoteAddress === '::1' ;
129
- if ( ! requestIsLocal && ! req . secure && ! allowInsecureHTTP ) {
130
- //Disallow HTTP requests except on localhost, to prevent the master key from being transmitted in cleartext
131
- return res . send ( { success : false , error : 'Parse Dashboard can only be remotely accessed via HTTPS' } ) ;
132
- }
133
-
134
- if ( ! requestIsLocal && ! users ) {
135
- //Accessing the dashboard over the internet can only be done with username and password
136
- return res . send ( { success : false , error : 'Configure a user to access Parse Dashboard remotely' } ) ;
137
- }
138
-
139
- const successfulAuth =
140
- //they provided auth
141
- auth &&
142
- //there are configured users
143
- users &&
144
- //the provided auth matches one of the users
145
- users . find ( user => {
146
- return user . user == auth . name &&
147
- user . pass == auth . pass
148
- } ) ;
149
- if ( successfulAuth ) {
150
- //They provided correct auth
151
- return res . json ( response ) ;
152
- }
153
-
154
- if ( users || auth ) {
155
- //They provided incorrect auth
156
- res . set ( 'WWW-Authenticate' , 'Basic realm=Authorization Required' ) ;
157
- return res . sendStatus ( 401 ) ;
158
- }
159
-
160
- //They didn't provide auth, and have configured the dashboard to not need auth
161
- //(ie. didn't supply usernames and passwords)
162
- if ( requestIsLocal ) {
163
- //Allow no-auth access on localhost only, if they have configured the dashboard to not need auth
164
- return res . json ( response ) ;
165
- }
166
- //We shouldn't get here. Fail closed.
167
- res . send ( { success : false , error : 'Something went wrong.' } ) ;
168
- } ) ;
169
-
170
- // For every other request, go to index.html. Let client-side handle the rest.
171
- app . get ( '/*' , function ( req , res ) {
172
- res . sendFile ( __dirname + '/index.html' ) ;
173
- } ) ;
174
-
94
+ app . use ( parseDashboard ( config . data ) ) ;
175
95
// Start the server.
176
96
app . listen ( port ) ;
177
97
0 commit comments