forked from jaredhanson/passport-google-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.js
149 lines (136 loc) · 4.62 KB
/
oauth2.js
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
/**
* Module dependencies.
*/
var util = require('util')
, OAuth2Strategy = require('passport-oauth').OAuth2Strategy
, InternalOAuthError = require('passport-oauth').InternalOAuthError;
/**
* `Strategy` constructor.
*
* The Google authentication strategy authenticates requests by delegating to
* Google using the OAuth 2.0 protocol.
*
* Applications must supply a `verify` callback which accepts an `accessToken`,
* `refreshToken` and service-specific `profile`, and then calls the `done`
* callback supplying a `user`, which should be set to `false` if the
* credentials are not valid. If an exception occured, `err` should be set.
*
* Options:
* - `clientID` your Google application's client id
* - `clientSecret` your Google application's client secret
* - `callbackURL` URL to which Google will redirect the user after granting authorization
*
* Examples:
*
* passport.use(new GoogleStrategy({
* clientID: '123-456-789',
* clientSecret: 'shhh-its-a-secret'
* callbackURL: 'https://www.example.net/auth/google/callback'
* },
* function(accessToken, refreshToken, profile, done) {
* User.findOrCreate(..., function (err, user) {
* done(err, user);
* });
* }
* ));
*
* @param {Object} options
* @param {Function} verify
* @api public
*/
function Strategy(options, verify) {
options = options || {};
options.authorizationURL = options.authorizationURL || 'https://accounts.google.com/o/oauth2/auth';
options.tokenURL = options.tokenURL || 'https://accounts.google.com/o/oauth2/token';
OAuth2Strategy.call(this, options, verify);
this.name = 'google';
}
/**
* Inherit from `OAuth2Strategy`.
*/
util.inherits(Strategy, OAuth2Strategy);
/**
* Retrieve user profile from Google.
*
* This function constructs a normalized profile, with the following properties:
*
* - `provider` always set to `google`
* - `id`
* - `username`
* - `displayName`
*
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
this._oauth2.get('https://www.googleapis.com/oauth2/v1/userinfo', accessToken, function (err, body, res) {
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
try {
var json = JSON.parse(body);
var profile = { provider: 'google' };
profile.id = json.id;
profile.displayName = json.name;
profile.name = { familyName: json.family_name,
givenName: json.given_name };
profile.emails = [{ value: json.email }];
profile._raw = body;
profile._json = json;
done(null, profile);
} catch(e) {
done(e);
}
});
}
/**
* Return extra Google-specific parameters to be included in the authorization
* request.
*
* @param {Object} options
* @return {Object}
* @api protected
*/
Strategy.prototype.authorizationParams = function(options) {
var params = {};
if (options.accessType) {
params['access_type'] = options.accessType;
}
if (options.approvalPrompt) {
params['approval_prompt'] = options.approvalPrompt;
}
if (options.prompt) {
// This parameter is undocumented in Google's official documentation.
// However, it was detailed by Breno de Medeiros (who works at Google) in
// this Stack Overflow answer:
// http://stackoverflow.com/questions/14384354/force-google-account-chooser/14393492#14393492
params['prompt'] = options.prompt;
}
if (options.loginHint) {
// This parameter is derived from OpenID Connect, and supported by Google's
// OAuth 2.0 endpoint.
// https://github.com/jaredhanson/passport-google-oauth/pull/8
// https://bitbucket.org/openid/connect/commits/970a95b83add
params['login_hint'] = options.loginHint;
}
if (options.userID) {
// Undocumented, but supported by Google's OAuth 2.0 endpoint. Appears to
// be equivalent to `login_hint`.
params['user_id'] = options.userID;
}
if (options.hostedDomain || options.hd) {
// This parameter is derived from Google's OAuth 1.0 endpoint, and (although
// undocumented) is supported by Google's OAuth 2.0 endpoint was well.
// https://developers.google.com/accounts/docs/OAuth_ref
params['hd'] = options.hostedDomain || options.hd;
}
if (options.openIdRealm) {
// This parameter is needed when migrating users from Google's OpenID 2.0 to OAuth 2.0
// https://developers.google.com/accounts/docs/OpenID?hl=ja#adjust-uri
params['openid.realm'] = options.openIdRealm;
}
return params;
}
/**
* Expose `Strategy`.
*/
module.exports = Strategy;