├── .gitignore ├── .jshintrc ├── .npmignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── examples └── basic │ ├── app.js │ └── package.json ├── lib ├── index.js └── strategy.js ├── package.json ├── support └── mk │ ├── coveralls.mk │ ├── istanbul.mk │ ├── jshint.mk │ ├── mocha.mk │ ├── node.mk │ └── notes.mk └── test ├── bootstrap └── node.js ├── package.test.js └── strategy.test.js /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | reports/ 3 | 4 | # Node.js 5 | node_modules/ 6 | npm-debug.log 7 | 8 | # Mac OS X 9 | .DS_Store 10 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "bitwise": true, 4 | "camelcase": true, 5 | "curly": true, 6 | "forin": true, 7 | "immed": true, 8 | "latedef": true, 9 | "newcap": true, 10 | "noarg": true, 11 | "noempty": true, 12 | "nonew": true, 13 | "quotmark": "single", 14 | "undef": true, 15 | "unused": true, 16 | "trailing": true, 17 | "laxcomma": true 18 | } 19 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | Makefile 2 | README.md 3 | build/ 4 | docs/ 5 | examples/ 6 | reports/ 7 | support/ 8 | test/ 9 | 10 | # Node.js 11 | .npmignore 12 | node_modules/ 13 | npm-debug.log 14 | 15 | # Mac OS X 16 | .DS_Store 17 | 18 | # Git 19 | .git* 20 | 21 | # Utilities 22 | .jshintrc 23 | .travis.yml 24 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: "node_js" 2 | node_js: 3 | - "0.10" 4 | - "0.8" 5 | # - "0.6" 6 | - "0.4" 7 | 8 | before_install: 9 | - "npm install istanbul -g" 10 | - "npm install coveralls -g" 11 | 12 | script: "make ci-travis" 13 | 14 | after_success: 15 | - "make submit-cov-to-coveralls" 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2012-2014 Jared Hanson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | SOURCES ?= lib/*.js 2 | TESTS ?= test/*.test.js 3 | 4 | test: test-mocha 5 | test-cov: test-istanbul-mocha 6 | view-cov: view-istanbul-report 7 | lint: lint-jshint 8 | lint-tests: lint-tests-jshint 9 | 10 | 11 | # ============================================================================== 12 | # Node.js 13 | # ============================================================================== 14 | include support/mk/node.mk 15 | include support/mk/mocha.mk 16 | include support/mk/istanbul.mk 17 | 18 | # ============================================================================== 19 | # Analysis 20 | # ============================================================================== 21 | include support/mk/notes.mk 22 | include support/mk/jshint.mk 23 | 24 | # ============================================================================== 25 | # Reports 26 | # ============================================================================== 27 | include support/mk/coveralls.mk 28 | 29 | # ============================================================================== 30 | # Continuous Integration 31 | # ============================================================================== 32 | submit-cov-to-coveralls: submit-istanbul-lcov-to-coveralls 33 | 34 | # Travis CI 35 | ci-travis: test test-cov 36 | 37 | # ============================================================================== 38 | # Clean 39 | # ============================================================================== 40 | clean: 41 | rm -rf build 42 | rm -rf reports 43 | 44 | clobber: clean clobber-node 45 | 46 | 47 | .PHONY: test test-cov view-cov lint lint-tests submit-cov-to-coveralls ci-travis clean clobber 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # passport-anonymous 2 | 3 | [![Build](https://travis-ci.org/jaredhanson/passport-anonymous.png)](https://travis-ci.org/jaredhanson/passport-anonymous) 4 | [![Coverage](https://coveralls.io/repos/jaredhanson/passport-anonymous/badge.png)](https://coveralls.io/r/jaredhanson/passport-anonymous) 5 | [![Quality](https://codeclimate.com/github/jaredhanson/passport-anonymous.png)](https://codeclimate.com/github/jaredhanson/passport-anonymous) 6 | [![Dependencies](https://david-dm.org/jaredhanson/passport-anonymous.png)](https://david-dm.org/jaredhanson/passport-anonymous) 7 | [![Tips](http://img.shields.io/gittip/jaredhanson.png)](https://www.gittip.com/jaredhanson/) 8 | 9 | 10 | [Passport](http://passportjs.org/) strategy for anonymous authentication. 11 | 12 | This module lets you provide anonymous authentication in your Node.js 13 | applications. By plugging into Passport, anonymous authentication can be easily 14 | and unobtrusively integrated into any application or framework that supports 15 | [Connect](http://www.senchalabs.org/connect/)-style middleware, including 16 | [Express](http://expressjs.com/). 17 | 18 | ## Install 19 | 20 | $ npm install passport-anonymous 21 | 22 | ## Usage 23 | 24 | #### Configure Strategy 25 | 26 | The anonymous authentication strategy passes authentication for a request, 27 | with `req.user` remaining `undefined`. 28 | 29 | passport.use(new AnonymousStrategy()); 30 | 31 | #### Authenticate Requests 32 | 33 | Use `passport.authenticate()`, specifying the `'anonymous'` strategy, to 34 | pass authentication of a request. This is typically used alongside a strategy 35 | that verifies credentials, as a fallback for routes that prefer authentication 36 | but can also respond to unauthenticated requests. 37 | 38 | For example, as route middleware in an [Express](http://expressjs.com/) 39 | application: 40 | 41 | app.post('/hello', 42 | passport.authenticate(['basic', 'anonymous'], { session: false }), 43 | function(req, res) { 44 | if (req.user) { 45 | res.json({ name: req.user.username }); 46 | } else { 47 | res.json({ name: 'anonymous' }); 48 | } 49 | }); 50 | 51 | ## Examples 52 | 53 | For a complete, working example, refer to the [login example](https://github.com/jaredhanson/passport-anonymous/tree/master/examples/basic). 54 | 55 | ## Tests 56 | 57 | $ npm install 58 | $ npm test 59 | 60 | ## Credits 61 | 62 | - [Jared Hanson](http://github.com/jaredhanson) 63 | 64 | ## License 65 | 66 | [The MIT License](http://opensource.org/licenses/MIT) 67 | 68 | Copyright (c) 2012-2013 Jared Hanson <[http://jaredhanson.net/](http://jaredhanson.net/)> 69 | 70 | Sponsor 71 | -------------------------------------------------------------------------------- /examples/basic/app.js: -------------------------------------------------------------------------------- 1 | var express = require('express') 2 | , passport = require('passport') 3 | , util = require('util') 4 | , BasicStrategy = require('passport-http').BasicStrategy 5 | , AnonymousStrategy = require('passport-anonymous').Strategy; 6 | 7 | 8 | var users = [ 9 | { id: 1, username: 'bob', password: 'secret', email: 'bob@example.com' } 10 | , { id: 2, username: 'joe', password: 'birthday', email: 'joe@example.com' } 11 | ]; 12 | 13 | function findByUsername(username, fn) { 14 | for (var i = 0, len = users.length; i < len; i++) { 15 | var user = users[i]; 16 | if (user.username === username) { 17 | return fn(null, user); 18 | } 19 | } 20 | return fn(null, null); 21 | } 22 | 23 | 24 | // Use the BasicStrategy within Passport. 25 | // Strategies in Passport require a `verify` function, which accept 26 | // credentials (in this case, a username and password), and invoke a callback 27 | // with a user object. 28 | passport.use(new BasicStrategy({ 29 | }, 30 | function(username, password, done) { 31 | // asynchronous verification, for effect... 32 | process.nextTick(function () { 33 | 34 | // Find the user by username. If there is no user with the given 35 | // username, or the password is not correct, set the user to `false` to 36 | // indicate failure. Otherwise, return the authenticated `user`. 37 | findByUsername(username, function(err, user) { 38 | if (err) { return done(err); } 39 | if (!user) { return done(null, false); } 40 | if (user.password != password) { return done(null, false); } 41 | return done(null, user); 42 | }) 43 | }); 44 | } 45 | )); 46 | 47 | // Use the BasicStrategy within Passport. 48 | // This is used as a fallback in requests that prefer authentication, but 49 | // support unauthenticated clients. 50 | passport.use(new AnonymousStrategy()); 51 | 52 | 53 | 54 | var app = express.createServer(); 55 | 56 | // configure Express 57 | app.configure(function() { 58 | app.use(express.logger()); 59 | // Initialize Passport! Note: no need to use session middleware when each 60 | // request carries authentication credentials, as is the case with HTTP Basic. 61 | app.use(passport.initialize()); 62 | app.use(app.router); 63 | app.use(express.static(__dirname + '/public')); 64 | }); 65 | 66 | 67 | // curl -v -I http://127.0.0.1:3000/ 68 | // curl -v -I --user bob:secret http://127.0.0.1:3000/ 69 | app.get('/', 70 | // Authenticate using HTTP Basic credentials, with session support disabled, 71 | // and allow anonymous requests. 72 | passport.authenticate(['basic', 'anonymous'], { session: false }), 73 | function(req, res){ 74 | if (req.user) { 75 | res.json({ username: req.user.username, email: req.user.email }); 76 | } else { 77 | res.json({ anonymous: true }); 78 | } 79 | }); 80 | 81 | app.listen(3000); 82 | -------------------------------------------------------------------------------- /examples/basic/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "passport-anonymous-example", 3 | "version": "0.0.0", 4 | "dependencies": { 5 | "express": ">= 2.x.x", 6 | "passport": ">= 0.0.0", 7 | "passport-http": ">= 0.0.0", 8 | "passport-anonymous": ">= 0.0.0" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies. 3 | */ 4 | var Strategy = require('./strategy'); 5 | 6 | 7 | /** 8 | * Expose `Strategy` directly from package. 9 | */ 10 | exports = module.exports = Strategy; 11 | 12 | /** 13 | * Export constructors. 14 | */ 15 | exports.Strategy = Strategy; 16 | -------------------------------------------------------------------------------- /lib/strategy.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Module dependencies. 3 | */ 4 | var passport = require('passport-strategy') 5 | , util = require('util'); 6 | 7 | 8 | /** 9 | * Creates an instance of `Strategy`. 10 | * 11 | * The anonymous authentication strategy passes authentication without verifying 12 | * credentials. 13 | * 14 | * Applications typically use this as a fallback on endpoints that can respond 15 | * to both authenticated and unauthenticated requests. If credentials are not 16 | * supplied, this stategy passes authentication while leaving `req.user` set to 17 | * `undefined`, allowing the route to handle unauthenticated requests as 18 | * desired. 19 | * 20 | * Examples: 21 | * 22 | * passport.use(new AnonymousStrategy()); 23 | * 24 | * @constructor 25 | * @api public 26 | */ 27 | function Strategy() { 28 | passport.Strategy.call(this); 29 | this.name = 'anonymous'; 30 | } 31 | 32 | /** 33 | * Inherit from `passport.Strategy`. 34 | */ 35 | util.inherits(Strategy, passport.Strategy); 36 | 37 | /** 38 | * Pass authentication without verifying credentials. 39 | * 40 | * @param {Object} req 41 | * @api protected 42 | */ 43 | Strategy.prototype.authenticate = function() { 44 | this.pass(); 45 | }; 46 | 47 | 48 | /** 49 | * Expose `Strategy`. 50 | */ 51 | module.exports = Strategy; 52 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "passport-anonymous", 3 | "version": "1.0.1", 4 | "description": "Anonymous authentication strategy for Passport.", 5 | "keywords": [ 6 | "passport", 7 | "authn", 8 | "authentication", 9 | "anonymous" 10 | ], 11 | "author": { 12 | "name": "Jared Hanson", 13 | "email": "jaredhanson@gmail.com", 14 | "url": "http://www.jaredhanson.net/" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git://github.com/jaredhanson/passport-anonymous.git" 19 | }, 20 | "bugs": { 21 | "url": "http://github.com/jaredhanson/passport-anonymous/issues" 22 | }, 23 | "licenses": [ 24 | { 25 | "type": "MIT", 26 | "url": "http://www.opensource.org/licenses/MIT" 27 | } 28 | ], 29 | "main": "./lib", 30 | "dependencies": { 31 | "passport-strategy": "1.x.x" 32 | }, 33 | "devDependencies": { 34 | "mocha": "1.x.x", 35 | "chai": "1.x.x", 36 | "chai-passport-strategy": "0.1.x" 37 | }, 38 | "engines": { 39 | "node": ">= 0.4.0" 40 | }, 41 | "scripts": { 42 | "test": "node_modules/.bin/mocha --reporter spec --require test/bootstrap/node test/*.test.js" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /support/mk/coveralls.mk: -------------------------------------------------------------------------------- 1 | COVERALLS ?= coveralls 2 | 3 | submit-istanbul-lcov-to-coveralls: 4 | cat $(ISTANBUL_LCOV_INFO_PATH) | $(COVERALLS) 5 | 6 | 7 | .PHONY: submit-istanbul-lcov-to-coveralls 8 | -------------------------------------------------------------------------------- /support/mk/istanbul.mk: -------------------------------------------------------------------------------- 1 | ISTANBUL ?= istanbul 2 | ISTANBUL_OUT ?= ./reports/coverage 3 | ISTANBUL_REPORT ?= lcov 4 | ISTANBUL_LCOV_INFO_PATH ?= $(ISTANBUL_OUT)/lcov.info 5 | ISTANBUL_HTML_REPORT_PATH ?= $(ISTANBUL_OUT)/lcov-report/index.html 6 | 7 | 8 | test-istanbul-mocha: node_modules 9 | NODE_PATH=$(NODE_PATH_TEST) \ 10 | $(ISTANBUL) cover \ 11 | --dir $(ISTANBUL_OUT) --report $(ISTANBUL_REPORT) \ 12 | $(_MOCHA) -- \ 13 | --reporter $(MOCHA_REPORTER) \ 14 | --require $(MOCHA_REQUIRE) \ 15 | $(TESTS) 16 | 17 | view-istanbul-report: 18 | open $(ISTANBUL_HTML_REPORT_PATH) 19 | 20 | 21 | .PHONY: test-istanbul-mocha view-istanbul-report 22 | -------------------------------------------------------------------------------- /support/mk/jshint.mk: -------------------------------------------------------------------------------- 1 | JSHINT ?= jshint 2 | 3 | lint-jshint: 4 | $(JSHINT) $(SOURCES) 5 | 6 | lint-tests-jshint: 7 | $(JSHINT) $(TESTS) 8 | 9 | 10 | .PHONY: lint-jshint lint-tests-jshint 11 | -------------------------------------------------------------------------------- /support/mk/mocha.mk: -------------------------------------------------------------------------------- 1 | MOCHA ?= ./node_modules/.bin/mocha 2 | _MOCHA ?= ./node_modules/.bin/_mocha 3 | MOCHA_REPORTER ?= spec 4 | MOCHA_REQUIRE ?= ./test/bootstrap/node 5 | 6 | test-mocha: node_modules 7 | NODE_PATH=$(NODE_PATH_TEST) \ 8 | $(MOCHA) \ 9 | --reporter $(MOCHA_REPORTER) \ 10 | --require $(MOCHA_REQUIRE) \ 11 | $(TESTS) 12 | 13 | 14 | .PHONY: test-mocha 15 | -------------------------------------------------------------------------------- /support/mk/node.mk: -------------------------------------------------------------------------------- 1 | node_modules: 2 | npm install 3 | 4 | clobber-node: 5 | rm -rf node_modules 6 | 7 | 8 | .PHONY: clobber-node 9 | -------------------------------------------------------------------------------- /support/mk/notes.mk: -------------------------------------------------------------------------------- 1 | NOTES ?= 'TODO|FIXME' 2 | 3 | notes: 4 | grep -Ern $(NOTES) $(SOURCES) $(TESTS) 5 | 6 | 7 | .PHONY: notes 8 | -------------------------------------------------------------------------------- /test/bootstrap/node.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | 3 | chai.use(require('chai-passport-strategy')); 4 | 5 | global.expect = chai.expect; 6 | -------------------------------------------------------------------------------- /test/package.test.js: -------------------------------------------------------------------------------- 1 | /* global describe, it, expect */ 2 | 3 | var strategy = require('..'); 4 | 5 | describe('passport-anonymous', function() { 6 | 7 | it('should export Strategy constructor directly from package', function() { 8 | expect(strategy).to.be.a('function'); 9 | expect(strategy).to.equal(strategy.Strategy); 10 | }); 11 | 12 | it('should export Strategy constructor', function() { 13 | expect(strategy.Strategy).to.be.a('function'); 14 | }); 15 | 16 | }); 17 | -------------------------------------------------------------------------------- /test/strategy.test.js: -------------------------------------------------------------------------------- 1 | /* global describe, it, expect, before */ 2 | /* jshint expr: true */ 3 | 4 | var chai = require('chai') 5 | , Strategy = require('../lib/strategy'); 6 | 7 | 8 | describe('Strategy', function() { 9 | 10 | var strategy = new Strategy(function(){}); 11 | 12 | it('should be named anonymous', function() { 13 | expect(strategy.name).to.equal('anonymous'); 14 | }); 15 | 16 | describe('handling a request', function() { 17 | var ok, request; 18 | 19 | before(function(done) { 20 | chai.passport(strategy) 21 | .pass(function() { 22 | ok = true; 23 | done(); 24 | }) 25 | .req(function(req) { 26 | request = req; 27 | }) 28 | .authenticate(); 29 | }); 30 | 31 | it('should call pass', function() { 32 | expect(ok).to.be.true; 33 | }); 34 | 35 | it('should leave req.user undefined', function() { 36 | expect(request.user).to.be.undefined; 37 | }); 38 | }); 39 | 40 | }); 41 | --------------------------------------------------------------------------------