Permalink
Browse files

get scoped credentials in publish

  • Loading branch information...
1 parent 0a5893b commit 27c46678eb1453f99fb0a0102d41ae99f72389aa @othiym23 othiym23 committed Jun 20, 2014
Showing with 124 additions and 32 deletions.
  1. +11 −3 lib/publish.js
  2. +2 −10 lib/request.js
  3. +21 −0 lib/util/nerf-dart.js
  4. +9 −5 test/lib/common.js
  5. +60 −0 test/publish-scoped-auth-token.js
  6. +21 −14 test/publish-scoped.js
View
@@ -7,14 +7,22 @@ var url = require("url")
, fs = require("fs")
, fixNameField = require("normalize-package-data/lib/fixer.js").fixNameField
+var toNerfDart = require("./util/nerf-dart.js")
+
function escaped(name) {
return name.replace("/", "%2f")
}
function publish (uri, data, tarball, cb) {
- var email = this.conf.get('email')
- var auth = this.conf.get('_auth')
- var username = this.conf.get('username')
+ var email = this.conf.get('email') ||
+ this.conf.get(toNerfDart(uri) + ':email')
+ var auth = this.conf.get('_auth') ||
+ this.conf.get(toNerfDart(uri) + ':_auth')
+ var username
+ if (auth) {
+ var creds = new Buffer(auth, "base64").toString("utf8")
+ if (creds) username = creds.split(":")[0]
+ }
if (!email || !auth || !username) {
var er = new Error("auth and email required for publishing")
View
@@ -8,8 +8,9 @@ var url = require("url")
, request = require("request")
, retry = require("retry")
, crypto = require("crypto")
- , pkg = require("../package.json")
+var pkg = require("../package.json")
+ , toNerfDart = require("./util/nerf-dart.js")
// npm: means
// 1. https
@@ -328,12 +329,3 @@ function requestDone (method, where, cb) {
return cb(er, parsed, data, response)
}.bind(this)
}
-
-function toNerfDart(uri) {
- var parsed = url.parse(uri)
- parsed.pathname = "/"
- delete parsed.protocol
- delete parsed.auth
-
- return url.format(parsed)
-}
View
@@ -0,0 +1,21 @@
+var url = require("url")
+
+module.exports = toNerfDart
+
+/**
+ * Maps a URL to an identifier.
+ *
+ * Name courtesy schiffertronix media LLC, a New Jersey corporation
+ *
+ * @param {String} uri The URL to be nerfed.
+ *
+ * @returns {String} A nerfed URL.
+ */
+function toNerfDart(uri) {
+ var parsed = url.parse(uri)
+ parsed.pathname = "/"
+ delete parsed.protocol
+ delete parsed.auth
+
+ return url.format(parsed)
+}
View
@@ -1,16 +1,20 @@
var resolve = require("path").resolve
-var server = require('./server.js')
-var RC = require('../../')
+var server = require("./server.js")
+var RC = require("../../")
+
+var REGISTRY = "http://localhost:" + server.port
module.exports = {
+ port : server.port,
+ registry : REGISTRY,
freshClient : function freshClient(config) {
config = config || {}
- config.cache = resolve(__dirname, '../fixtures/cache')
- config.registry = 'http://localhost:' + server.port
+ config.cache = resolve(__dirname, "../fixtures/cache")
+ config.registry = REGISTRY
var client = new RC(config)
server.log = client.log
- client.log.level = 'silent'
+ client.log.level = "silent"
return client
}
@@ -0,0 +1,60 @@
+var tap = require("tap")
+var crypto = require("crypto")
+var fs = require("fs")
+
+var toNerfDart = require("../lib/util/nerf-dart.js")
+var server = require("./lib/server.js")
+var common = require("./lib/common.js")
+
+var configuration = {"always-auth" : true}
+
+var authKey = toNerfDart(common.registry) + ":_auth"
+configuration[authKey] = new Buffer("username:password").toString("base64")
+
+var emailKey = toNerfDart(common.registry) + ":email"
+configuration[emailKey] = "ogd@aoaioxxysz.net"
+
+var tokenKey = toNerfDart(common.registry) + ":_authToken"
+configuration[tokenKey] = "of-glad-tidings"
+
+var client = common.freshClient(configuration)
+
+tap.test("publish", function (t) {
+ // not really a tarball, but doesn't matter
+ var tarball = require.resolve("../package.json")
+ var pd = fs.readFileSync(tarball, "base64")
+ var pkg = require("../package.json")
+ pkg.name = "@npm/npm-registry-client"
+
+ server.expect("/@npm%2fnpm-registry-client", function (req, res) {
+ t.equal(req.method, "PUT")
+ t.equal(req.headers.authorization, "Bearer of-glad-tidings")
+
+ var b = ""
+ req.setEncoding("utf8")
+ req.on("data", function (d) {
+ b += d
+ })
+
+ req.on("end", function () {
+ var o = JSON.parse(b)
+ t.equal(o._id, "@npm/npm-registry-client")
+ t.equal(o["dist-tags"].latest, pkg.version)
+ t.has(o.versions[pkg.version], pkg)
+ t.same(o.maintainers, [ { name: "username", email: "ogd@aoaioxxysz.net" } ])
+ t.same(o.maintainers, o.versions[pkg.version].maintainers)
+ var att = o._attachments[ pkg.name + "-" + pkg.version + ".tgz" ]
+ t.same(att.data, pd)
+ var hash = crypto.createHash("sha1").update(pd, "base64").digest("hex")
+ t.equal(o.versions[pkg.version].dist.shasum, hash)
+ res.statusCode = 201
+ res.json({created:true})
+ })
+ })
+
+ client.publish(common.registry, pkg, tarball, function (er, data) {
+ if (er) throw er
+ t.deepEqual(data, { created: true })
+ t.end()
+ })
+})
View
@@ -2,20 +2,32 @@ var tap = require("tap")
var crypto = require("crypto")
var fs = require("fs")
+var toNerfDart = require("../lib/util/nerf-dart.js")
var server = require("./lib/server.js")
var common = require("./lib/common.js")
-var client = common.freshClient({
- username: "username",
- password: "password",
- email: "i@izs.me",
- _auth: new Buffer("username:password").toString("base64"),
- "always-auth": true
-})
+var configuration = {"always-auth" : true}
+
+var authKey = toNerfDart(common.registry) + ":_auth"
+var _auth = new Buffer("username:password").toString("base64")
+configuration[authKey] = _auth
+
+var emailKey = toNerfDart(common.registry) + ":email"
+configuration[emailKey] = "ogd@aoaioxxysz.net"
+
+var client = common.freshClient(configuration)
tap.test("publish", function (t) {
+ // not really a tarball, but doesn't matter
+ var tarball = require.resolve("../package.json")
+ var pd = fs.readFileSync(tarball, "base64")
+ var pkg = require("../package.json")
+ pkg.name = "@npm/npm-registry-client"
+
server.expect("/@npm%2fnpm-registry-client", function (req, res) {
t.equal(req.method, "PUT")
+ t.equal(req.headers.authorization, "Basic " + _auth)
+
var b = ""
req.setEncoding("utf8")
req.on("data", function (d) {
@@ -27,7 +39,7 @@ tap.test("publish", function (t) {
t.equal(o._id, "@npm/npm-registry-client")
t.equal(o["dist-tags"].latest, pkg.version)
t.has(o.versions[pkg.version], pkg)
- t.same(o.maintainers, [ { name: "username", email: "i@izs.me" } ])
+ t.same(o.maintainers, [ { name: "username", email: "ogd@aoaioxxysz.net" } ])
t.same(o.maintainers, o.versions[pkg.version].maintainers)
var att = o._attachments[ pkg.name + "-" + pkg.version + ".tgz" ]
t.same(att.data, pd)
@@ -38,12 +50,7 @@ tap.test("publish", function (t) {
})
})
- // not really a tarball, but doesn't matter
- var tarball = require.resolve("../package.json")
- var pd = fs.readFileSync(tarball, "base64")
- var pkg = require("../package.json")
- pkg.name = '@npm/npm-registry-client'
- client.publish("http://localhost:1337/", pkg, tarball, function (er, data) {
+ client.publish(common.registry, pkg, tarball, function (er, data) {
if (er) throw er
t.deepEqual(data, { created: true })
t.end()

0 comments on commit 27c4667

Please sign in to comment.