Skip to content

Commit a22e6f5

Browse files
authored
profile: update profile to support new npm-profile API (#131)
PR-URL: #131 Credit: @zkat Reviewed-By: @aeschright
1 parent 2401b75 commit a22e6f5

File tree

1 file changed

+65
-60
lines changed

1 file changed

+65
-60
lines changed

lib/profile.js

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
'use strict'
2-
const profile = require('libnpm/profile')
3-
const npm = require('./npm.js')
2+
3+
const BB = require('bluebird')
4+
5+
const ansistyles = require('ansistyles')
6+
const figgyPudding = require('figgy-pudding')
7+
const inspect = require('util').inspect
48
const log = require('npmlog')
9+
const npm = require('./npm.js')
10+
const npmConfig = require('./config/figgy-config.js')
11+
const otplease = require('./utils/otplease.js')
512
const output = require('./utils/output.js')
13+
const profile = require('libnpm/profile')
14+
const pulseTillDone = require('./utils/pulse-till-done.js')
15+
const qrcodeTerminal = require('qrcode-terminal')
16+
const queryString = require('query-string')
617
const qw = require('qw')
7-
const Table = require('cli-table3')
8-
const ansistyles = require('ansistyles')
9-
const Bluebird = require('bluebird')
1018
const readUserInfo = require('./utils/read-user-info.js')
11-
const qrcodeTerminal = require('qrcode-terminal')
19+
const Table = require('cli-table3')
1220
const url = require('url')
13-
const queryString = require('query-string')
14-
const pulseTillDone = require('./utils/pulse-till-done.js')
15-
const inspect = require('util').inspect
1621

1722
module.exports = profileCmd
1823

@@ -48,6 +53,13 @@ function withCb (prom, cb) {
4853
prom.then((value) => cb(null, value), cb)
4954
}
5055

56+
const ProfileOpts = figgyPudding({
57+
json: {},
58+
otp: {},
59+
parseable: {},
60+
registry: {}
61+
})
62+
5163
function profileCmd (args, cb) {
5264
if (args.length === 0) return cb(new Error(profileCmd.usage))
5365
log.gauge.show('profile')
@@ -75,36 +87,13 @@ function profileCmd (args, cb) {
7587
}
7688
}
7789

78-
function config () {
79-
const conf = {
80-
json: npm.config.get('json'),
81-
parseable: npm.config.get('parseable'),
82-
registry: npm.config.get('registry'),
83-
otp: npm.config.get('otp')
84-
}
85-
const creds = npm.config.getCredentialsByURI(conf.registry)
86-
if (creds.token) {
87-
conf.auth = {token: creds.token}
88-
} else if (creds.username) {
89-
conf.auth = {basic: {username: creds.username, password: creds.password}}
90-
} else if (creds.auth) {
91-
const auth = Buffer.from(creds.auth, 'base64').toString().split(':', 2)
92-
conf.auth = {basic: {username: auth[0], password: auth[1]}}
93-
} else {
94-
conf.auth = {}
95-
}
96-
97-
if (conf.otp) conf.auth.otp = conf.otp
98-
return conf
99-
}
100-
10190
const knownProfileKeys = qw`
10291
name email ${'two-factor auth'} fullname homepage
10392
freenode twitter github created updated`
10493

10594
function get (args) {
10695
const tfa = 'two-factor auth'
107-
const conf = config()
96+
const conf = ProfileOpts(npmConfig())
10897
return pulseTillDone.withPromise(profile.get(conf)).then((info) => {
10998
if (!info.cidr_whitelist) delete info.cidr_whitelist
11099
if (conf.json) {
@@ -150,7 +139,7 @@ const writableProfileKeys = qw`
150139
email password fullname homepage freenode twitter github`
151140

152141
function set (args) {
153-
const conf = config()
142+
let conf = ProfileOpts(npmConfig())
154143
const prop = (args[0] || '').toLowerCase().trim()
155144
let value = args.length > 1 ? args.slice(1).join(' ') : null
156145
if (prop !== 'password' && value === null) {
@@ -164,7 +153,7 @@ function set (args) {
164153
if (writableProfileKeys.indexOf(prop) === -1) {
165154
return Promise.reject(Error(`"${prop}" is not a property we can set. Valid properties are: ` + writableProfileKeys.join(', ')))
166155
}
167-
return Bluebird.try(() => {
156+
return BB.try(() => {
168157
if (prop === 'password') {
169158
return readUserInfo.password('Current password: ').then((current) => {
170159
return readPasswords().then((newpassword) => {
@@ -193,23 +182,18 @@ function set (args) {
193182
const newUser = {}
194183
writableProfileKeys.forEach((k) => { newUser[k] = user[k] })
195184
newUser[prop] = value
196-
return profile.set(newUser, conf).catch((err) => {
197-
if (err.code !== 'EOTP') throw err
198-
return readUserInfo.otp().then((otp) => {
199-
conf.auth.otp = otp
200-
return profile.set(newUser, conf)
185+
return otplease(conf, conf => profile.set(newUser, conf))
186+
.then((result) => {
187+
if (conf.json) {
188+
output(JSON.stringify({[prop]: result[prop]}, null, 2))
189+
} else if (conf.parseable) {
190+
output(prop + '\t' + result[prop])
191+
} else if (result[prop] != null) {
192+
output('Set', prop, 'to', result[prop])
193+
} else {
194+
output('Set', prop)
195+
}
201196
})
202-
}).then((result) => {
203-
if (conf.json) {
204-
output(JSON.stringify({[prop]: result[prop]}, null, 2))
205-
} else if (conf.parseable) {
206-
output(prop + '\t' + result[prop])
207-
} else if (result[prop] != null) {
208-
output('Set', prop, 'to', result[prop])
209-
} else {
210-
output('Set', prop)
211-
}
212-
})
213197
}))
214198
})
215199
}
@@ -225,7 +209,7 @@ function enable2fa (args) {
225209
' auth-only - Require two-factor authentication only when logging in\n' +
226210
' auth-and-writes - Require two-factor authentication when logging in AND when publishing'))
227211
}
228-
const conf = config()
212+
const conf = ProfileOpts(npmConfig())
229213
if (conf.json || conf.parseable) {
230214
return Promise.reject(new Error(
231215
'Enabling two-factor authentication is an interactive operation and ' +
@@ -238,15 +222,18 @@ function enable2fa (args) {
238222
}
239223
}
240224

241-
return Bluebird.try(() => {
225+
return BB.try(() => {
242226
// if they're using legacy auth currently then we have to update them to a
243227
// bearer token before continuing.
244-
if (conf.auth.basic) {
228+
const auth = getAuth(conf)
229+
if (auth.basic) {
245230
log.info('profile', 'Updating authentication to bearer token')
246-
return profile.login(conf.auth.basic.username, conf.auth.basic.password, conf).then((result) => {
231+
return profile.createToken(
232+
auth.basic.password, false, [], conf
233+
).then((result) => {
247234
if (!result.token) throw new Error('Your registry ' + conf.registry + 'does not seem to support bearer tokens. Bearer tokens are required for two-factor authentication')
248235
npm.config.setCredentialsByURI(conf.registry, {token: result.token})
249-
return Bluebird.fromNode((cb) => npm.config.save('user', cb))
236+
return BB.fromNode((cb) => npm.config.save('user', cb))
250237
})
251238
}
252239
}).then(() => {
@@ -295,18 +282,36 @@ function enable2fa (args) {
295282
})
296283
}
297284

285+
function getAuth (conf) {
286+
const creds = npm.config.getCredentialsByURI(conf.registry)
287+
let auth
288+
if (creds.token) {
289+
auth = {token: creds.token}
290+
} else if (creds.username) {
291+
auth = {basic: {username: creds.username, password: creds.password}}
292+
} else if (creds.auth) {
293+
const basic = Buffer.from(creds.auth, 'base64').toString().split(':', 2)
294+
auth = {basic: {username: basic[0], password: basic[1]}}
295+
} else {
296+
auth = {}
297+
}
298+
299+
if (conf.otp) auth.otp = conf.otp
300+
return auth
301+
}
302+
298303
function disable2fa (args) {
299-
const conf = config()
304+
let conf = ProfileOpts(npmConfig())
300305
return pulseTillDone.withPromise(profile.get(conf)).then((info) => {
301306
if (!info.tfa || info.tfa.pending) {
302307
output('Two factor authentication not enabled.')
303308
return
304309
}
305310
return readUserInfo.password().then((password) => {
306-
return Bluebird.try(() => {
307-
if (conf.auth.otp) return
311+
return BB.try(() => {
312+
if (conf.otp) return
308313
return readUserInfo.otp('Enter one-time password from your authenticator: ').then((otp) => {
309-
conf.auth.otp = otp
314+
conf = conf.concat({otp})
310315
})
311316
}).then(() => {
312317
log.info('profile', 'disabling tfa')

0 commit comments

Comments
 (0)