Skip to content

Commit 8801a92

Browse files
committed
Implement TokenError
1 parent 9090aae commit 8801a92

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
package-lock.json

lib/errors/tokenerror.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class TokenError extends Error {
2+
/**
3+
* @param {string} [message]
4+
* @param {string} [code]
5+
* @param {string} [uri]
6+
* @param {number} [status]
7+
*/
8+
constructor(message, code, uri, status) {
9+
super(message);
10+
Error.captureStackTrace(this, this.constructor);
11+
this.name = 'TokenError';
12+
this.message = message;
13+
this.code = code || 'invalid_request';
14+
this.uri = uri;
15+
this.status = status || 500;
16+
}
17+
}
18+
19+
module.exports = TokenError;

lib/strategy.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const jwt = require('jsonwebtoken');
88
const NullStateStore = require('./state/null');
99
const SessionStateStore = require('./state/session');
1010
const AuthorizationError = require('./errors/authorizationerror');
11+
const TokenError = require('./errors/tokenerror');
1112
const InternalOAuthError = require('./errors/internaloautherror');
1213

1314
class AppleStrategy extends passport.Strategy {
@@ -87,7 +88,7 @@ class AppleStrategy extends passport.Strategy {
8788
const oauth2 = this._getOAuth2Client();
8889

8990
oauth2.getOAuthAccessToken(code, params, (err, accessToken, refreshToken, params) => {
90-
if (err) return this.error(new InternalOAuthError('Failed to obtain access token', err));
91+
if (err) return this.error(this._createOAuthError('Failed to obtain access token', err));
9192

9293
const idToken = params['id_token'];
9394
if (!idToken) return this.error(new Error('ID Token not present in token response'));
@@ -179,6 +180,21 @@ class AppleStrategy extends passport.Strategy {
179180
}
180181
}
181182

183+
/**
184+
* @param {string} body
185+
* @returns {Error}
186+
*/
187+
parseErrorResponse(body) {
188+
const json = JSON.parse(body);
189+
if (json.error) {
190+
return new TokenError(json.error_description, json.error, json.error_uri);
191+
}
192+
return null;
193+
}
194+
195+
/**
196+
* @returns {oauth2.OAuth2}
197+
*/
182198
_getOAuth2Client() {
183199
const clientSecret = jwt.sign({}, this._key, {
184200
algorithm: 'ES256',
@@ -191,6 +207,24 @@ class AppleStrategy extends passport.Strategy {
191207

192208
return new OAuth2(this._clientID, clientSecret, '', this._authorizationURL, this._tokenURL);
193209
}
210+
211+
/**
212+
* @param {string} message
213+
* @param {object|Error} err
214+
* @returns {Error}
215+
*/
216+
_createOAuthError(message, err) {
217+
let e;
218+
if (err.statusCode && err.data) {
219+
try {
220+
e = this.parseErrorResponse(err.data);
221+
} catch (_) {
222+
// ignore
223+
}
224+
}
225+
if (!e) e = new InternalOAuthError(message, err);
226+
return e;
227+
}
194228
}
195229

196230
module.exports = AppleStrategy;

0 commit comments

Comments
 (0)