Skip to content

Commit

Permalink
feat: Add support for web auth, utilizing code from npm-profile
Browse files Browse the repository at this point in the history
  • Loading branch information
jumoel committed Jun 29, 2022
1 parent a5e86d5 commit 835033d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/utils/otplease.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ async function otplease (npm, opts, fn) {

const isBackwardsCompatibleOTP = err.code === 'E401' && /one-time pass/.test(err.body)
const isClassicOTP = err.code === 'EOTP'
const isWebOTP = err.code === 'EOTP' && err.body.authUrl && err.body.doneUrl

if (isWebOTP) {
const webAuth = require('./web-auth')
const openUrlPrompt = require('./open-url-prompt')

const openerPromise = (url, emitter) =>
openUrlPrompt(
npm,
url,
'Authenticate your account at',
'Press ENTER to open in the browser...',
emitter
)
const otp = await webAuth(openerPromise, err.body.authUrl, err.body.doneUrl, opts)
return await fn({ ...opts, otp })
}

if (isClassicOTP || isBackwardsCompatibleOTP) {
const readUserInfo = require('./read-user-info.js')
Expand Down
22 changes: 22 additions & 0 deletions lib/utils/web-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const EventEmitter = require('events')
const { webAuthCheckLogin } = require('npm-profile')

function webAuth (opener, initialUrl, doneUrl, opts) {
const doneEmitter = new EventEmitter()

const openPromise = opener(initialUrl, doneEmitter)
const webAuthCheckPromise = webAuthCheckLogin(doneUrl, { ...opts, cache: false })
.then(authResult => {
// cancel open prompt if it's present
doneEmitter.emit('abort')

return authResult
})

return Promise.all([openPromise, webAuthCheckPromise]).then(
// pick the auth result and pass it along
([, authResult]) => authResult
)
}

module.exports = webAuth

0 comments on commit 835033d

Please sign in to comment.