profile.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. 'use strict'
  2. const BB = require('bluebird')
  3. const ansistyles = require('ansistyles')
  4. const figgyPudding = require('figgy-pudding')
  5. const inspect = require('util').inspect
  6. const log = require('npmlog')
  7. const npm = require('./npm.js')
  8. const npmConfig = require('./config/figgy-config.js')
  9. const otplease = require('./utils/otplease.js')
  10. const output = require('./utils/output.js')
  11. const profile = require('libnpm/profile')
  12. const pulseTillDone = require('./utils/pulse-till-done.js')
  13. const qrcodeTerminal = require('qrcode-terminal')
  14. const queryString = require('query-string')
  15. const qw = require('qw')
  16. const readUserInfo = require('./utils/read-user-info.js')
  17. const Table = require('cli-table3')
  18. const url = require('url')
  19. module.exports = profileCmd
  20. profileCmd.usage =
  21. 'npm profile enable-2fa [auth-only|auth-and-writes]\n' +
  22. 'npm profile disable-2fa\n' +
  23. 'npm profile get [<key>]\n' +
  24. 'npm profile set <key> <value>'
  25. profileCmd.subcommands = qw`enable-2fa disable-2fa get set`
  26. profileCmd.completion = function (opts, cb) {
  27. var argv = opts.conf.argv.remain
  28. switch (argv[2]) {
  29. case 'enable-2fa':
  30. case 'enable-tfa':
  31. if (argv.length === 3) {
  32. return cb(null, qw`auth-and-writes auth-only`)
  33. } else {
  34. return cb(null, [])
  35. }
  36. case 'disable-2fa':
  37. case 'disable-tfa':
  38. case 'get':
  39. case 'set':
  40. return cb(null, [])
  41. default:
  42. return cb(new Error(argv[2] + ' not recognized'))
  43. }
  44. }
  45. function withCb (prom, cb) {
  46. prom.then((value) => cb(null, value), cb)
  47. }
  48. const ProfileOpts = figgyPudding({
  49. json: {},
  50. otp: {},
  51. parseable: {},
  52. registry: {}
  53. })
  54. function profileCmd (args, cb) {
  55. if (args.length === 0) return cb(new Error(profileCmd.usage))
  56. log.gauge.show('profile')
  57. switch (args[0]) {
  58. case 'enable-2fa':
  59. case 'enable-tfa':
  60. case 'enable2fa':
  61. case 'enabletfa':
  62. withCb(enable2fa(args.slice(1)), cb)
  63. break
  64. case 'disable-2fa':
  65. case 'disable-tfa':
  66. case 'disable2fa':
  67. case 'disabletfa':
  68. withCb(disable2fa(), cb)
  69. break
  70. case 'get':
  71. withCb(get(args.slice(1)), cb)
  72. break
  73. case 'set':
  74. withCb(set(args.slice(1)), cb)
  75. break
  76. default:
  77. cb(new Error('Unknown profile command: ' + args[0]))
  78. }
  79. }
  80. const knownProfileKeys = qw`
  81. name email ${'two-factor auth'} fullname homepage
  82. freenode twitter github created updated`
  83. function get (args) {
  84. const tfa = 'two-factor auth'
  85. const conf = ProfileOpts(npmConfig())
  86. return pulseTillDone.withPromise(profile.get(conf)).then((info) => {
  87. if (!info.cidr_whitelist) delete info.cidr_whitelist
  88. if (conf.json) {
  89. output(JSON.stringify(info, null, 2))
  90. return
  91. }
  92. const cleaned = {}
  93. knownProfileKeys.forEach((k) => { cleaned[k] = info[k] || '' })
  94. Object.keys(info).filter((k) => !(k in cleaned)).forEach((k) => { cleaned[k] = info[k] || '' })
  95. delete cleaned.tfa
  96. delete cleaned.email_verified
  97. cleaned['email'] += info.email_verified ? ' (verified)' : '(unverified)'
  98. if (info.tfa && !info.tfa.pending) {
  99. cleaned[tfa] = info.tfa.mode
  100. } else {
  101. cleaned[tfa] = 'disabled'
  102. }
  103. if (args.length) {
  104. const values = args // comma or space separated ↓
  105. .join(',').split(/,/).map((arg) => arg.trim()).filter((arg) => arg !== '')
  106. .map((arg) => cleaned[arg])
  107. .join('\t')
  108. output(values)
  109. } else {
  110. if (conf.parseable) {
  111. Object.keys(info).forEach((key) => {
  112. if (key === 'tfa') {
  113. output(`${key}\t${cleaned[tfa]}`)
  114. } else {
  115. output(`${key}\t${info[key]}`)
  116. }
  117. })
  118. } else {
  119. const table = new Table()
  120. Object.keys(cleaned).forEach((k) => table.push({[ansistyles.bright(k)]: cleaned[k]}))
  121. output(table.toString())
  122. }
  123. }
  124. })
  125. }
  126. const writableProfileKeys = qw`
  127. email password fullname homepage freenode twitter github`
  128. function set (args) {
  129. let conf = ProfileOpts(npmConfig())
  130. const prop = (args[0] || '').toLowerCase().trim()
  131. let value = args.length > 1 ? args.slice(1).join(' ') : null
  132. if (prop !== 'password' && value === null) {
  133. return Promise.reject(Error('npm profile set <prop> <value>'))
  134. }
  135. if (prop === 'password' && value !== null) {
  136. return Promise.reject(Error(
  137. 'npm profile set password\n' +
  138. 'Do not include your current or new passwords on the command line.'))
  139. }
  140. if (writableProfileKeys.indexOf(prop) === -1) {
  141. return Promise.reject(Error(`"${prop}" is not a property we can set. Valid properties are: ` + writableProfileKeys.join(', ')))
  142. }
  143. return BB.try(() => {
  144. if (prop === 'password') {
  145. return readUserInfo.password('Current password: ').then((current) => {
  146. return readPasswords().then((newpassword) => {
  147. value = {old: current, new: newpassword}
  148. })
  149. })
  150. } else if (prop === 'email') {
  151. return readUserInfo.password('Password: ').then((current) => {
  152. return {password: current, email: value}
  153. })
  154. }
  155. function readPasswords () {
  156. return readUserInfo.password('New password: ').then((password1) => {
  157. return readUserInfo.password(' Again: ').then((password2) => {
  158. if (password1 !== password2) {
  159. log.warn('profile', 'Passwords do not match, please try again.')
  160. return readPasswords()
  161. }
  162. return password1
  163. })
  164. })
  165. }
  166. }).then(() => {
  167. // FIXME: Work around to not clear everything other than what we're setting
  168. return pulseTillDone.withPromise(profile.get(conf).then((user) => {
  169. const newUser = {}
  170. writableProfileKeys.forEach((k) => { newUser[k] = user[k] })
  171. newUser[prop] = value
  172. return otplease(conf, conf => profile.set(newUser, conf))
  173. .then((result) => {
  174. if (conf.json) {
  175. output(JSON.stringify({[prop]: result[prop]}, null, 2))
  176. } else if (conf.parseable) {
  177. output(prop + '\t' + result[prop])
  178. } else if (result[prop] != null) {
  179. output('Set', prop, 'to', result[prop])
  180. } else {
  181. output('Set', prop)
  182. }
  183. })
  184. }))
  185. })
  186. }
  187. function enable2fa (args) {
  188. if (args.length > 1) {
  189. return Promise.reject(new Error('npm profile enable-2fa [auth-and-writes|auth-only]'))
  190. }
  191. const mode = args[0] || 'auth-and-writes'
  192. if (mode !== 'auth-only' && mode !== 'auth-and-writes') {
  193. return Promise.reject(new Error(`Invalid two-factor authentication mode "${mode}".\n` +
  194. 'Valid modes are:\n' +
  195. ' auth-only - Require two-factor authentication only when logging in\n' +
  196. ' auth-and-writes - Require two-factor authentication when logging in AND when publishing'))
  197. }
  198. const conf = ProfileOpts(npmConfig())
  199. if (conf.json || conf.parseable) {
  200. return Promise.reject(new Error(
  201. 'Enabling two-factor authentication is an interactive operation and ' +
  202. (conf.json ? 'JSON' : 'parseable') + ' output mode is not available'))
  203. }
  204. const info = {
  205. tfa: {
  206. mode: mode
  207. }
  208. }
  209. return BB.try(() => {
  210. // if they're using legacy auth currently then we have to update them to a
  211. // bearer token before continuing.
  212. const auth = getAuth(conf)
  213. if (auth.basic) {
  214. log.info('profile', 'Updating authentication to bearer token')
  215. return profile.createToken(
  216. auth.basic.password, false, [], conf
  217. ).then((result) => {
  218. 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')
  219. npm.config.setCredentialsByURI(conf.registry, {token: result.token})
  220. return BB.fromNode((cb) => npm.config.save('user', cb))
  221. })
  222. }
  223. }).then(() => {
  224. log.notice('profile', 'Enabling two factor authentication for ' + mode)
  225. return readUserInfo.password()
  226. }).then((password) => {
  227. info.tfa.password = password
  228. log.info('profile', 'Determine if tfa is pending')
  229. return pulseTillDone.withPromise(profile.get(conf)).then((info) => {
  230. if (!info.tfa) return
  231. if (info.tfa.pending) {
  232. log.info('profile', 'Resetting two-factor authentication')
  233. return pulseTillDone.withPromise(profile.set({tfa: {password, mode: 'disable'}}, conf))
  234. } else {
  235. if (conf.auth.otp) return
  236. return readUserInfo.otp('Enter one-time password from your authenticator app: ').then((otp) => {
  237. conf.auth.otp = otp
  238. })
  239. }
  240. })
  241. }).then(() => {
  242. log.info('profile', 'Setting two-factor authentication to ' + mode)
  243. return pulseTillDone.withPromise(profile.set(info, conf))
  244. }).then((challenge) => {
  245. if (challenge.tfa === null) {
  246. output('Two factor authentication mode changed to: ' + mode)
  247. return
  248. }
  249. if (typeof challenge.tfa !== 'string' || !/^otpauth:[/][/]/.test(challenge.tfa)) {
  250. throw new Error('Unknown error enabling two-factor authentication. Expected otpauth URL, got: ' + inspect(challenge.tfa))
  251. }
  252. const otpauth = url.parse(challenge.tfa)
  253. const opts = queryString.parse(otpauth.query)
  254. return qrcode(challenge.tfa).then((code) => {
  255. output('Scan into your authenticator app:\n' + code + '\n Or enter code:', opts.secret)
  256. }).then((code) => {
  257. return readUserInfo.otp('And an OTP code from your authenticator: ')
  258. }).then((otp1) => {
  259. log.info('profile', 'Finalizing two-factor authentication')
  260. return profile.set({tfa: [otp1]}, conf)
  261. }).then((result) => {
  262. output('2FA successfully enabled. Below are your recovery codes, please print these out.')
  263. output('You will need these to recover access to your account if you lose your authentication device.')
  264. result.tfa.forEach((c) => output('\t' + c))
  265. })
  266. })
  267. }
  268. function getAuth (conf) {
  269. const creds = npm.config.getCredentialsByURI(conf.registry)
  270. let auth
  271. if (creds.token) {
  272. auth = {token: creds.token}
  273. } else if (creds.username) {
  274. auth = {basic: {username: creds.username, password: creds.password}}
  275. } else if (creds.auth) {
  276. const basic = Buffer.from(creds.auth, 'base64').toString().split(':', 2)
  277. auth = {basic: {username: basic[0], password: basic[1]}}
  278. } else {
  279. auth = {}
  280. }
  281. if (conf.otp) auth.otp = conf.otp
  282. return auth
  283. }
  284. function disable2fa (args) {
  285. let conf = ProfileOpts(npmConfig())
  286. return pulseTillDone.withPromise(profile.get(conf)).then((info) => {
  287. if (!info.tfa || info.tfa.pending) {
  288. output('Two factor authentication not enabled.')
  289. return
  290. }
  291. return readUserInfo.password().then((password) => {
  292. return BB.try(() => {
  293. if (conf.otp) return
  294. return readUserInfo.otp('Enter one-time password from your authenticator: ').then((otp) => {
  295. conf = conf.concat({otp})
  296. })
  297. }).then(() => {
  298. log.info('profile', 'disabling tfa')
  299. return pulseTillDone.withPromise(profile.set({tfa: {password: password, mode: 'disable'}}, conf)).then(() => {
  300. if (conf.json) {
  301. output(JSON.stringify({tfa: false}, null, 2))
  302. } else if (conf.parseable) {
  303. output('tfa\tfalse')
  304. } else {
  305. output('Two factor authentication disabled.')
  306. }
  307. })
  308. })
  309. })
  310. })
  311. }
  312. function qrcode (url) {
  313. return new Promise((resolve) => qrcodeTerminal.generate(url, resolve))
  314. }