]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/lib/adduser.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / lib / adduser.js
1 module.exports = adduser
2
3 var log = require("npmlog")
4   , npm = require("./npm.js")
5   , read = require("read")
6   , userValidate = require("npm-user-validate")
7   , crypto
8
9 try {
10   crypto = require("crypto")
11 } catch (ex) {}
12
13 adduser.usage = 'npm adduser [--registry=url] [--scope=@orgname] [--always-auth]' +
14                 '\n\naliases: login'
15
16 function adduser (args, cb) {
17   npm.spinner.stop()
18   if (!crypto) return cb(new Error(
19     "You must compile node with ssl support to use the adduser feature"))
20
21   var creds = npm.config.getCredentialsByURI(npm.config.get("registry"))
22   var c = { u : creds.username || ""
23           , p : creds.password || ""
24           , e : creds.email || ""
25           }
26     , u = {}
27     , fns = [readUsername, readPassword, readEmail, save]
28
29   loop()
30   function loop (er) {
31     if (er) return cb(er)
32     var fn = fns.shift()
33     if (fn) return fn(c, u, loop)
34     cb()
35   }
36 }
37
38 function readUsername (c, u, cb) {
39   var v = userValidate.username
40   read({prompt: "Username: ", default: c.u || ""}, function (er, un) {
41     if (er) {
42       return cb(er.message === "cancelled" ? er.message : er)
43     }
44
45     // make sure it's valid.  we have to do this here, because
46     // couchdb will only ever say "bad password" with a 401 when
47     // you try to PUT a _users record that the validate_doc_update
48     // rejects for *any* reason.
49
50     if (!un) {
51       return readUsername(c, u, cb)
52     }
53
54     var error = v(un)
55     if (error) {
56       log.warn(error.message)
57       return readUsername(c, u, cb)
58     }
59
60     c.changed = c.u !== un
61     u.u = un
62     cb(er)
63   })
64 }
65
66 function readPassword (c, u, cb) {
67   var v = userValidate.pw
68
69   var prompt
70   if (c.p && !c.changed) {
71     prompt = "Password: (or leave unchanged) "
72   } else {
73     prompt = "Password: "
74   }
75
76   read({prompt: prompt, silent: true}, function (er, pw) {
77     if (er) {
78       return cb(er.message === "cancelled" ? er.message : er)
79     }
80
81     if (!c.changed && pw === "") {
82       // when the username was not changed,
83       // empty response means "use the old value"
84       pw = c.p
85     }
86
87     if (!pw) {
88       return readPassword(c, u, cb)
89     }
90
91     var error = v(pw)
92     if (error) {
93       log.warn(error.message)
94       return readPassword(c, u, cb)
95     }
96
97     c.changed = c.changed || c.p !== pw
98     u.p = pw
99     cb(er)
100   })
101 }
102
103 function readEmail (c, u, cb) {
104   var v = userValidate.email
105   var r = { prompt: "Email: (this IS public) ", default: c.e || "" }
106   read(r, function (er, em) {
107     if (er) {
108       return cb(er.message === "cancelled" ? er.message : er)
109     }
110
111     if (!em) {
112       return readEmail(c, u, cb)
113     }
114
115     var error = v(em)
116     if (error) {
117       log.warn(error.message)
118       return readEmail(c, u, cb)
119     }
120
121     u.e = em
122     cb(er)
123   })
124 }
125
126 function save (c, u, cb) {
127   npm.spinner.start()
128
129   // save existing configs, but yank off for this PUT
130   var uri   = npm.config.get("registry")
131   var scope = npm.config.get("scope")
132
133   // there may be a saved scope and no --registry (for login)
134   if (scope) {
135     if (scope.charAt(0) !== "@") scope = "@" + scope
136
137     var scopedRegistry = npm.config.get(scope + ":registry")
138     var cliRegistry = npm.config.get("registry", "cli")
139     if (scopedRegistry && !cliRegistry) uri = scopedRegistry
140   }
141
142   var params = {
143     auth : {
144       username : u.u,
145       password : u.p,
146       email    : u.e
147     }
148   }
149   npm.registry.adduser(uri, params, function (er, doc) {
150     npm.spinner.stop()
151     if (er) return cb(er)
152
153     // don't want this polluting the configuration
154     npm.config.del("_token", "user")
155
156     if (scope) npm.config.set(scope + ":registry", uri, "user")
157
158     if (doc && doc.token) {
159       npm.config.setCredentialsByURI(uri, {
160         token : doc.token
161       })
162     }
163     else {
164       npm.config.setCredentialsByURI(uri, {
165         username   : u.u,
166         password   : u.p,
167         email      : u.e,
168         alwaysAuth : npm.config.get("always-auth")
169       })
170     }
171
172     log.info('adduser', 'Authorized user %s', u.u)
173     var scopeMessage = scope ? ' to scope ' + scope : ''
174     console.log('Logged in as %s%s on %s.', u.u, scopeMessage, uri)
175     npm.config.save('user', cb)
176   })
177 }