Skip to content

Commit febf6ab

Browse files
Implemented Metabase SSO w/ JWT
1 parent 102e9fb commit febf6ab

File tree

139 files changed

+10071
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

139 files changed

+10071
-3
lines changed

index.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
'use strict'
22

3+
const METABASE_SITE_URL = process.env.METABASE_SITE_URL || "http://localhost:3000";
4+
const METABASE_JWT_SHARED_SECRET =
5+
process.env.METABASE_JWT_SHARED_SECRET ||
6+
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff";
7+
38
/**
49
* Module dependencies.
510
*/
@@ -8,6 +13,8 @@ const express = require('express')
813
const hash = require('pbkdf2-password')()
914
const path = require('path');
1015
const session = require('express-session');
16+
const jwt = require("jsonwebtoken");
17+
const url = require("url");
1118

1219
var app = module.exports = express();
1320

@@ -89,8 +96,19 @@ function restrict(req, res, next) {
8996
}
9097
}
9198

99+
const signUserToken = user =>
100+
jwt.sign(
101+
{
102+
email: user.email,
103+
first_name: user.firstName,
104+
last_name: user.lastName,
105+
exp: Math.round(Date.now() / 1000) + 60 * 10, // 10 minute expiration
106+
},
107+
METABASE_JWT_SHARED_SECRET
108+
);
109+
92110
app.get('/', function(req, res){
93-
res.redirect('/login');
111+
res.redirect('/restricted');
94112
});
95113

96114
app.get('/restricted', restrict, function(req, res){
@@ -123,8 +141,7 @@ app.post('/login', function (req, res, next) {
123141
req.session.user = user;
124142
req.session.success = 'Authenticated as ' + user.firstName + '' + user.lastName
125143
+ ' click to <a href="/logout">logout</a>. '
126-
+ ' You may now access <a href="/restricted">/restricted</a>.';
127-
//res.redirect('back');
144+
+ ' Redirecting to ' + returnTo + '.';
128145
res.redirect(returnTo || '/restricted');
129146
delete req.session.returnTo;
130147
});
@@ -137,6 +154,18 @@ app.post('/login', function (req, res, next) {
137154
});
138155
});
139156

157+
app.get("/sso/metabase", restrict, (req, res) => {
158+
res.redirect(
159+
url.format({
160+
pathname: `${METABASE_SITE_URL}/auth/sso`,
161+
query: {
162+
jwt: signUserToken(req.session.user),
163+
return_to: `${req.query.return_to || '/'}`
164+
}
165+
})
166+
);
167+
});
168+
140169
/* istanbul ignore next */
141170
if (!module.parent) {
142171
app.listen(3000);

node_modules/.bin/semver

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.package-lock.json

Lines changed: 123 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/buffer-equal-constant-time/.npmignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/buffer-equal-constant-time/.travis.yml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/buffer-equal-constant-time/LICENSE.txt

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/buffer-equal-constant-time/README.md

Lines changed: 50 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/buffer-equal-constant-time/index.js

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/buffer-equal-constant-time/package.json

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)