MCPcopy Index your code
hub / github.com/nodejs/node / enable2fa

Method enable2fa

deps/npm/lib/commands/profile.js:224–340  ·  view source on GitHub ↗
(args)

Source from the content-addressed store, hash-verified

222 }
223
224 async enable2fa (args) {
225 const conf = { ...this.npm.flatOptions }
226
227 if (args.length > 1) {
228 throw new Error('npm profile enable-2fa [auth-and-writes|auth-only]')
229 }
230
231 const mode = args[0] || 'auth-and-writes'
232 if (mode !== 'auth-only' && mode !== 'auth-and-writes') {
233 throw new Error(
234 `Invalid two-factor authentication mode "${mode}".\n` +
235 'Valid modes are:\n' +
236 ' auth-only - Require two-factor authentication only when logging in\n' +
237 ' auth-and-writes - Require two-factor authentication when logging in ' +
238 'AND when publishing'
239 )
240 }
241
242 if (this.npm.config.get('json') || this.npm.config.get('parseable')) {
243 throw new Error(
244 'Enabling two-factor authentication is an interactive operation and ' +
245 (this.npm.config.get('json') ? 'JSON' : 'parseable') + ' output mode is not available'
246 )
247 }
248
249 const userInfo = await get(conf)
250
251 if (!userInfo?.tfa?.pending && userInfo?.tfa?.mode === mode) {
252 output.standard('Two factor authentication is already enabled and set to ' + mode)
253 return
254 }
255
256 const info = {
257 tfa: {
258 mode,
259 },
260 }
261
262 // if they're using legacy auth currently then we have to update them to a bearer token before continuing.
263 const creds = this.npm.config.getCredentialsByURI(this.npm.config.get('registry'))
264 const auth = {}
265
266 if (creds.token) {
267 auth.token = creds.token
268 } else if (creds.username) {
269 auth.basic = { username: creds.username, password: creds.password }
270 } else if (creds.auth) {
271 const basic = Buffer.from(creds.auth, 'base64').toString().split(':', 2)
272 auth.basic = { username: basic[0], password: basic[1] }
273 }
274
275 if (!auth.basic && !auth.token) {
276 throw new Error(
277 'You need to be logged in to registry ' +
278 `${this.npm.config.get('registry')} in order to enable 2fa`
279 )
280 }
281

Callers 1

execMethod · 0.95

Calls 14

otpleaseFunction · 0.85
qrcodeFunction · 0.85
infoMethod · 0.80
passwordMethod · 0.80
getMethod · 0.65
getFunction · 0.50
createTokenFunction · 0.50
setFunction · 0.50
inspectFunction · 0.50
splitMethod · 0.45
toStringMethod · 0.45
fromMethod · 0.45

Tested by

no test coverage detected