All files / src/handlers login-consent-request.js

94.33% Statements 50/53
90.47% Branches 19/21
89.47% Functions 17/19
94.33% Lines 50/53

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    1x       20x 20x 20x                   4x 4x 1x     3x   3x 1x 1x     2x                 15x   15x           15x       17x 17x 17x 17x 17x                 12x   12x 12x     12x             12x 8x 8x 8x       4x   4x 1x   3x     1x             13x       16x       12x         1x       10x 10x       7x       3x 3x 3x 3x 3x 3x   3x   3x       3x       1x  
'use strict'
 
const AuthResponseSent = require('../errors/auth-response-sent')
 
class LoginConsentRequest {
  constructor (options) {
    this.opAuthRequest = options.opAuthRequest
    this.params = options.params
    this.response = options.response
  }
 
  /**
   * @param opAuthRequest {OPAuthenticationRequest}
   * @param skipConsent {boolean}
   *
   * @return {Promise<OPAuthenticationRequest>}
   */
  static handle (opAuthRequest, skipConsent = false) {
    const notLoggedIn = !opAuthRequest.subject
    if (notLoggedIn) {
      return Promise.resolve(opAuthRequest) // pass through
    }
 
    const consentRequest = LoginConsentRequest.from(opAuthRequest)
 
    if (skipConsent) {
      consentRequest.markConsentSuccess(opAuthRequest)
      return Promise.resolve(opAuthRequest) // pass through
    }
 
    return LoginConsentRequest.obtainConsent(consentRequest)
  }
 
  /**
   * @param opAuthRequest {OPAuthenticationRequest}
   *
   * @return {LoginConsentRequest}
   */
  static from (opAuthRequest) {
    const params = LoginConsentRequest.extractParams(opAuthRequest)
 
    const options = {
      opAuthRequest,
      params,
      response: opAuthRequest.res
    }
 
    return new LoginConsentRequest(options)
  }
 
  static extractParams (opAuthRequest) {
    const req = opAuthRequest.req
    const query = req.query || {}
    const body = req.body || {}
    const params = query.client_id ? query : body
    return params
  }
 
  /**
   * @param consentRequest {LoginConsentRequest}
   *
   * @return {Promise<OPAuthenticationRequest>}
   */
  static obtainConsent (consentRequest) {
    const { opAuthRequest, clientId } = consentRequest
 
    const parsedAppOrigin = new URL(consentRequest.opAuthRequest.params.redirect_uri)
    const appOrigin = parsedAppOrigin.origin
 
    // Consent for the local RP client (the home pod) is implied
    Iif (consentRequest.isLocalRpClient(appOrigin)) {
      return Promise.resolve()
        .then(() => { consentRequest.markConsentSuccess(opAuthRequest) })
        .then(() => opAuthRequest)
    }
 
    // Check if user has submitted this from a Consent page
    if (consentRequest.hasAlreadyConsented(appOrigin)) {
      return consentRequest.saveConsentForClient(clientId)
        .then(() => { consentRequest.markConsentSuccess(opAuthRequest) })
        .then(() => opAuthRequest)
    }
 
    // Otherwise, need to obtain explicit consent from the user via UI
    return consentRequest.checkSavedConsentFor(clientId)
      .then(priorConsent => {
        if (priorConsent) {
          consentRequest.markConsentSuccess(opAuthRequest)
        } else {
          consentRequest.redirectToConsent()
        }
      })
      .then(() => opAuthRequest)
  }
 
  /**
   * @return {string}
   */
  get clientId () {
    return this.params.client_id
  }
 
  isLocalRpClient (appOrigin) {
    return this.opAuthRequest.req.app.locals.ldp.serverUri === appOrigin
  }
 
  hasAlreadyConsented (appOrigin) {
    return this.opAuthRequest.req.session.consentedOrigins &&
      this.opAuthRequest.req.session.consentedOrigins.includes(appOrigin)
  }
 
  checkSavedConsentFor (opAuthRequest) {
    return Promise.resolve(false)
  }
 
  markConsentSuccess (opAuthRequest) {
    opAuthRequest.consent = true
    opAuthRequest.scope = this.params.scope
  }
 
  saveConsentForClient (clientId) {
    return Promise.resolve(clientId)
  }
 
  redirectToConsent (authRequest) {
    const { opAuthRequest } = this
    console.log('Redirecting user to /sharing')
    console.log(opAuthRequest.host)
    const queryString = new URLSearchParams(opAuthRequest.req.query).toString()
    const consentUrl = `/sharing${queryString ? '?' + queryString : ''}`
    opAuthRequest.subject = null
 
    opAuthRequest.res.redirect(consentUrl)
 
    this.signalResponseSent()
  }
 
  signalResponseSent () {
    throw new AuthResponseSent('User redirected')
  }
}
 
module.exports = LoginConsentRequest