-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy.js
More file actions
executable file
·150 lines (129 loc) · 4.76 KB
/
strategy.js
File metadata and controls
executable file
·150 lines (129 loc) · 4.76 KB
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
/**
* Module dependencies.
*/
var util = require('util')
, OAuth2Strategy = require('passport-oauth2')
, InternalOAuthError = require('passport-oauth2').InternalOAuthError
, cj = require('collection-json')
, debug = require('debug')('passport-teamsnap');
/**
* `Strategy` constructor.
*
* The Teamsnap authentication strategy authenticates requests by delegating to
* Teamsnap 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 occurred, `err` should be set.
*
* Options:
* - `apiVersion` (optional) the Teamsnap API version to use (Only '3' currently). Default is '3'.
* - `clientID` your Teamsnap application's app key found in the App Console
* - `clientSecret` your Teamsnap application's app secret
* - `callbackURL` URL to which Dropbox will redirect the user after granting authorization
*
* Examples:
*
* passport.use(new TeamsnapStrategy({
* clientID: 'yourAppKey',
* clientSecret: 'yourAppSecret'
* callbackURL: 'https://www.example.net/auth/dropbox-teamsnap/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) {
if (typeof options == 'function') {
verify = options;
options = undefined;
}
var supportedApiVersions = ['3'],
defaultOptionsByApiVersion = {
3: {
profileURL:'https://api.teamsnap.com/v3/me',
authorizationURL: 'https://auth.teamsnap.com/oauth/authorize',
tokenURL: 'https://auth.teamsnap.com/oauth/token',
scopeSeparator: ' ',
customHeaders: {
'Content-Type': 'application/json'
}
}
};
options = options || {};
if (!verify) { throw new TypeError('TeamsnapStrategy requires a verify callback'); }
if (!options.clientID) { throw new TypeError('TeamsnapStrategy requires a clientID option'); }
if (options.apiVersion != null && supportedApiVersions.indexOf(options.apiVersion.toString()) === -1) {
throw new Error('Unsupported Teamsnap API version. Supported versions is "3".');
}
this._apiVersion = options.apiVersion || '3';
this._profileURL = options.profileURL || defaultOptionsByApiVersion[this._apiVersion].profileURL;
options.authorizationURL = options.authorizationURL || defaultOptionsByApiVersion[this._apiVersion].authorizationURL;
options.tokenURL = options.tokenURL || defaultOptionsByApiVersion[this._apiVersion].tokenURL;
options.scopeSeparator = options.scopeSeparator || defaultOptionsByApiVersion[this._apiVersion].scopeSeparator;
options.customHeaders = options.customHeaders || defaultOptionsByApiVersion[this._apiVersion].customHeaders;
OAuth2Strategy.call(this, options, verify);
this.name = 'teamsnap';
}
/**
* Inherit from `OAuth2Strategy`.
*/
util.inherits(Strategy, OAuth2Strategy);
/**
* Retrieve user profile from Teamsnap.
*
* This function constructs a normalized profile, with the following properties:
*
* - `provider` always set to `teamsnap`
* - `data` will contains the data array from Teamsnap
*
* @param {String} accessToken
* @param {Function} done
* @api protected
*/
Strategy.prototype.userProfile = function(accessToken, done) {
this._oauth2.useAuthorizationHeaderforGET(true);
this._oauth2.get(this._profileURL, accessToken, function (err, body, res) {
if (res && res.statusCode === 404) {
return done(new InternalOAuthError(res.headers['x-caserrorcode'], res.statusCode));
}
var json;
if (err) {
if (err.data) {
try {
json = JSON.parse(err.data);
} catch (_) {}
}
if (json && json.error) {
return done(new InternalOAuthError(json.error.message, json.error.code));
}
return done(new InternalOAuthError('Failed to fetch user profile', err));
}
try {
var profile = {};
profile.provider = 'teamsnap';
profile._raw = body;
var collection = new cj.Collection(JSON.parse(body));
debug("collection --> " + collection);
if (collection.items) {
profile.data = collection.items;
}
return done(null,profile);
} catch (ex) {
debug("error --> " + ex);
return done(new InternalOAuthError('Failed to parse user profile',ex));
}
});
};
/**
* Expose `Strategy`.
*/
module.exports = Strategy;