Created
June 16, 2020 16:57
-
-
Save MikaelSmith/ac1e94bc8e5d97c78dbf0e90ad749335 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Express = require('express'); | |
const Session = require('express-session'); | |
const BodyParser = require('body-parser'); | |
const Keycloak = require('keycloak-connect'); | |
const Cors = require('cors'); | |
const hostname = '127.0.0.1'; | |
const port = 3000; | |
var app = Express(); | |
app.use(BodyParser.json()); | |
// Enable CORS support | |
app.use(Cors()); | |
// Create a session-store to be used by both the express-session | |
// middleware and the keycloak middleware. | |
var memoryStore = new Session.MemoryStore(); | |
app.use(Session({ | |
secret: 'some secret', | |
resave: false, | |
saveUninitialized: true, | |
store: memoryStore | |
})); | |
// Provide the session store to the Keycloak so that sessions | |
// can be invalidated from the Keycloak console callback. | |
// | |
// Additional configuration is read from keycloak.json file | |
// installed from the Keycloak web console. | |
var keycloak = new Keycloak({ | |
store: memoryStore | |
}); | |
app.use(keycloak.middleware({ | |
logout: '/logout', | |
admin: '/' | |
})); | |
app.get('/service/sso', keycloak.checkSso(), function (req, res) { | |
res.json({message: 'sso'}); | |
}); | |
app.get('/service/public', function (req, res) { | |
res.json({message: 'public'}); | |
}); | |
app.get('/service/secured', keycloak.protect('realm:user'), function (req, res) { | |
res.json({message: 'secured'}); | |
}); | |
app.get('/service/admin', keycloak.protect('realm:admin'), function (req, res) { | |
res.json({message: 'admin'}); | |
}); | |
app.use('*', function (req, res) { | |
res.send('Not found!'); | |
}); | |
app.listen(port, hostname, function () { | |
console.log(`Server running at http://${hostname}:${port}/`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment