Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 5623e0afecc5ee58a59d28074035048f5876796a @jaredhanson committed Dec 1, 2011
@@ -0,0 +1,2 @@
+.DS_Store
+node_modules
@@ -0,0 +1,8 @@
+*.md
+.DS_Store
+.git*
+Makefile
+docs/
+examples/
+support/
+test/
@@ -0,0 +1,20 @@
+(The MIT License)
+
+Copyright (c) 2011 Jared Hanson
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,19 @@
+NODE = node
+TEST = vows
+TESTS ?= test/*-test.js
+
+test:
+ @NODE_ENV=test NODE_PATH=lib $(TEST) $(TEST_FLAGS) $(TESTS)
+
+docs: docs/api.html
+
+docs/api.html: lib/passport-yahoo/*.js
+ dox \
+ --title Passport-Yahoo \
+ --desc "Yahoo authentication strategy for Passport" \
+ $(shell find lib/passport-yahoo/* -type f) > $@
+
+docclean:
+ rm -f docs/*.{1,html}
+
+.PHONY: test docs docclean
@@ -0,0 +1,31 @@
+# Passport-Yahoo
+
+[Passport](https://github.com/jaredhanson/passport) strategy for authenticating
+with [Yahoo](http://www.yahoo.com/) using OpenID 2.0.
+
+## Credits
+
+ - [Jared Hanson](http://github.com/jaredhanson)
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2011 Jared Hanson
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,121 @@
+var express = require('express')
+ , passport = require('passport')
+ , util = require('util')
+ , YahooStrategy = require('passport-yahoo').Strategy;
+
+
+// Passport session setup.
+// To support persistent login sessions, Passport needs to be able to
+// serialize users into and deserialize users out of the session. Typically,
+// this will be as simple as storing the user ID when serializing, and finding
+// the user by ID when deserializing. However, since this example does not
+// have a database of user records, the complete Yahoo profile is serialized
+// and deserialized.
+passport.serializeUser(function(user, done) {
+ done(null, user);
+});
+
+passport.deserializeUser(function(obj, done) {
+ done(null, obj);
+});
+
+
+// Use the YahooStrategy within Passport.
+// Strategies in passport require a `validate` function, which accept
+// credentials (in this case, an OpenID identifier and profile), and invoke a
+// callback with a user object.
+passport.use(new YahooStrategy({
+ returnURL: 'http://localhost:3000/auth/yahoo/return',
+ realm: 'http://localhost:3000/'
+ },
+ function(identifier, profile, done) {
+ console.log('YAHOO ID: ' + identifier);
+ console.log('YAHOO PROFILE: ' + util.inspect(profile));
+
+ // asynchronous verification, for effect...
+ process.nextTick(function () {
+
+ // To keep the example simple, the user's Yahoo profile is returned to
+ // represent the logged-in user. In a typical application, you would want
+ // to associate the Yahoo account with a user record in your database,
+ // and return that user instead.
+ profile.identifier = identifier;
+ return done(null, profile);
+ });
+ }
+));
+
+
+
+
+var app = express.createServer();
+
+// configure Express
+app.configure(function() {
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'ejs');
+ app.use(express.logger());
+ app.use(express.cookieParser());
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(express.session({ secret: 'keyboard cat' }));
+ // Initialize Passport! Also use passport.session() middleware, to support
+ // persistent login sessions (recommended).
+ app.use(passport.initialize());
+ app.use(passport.session());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/../../public'));
+});
+
+
+app.get('/', function(req, res){
+ res.render('index', { user: req.user });
+});
+
+app.get('/account', ensureAuthenticated, function(req, res){
+ res.render('account', { user: req.user });
+});
+
+app.get('/login', function(req, res){
+ res.render('login', { user: req.user });
+});
+
+// GET /auth/yahoo
+// Use passport.authenticate() as route middleware to authenticate the
+// request. The first step in Yahoo authentication will involve redirecting
+// the user to yahoo.com. After authenticating, Yahoo will redirect the
+// user back to this application at /auth/yahoo/return
+app.get('/auth/yahoo',
+ passport.authenticate('yahoo', { failureRedirect: '/login' }),
+ function(req, res) {
+ res.redirect('/');
+ });
+
+// GET /auth/yahoo/return
+// Use passport.authenticate() as route middleware to authenticate the
+// request. If authentication fails, the user will be redirected back to the
+// login page. Otherwise, the primary route function function will be called,
+// which, in this example, will redirect the user to the home page.
+app.get('/auth/yahoo/return',
+ passport.authenticate('yahoo', { failureRedirect: '/login' }),
+ function(req, res) {
+ res.redirect('/');
+ });
+
+app.get('/logout', function(req, res){
+ req.logout();
+ res.redirect('/');
+});
+
+app.listen(3000);
+
+
+// Simple route middleware to ensure user is authenticated.
+// Use this route middleware on any resource that needs to be protected. If
+// the request is authenticated (typically via a persistent login session),
+// the request will proceed. Otherwise, the user will be redirected to the
+// login page.
+function ensureAuthenticated(req, res, next) {
+ if (req.isAuthenticated()) { return next(); }
+ res.redirect('/login')
+}
@@ -0,0 +1,10 @@
+{
+ "name": "passport-yahoo-examples-signon",
+ "version": "0.0.0",
+ "dependencies": {
+ "express": ">= 0.0.0",
+ "ejs": ">= 0.0.0",
+ "passport": ">= 0.0.0",
+ "passport-yahoo": ">= 0.0.0"
+ }
+}
@@ -0,0 +1,2 @@
+<p>ID: <%= user.identifier %></p>
+<p>Email: <%= user.emails[0].value %></p>
@@ -0,0 +1,5 @@
+<% if (!user) { %>
+ <h2>Welcome! Please log in.</h2>
+<% } else { %>
+ <h2>Hello, <%= user.identifier %>.</h2>
+<% } %>
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Passport-Yahoo Example</title>
+ </head>
+ <body>
+ <% if (!user) { %>
+ <p>
+ <a href="/">Home</a> |
+ <a href="/login">Log In</a>
+ </p>
+ <% } else { %>
+ <p>
+ <a href="/">Home</a> |
+ <a href="/account">Account</a> |
+ <a href="/logout">Log Out</a>
+ </p>
+ <% } %>
+ <%- body %>
+ </body>
+</html>
@@ -0,0 +1 @@
+<a href="/auth/yahoo">Sign On with Yahoo</a>
@@ -0,0 +1,15 @@
+/**
+ * Module dependencies.
+ */
+var Strategy = require('./strategy');
+
+
+/**
+ * Framework version.
+ */
+exports.version = '0.1.0';
+
+/**
+ * Expose constructors.
+ */
+exports.Strategy = Strategy;
@@ -0,0 +1,59 @@
+/**
+ * Module dependencies.
+ */
+var util = require('util')
+ , OpenIDStrategy = require('passport-openid').Strategy;
+
+
+/**
+ * `Strategy` constructor.
+ *
+ * The Yahoo authentication strategy authenticates requests by delegating to
+ * Yahoo using the OpenID 2.0 protocol.
+ *
+ * Applications must supply a `validate` callback which accepts an `identifier`,
+ * and optionally a service-specific `profile`, and then calls the `done`
+ * callback supplying a `user`, which should be set to `false` if the
+ * credentials are not valid. If an exception occured, `err` should be set.
+ *
+ * Options:
+ * - `returnURL` URL to which Yahoo will redirect the user after authentication
+ * - `realm` the part of URL-space for which an OpenID authentication request is valid
+ * - `profile` enable profile exchange, defaults to _true_
+ *
+ * Examples:
+ *
+ * passport.use(new YahooStrategy({
+ * returnURL: 'http://localhost:3000/auth/yahoo/return',
+ * realm: 'http://localhost:3000/'
+ * },
+ * function(identifier, profile, done) {
+ * User.findByOpenID(identifier, function (err, user) {
+ * done(err, user);
+ * });
+ * }
+ * ));
+ *
+ * @param {Object} options
+ * @param {Function} verify
+ * @api public
+ */
+function Strategy(options, validate) {
+ options = options || {};
+ options.providerURL = options.providerURL || 'http://open.login.yahooapis.com/openid20/www.yahoo.com/xrds';
+ options.profile = (options.profile === undefined) ? true : options.profile;
+
+ OpenIDStrategy.call(this, options, validate);
+ this.name = 'yahoo';
+}
+
+/**
+ * Inherit from `OpenIDStrategy`.
+ */
+util.inherits(Strategy, OpenIDStrategy);
+
+
+/**
+ * Expose `Strategy`.
+ */
+module.exports = Strategy;
@@ -0,0 +1,16 @@
+{
+ "name": "passport-yahoo",
+ "version": "0.1.0",
+ "description": "Yahoo authentication strategy for Passport.",
+ "author": "Jared Hanson <jaredhanson@gmail.com> (http://www.jaredhanson.net/)",
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/jaredhanson/passport-yahoo.git"
+ },
+ "main": "./lib/passport-yahoo",
+ "dependencies": {
+ "passport-openid": ">= 0.1.1"
+ },
+ "engines": { "node": ">= 0.4.0" },
+ "keywords": ["passport", "yahoo", "auth", "authn", "authentication", "identity"]
+}
@@ -0,0 +1,15 @@
+var vows = require('vows');
+var assert = require('assert');
+var util = require('util');
+var yahoo = require('passport-yahoo');
+
+
+vows.describe('passport-yahoo').addBatch({
+
+ 'module': {
+ 'should report a version': function (x) {
+ assert.isString(yahoo.version);
+ },
+ },
+
+}).export(module);
@@ -0,0 +1,24 @@
+var vows = require('vows');
+var assert = require('assert');
+var util = require('util');
+var YahooStrategy = require('passport-yahoo/strategy');
+
+
+vows.describe('YahooStrategy').addBatch({
+
+ 'strategy': {
+ topic: function() {
+ return new YahooStrategy({ returnURL: 'https://www.example.com/auth/yahoo/return' },
+ function() {}
+ );
+ },
+
+ 'should be named yahoo': function (strategy) {
+ assert.equal(strategy.name, 'yahoo');
+ },
+ 'should have correct provider URL': function (strategy) {
+ assert.equal(strategy._providerURL, 'http://open.login.yahooapis.com/openid20/www.yahoo.com/xrds');
+ },
+ },
+
+}).export(module);

0 comments on commit 5623e0a

Please sign in to comment.