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

Commit

Permalink
logout: add new command
Browse files Browse the repository at this point in the history
Make it do something useful on both bearer-based and basic-based authed
clients.
  • Loading branch information
othiym23 committed Feb 13, 2015
1 parent d0790dc commit 38c4825
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
50 changes: 50 additions & 0 deletions doc/cli/npm-logout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
npm-logout(1) -- Log out of the registry
========================================

## SYNOPSIS

npm logout [--registry=url] [--scope=@orgname]

## DESCRIPTION

When logged into a registry that supports token-based authentication, tell the
server to end this token's session. This will invalidate the token everywhere
you're using it, not just for the current environment.

When logged into a legacy registry that uses username and password authentication, this will
clear the credentials in your user configuration. In this case, it will _only_ affect
the current environment.

If `--scope` is provided, this will find the credentials for the registry
connected to that scope, if set.

## CONFIGURATION

### registry

Default: http://registry.npmjs.org/

The base URL of the npm package registry. If `scope` is also specified,
it takes precedence.

### scope

Default: none

If specified, the user and login credentials given will be associated
with the specified scope. See `npm-scope(7)`. You can use both at the same time,
e.g.

npm adduser --registry=http://myregistry.example.com --scope=@myco

This will set a registry for the given scope and login or create a user for
that registry at the same time.

## SEE ALSO

* npm-adduser(1)
* npm-registry(7)
* npm-config(1)
* npm-config(7)
* npmrc(5)
* npm-whoami(1)
40 changes: 40 additions & 0 deletions lib/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = logout

var dezalgo = require("dezalgo")
var log = require("npmlog")

var npm = require("./npm.js")
var mapToRegistry = require("./utils/map-to-registry.js")

logout.usage = "npm logout [--registry] [--scope]"

function logout (args, cb) {
npm.spinner.start()
cb = dezalgo(cb)

mapToRegistry("/", npm.config, function (err, uri, auth, normalized) {
if (err) return cb(err)

if (auth.token) {
log.verbose("logout", "clearing session token for", normalized)
npm.registry.logout(normalized, { auth: auth }, function (err) {
if (err) return cb(err)

npm.config.clearCredentialsByURI(normalized)
npm.spinner.stop()
npm.config.save("user", cb)
})
}
else if (auth.username || auth.password) {
log.verbose("logout", "clearing user credentials for", normalized)
npm.config.clearCredentialsByURI(normalized)
npm.spinner.stop()
npm.config.save("user", cb)
}
else {
cb(new Error(
"Not logged in to", normalized + ",", "so can't log out."
))
}
})
}
1 change: 1 addition & 0 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ var commandCache = {}
, "stars"
, "tag"
, "adduser"
, "logout"
, "unpublish"
, "owner"
, "access"
Expand Down
67 changes: 67 additions & 0 deletions test/tap/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
var fs = require("fs")
var path = require("path")

var mkdirp = require("mkdirp")
var mr = require("npm-registry-mock")
var rimraf = require("rimraf")
var test = require("tap").test

var common = require("../common-tap.js")

var pkg = path.resolve(__dirname, "logout")
var outfile = path.join(pkg, "_npmrc")
var opts = { cwd: pkg }

var contents = function () {/*
foo=boo
//localhost:1337/:_authToken=glarb
*/}.toString().split("\n").slice(1, -1).join("\n")

function mocks (server) {
server.delete("/-/user/token/glarb")
.reply(200, {})
}

test("setup", function (t) {
cleanup()
setup()
t.end()
})

test("npm logout", function (t) {
mr({ port: common.port, plugin: mocks }, function (err, s) {
if (err) throw err

common.npm(
[
"logout",
"--registry", common.registry,
"--loglevel", "silent",
"--userconfig", outfile
],
opts,
function (err, code) {
t.ifError(err, "no error output")
t.notOk(code, "exited OK")

var config = fs.readFileSync(outfile, "utf8")
t.equal(config, "foo=boo\n", "creds gone")
s.close()
t.end()
})
})
})

test("cleanup", function (t) {
cleanup()
t.end()
})

function setup () {
mkdirp.sync(pkg)
fs.writeFileSync(outfile, contents)
}

function cleanup () {
rimraf.sync(pkg)
}

0 comments on commit 38c4825

Please sign in to comment.