Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
get scoped credentials in publish
Browse files Browse the repository at this point in the history
  • Loading branch information
othiym23 committed Jul 2, 2014
1 parent 0a5893b commit 27c4667
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 32 deletions.
14 changes: 11 additions & 3 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 2 additions & 10 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
21 changes: 21 additions & 0 deletions lib/util/nerf-dart.js
Original file line number Diff line number Diff line change
@@ -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)
}
14 changes: 9 additions & 5 deletions test/lib/common.js
Original file line number Diff line number Diff line change
@@ -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
}
Expand Down
60 changes: 60 additions & 0 deletions test/publish-scoped-auth-token.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
35 changes: 21 additions & 14 deletions test/publish-scoped.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit 27c4667

Please sign in to comment.