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 | 1× 1× 1× 1× 1× 1× 1× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× | 'use strict'; var stormpath = require('stormpath'); var util = require('util'); var xtend = require('xtend'); var createSession = require('../helpers/create-session'); var expandAccount = require('../helpers/expand-account'); var AccessTokenAuthenticator = require('../okta/access-token-authenticator'); /** * This callback, when called, will simply continue processing the HTTP * request. * * @callback nextCallback * @private */ /** * Attempts to retrieve a user object from either the session, a user's API * keys, or a user's OAuth tokens, making it available in the current context. * If a user cannot be found, nothing will be done and the request will * continue processing. * * @method * @private * * @param {Object} req - The http request. * @param {Object} res - The http response. * @param {nextCallback} next - The callback which is called to continue * processing the request. */ module.exports = function (req, res, next) { var application = req.app.get('stormpathApplication'); var client = req.app.get('stormpathClient'); var config = req.app.get('stormpathConfig'); var logger = req.app.get('stormpathLogger'); // In the event this has already been run (this can happen due to Express // routing logic) -- don't re-run this function. Iif (req.user) { return next(); } var issuer = config.org + '/oauth2/' + config.authorizationServerId; var accessTokenAuthenticator = new AccessTokenAuthenticator(client).forIssuer(issuer); Eif (config.web.oauth2.password.validationStrategy === 'local') { accessTokenAuthenticator.withLocalValidation(); } var cookies = req.cookies; var accessTokenCookieName = config.web.accessTokenCookie.name; var accessTokenFromCookie = cookies && cookies[accessTokenCookieName]; var refreshTokenCookieName = config.web.refreshTokenCookie.name; var refreshTokenFromCookie = cookies && cookies[refreshTokenCookieName]; var authorizationHeader = req.headers.Authorization || req.headers.authorization || ''; var accessTokenFromHeader = authorizationHeader.match(/Bearer [^;]+/) ? authorizationHeader.split('Bearer ')[1] : null; var resolvedAccessToken = accessTokenFromHeader || accessTokenFromCookie; Iif (resolvedAccessToken) { accessTokenAuthenticator.authenticate(resolvedAccessToken, function (err, authenticationResult) { if (err) { req.authenticationError = err; logger.info(err); if (!refreshTokenFromCookie) { return next(); } return new stormpath.OAuthRefreshTokenGrantRequestAuthenticator(application).authenticate({ refresh_token: refreshTokenFromCookie }, function (err, refreshGrantResponse) { if (err) { logger.info('Failed to refresh an access_token given a refresh_token.'); return next(); } refreshGrantResponse.getAccount(function (err, account) { if (err) { logger.info('Failed to get account from refreshGrantResponse.'); return next(); } if (account.status !== 'ENABLED') { logger.info('Account for refresh_token cookie is not ENALBED.'); return next(); } expandAccount(account, config.expand, logger, function (err, expandedAccount) { if (err) { logger.info('Failed to expand the user\'s account.'); return next(); } res.locals.user = expandedAccount; req.user = expandedAccount; if (refreshGrantResponse.grantedScopes) { res.locals.permissions = refreshGrantResponse.grantedScopes; req.user = expandedAccount; req.permissions = refreshGrantResponse.grantedScopes; } createSession(refreshGrantResponse, account, req, res); next(); }); }); }); } req.authenticationResult = authenticationResult; // Backcompat: Provide the expanded JWT as req.accessToken, // which has the claims under the 'body' key. Object.defineProperty(req, 'accessToken', { get: util.deprecate(function () { var jwt = xtend({}, authenticationResult.expandedJwt); jwt.body = xtend({}, jwt.claims); return jwt; }, 'req.accessToken is deprecated, use req.authenticationResult instead.') }); authenticationResult.getAccount(function (err, user) { if (err) { logger.info(err); return next(); } res.locals.user = user; req.user = user; next(); }); }); } else Iif (refreshTokenFromCookie) { new stormpath.OAuthRefreshTokenGrantRequestAuthenticator(application).authenticate({ refresh_token: cookies[refreshTokenCookieName] }, function (err, refreshGrantResponse) { if (err) { logger.info('Failed to refresh an access_token given a refresh_token.'); return next(); } refreshGrantResponse.getAccount(function (err, account) { if (err) { logger.info('Failed to get account from refreshGrantResponse.'); return next(); } if (account.status !== 'ENABLED') { logger.info('Account for refresh_token cookie is not ENALBED.'); return next(); } expandAccount(account, config.expand, logger, function (err, expandedAccount) { if (err) { logger.info('Failed to expand the user\'s account.'); return next(); } res.locals.user = expandedAccount; req.user = expandedAccount; if (refreshGrantResponse.grantedScopes) { res.locals.permissions = refreshGrantResponse.grantedScopes; req.user = expandedAccount; req.permissions = refreshGrantResponse.grantedScopes; } createSession(refreshGrantResponse, account, req, res); next(); }); }); }); } else { // TODO - implement a mechanism for handling client credential authentication via basic auth next(); } }; |