|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const debug = require('./../debug').authentication |
| 4 | + |
| 5 | +const AuthRequest = require('./auth-request') |
| 6 | + |
| 7 | +/** |
| 8 | + * Models a local Login request |
| 9 | + */ |
| 10 | +class ConsentRequest extends AuthRequest { |
| 11 | + /** |
| 12 | + * @constructor |
| 13 | + * @param options {Object} |
| 14 | + * |
| 15 | + * @param [options.response] {ServerResponse} middleware `res` object |
| 16 | + * @param [options.session] {Session} req.session |
| 17 | + * @param [options.userStore] {UserStore} |
| 18 | + * @param [options.accountManager] {AccountManager} |
| 19 | + * @param [options.returnToUrl] {string} |
| 20 | + * @param [options.authQueryParams] {Object} Key/value hashmap of parsed query |
| 21 | + * parameters that will be passed through to the /authorize endpoint. |
| 22 | + * @param [options.authenticator] {Authenticator} Auth strategy by which to |
| 23 | + * log in |
| 24 | + */ |
| 25 | + constructor (options) { |
| 26 | + super(options) |
| 27 | + |
| 28 | + this.authenticator = options.authenticator |
| 29 | + this.authMethod = options.authMethod |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Factory method, returns an initialized instance of LoginRequest |
| 34 | + * from an incoming http request. |
| 35 | + * |
| 36 | + * @param req {IncomingRequest} |
| 37 | + * @param res {ServerResponse} |
| 38 | + * @param authMethod {string} |
| 39 | + * |
| 40 | + * @return {LoginRequest} |
| 41 | + */ |
| 42 | + static fromParams (req, res) { |
| 43 | + let options = AuthRequest.requestOptions(req, res) |
| 44 | + |
| 45 | + return new ConsentRequest(options) |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Handles a Login GET request on behalf of a middleware handler, displays |
| 50 | + * the Login page. |
| 51 | + * Usage: |
| 52 | + * |
| 53 | + * ``` |
| 54 | + * app.get('/login', LoginRequest.get) |
| 55 | + * ``` |
| 56 | + * |
| 57 | + * @param req {IncomingRequest} |
| 58 | + * @param res {ServerResponse} |
| 59 | + */ |
| 60 | + static get (req, res) { |
| 61 | + const request = ConsentRequest.fromParams(req, res) |
| 62 | + |
| 63 | + request.renderForm(null, req) |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Performs the login operation -- loads and validates the |
| 68 | + * appropriate user, inits the session with credentials, and redirects the |
| 69 | + * user to continue their auth flow. |
| 70 | + * |
| 71 | + * @param request {LoginRequest} |
| 72 | + * |
| 73 | + * @return {Promise} |
| 74 | + */ |
| 75 | + static giveConsent (req, res) { |
| 76 | + let request = ConsentRequest.fromParams(req, res) |
| 77 | + console.log(request.authQueryParams) |
| 78 | + // debug('Providing consent for app sharing') |
| 79 | + // return request.authenticator.findValidUser() |
| 80 | + |
| 81 | + // .then(validUser => { |
| 82 | + // request.initUserSession(validUser) |
| 83 | + |
| 84 | + // request.redirectPostLogin(validUser) |
| 85 | + // }) |
| 86 | + |
| 87 | + // .catch(error => request.error(error)) |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns a URL to redirect the user to after login. |
| 92 | + * Either uses the provided `redirect_uri` auth query param, or simply |
| 93 | + * returns the user profile URI if none was provided. |
| 94 | + * |
| 95 | + * @param validUser {UserAccount} |
| 96 | + * |
| 97 | + * @return {string} |
| 98 | + */ |
| 99 | + postConsentUrl (validUser) { |
| 100 | + return this.authorizeUrl() |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Redirects the Login request to continue on the OIDC auth workflow. |
| 105 | + */ |
| 106 | + redirectPostLogin (validUser) { |
| 107 | + let uri = this.postLoginUrl(validUser) |
| 108 | + debug('Login successful, redirecting to ', uri) |
| 109 | + this.response.redirect(uri) |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Renders the login form |
| 114 | + */ |
| 115 | + renderForm (error, req) { |
| 116 | + let queryString = req && req.url && req.url.replace(/[^?]+\?/, '') || '' |
| 117 | + let params = Object.assign({}, this.authQueryParams, |
| 118 | + { |
| 119 | + registerUrl: this.registerUrl(), |
| 120 | + returnToUrl: this.returnToUrl, |
| 121 | + enablePassword: this.localAuth.password, |
| 122 | + enableTls: this.localAuth.tls, |
| 123 | + tlsUrl: `/login/tls?${encodeURIComponent(queryString)}` |
| 124 | + }) |
| 125 | + |
| 126 | + if (error) { |
| 127 | + params.error = error.message |
| 128 | + this.response.status(error.statusCode) |
| 129 | + } |
| 130 | + |
| 131 | + this.response.render('auth/consent', params) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +module.exports = { |
| 136 | + ConsentRequest |
| 137 | +} |
0 commit comments