Permalink
Browse files

Initial commit.

0 parents commit a8696ea6edab918f5cdb27d55cad927c467b1783 @jaredhanson committed Nov 4, 2011
@@ -0,0 +1,2 @@
+.DS_Store
+node_modules
@@ -0,0 +1,8 @@
+*.md
+.DS_Store
+.git*
+Makefile
+docs/
+examples/
+support/
+test/
20 LICENSE
@@ -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-google/*.js
+ dox \
+ --title Passport-Google \
+ --desc "Google authentication strategy for Passport" \
+ $(shell find lib/passport-google/* -type f) > $@
+
+docclean:
+ rm -f docs/*.{1,html}
+
+.PHONY: test docs docclean
@@ -0,0 +1,31 @@
+# Passport-Google
+
+[Passport](https://github.com/jaredhanson/passport) strategy for authenticating
+with [Google](http://www.google.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,118 @@
+var express = require('express')
+ , passport = require('passport')
+ , util = require('util')
+ , GoogleStrategy = require('passport-google').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 Google profile is serialized
+// and deserialized.
+passport.serializeUser(function(user, done) {
+ done(null, user);
+});
+
+passport.deserializeUser(function(obj, done) {
+ done(null, obj);
+});
+
+
+// Use the GoogleStrategy 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 GoogleStrategy({
+ returnURL: 'http://localhost:3000/auth/google/return',
+ realm: 'http://localhost:3000/'
+ },
+ function(identifier, profile, done) {
+ // asynchronous verification, for effect...
+ process.nextTick(function () {
+
+ // To keep the example simple, the user's Google profile is returned to
+ // represent the logged-in user. In a typical application, you would want
+ // to associate the Google 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/google
+// Use passport.authenticate() as route middleware to authenticate the
+// request. The first step in Google authentication will involve redirecting
+// the user to google.com. After authenticating, Google will redirect the
+// user back to this application at /auth/google/return
+app.get('/auth/google',
+ passport.authenticate('google', { failureRedirect: '/login' }),
+ function(req, res) {
+ res.redirect('/');
+ });
+
+// GET /auth/google/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/google/return',
+ passport.authenticate('google', { 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-google-examples-signon",
+ "version": "0.0.0",
+ "dependencies": {
+ "express": ">= 0.0.0",
+ "ejs": ">= 0.0.0",
+ "passport": ">= 0.0.0",
+ "passport-google": ">= 0.0.0"
+ }
+}
@@ -0,0 +1,3 @@
+<p>ID: <%= user.identifier %></p>
+<p>Name: <%= user.displayName %></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-OpenID 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/google">Sign On with Google</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,26 @@
+/**
+ * Module dependencies.
+ */
+var util = require('util')
+ , OpenIDStrategy = require('passport-openid').Strategy;
+
+
+function Strategy(options, validate) {
+ options = options || {};
+ options.providerURL = options.providerURL || 'https://www.google.com/accounts/o8/id';
+ options.profile = (options.profile === undefined) ? true : options.profile;
+
+ OpenIDStrategy.call(this, options, validate);
+ this.name = 'google';
+}
+
+/**
+ * Inherit from `OpenIDStrategy`.
+ */
+util.inherits(Strategy, OpenIDStrategy);
+
+
+/**
+ * Expose `Strategy`.
+ */
+module.exports = Strategy;
@@ -0,0 +1,16 @@
+{
+ "name": "passport-google",
+ "version": "0.1.0",
+ "description": "Google authentication strategy for Passport.",
+ "author": "Jared Hanson <jaredhanson@gmail.com> (http://www.jaredhanson.net/)",
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/jaredhanson/passport-google.git"
+ },
+ "main": "./lib/passport-google",
+ "dependencies": {
+ "passport-openid": ">= 0.1.1"
+ },
+ "engines": { "node": ">= 0.4.0" },
+ "keywords": ["passport", "google", "auth", "authn", "authentication", "identity"]
+}

0 comments on commit a8696ea

Please sign in to comment.