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 | 1× 1× 1× 1× 1× 1× 1× 1× 1× | 'use strict'; var oauth = require('../oauth'); var writeJsonError = require('./write-json-error'); var loginResponder = require('./login-responder'); var expandAccount = require('./expand-account'); var createSession = require('./create-session'); var exchangeStormpathToken = require('./exchange-stormpath-token'); /** * loginWithOAuthProvider takes provider data, such as an access token, * and responds with a new session if the provider data is valid. * * @method * * @param {Object} options - Should contain the provider data sent to application.getAccount. * @param {Object} req - The http request. * @param {Object} res - The http response. */ module.exports = function loginWithOAuthProvider(options, req, res) { var config = req.app.get('stormpathConfig'); var logger = req.app.get('stormpathLogger'); var application = req.app.get('stormpathApplication'); var preLoginHandler = config.preLoginHandler; var postLoginHandler = config.postLoginHandler; var preRegistrationHandler = config.preRegistrationHandler; var postRegistrationHandler = config.postRegistrationHandler; application.getAccount(options, function (err, providerAccountResult) { if (err) { return oauth.errorResponder(req, res, err); } var account = providerAccountResult.account; function continueWithTokenExchange() { exchangeStormpathToken(req, account, function (err, authResult) { if (err) { return oauth.errorResponder(req, res, err); } expandAccount(account, config.expand, logger, function (err, expandedAccount) { if (err) { return writeJsonError(res, err); } createSession(authResult, expandedAccount, req, res); loginResponder(req, res); }); }); } function continueWithHandlers(preHandler, postHandler, onCompleted) { // If there's no preHandler, then provide a default one. if (!preHandler) { preHandler = function (options, req, res, callback) { callback(); }; } preHandler(options, req, res, function (err) { if (err) { return oauth.errorResponder(req, res, err); } if (postHandler) { return postHandler(account, req, res, function (err) { if (err) { return oauth.errorResponder(req, res, err); } onCompleted(); }); } onCompleted(); }); } if (preRegistrationHandler && providerAccountResult.created) { return continueWithHandlers(preRegistrationHandler, postRegistrationHandler, function () { continueWithHandlers(preLoginHandler, postLoginHandler, continueWithTokenExchange); }); } if (preLoginHandler) { return continueWithHandlers(preLoginHandler, postLoginHandler, continueWithTokenExchange); } continueWithTokenExchange(); }); }; |