-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
55 lines (44 loc) · 1.41 KB
/
Copy pathindex.js
File metadata and controls
55 lines (44 loc) · 1.41 KB
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
'use strict'
const express = require('express')
const Keycloak = require('keycloak-connect')
const join = require('path').join
// You can get this from the "Installation" tab of your Realm (app) in Keycloak
const keycloakConf = {
"realm": "TestApp",
"bearer-only": true,
"auth-server-url": "http://localhost:8080/auth",
"ssl-required": "external",
"resource": "Mobile Backend",
"confidential-port": 0
}
const kc = new Keycloak({
// Session store etc. from keycloak samples is not necessary...
}, keycloakConf)
// Our express web application
const app = express()
// Requests that aren't matched by express.static will pass through this
// This function will perfrom some keycloak setup on the request object
app.use(kc.middleware({
logout: '/logout',
admin: '/'
}))
app.use((req, res, next) => {
console.log(`[${req.originalUrl}] authorisation header ${req.headers.authorization}`)
next()
})
// This endpoint can be accessed without a bearer token
app.get('/ping-unprotected', (req, res, next) => {
res.end('pong-unprotected')
})
app.get('/ping-protected', kc.protect(), (req, res, next) => {
res.end('pong-protected')
})
// Serves static assets from www directory if matches are found
app.use(express.static(join(__dirname, 'www')))
app.listen(3030, (err) => {
if (err) {
console.log('failed to start server')
throw err
}
console.log('test application started on http://localhost:3030')
})