|
| 1 | +/** |
| 2 | + * Module dependencies. |
| 3 | + */ |
| 4 | +var util = require('util') |
| 5 | + , OAuthStrategy = require('passport-oauth').OAuthStrategy |
| 6 | + , InternalOAuthError = require('passport-oauth').InternalOAuthError; |
| 7 | + |
| 8 | + |
| 9 | +/** |
| 10 | + * `LinkedInTokenStrategy` constructor. |
| 11 | + * |
| 12 | + * The LinkedIn authentication strategy authenticates requests by delegating to |
| 13 | + * LinkedIn using the OAuth protocol. |
| 14 | + * |
| 15 | + * Applications must supply a `verify` callback which accepts a `token`, |
| 16 | + * `tokenSecret` and service-specific `profile`, and then calls the `done` |
| 17 | + * callback supplying a `user`, which should be set to `false` if the |
| 18 | + * credentials are not valid. If an exception occured, `err` should be set. |
| 19 | + * |
| 20 | + * Options: |
| 21 | + * - `consumerKey` identifies client to LinkedIn |
| 22 | + * - `consumerSecret` secret used to establish ownership of the consumer key |
| 23 | + * |
| 24 | + * Examples: |
| 25 | + * |
| 26 | + * passport.use(new LinkedInTokenStrategy({ |
| 27 | + * consumerKey: '123-456-789', |
| 28 | + * consumerSecret: 'shhh-its-a-secret' |
| 29 | + * }, |
| 30 | + * function(token, tokenSecret, profile, done) { |
| 31 | + * User.findOrCreate(..., function (err, user) { |
| 32 | + * done(err, user); |
| 33 | + * }); |
| 34 | + * } |
| 35 | + * )); |
| 36 | + * |
| 37 | + * @param {Object} options |
| 38 | + * @param {Function} verify |
| 39 | + * @api public |
| 40 | + */ |
| 41 | +function LinkedInTokenStrategy(options, verify) { |
| 42 | + options = options || {}; |
| 43 | + options.requestTokenURL = options.requestTokenURL || 'https://api.linkedin.com/uas/oauth/requestToken'; |
| 44 | + options.accessTokenURL = options.accessTokenURL || 'https://api.linkedin.com/uas/oauth/accessToken'; |
| 45 | + options.userAuthorizationURL = options.userAuthorizationURL || 'https://www.linkedin.com/uas/oauth/authenticate'; |
| 46 | + options.sessionKey = options.sessionKey || 'oauth:linkedin'; |
| 47 | + |
| 48 | + OAuthStrategy.call(this, options, verify); |
| 49 | + this.name = 'linkedin-token'; |
| 50 | + |
| 51 | + this._skipExtendedUserProfile = (options.skipExtendedUserProfile === undefined) ? false : options.skipExtendedUserProfile; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * Inherit from `OAuthLinkedInTokenStrategy`. |
| 56 | + */ |
| 57 | +util.inherits(LinkedInTokenStrategy, OAuthStrategy); |
| 58 | + |
| 59 | + |
| 60 | +/** |
| 61 | + * Authenticate request by delegating to LinkedIn using OAuth. |
| 62 | + * |
| 63 | + * @param {Object} req |
| 64 | + * @api protected |
| 65 | + */ |
| 66 | +LinkedInTokenStrategy.prototype.authenticate = function(req, options) { |
| 67 | + // When a user denies authorization on LinkedIn, they are presented with a link |
| 68 | + // to return to the application in the following format (where xxx is the |
| 69 | + // value of the request token): |
| 70 | + // |
| 71 | + // http://www.example.com/auth/linkedin/callback?denied=xxx |
| 72 | + // |
| 73 | + // Following the link back to the application is interpreted as an |
| 74 | + // authentication failure. |
| 75 | + if (req.query && req.query.denied) { |
| 76 | + return this.fail(); |
| 77 | + } |
| 78 | + |
| 79 | + var self = this; |
| 80 | + var token = req.body.oauth_token || req.query.oauth_token; |
| 81 | + var tokenSecret = req.body.oauth_token_secret || req.query.oauth_token_secret; |
| 82 | + |
| 83 | + var params = { }; |
| 84 | + |
| 85 | + self._loadUserProfile(token, tokenSecret, params, function(err, profile) { |
| 86 | + if (err) { return self.error(err); }; |
| 87 | + |
| 88 | + function verified(err, user, info) { |
| 89 | + if (err) { return self.error(err); } |
| 90 | + if (!user) { return self.fail(info); } |
| 91 | + self.success(user, info); |
| 92 | + } |
| 93 | + |
| 94 | + if (self._passReqToCallback) { |
| 95 | + var arity = self._verify.length; |
| 96 | + if (arity == 6) { |
| 97 | + self._verify(req, token, tokenSecret, params, profile, verified); |
| 98 | + } else { // arity == 5 |
| 99 | + self._verify(req, token, tokenSecret, profile, verified); |
| 100 | + } |
| 101 | + } else { |
| 102 | + var arity = self._verify.length; |
| 103 | + if (arity == 5) { |
| 104 | + self._verify(token, tokenSecret, params, profile, verified); |
| 105 | + } else { // arity == 4 |
| 106 | + self._verify(token, tokenSecret, profile, verified); |
| 107 | + } |
| 108 | + } |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +/** |
| 113 | + * Retrieve user profile from LinkedIn. |
| 114 | + * |
| 115 | + * This function constructs a normalized profile, with the following properties: |
| 116 | + * |
| 117 | + * - `id` (equivalent to `user_id`) |
| 118 | + * - `username` (equivalent to `screen_name`) |
| 119 | + * |
| 120 | + * Note that because LinkedIn supplies basic profile information in query |
| 121 | + * parameters when redirecting back to the application, loading of LinkedIn |
| 122 | + * profiles *does not* result in an additional HTTP request, when the |
| 123 | + * `skipExtendedUserProfile` is enabled. |
| 124 | + * |
| 125 | + * @param {String} token |
| 126 | + * @param {String} tokenSecret |
| 127 | + * @param {Object} params |
| 128 | + * @param {Function} done |
| 129 | + * @api protected |
| 130 | + */ |
| 131 | +LinkedInTokenStrategy.prototype.userProfile = function(token, tokenSecret, params, done) { |
| 132 | + var url = 'https://api.linkedin.com/v1/people/~:(id,first-name,last-name,public-profile-url)?format=json'; |
| 133 | + if (this._profileFields) { |
| 134 | + var fields = this._convertProfileFields(this._profileFields); |
| 135 | + url = 'https://api.linkedin.com/v1/people/~:(' + fields + ')?format=json'; |
| 136 | + } |
| 137 | + |
| 138 | + this._oauth.get(url, token, tokenSecret, function (err, body, res) { |
| 139 | + if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); } |
| 140 | + |
| 141 | + try { |
| 142 | + var json = JSON.parse(body); |
| 143 | + |
| 144 | + var profile = { provider: 'linkedin' }; |
| 145 | + profile.id = json.id; |
| 146 | + profile.displayName = json.firstName + ' ' + json.lastName; |
| 147 | + profile.name = { familyName: json.lastName, |
| 148 | + givenName: json.firstName }; |
| 149 | + if (json.emailAddress) { profile.emails = [{ value: json.emailAddress }]; } |
| 150 | + |
| 151 | + profile._raw = body; |
| 152 | + profile._json = json; |
| 153 | + |
| 154 | + done(null, profile); |
| 155 | + } catch(e) { |
| 156 | + done(e); |
| 157 | + } |
| 158 | + }); |
| 159 | +} |
| 160 | + |
| 161 | +/** |
| 162 | + * Return extra LinkedIn-specific parameters to be included in the user |
| 163 | + * authorization request. |
| 164 | + * |
| 165 | + * @param {Object} options |
| 166 | + * @return {Object} |
| 167 | + * @api protected |
| 168 | + */ |
| 169 | +LinkedInTokenStrategy.prototype.userAuthorizationParams = function(options) { |
| 170 | + var params = {}; |
| 171 | + if (options.forceLogin) { |
| 172 | + params['force_login'] = options.forceLogin; |
| 173 | + } |
| 174 | + if (options.screenName) { |
| 175 | + params['screen_name'] = options.screenName; |
| 176 | + } |
| 177 | + return params; |
| 178 | +} |
| 179 | + |
| 180 | + |
| 181 | +/** |
| 182 | + * Expose `LinkedInTokenStrategy`. |
| 183 | + */ |
| 184 | +module.exports = LinkedInTokenStrategy; |
0 commit comments