1
- const express = require ( ' express' ) ;
1
+ const express = require ( " express" ) ;
2
2
const app = express ( ) ;
3
- const dotenv = require ( ' dotenv' ) . config ( { path : ' ../.env' } ) ;
4
- const cors = require ( ' cors' ) ;
3
+ const dotenv = require ( " dotenv" ) . config ( { path : " ../.env" } ) ;
4
+ const cors = require ( " cors" ) ;
5
5
const jwt = require ( "jsonwebtoken" ) ;
6
6
7
- app . get ( '/' , ( req , res ) => {
8
- res . send ( ' Hello from our server!' )
9
- } )
7
+ app . get ( "/" , ( req , res ) => {
8
+ res . send ( " Hello from our server!" ) ;
9
+ } ) ;
10
10
11
- const AUTH_PROVIDER_PORT = process . env . AUTH_PROVIDER_PORT
12
- const METABASE_INSTANCE_URL = process . env . METABASE_INSTANCE_URL
13
- const METABASE_JWT_SHARED_SECRET = process . env . METABASE_JWT_SHARED_SECRET
11
+ const AUTH_PROVIDER_PORT = process . env . AUTH_PROVIDER_PORT ;
12
+ const METABASE_INSTANCE_URL = process . env . METABASE_INSTANCE_URL ;
13
+ const METABASE_JWT_SHARED_SECRET = process . env . METABASE_JWT_SHARED_SECRET ;
14
14
15
- app . use ( cors ( { credentials : true , origin :true } ) ) ; //https://stackoverflow.com/a/66437447
15
+ app . use ( cors ( { credentials : true , origin : true } ) ) ; //https://stackoverflow.com/a/66437447
16
16
17
17
app . get ( "/sso/metabase" , async ( req , res ) => {
18
-
19
18
// Usually, you would grab the user from the current session
20
19
// Here it is hardcoded for demonstration purposes
21
20
// Example:
@@ -24,15 +23,15 @@ app.get("/sso/metabase", async (req, res) => {
24
23
email : "rene@example.com" ,
25
24
firstName : "Rene" ,
26
25
lastName : "Descartes" ,
27
- group : "Customer"
28
- }
26
+ group : "Customer" ,
27
+ } ;
29
28
30
29
if ( ! user ) {
31
30
console . log ( "no user" ) ;
32
31
return res . status ( 401 ) . json ( {
33
- status : ' error' ,
34
- message : ' not authenticated' ,
35
- } )
32
+ status : " error" ,
33
+ message : " not authenticated" ,
34
+ } ) ;
36
35
}
37
36
38
37
const token = jwt . sign (
@@ -44,28 +43,36 @@ app.get("/sso/metabase", async (req, res) => {
44
43
exp : Math . round ( Date . now ( ) / 1000 ) + 60 * 10 , // 10 minutes expiration
45
44
} ,
46
45
// This is the JWT signing secret in your Metabase JWT authentication setting
47
- METABASE_JWT_SHARED_SECRET
48
- )
49
- const ssoUrl = `${ METABASE_INSTANCE_URL } /auth/sso?token=true&jwt=${ token } `
50
- console . log ( 'Hitting MB SSO endpoint' , ssoUrl ) ;
46
+ METABASE_JWT_SHARED_SECRET ,
47
+ ) ;
48
+
49
+ if ( req . query . response === "json" ) {
50
+ return res
51
+ . status ( 200 )
52
+ . set ( "Content-Type" , "application/json" )
53
+ . end ( { jwt : token } ) ;
54
+ }
55
+
56
+ const ssoUrl = `${ METABASE_INSTANCE_URL } /auth/sso?token=true&jwt=${ token } ` ;
57
+ console . log ( "Hitting MB SSO endpoint" , ssoUrl ) ;
51
58
52
59
try {
53
- const response = await fetch ( ssoUrl , { method : ' GET' } )
54
- const session = await response . text ( )
60
+ const response = await fetch ( ssoUrl , { method : " GET" } ) ;
61
+ const session = await response . text ( ) ;
55
62
56
- console . log ( "Received session" , session )
57
- return res . status ( 200 ) . set ( "Content-Type" , "application/json" ) . end ( session )
63
+ console . log ( "Received session" , session ) ;
64
+ return res . status ( 200 ) . set ( "Content-Type" , "application/json" ) . end ( session ) ;
58
65
} catch ( error ) {
59
66
if ( error instanceof Error ) {
60
67
res . status ( 401 ) . json ( {
61
- status : ' error' ,
62
- message : ' authentication failed' ,
68
+ status : " error" ,
69
+ message : " authentication failed" ,
63
70
error : error . message ,
64
- } )
71
+ } ) ;
65
72
}
66
73
}
67
- } )
74
+ } ) ;
68
75
69
76
app . listen ( AUTH_PROVIDER_PORT , ( ) => {
70
- console . log ( `server listening on port ${ AUTH_PROVIDER_PORT } ` )
71
- } )
77
+ console . log ( `server listening on port ${ AUTH_PROVIDER_PORT } ` ) ;
78
+ } ) ;
0 commit comments