Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 1x 1x 12x 12x 12x 12x 12x 12x 3x 1x 1x 1x 2x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 6x 2x 4x 2x 4x 1x 1x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | 'use strict'
const validUrl = require('valid-url')
const { preferredProviderFor } = require('../preferred-provider')
class SelectProviderRequest {
/**
* @constructor
*
* @param options {Object}
* @param [options.webId] {string}
* @param [options.oidcManager] {OidcManager}
* @param [options.response] {HttpResponse}
* @param [options.serverUri] {string}
* @param [options.returnToUrl] {string} Url of the original resource
* a client was trying to access before being redirected to select provider
*/
constructor (options) {
this.webId = options.webId
this.oidcManager = options.oidcManager
this.response = options.response
this.session = options.session
this.serverUri = options.serverUri
this.returnToUrl = options.returnToUrl
}
/**
* Validates the request and throws an error if invalid.
*
* @throws {Error} HTTP 400 if required parameters are missing
*/
validate () {
if (!this.webId) {
const error = new Error('No webid is given for Provider Discovery')
error.statusCode = 400
throw error
}
if (!validUrl.isUri(this.webId)) {
const error = new Error('Invalid webid given for Provider Discovery')
error.statusCode = 400
throw error
}
Eif (!this.oidcManager) {
const error = new Error('OIDC multi-rp client not initialized')
error.statusCode = 500
throw error
}
}
/**
* Factory method, creates and returns an initialized and validated instance
* of SelectProviderRequest from a submitted POST form.
*
* @param req {IncomingRequest}
* @param [req.body.webid] {string}
*
* @param res {ServerResponse}
* @return {SelectProviderRequest}
*/
static fromParams (req, res) {
const body = req.body || {}
const query = req.query || {}
const webId = SelectProviderRequest.normalizeUri(body.webid)
let oidcManager, serverUri
Eif (req.app && req.app.locals) {
const locals = req.app.locals
oidcManager = locals.oidc
serverUri = locals.host.serverUri
}
const options = {
webId,
oidcManager,
serverUri,
returnToUrl: query.returnToUrl,
response: res,
session: req.session
}
const request = new SelectProviderRequest(options)
return request
}
/**
* Attempts to return a normalized URI by prepending `https://` to a given
* value, if a protocol is missing.
*
* @param uri {string}
*
* @return {string}
*/
static normalizeUri (uri) {
if (!uri) {
return uri
}
if (!uri.startsWith('http')) {
uri = 'https://' + uri
}
return uri
}
/**
* Handles the Select Provider POST request. Usage:
*
* ```
* app.post('/api/auth/select-provider', bodyParser, SelectProviderRequest.post })
* ```
*
* @param req {IncomingRequest}
* @param res {ServerResponse}
*
* @throws {Error}
*
* @return {Promise}
*/
static post (req, res) {
const request = SelectProviderRequest.fromParams(req, res)
return SelectProviderRequest.handlePost(request)
}
static handlePost (request) {
return Promise.resolve()
.then(() => request.validate())
.then(() => request.saveReturnToUrl())
.then(() => request.selectProvider())
.catch(err => request.error(err))
}
/**
* Handles a Select Provider GET request on behalf of a middleware handler. Usage:
*
* ```
* app.get('/api/auth/select-provider', SelectProviderRequest.get)
* ```
*
* @param req {IncomingRequest}
* @param res {ServerResponse}
*/
static get (req, res) {
const request = SelectProviderRequest.fromParams(req, res)
request.renderView()
}
/**
* Performs provider discovery by determining a user's preferred provider uri,
* constructing an authentication url for that provider, and redirecting the
* user to it.
*
* @throws {Error}
*
* @return {Promise}
*/
selectProvider () {
return this.preferredProviderUrl()
.then(providerUrl => {
this.oidcManager.debug('Building /authorize url for provider:', providerUrl)
return this.authUrlFor(providerUrl)
})
.then(providerAuthUrl => this.response.redirect(providerAuthUrl))
}
/**
* Saves `returnToUrl` param for later use in AuthCallbackRequest handler,
* to redirect the client to the original resource they were trying to access
* before entering the authn workflow.
*/
saveReturnToUrl () {
this.session.returnToUrl = this.returnToUrl
}
/**
* @throws {Error}
*
* @returns {Promise<string>} Resolves to the preferred OIDC provider for
* the url the user entered
*/
preferredProviderUrl () {
this.oidcManager.debug('Discovering provider for uri:', this.webId)
return preferredProviderFor(this.webId)
}
/**
* Constructs the OIDC authorization URL for a given provider.
*
* @param providerUri {string} Identity provider URI
*
* @return {Promise<string>}
*/
authUrlFor (providerUri) {
const multiRpClient = this.oidcManager.clients
return multiRpClient.authUrlForIssuer(providerUri, this.session)
}
error (error) {
const res = this.response
res.status(error.statusCode || 400)
res.render('auth/select-provider', { error: error.message })
}
renderView () {
const res = this.response
res.render('auth/select-provider', { serverUri: this.serverUri })
}
}
module.exports = SelectProviderRequest
|