-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = authenticationRequestError | ||
|
||
const HttpError = require('@octokit/request/lib/http-error') | ||
|
||
function authenticationRequestError (state, error, options) { | ||
// handle "2FA required" error only | ||
if (error.status !== 401 || !/required/.test(error.headers['x-github-otp'] || '')) { | ||
throw error | ||
} | ||
|
||
if (typeof state.auth.on2fa !== 'function') { | ||
throw new HttpError('2FA required, but options.on2fa is not a function. See https://github.com/octokit/rest.js#authentication', 401, error.headers, options) | ||
} | ||
|
||
return Promise.resolve() | ||
.then(() => { | ||
return state.auth.on2fa() | ||
}) | ||
.then((oneTimePassword) => { | ||
const newOptions = Object.assign(options, { | ||
headers: Object.assign({ 'x-github-otp': oneTimePassword }, options.headers) | ||
}) | ||
return state.octokit.request(newOptions) | ||
}) | ||
} |