1
- ' use strict'
1
+ " use strict" ;
2
2
3
- const METABASE_SITE_URL = process . env . METABASE_SITE_URL || "http://localhost:3000" ;
3
+ const METABASE_SITE_URL =
4
+ process . env . METABASE_SITE_URL || "http://localhost:3000" ;
4
5
const METABASE_JWT_SHARED_SECRET =
5
6
process . env . METABASE_JWT_SHARED_SECRET ||
6
7
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" ;
@@ -9,80 +10,94 @@ const mods = 'logo=false&top_nav=false&search=false&new_button=false&side_nav=fa
9
10
* Module dependencies.
10
11
*/
11
12
12
- const express = require ( ' express' )
13
- const hash = require ( ' pbkdf2-password' ) ( )
14
- const path = require ( ' path' ) ;
15
- const session = require ( ' express-session' ) ;
13
+ const express = require ( " express" ) ;
14
+ const hash = require ( " pbkdf2-password" ) ( ) ;
15
+ const path = require ( " path" ) ;
16
+ const session = require ( " express-session" ) ;
16
17
const jwt = require ( "jsonwebtoken" ) ;
17
18
const url = require ( "url" ) ;
18
19
19
- var app = module . exports = express ( ) ;
20
+ var app = ( module . exports = express ( ) ) ;
20
21
21
22
// config
22
23
23
- app . set ( ' view engine' , ' ejs' ) ;
24
- app . set ( ' views' , path . join ( __dirname , ' views' ) ) ;
24
+ app . set ( " view engine" , " ejs" ) ;
25
+ app . set ( " views" , path . join ( __dirname , " views" ) ) ;
25
26
26
27
// middleware
27
28
28
- app . use ( express . urlencoded ( { extended : false } ) )
29
- app . use ( session ( {
30
- resave : false , // don't save session if unmodified
31
- saveUninitialized : false , // don't create session until something stored
32
- secret : 'shhhh, very secret'
33
- } ) ) ;
29
+ app . use ( express . urlencoded ( { extended : false } ) ) ;
30
+ app . use (
31
+ session ( {
32
+ resave : false , // don't save session if unmodified
33
+ saveUninitialized : false , // don't create session until something stored
34
+ secret : "shhhh, very secret" ,
35
+ } )
36
+ ) ;
34
37
35
38
// Session-persisted message middleware
36
39
37
- app . use ( function ( req , res , next ) {
40
+ app . use ( function ( req , res , next ) {
38
41
var err = req . session . error ;
39
42
var msg = req . session . success ;
40
43
delete req . session . error ;
41
44
delete req . session . success ;
42
- res . locals . message = '' ;
43
- if ( err ) res . locals . message = '<p class="msg error">' + err + ' </p>' ;
44
- if ( msg ) res . locals . message = '<p class="msg success">' + msg + ' </p>' ;
45
+ res . locals . message = "" ;
46
+ if ( err ) res . locals . message = '<p class="msg error">' + err + " </p>" ;
47
+ if ( msg ) res . locals . message = '<p class="msg success">' + msg + " </p>" ;
45
48
next ( ) ;
46
49
} ) ;
47
50
48
51
// dummy database
49
52
50
53
var users = [
51
- { firstName :
'Rene' , lastName :
'Mueller' , email :
'[email protected] ' , accountId :
28 , accountName :
'Customer-Acme' } ,
52
- { firstName :
'Cecilia' , lastName :
'Stark' , email :
'[email protected] ' , accountId :
132 , accountName :
'Customer-Fake' }
54
+ {
55
+ firstName : "Rene" ,
56
+ lastName : "Mueller" ,
57
+
58
+ accountId : 28 ,
59
+ accountName : "Customer-Acme" ,
60
+ } ,
61
+ {
62
+ firstName : "Cecilia" ,
63
+ lastName : "Stark" ,
64
+
65
+ accountId : 132 ,
66
+ accountName : "Customer-Fake" ,
67
+ } ,
53
68
] ;
54
69
55
70
// when you create a user, generate a salt
56
71
// and hash the password ('foobar' is the pass here)
57
72
58
- hash ( { password : ' foobar' } , function ( err , pass , salt , hash ) {
73
+ hash ( { password : " foobar" } , function ( err , pass , salt , hash ) {
59
74
if ( err ) throw err ;
60
75
// store the salt & hash in the "db"
61
76
users . forEach ( ( element ) => {
62
- element . salt = salt ;
63
- element . hash = hash ;
64
- } ) ;
77
+ element . salt = salt ;
78
+ element . hash = hash ;
79
+ } ) ;
65
80
} ) ;
66
81
67
82
function findUserbyEmail ( email ) {
68
- var u = users . find ( u => u . email === email ) ;
83
+ var u = users . find ( ( u ) => u . email === email ) ;
69
84
return u ;
70
85
}
71
86
72
87
// Authenticate using our plain-object database of doom!
73
88
74
89
function authenticate ( email , pass , fn ) {
75
- if ( ! module . parent ) console . log ( ' authenticating %s:%s' , email , pass ) ;
90
+ if ( ! module . parent ) console . log ( " authenticating %s:%s" , email , pass ) ;
76
91
var user = findUserbyEmail ( email ) ;
77
92
// query the db for the given email
78
- if ( ! user ) return fn ( null , null )
93
+ if ( ! user ) return fn ( null , null ) ;
79
94
// apply the same algorithm to the POSTed password, applying
80
95
// the hash against the pass / salt, if there is a match we
81
96
// found the user
82
97
hash ( { password : pass , salt : user . salt } , function ( err , pass , salt , hash ) {
83
98
if ( err ) return fn ( err ) ;
84
- if ( hash === user . hash ) return fn ( null , user )
85
- fn ( null , null )
99
+ if ( hash === user . hash ) return fn ( null , user ) ;
100
+ fn ( null , null ) ;
86
101
} ) ;
87
102
}
88
103
@@ -91,12 +106,12 @@ function restrict(req, res, next) {
91
106
next ( ) ;
92
107
} else {
93
108
req . session . returnTo = req . originalUrl ;
94
- req . session . error = ' Access denied!' ;
95
- res . redirect ( ' /login' ) ;
109
+ req . session . error = " Access denied!" ;
110
+ res . redirect ( " /login" ) ;
96
111
}
97
112
}
98
113
99
- const signUserToken = user =>
114
+ const signUserToken = ( user ) =>
100
115
jwt . sign (
101
116
{
102
117
email : user . email ,
@@ -109,52 +124,59 @@ const signUserToken = user =>
109
124
METABASE_JWT_SHARED_SECRET
110
125
) ;
111
126
112
- app . get ( '/' , function ( req , res ) {
113
- res . redirect ( ' /analytics' ) ;
127
+ app . get ( "/" , function ( req , res ) {
128
+ res . redirect ( " /analytics" ) ;
114
129
} ) ;
115
130
116
- app . get ( ' /analytics' , restrict , function ( req , res ) {
117
- // replace ID "1" with the ID number in the path of your dashboard in Metabase.
118
- const METABASE_DASHBOARD_PATH = "/dashboard/1"
131
+ app . get ( " /analytics" , restrict , function ( req , res ) {
132
+ // replace ID "1" with the ID number in the path of your dashboard in Metabase.
133
+ const METABASE_DASHBOARD_PATH = "/dashboard/1" ;
119
134
var iframeUrl = `/sso/metabase?return_to=${ METABASE_DASHBOARD_PATH } ` ;
120
- res . send ( `<iframe src="${ iframeUrl } " frameborder="0" width="1280" height="1000" allowtransparency></iframe>` ) ;
135
+ res . send (
136
+ `<iframe src="${ iframeUrl } " frameborder="0" width="1280" height="1000" allowtransparency></iframe>`
137
+ ) ;
121
138
} ) ;
122
139
123
- app . get ( ' /logout' , function ( req , res ) {
140
+ app . get ( " /logout" , function ( req , res ) {
124
141
// destroy the user's session to log them out
125
142
// will be re-created next request
126
- req . session . destroy ( function ( ) {
127
- res . redirect ( '/' ) ;
143
+ req . session . destroy ( function ( ) {
144
+ res . redirect ( "/" ) ;
128
145
} ) ;
129
146
} ) ;
130
147
131
- app . get ( ' /login' , function ( req , res ) {
132
- res . render ( ' login' ) ;
148
+ app . get ( " /login" , function ( req , res ) {
149
+ res . render ( " login" ) ;
133
150
} ) ;
134
151
135
- app . post ( ' /login' , function ( req , res , next ) {
136
- authenticate ( req . body . email , req . body . password , function ( err , user ) {
137
- if ( err ) return next ( err )
152
+ app . post ( " /login" , function ( req , res , next ) {
153
+ authenticate ( req . body . email , req . body . password , function ( err , user ) {
154
+ if ( err ) return next ( err ) ;
138
155
if ( user ) {
139
156
// Regenerate session when signing in
140
157
// to prevent fixation
141
158
var returnTo = req . session . returnTo ;
142
- req . session . regenerate ( function ( ) {
159
+ req . session . regenerate ( function ( ) {
143
160
// Store the user's primary key
144
161
// in the session store to be retrieved,
145
162
// or in this case the entire user object
146
163
req . session . user = user ;
147
- req . session . success = 'Authenticated as ' + user . firstName + '' + user . lastName
148
- + ' click to <a href="/logout">logout</a>. '
149
- + ' click to access <a href="/analytics">analytics</a>' ;
150
- res . redirect ( returnTo || '/' ) ;
164
+ req . session . success =
165
+ "Authenticated as " +
166
+ user . firstName +
167
+ "" +
168
+ user . lastName +
169
+ ' click to <a href="/logout">logout</a>. ' +
170
+ ' click to access <a href="/analytics">analytics</a>' ;
171
+ res . redirect ( returnTo || "/" ) ;
151
172
delete req . session . returnTo ;
152
173
} ) ;
153
174
} else {
154
- req . session . error = 'Authentication failed, please check your '
155
- + ' email and password.'
156
- + ' (use "[email protected] " or "[email protected] " and password "foobar")' ;
157
- res . redirect ( '/login' ) ;
175
+ req . session . error =
176
+ "Authentication failed, please check your " +
177
+ " email and password." +
178
+ ' (use "[email protected] " or "[email protected] " and password "foobar")' ;
179
+ res . redirect ( "/login" ) ;
158
180
}
159
181
} ) ;
160
182
} ) ;
@@ -165,8 +187,8 @@ app.get("/sso/metabase", restrict, (req, res) => {
165
187
pathname : `${ METABASE_SITE_URL } /auth/sso` ,
166
188
query : {
167
189
jwt : signUserToken ( req . session . user ) ,
168
- return_to : `${ req . query . return_to || '/' } ?${ mods } `
169
- }
190
+ return_to : `${ req . query . return_to || "/" } ?${ mods } ` ,
191
+ } ,
170
192
} )
171
193
) ;
172
194
} ) ;
0 commit comments