|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('./../debug').authentication |
| 4 | + |
| 5 | +const AuthRequest = require('./auth-request') |
| 6 | + |
| 7 | +const url = require('url') |
| 8 | +const intoStream = require('into-stream') |
| 9 | + |
| 10 | +const $rdf = require('rdflib') |
| 11 | +const ACL = $rdf.Namespace('http://www.w3.org/ns/auth/acl#') |
| 12 | + |
| 13 | +/** |
| 14 | + * Models a local Login request |
| 15 | + */ |
| 16 | +class ConsentRequest extends AuthRequest { |
| 17 | + /** |
| 18 | + * @constructor |
| 19 | + * @param options {Object} |
| 20 | + * |
| 21 | + * @param [options.response] {ServerResponse} middleware `res` object |
| 22 | + * @param [options.session] {Session} req.session |
| 23 | + * @param [options.userStore] {UserStore} |
| 24 | + * @param [options.accountManager] {AccountManager} |
| 25 | + * @param [options.returnToUrl] {string} |
| 26 | + * @param [options.authQueryParams] {Object} Key/value hashmap of parsed query |
| 27 | + * parameters that will be passed through to the /authorize endpoint. |
| 28 | + * @param [options.authenticator] {Authenticator} Auth strategy by which to |
| 29 | + * log in |
| 30 | + */ |
| 31 | + constructor (options) { |
| 32 | + super(options) |
| 33 | + |
| 34 | + this.authenticator = options.authenticator |
| 35 | + this.authMethod = options.authMethod |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Factory method, returns an initialized instance of LoginRequest |
| 40 | + * from an incoming http request. |
| 41 | + * |
| 42 | + * @param req {IncomingRequest} |
| 43 | + * @param res {ServerResponse} |
| 44 | + * @param authMethod {string} |
| 45 | + * |
| 46 | + * @return {LoginRequest} |
| 47 | + */ |
| 48 | + static fromParams (req, res) { |
| 49 | + let options = AuthRequest.requestOptions(req, res) |
| 50 | + |
| 51 | + return new ConsentRequest(options) |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Handles a Login GET request on behalf of a middleware handler, displays |
| 56 | + * the Login page. |
| 57 | + * Usage: |
| 58 | + * |
| 59 | + * ``` |
| 60 | + * app.get('/login', LoginRequest.get) |
| 61 | + * ``` |
| 62 | + * |
| 63 | + * @param req {IncomingRequest} |
| 64 | + * @param res {ServerResponse} |
| 65 | + */ |
| 66 | + static async get (req, res) { |
| 67 | + const request = ConsentRequest.fromParams(req, res) |
| 68 | + |
| 69 | + const appOrigin = request.getAppOrigin() |
| 70 | + // Check if is already registered or is data browser |
| 71 | + if (request.isUserLoggedIn()) { |
| 72 | + if ( |
| 73 | + appOrigin === req.app.locals.ldp.serverUri || |
| 74 | + await request.isAppRegistered(req.app.locals.ldp, appOrigin, request.session.subject._id) |
| 75 | + ) { |
| 76 | + request.setUserConsent(appOrigin) |
| 77 | + request.redirectPostConsent() |
| 78 | + } else { |
| 79 | + request.renderForm(null, req) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Performs the login operation -- loads and validates the |
| 86 | + * appropriate user, inits the session with credentials, and redirects the |
| 87 | + * user to continue their auth flow. |
| 88 | + * |
| 89 | + * @param request {LoginRequest} |
| 90 | + * |
| 91 | + * @return {Promise} |
| 92 | + */ |
| 93 | + static async giveConsent (req, res) { |
| 94 | + let accessModes = [] |
| 95 | + let consented = false |
| 96 | + if (req.body) { |
| 97 | + accessModes = req.body.access_mode |
| 98 | + consented = req.body.consent |
| 99 | + } |
| 100 | + |
| 101 | + let request = ConsentRequest.fromParams(req, res) |
| 102 | + |
| 103 | + if (request.isUserLoggedIn()) { |
| 104 | + const appOrigin = request.getAppOrigin() |
| 105 | + debug('Providing consent for app sharing') |
| 106 | + |
| 107 | + if (consented) { |
| 108 | + await request.registerApp(req.app.locals.ldp, appOrigin, accessModes, request.session.subject._id) |
| 109 | + request.setUserConsent(appOrigin) |
| 110 | + } |
| 111 | + |
| 112 | + // Redirect once that's all done |
| 113 | + request.redirectPostConsent() |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + setUserConsent (appOrigin) { |
| 118 | + if (!this.session.consentedOrigins) { |
| 119 | + this.session.consentedOrigins = [] |
| 120 | + } |
| 121 | + if (!this.session.consentedOrigins.includes(appOrigin)) { |
| 122 | + this.session.consentedOrigins.push(appOrigin) |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + isUserLoggedIn () { |
| 127 | + // Ensure the user arrived here by logging in |
| 128 | + if (!this.session.subject || !this.session.subject._id) { |
| 129 | + this.response.status(401) |
| 130 | + this.response.send('User not logged in 2') |
| 131 | + return false |
| 132 | + } |
| 133 | + return true |
| 134 | + } |
| 135 | + |
| 136 | + getAppOrigin () { |
| 137 | + const parsed = url.parse(this.authQueryParams.redirect_uri) |
| 138 | + return `${parsed.protocol}//${parsed.host}` |
| 139 | + } |
| 140 | + |
| 141 | + async getProfileGraph (ldp, webId) { |
| 142 | + return await new Promise(async (resolve, reject) => { |
| 143 | + const store = $rdf.graph() |
| 144 | + const profileText = await ldp.readResource(webId) |
| 145 | + $rdf.parse(profileText.toString(), store, 'https://localhost:8443/profile/card', 'text/turtle', (error, kb) => { |
| 146 | + if (error) { |
| 147 | + reject(error) |
| 148 | + } else { |
| 149 | + resolve(kb) |
| 150 | + } |
| 151 | + }) |
| 152 | + }) |
| 153 | + } |
| 154 | + |
| 155 | + async saveProfileGraph (ldp, store, webId) { |
| 156 | + const text = $rdf.serialize(undefined, store, webId, 'text/turtle') |
| 157 | + await ldp.put(webId, intoStream(text), 'text/turtle') |
| 158 | + } |
| 159 | + |
| 160 | + async isAppRegistered (ldp, appOrigin, webId) { |
| 161 | + const store = await this.getProfileGraph(ldp, webId) |
| 162 | + return store.each($rdf.sym(webId), ACL('trustedApp')).find((app) => { |
| 163 | + return store.each(app, ACL('origin')).find(rdfAppOrigin => rdfAppOrigin.value === appOrigin) |
| 164 | + }) |
| 165 | + } |
| 166 | + |
| 167 | + async registerApp (ldp, appOrigin, accessModes, webId) { |
| 168 | + const store = await this.getProfileGraph(ldp, webId) |
| 169 | + const origin = $rdf.sym(appOrigin) |
| 170 | + // remove existing statements on same origin - if it exists |
| 171 | + store.statementsMatching(null, ACL('origin'), origin).forEach(st => { |
| 172 | + store.removeStatements([...store.statementsMatching(null, ACL('trustedApp'), st.subject)]) |
| 173 | + store.removeStatements([...store.statementsMatching(st.subject)]) |
| 174 | + }) |
| 175 | + |
| 176 | + // add new triples |
| 177 | + const application = new $rdf.BlankNode() |
| 178 | + store.add($rdf.sym(webId), ACL('trustedApp'), application, webId) |
| 179 | + store.add(application, ACL('origin'), origin, webId) |
| 180 | + accessModes.forEach(mode => { |
| 181 | + store.add(application, ACL('mode'), ACL(mode)) |
| 182 | + }) |
| 183 | + await this.saveProfileGraph(ldp, store, webId) |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Returns a URL to redirect the user to after login. |
| 188 | + * Either uses the provided `redirect_uri` auth query param, or simply |
| 189 | + * returns the user profile URI if none was provided. |
| 190 | + * |
| 191 | + * @param validUser {UserAccount} |
| 192 | + * |
| 193 | + * @return {string} |
| 194 | + */ |
| 195 | + postConsentUrl () { |
| 196 | + return this.authorizeUrl() |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Redirects the Login request to continue on the OIDC auth workflow. |
| 201 | + */ |
| 202 | + redirectPostConsent () { |
| 203 | + let uri = this.postConsentUrl() |
| 204 | + debug('Login successful, redirecting to ', uri) |
| 205 | + this.response.redirect(uri) |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Renders the login form |
| 210 | + */ |
| 211 | + renderForm (error, req) { |
| 212 | + let queryString = req && req.url && req.url.replace(/[^?]+\?/, '') || '' |
| 213 | + let params = Object.assign({}, this.authQueryParams, |
| 214 | + { |
| 215 | + registerUrl: this.registerUrl(), |
| 216 | + returnToUrl: this.returnToUrl, |
| 217 | + enablePassword: this.localAuth.password, |
| 218 | + enableTls: this.localAuth.tls, |
| 219 | + tlsUrl: `/login/tls?${encodeURIComponent(queryString)}` |
| 220 | + }) |
| 221 | + |
| 222 | + if (error) { |
| 223 | + params.error = error.message |
| 224 | + this.response.status(error.statusCode) |
| 225 | + } |
| 226 | + |
| 227 | + this.response.render('auth/consent', params) |
| 228 | + } |
| 229 | +} |
| 230 | + |
| 231 | +module.exports = { |
| 232 | + ConsentRequest |
| 233 | +} |
0 commit comments