set-credentials-by-uri.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var assert = require('assert')
  2. var toNerfDart = require('./nerf-dart.js')
  3. module.exports = setCredentialsByURI
  4. function setCredentialsByURI (uri, c) {
  5. assert(uri && typeof uri === 'string', 'registry URL is required')
  6. assert(c && typeof c === 'object', 'credentials are required')
  7. var nerfed = toNerfDart(uri)
  8. if (c.token) {
  9. this.set(nerfed + ':_authToken', c.token, 'user')
  10. this.del(nerfed + ':_password', 'user')
  11. this.del(nerfed + ':username', 'user')
  12. this.del(nerfed + ':email', 'user')
  13. this.del(nerfed + ':always-auth', 'user')
  14. } else if (c.username || c.password || c.email) {
  15. assert(c.username, 'must include username')
  16. assert(c.password, 'must include password')
  17. assert(c.email, 'must include email address')
  18. this.del(nerfed + ':_authToken', 'user')
  19. var encoded = Buffer.from(c.password, 'utf8').toString('base64')
  20. this.set(nerfed + ':_password', encoded, 'user')
  21. this.set(nerfed + ':username', c.username, 'user')
  22. this.set(nerfed + ':email', c.email, 'user')
  23. if (c.alwaysAuth !== undefined) {
  24. this.set(nerfed + ':always-auth', c.alwaysAuth, 'user')
  25. } else {
  26. this.del(nerfed + ':always-auth', 'user')
  27. }
  28. } else {
  29. throw new Error('No credentials to set.')
  30. }
  31. }