-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathexample-oauth2orize-consumer.js
107 lines (95 loc) · 3.18 KB
/
example-oauth2orize-consumer.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
'use strict';
/**
* Module dependencies.
*/
var util = require('util')
, OAuth2Strategy = require('passport-oauth').OAuth2Strategy
, InternalOAuthError = require('passport-oauth').InternalOAuthError
, pConf = require('./provider-config')
;
/**
* `Strategy` constructor.
*
* The example-oauth2orize authentication strategy authenticates requests by delegating to
* example-oauth2orize 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 example-oauth2orize application's client id
* - `clientSecret` your example-oauth2orize application's client secret
* - `callbackURL` URL to which example-oauth2orize will redirect the user after granting authorization
*
* Examples:
*
* passport.use(new Thirty7SignalsStrategy({
* clientID: '123-456-789',
* clientSecret: 'shhh-its-a-secret'
* callbackURL: 'https://www.example.net/auth/example-oauth2orize/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 || (pConf.protocol + '://' + pConf.host + '/dialog/authorize');
options.tokenURL = options.tokenURL || (pConf.protocol + '://' + pConf.host + '/oauth/token');
OAuth2Strategy.call(this, options, verify);
this.name = 'example-oauth2orize';
}
/**
* Inherit from `OAuth2Strategy`.
*/
util.inherits(Strategy, OAuth2Strategy);
/**
* Retrieve user profile from example-oauth2orize.
*
* This function constructs a normalized profile, with the following properties:
*
* - `provider` always set to `example-oauth2orize`
* - `id`
* - `username`
* - `displayName`
*
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
this._oauth2.get(pConf.protocol + '://' + pConf.host + '/api/userinfo', accessToken, function (err, body/*, res*/) {
if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); }
try {
var json = JSON.parse(body)
, profile = { provider: 'example-oauth2orize' }
;
console.log(json);
/*
profile.id = json.identity.id;
profile.displayName = json.identity.first_name + ' ' + json.identity.last_name;
profile.name = { familyName: json.identity.last_name,
givenName: json.identity.first_name };
profile.emails = [{ value: json.identity.email_address }];
profile._raw = body;
*/
profile._json = json;
done(null, profile);
} catch(e) {
done(e);
}
});
};
/**
* Expose `Strategy`.
*/
module.exports.Strategy = Strategy;