UNPKG

1.59 kBJavaScriptView Raw
1var assert = require("assert")
2
3var toNerfDart = require("./nerf-dart.js")
4
5module.exports = getCredentialsByURI
6
7function getCredentialsByURI (uri) {
8 assert(uri && typeof uri === "string", "registry URL is required")
9 var nerfed = toNerfDart(uri)
10 var defnerf = toNerfDart(this.get("registry"))
11
12 var c = {scope : nerfed}
13
14 if (this.get(nerfed + ":_authToken")) {
15 c.token = this.get(nerfed + ":_authToken")
16 // the bearer token is enough, don't confuse things
17 return c
18 }
19
20 // Handle the old-style _auth=<base64> style for the default
21 // registry, if set.
22 //
23 // XXX(isaacs): Remove when npm 1.4 is no longer relevant
24 var authDef = this.get("_auth")
25 var userDef = this.get("username")
26 var passDef = this.get("_password")
27 if (authDef && !(userDef && passDef)) {
28 authDef = new Buffer(authDef, "base64").toString()
29 authDef = authDef.split(":")
30 userDef = authDef.shift()
31 passDef = authDef.join(":")
32 }
33
34 if (this.get(nerfed + ":_password")) {
35 c.password = new Buffer(this.get(nerfed + ":_password"), "base64").toString("utf8")
36 } else if (nerfed === defnerf && passDef) {
37 c.password = passDef
38 }
39
40 if (this.get(nerfed + ":username")) {
41 c.username = this.get(nerfed + ":username")
42 } else if (nerfed === defnerf && userDef) {
43 c.username = userDef
44 }
45
46 if (this.get(nerfed + ":email")) {
47 c.email = this.get(nerfed + ":email")
48 } else if (this.get("email")) {
49 c.email = this.get("email")
50 }
51
52 if (c.username && c.password) {
53 c.auth = new Buffer(c.username + ":" + c.password).toString("base64")
54 }
55
56 return c
57}