forked from hirsch88/auth0-mock-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
71 lines (60 loc) · 2.06 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
process.env.DEBUG = 'app*';
var express = require('express');
var app = express();
var jwt = require('jsonwebtoken');
var Debug = require('debug');
var path = require('path');
var cors = require('cors');
var bodyParser = require('body-parser');
var favicon = require('serve-favicon');
var debug = Debug('app');
// Configure our small auth0-mock-server
app.options('*', cors())
.use(cors())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.use(express.static(`${__dirname}/public`))
.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
// This route can be used to generate a valid jwt-token.
app.post('/token', function (req, res) {
if (!req.body.email || !req.body.password) {
debug('Body is invalid!');
return res.status(400).send('Email or password is missing!');
}
var token = jwt.sign({
user_id: 'auth0|' + req.body.email,
}, 'auth0-mock');
debug('Signed token for ' + req.body.email);
res.json({ token });
});
// This route can be used to generate a valid jwt-token.
app.get('/token/:email', function (req, res) {
if (!req.params.email) {
debug('No user was given!');
return res.status(400).send('user is missing');
}
var token = jwt.sign({
user_id: 'auth0|' + req.params.email,
}, 'auth0-mock');
debug('Signed token for ' + req.params.email);
res.json({ token });
});
// This route returns the inside of a jwt-token. Your main application
// should use this route to keep the auth0-flow
app.post('/tokeninfo', function (req, res) {
if (!req.body.id_token) {
debug('No token given in the body!');
return res.status(401).send('missing id_token');
}
var data = jwt.decode(req.body.id_token);
if (data) {
debug('Return token data from ' + data.user_id);
res.json(data);
} else {
debug('The token was invalid and could not be decoded!');
res.status(401).send('invalid id_token');
}
});
app.listen(3333, function () {
debug('Auth0-Mock-Server listening on port 3333!');
});