1- const express = require ( ' express' ) ;
1+ const express = require ( " express" ) ;
22const 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" ) ;
55const jwt = require ( "jsonwebtoken" ) ;
66
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+ } ) ;
1010
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 ;
1414
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
1616
1717app . get ( "/sso/metabase" , async ( req , res ) => {
18-
1918 // Usually, you would grab the user from the current session
2019 // Here it is hardcoded for demonstration purposes
2120 // Example:
@@ -24,15 +23,15 @@ app.get("/sso/metabase", async (req, res) => {
24232524 firstName : "Rene" ,
2625 lastName : "Descartes" ,
27- group : "Customer"
28- }
26+ group : "Customer" ,
27+ } ;
2928
3029 if ( ! user ) {
3130 console . log ( "no user" ) ;
3231 return res . status ( 401 ) . json ( {
33- status : ' error' ,
34- message : ' not authenticated' ,
35- } )
32+ status : " error" ,
33+ message : " not authenticated" ,
34+ } ) ;
3635 }
3736
3837 const token = jwt . sign (
@@ -44,28 +43,36 @@ app.get("/sso/metabase", async (req, res) => {
4443 exp : Math . round ( Date . now ( ) / 1000 ) + 60 * 10 , // 10 minutes expiration
4544 } ,
4645 // 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+ . send ( { jwt : token } ) ;
54+ }
55+
56+ const ssoUrl = `${ METABASE_INSTANCE_URL } /auth/sso?token=true&jwt=${ token } ` ;
57+ console . log ( "Hitting MB SSO endpoint" , ssoUrl ) ;
5158
5259 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 ( ) ;
5562
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 ) ;
5865 } catch ( error ) {
5966 if ( error instanceof Error ) {
6067 res . status ( 401 ) . json ( {
61- status : ' error' ,
62- message : ' authentication failed' ,
68+ status : " error" ,
69+ message : " authentication failed" ,
6370 error : error . message ,
64- } )
71+ } ) ;
6572 }
6673 }
67- } )
74+ } ) ;
6875
6976app . 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