-
Notifications
You must be signed in to change notification settings - Fork 5
/
traktDeviceAuthPoller.js
166 lines (154 loc) · 4.42 KB
/
traktDeviceAuthPoller.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
(function(){
'use strict';
var PinkiePromise = require('pinkie-promise');
/**
* Private class which checks the result of a device authentication request.
*
* Example Usage:
* ```(javascript)
* var trakt = new Trakt({...});
* new TraktDeviceAuthPoller(trakt, {
* device_code: '...',
* interval: 5,
* expires_in: 600,
* ...
* }).catch(function(error) {
* console.log(error);
* }).then(function(result) {
* if (result) {
* console.log('Yay! We are authorized!');
* } else {
* console.log('User denied our authentication request :(');
* }
* });
* ```
*
* @namespace TraktDeviceAuthPoller
* @param {Trakt} instance The Trakt Instance to use when polling.
* @param {Object} body The body received when requesting a code from the API.
* @return {Promise} Result of the authorization as a promise.
*/
function TraktDeviceAuthPoller(instance, body) {
return new PinkiePromise(function(resolve, reject) {
this.trakt = instance;
this.resolve = resolve;
this.reject = reject;
this.device_code = body.device_code;
this.startPoll(body.interval * 1000);
this.timeout = setTimeout(this.onTimeout.bind(this), body.expires_in * 1000);
}.bind(this));
};
/**
* @private
* @memberof TraktDeviceAuthPoller
*/
var prot = TraktDeviceAuthPoller.prototype;
/**
* Triggers a timeout failure and exits cleanly through promise.
*
* @memberof TraktDeviceAuthPoller
*/
prot.onTimeout = function() {
this.Fail({
error: -1,
error_description: "Authorization timed out",
original_error: null,
response: null
});
};
/**
* (Re)Sets polling interval and initiates it.
*
* @memberof TraktDeviceAuthPoller
* @param {number} interval Polling interval in milliseconds.
*/
prot.startPoll = function(interval) {
this.intervalMs = interval;
if (this.interval) clearInterval(this.interval);
this.interval = setInterval(this.doPoll.bind(this), this.intervalMs);
};
/**
* Handles any HTTP Error from trakt when requesting auth result.
*
* @memberof TraktDeviceAuthPoller
* @param {Object} err Error object from Trakt API.
*/
prot.handleError = function(err) {
switch(err.error) {
case 400: return; // Pending
case 409: return; // Already Approved code
case 429: // Polling to fast
this.startPoll(this.intervalMs * 0.75); // Slow down 25%
break;
case 404: // Invalid device_code
this.Fail({
error: 'Invalid device code',
error_description: 'Received device code was not valid!',
original_error: err,
response: err.response
});
break;
case 410: // Expired
this.onTimeout();
break;
case 418: // Denied
this.onResult(false);
break;
default: {
this.Fail({
error: err.statusCode,
error_description: err.statusMessage,
original_error: err,
response: err.response
});
} break;
}
};
/**
* Actual polling function.
* This triggers a get token request to the Trakt API and
* handles any result, or passes any error to the errorhandler.
*
* @memberof TraktDeviceAuthPoller
*/
prot.doPoll = function() {
this.trakt.oauth.device.token({
client_id: this.trakt._settings.client_id,
client_secret: this.trakt._settings.client_secret,
code: this.device_code
})
.catch(this.handleError.bind(this))
.then(function(token) {
this.trakt.setAccessToken({
refresh_token: token.refresh_token,
access_token: token.access_token,
expires: Date.now() + (token.expires_in * 1000)
});
this.onResult(true);
}.bind(this));
};
/**
* Serves result and exits cleanly.
*
* @memberof TraktDeviceAuthPoller
* @param {bool} result Boolean result of authentication.
*/
prot.onResult = function(result) {
this.resolve(result);
clearInterval(this.interval);
clearTimeout(this.timeout);
}
/**
* Serves error and exits cleanly.
*
* @memberof TraktDeviceAuthPoller
* @param {Object} err Error object to fail promise with.
*/
prot.onError = function(err) {
this.reject(err);
clearInterval(this.interval);
clearTimeout(this.timeout);
};
module.exports = TraktDeviceAuthPoller;
return;
})();