|
| 1 | +'use strict' |
| 2 | +/** |
| 3 | + * OIDC Relying Party API handler module. |
| 4 | + */ |
| 5 | + |
| 6 | +const express = require('express') |
| 7 | +const debug = require('../../debug') |
| 8 | +const util = require('../../utils') |
| 9 | +const error = require('../../http-error') |
| 10 | +const bodyParser = require('body-parser').urlencoded({ extended: false }) |
| 11 | + |
| 12 | +const DiscoverProviderRequest = require('../../requests/discover-provider-request') |
| 13 | +const { LoginByPasswordRequest } = require('../../requests/login-request') |
| 14 | + |
| 15 | +/** |
| 16 | + * Returns a router with OIDC Relying Party and Identity Provider middleware: |
| 17 | + * |
| 18 | + * 1. Adds a Relying Party (RP) callback handler on '/api/oidc/rp/:issuer_id' |
| 19 | + * 2. Sets up a static content handler for signin/signup apps |
| 20 | + * 3. Adds a set of Identity Provider (OP) endpoints on '/' |
| 21 | + * |
| 22 | + * Usage (in create-app.js): |
| 23 | + * |
| 24 | + * ``` |
| 25 | + * app.use('/', oidcHandler.api(oidc)) |
| 26 | + * ``` |
| 27 | + * @method middleware |
| 28 | + * |
| 29 | + * @param oidc {OidcManager} |
| 30 | + * |
| 31 | + * @return {Router} Express router |
| 32 | + */ |
| 33 | +function middleware (oidc) { |
| 34 | + const router = express.Router('/') |
| 35 | + |
| 36 | + // User-facing Authentication API |
| 37 | + router.get('/api/auth/discover', (req, res) => { |
| 38 | + res.sendFile('discover-provider.html', { root: './static/oidc/' }) |
| 39 | + }) |
| 40 | + router.post('/api/auth/discover', bodyParser, discoverProvider) |
| 41 | + |
| 42 | + router.post(['/login', '/signin'], bodyParser, login) |
| 43 | + |
| 44 | + router.post('/logout', logout) |
| 45 | + |
| 46 | + // The relying party callback is called at the end of the OIDC signin process |
| 47 | + router.get('/api/oidc/rp/:issuer_id', (req, res, next) => { |
| 48 | + // Exchange authorization code for id token |
| 49 | + authCodeFlowCallback(oidc, req) |
| 50 | + // Redirect the user back to returnToUrl |
| 51 | + .then(() => { resumeUserFlow(req, res) }) |
| 52 | + .catch(next) |
| 53 | + }) |
| 54 | + |
| 55 | + // Initialize the OIDC Identity Provider routes/api |
| 56 | + // router.get('/.well-known/openid-configuration', discover.bind(provider)) |
| 57 | + // router.get('/jwks', jwks.bind(provider)) |
| 58 | + // router.post('/register', register.bind(provider)) |
| 59 | + // router.get('/authorize', authorize.bind(provider)) |
| 60 | + // router.post('/authorize', authorize.bind(provider)) |
| 61 | + // router.post('/token', token.bind(provider)) |
| 62 | + // router.get('/userinfo', userinfo.bind(provider)) |
| 63 | + // router.get('/logout', logout.bind(provider)) |
| 64 | + let oidcProviderApi = require('oidc-op-express')(oidc.provider) |
| 65 | + router.use('/', oidcProviderApi) |
| 66 | + |
| 67 | + return router |
| 68 | +} |
| 69 | + |
| 70 | +function discoverProvider (req, res, next) { |
| 71 | + return DiscoverProviderRequest.handle(req, res) |
| 72 | + .catch(error => { |
| 73 | + error.status = error.status || 400 |
| 74 | + next(error) |
| 75 | + }) |
| 76 | +} |
| 77 | + |
| 78 | +function login (req, res, next) { |
| 79 | + return LoginByPasswordRequest.handle(req, res) |
| 80 | + .catch(error => { |
| 81 | + error.status = error.status || 400 |
| 82 | + next(error) |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +function logout (req, res, next) { |
| 87 | + req.session.userId = '' |
| 88 | + req.session.identified = false |
| 89 | + res.status(200).send() |
| 90 | +} |
| 91 | + |
| 92 | +function authCodeFlowCallback (oidc, req) { |
| 93 | + debug.oidc('in authCodeFlowCallback()') |
| 94 | + |
| 95 | + if (!req.params || !req.params.issuer_id) { |
| 96 | + return Promise.reject(error(400, 'Invalid auth response uri - missing issuer id')) |
| 97 | + } |
| 98 | + |
| 99 | + let issuer = getIssuerId(req) |
| 100 | + |
| 101 | + return oidc.clients.clientForIssuer(issuer) |
| 102 | + .then(client => { |
| 103 | + return validateResponse(client, req) |
| 104 | + }) |
| 105 | + .then(response => { |
| 106 | + initSessionUserAuth(response, req) |
| 107 | + }) |
| 108 | + .catch((err) => { |
| 109 | + debug.oidc(err) |
| 110 | + throw error(400, err) |
| 111 | + }) |
| 112 | +} |
| 113 | + |
| 114 | +function getIssuerId (req = {}) { |
| 115 | + return req.params && decodeURIComponent(req.params.issuer_id) |
| 116 | +} |
| 117 | + |
| 118 | +function validateResponse (client, req) { |
| 119 | + let url = util.fullUrlForReq(req) |
| 120 | + return client.validateResponse(url, req.session) |
| 121 | +} |
| 122 | + |
| 123 | +function initSessionUserAuth (authResponse, req) { |
| 124 | + let webId = extractWebId(authResponse) |
| 125 | + req.session.accessToken = authResponse.params.access_token |
| 126 | + req.session.refreshToken = authResponse.params.refresh_token |
| 127 | + req.session.userId = webId |
| 128 | + req.session.identified = true |
| 129 | +} |
| 130 | + |
| 131 | +function extractWebId (authResponse) { |
| 132 | + return authResponse.decoded.payload.sub |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Redirects the user back to their original requested resource, at the end |
| 137 | + * of the OIDC authentication process. |
| 138 | + * @method resumeUserFlow |
| 139 | + */ |
| 140 | +function resumeUserFlow (req, res) { |
| 141 | + debug.oidc('In resumeUserFlow handler:') |
| 142 | + |
| 143 | + if (req.session.returnToUrl) { |
| 144 | + let returnToUrl = req.session.returnToUrl |
| 145 | + // if (req.session.accessToken) { |
| 146 | + // returnToUrl += '?access_token=' + req.session.accessToken |
| 147 | + // } |
| 148 | + debug.oidc(' Redirecting to ' + returnToUrl) |
| 149 | + delete req.session.returnToUrl |
| 150 | + return res.redirect(302, returnToUrl) |
| 151 | + } |
| 152 | + res.send('Resume User Flow (failed)') |
| 153 | +} |
| 154 | + |
| 155 | +module.exports = { |
| 156 | + middleware, |
| 157 | + discoverProvider, |
| 158 | + login, |
| 159 | + logout, |
| 160 | + extractWebId, |
| 161 | + authCodeFlowCallback, |
| 162 | + getIssuerId, |
| 163 | + initSessionUserAuth, |
| 164 | + resumeUserFlow, |
| 165 | + validateResponse |
| 166 | +} |
0 commit comments