1
1
'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
4
8
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' )
5
12
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' )
6
17
const qw = require ( 'qw' )
7
- const Table = require ( 'cli-table3' )
8
- const ansistyles = require ( 'ansistyles' )
9
- const Bluebird = require ( 'bluebird' )
10
18
const readUserInfo = require ( './utils/read-user-info.js' )
11
- const qrcodeTerminal = require ( 'qrcode-terminal ' )
19
+ const Table = require ( 'cli-table3 ' )
12
20
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
16
21
17
22
module . exports = profileCmd
18
23
@@ -48,6 +53,13 @@ function withCb (prom, cb) {
48
53
prom . then ( ( value ) => cb ( null , value ) , cb )
49
54
}
50
55
56
+ const ProfileOpts = figgyPudding ( {
57
+ json : { } ,
58
+ otp : { } ,
59
+ parseable : { } ,
60
+ registry : { }
61
+ } )
62
+
51
63
function profileCmd ( args , cb ) {
52
64
if ( args . length === 0 ) return cb ( new Error ( profileCmd . usage ) )
53
65
log . gauge . show ( 'profile' )
@@ -75,36 +87,13 @@ function profileCmd (args, cb) {
75
87
}
76
88
}
77
89
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
-
101
90
const knownProfileKeys = qw `
102
91
name email ${ 'two-factor auth' } fullname homepage
103
92
freenode twitter github created updated`
104
93
105
94
function get ( args ) {
106
95
const tfa = 'two-factor auth'
107
- const conf = config ( )
96
+ const conf = ProfileOpts ( npmConfig ( ) )
108
97
return pulseTillDone . withPromise ( profile . get ( conf ) ) . then ( ( info ) => {
109
98
if ( ! info . cidr_whitelist ) delete info . cidr_whitelist
110
99
if ( conf . json ) {
@@ -150,7 +139,7 @@ const writableProfileKeys = qw`
150
139
email password fullname homepage freenode twitter github`
151
140
152
141
function set ( args ) {
153
- const conf = config ( )
142
+ let conf = ProfileOpts ( npmConfig ( ) )
154
143
const prop = ( args [ 0 ] || '' ) . toLowerCase ( ) . trim ( )
155
144
let value = args . length > 1 ? args . slice ( 1 ) . join ( ' ' ) : null
156
145
if ( prop !== 'password' && value === null ) {
@@ -164,7 +153,7 @@ function set (args) {
164
153
if ( writableProfileKeys . indexOf ( prop ) === - 1 ) {
165
154
return Promise . reject ( Error ( `"${ prop } " is not a property we can set. Valid properties are: ` + writableProfileKeys . join ( ', ' ) ) )
166
155
}
167
- return Bluebird . try ( ( ) => {
156
+ return BB . try ( ( ) => {
168
157
if ( prop === 'password' ) {
169
158
return readUserInfo . password ( 'Current password: ' ) . then ( ( current ) => {
170
159
return readPasswords ( ) . then ( ( newpassword ) => {
@@ -193,23 +182,18 @@ function set (args) {
193
182
const newUser = { }
194
183
writableProfileKeys . forEach ( ( k ) => { newUser [ k ] = user [ k ] } )
195
184
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
+ }
201
196
} )
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
- } )
213
197
} ) )
214
198
} )
215
199
}
@@ -225,7 +209,7 @@ function enable2fa (args) {
225
209
' auth-only - Require two-factor authentication only when logging in\n' +
226
210
' auth-and-writes - Require two-factor authentication when logging in AND when publishing' ) )
227
211
}
228
- const conf = config ( )
212
+ const conf = ProfileOpts ( npmConfig ( ) )
229
213
if ( conf . json || conf . parseable ) {
230
214
return Promise . reject ( new Error (
231
215
'Enabling two-factor authentication is an interactive operation and ' +
@@ -238,15 +222,18 @@ function enable2fa (args) {
238
222
}
239
223
}
240
224
241
- return Bluebird . try ( ( ) => {
225
+ return BB . try ( ( ) => {
242
226
// if they're using legacy auth currently then we have to update them to a
243
227
// bearer token before continuing.
244
- if ( conf . auth . basic ) {
228
+ const auth = getAuth ( conf )
229
+ if ( auth . basic ) {
245
230
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 ) => {
247
234
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' )
248
235
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 ) )
250
237
} )
251
238
}
252
239
} ) . then ( ( ) => {
@@ -295,18 +282,36 @@ function enable2fa (args) {
295
282
} )
296
283
}
297
284
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
+
298
303
function disable2fa ( args ) {
299
- const conf = config ( )
304
+ let conf = ProfileOpts ( npmConfig ( ) )
300
305
return pulseTillDone . withPromise ( profile . get ( conf ) ) . then ( ( info ) => {
301
306
if ( ! info . tfa || info . tfa . pending ) {
302
307
output ( 'Two factor authentication not enabled.' )
303
308
return
304
309
}
305
310
return readUserInfo . password ( ) . then ( ( password ) => {
306
- return Bluebird . try ( ( ) => {
307
- if ( conf . auth . otp ) return
311
+ return BB . try ( ( ) => {
312
+ if ( conf . otp ) return
308
313
return readUserInfo . otp ( 'Enter one-time password from your authenticator: ' ) . then ( ( otp ) => {
309
- conf . auth . otp = otp
314
+ conf = conf . concat ( { otp} )
310
315
} )
311
316
} ) . then ( ( ) => {
312
317
log . info ( 'profile' , 'disabling tfa' )
0 commit comments