Skip to content

Commit

Permalink
Refactor OAuth2 to use Passport.js (GoogleCloudPlatform#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Apr 12, 2016
1 parent da601f7 commit d8adabf
Show file tree
Hide file tree
Showing 38 changed files with 1,429 additions and 690 deletions.
2 changes: 1 addition & 1 deletion 1-hello-world/test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'use strict';

var assert = require('assert');
var config = require('../config');
var config = require('./config');
var request = require('supertest');
var utils = require('../../test/utils');

Expand Down
2 changes: 1 addition & 1 deletion 1-hello-world/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

'use strict';

var config = require('../config');
var config = require('./config');
var utils = require('../../test/utils');

describe(config.test + '/', function () {
Expand Down
1 change: 1 addition & 0 deletions 3-binary-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"mongodb": "^2.1.15",
"multer": "^1.1.0",
"mysql": "^2.10.2",
"nconf": "^0.8.4",
"prompt": "^1.0.0"
},
"devDependencies": {
Expand Down
27 changes: 20 additions & 7 deletions 4-auth/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

var path = require('path');
var express = require('express');
var session = require('cookie-session');
var session = require('express-session');
var MemcachedStore = require('connect-memcached')(session);
var passport = require('passport');
var config = require('./config');

var app = express();
Expand All @@ -25,18 +27,29 @@ app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.set('trust proxy', true);

// Configure the session and session storage.
// MemoryStore isn't viable in a multi-server configuration, so we
// use encrypted cookies. Redis or Memcache is a great option for
// more secure sessions, if desired.
// [START session]
app.use(session({
// Configure the session and session storage.
var sessionConfig = {
resave: false,
saveUninitialized: false,
secret: config.get('SECRET'),
signed: true
}));
};

// In production use the App Engine Memcache instance to store session data,
// otherwise fallback to the default MemoryStore in development.
if (config.get('NODE_ENV') === 'production') {
sessionConfig.store = new MemcachedStore({
hosts: [config.get('MEMCACHE_URL')]
});
}

app.use(session(sessionConfig));
// [END session]

// OAuth2
app.use(passport.initialize());
app.use(passport.session());
app.use(require('./lib/oauth2').router);

// Books
Expand Down
9 changes: 4 additions & 5 deletions 4-auth/books/crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var router = express.Router();

// Use the oauth middleware to automatically get the user's profile
// information and expose login/logout URLs to templates.
router.use(oauth2.aware);
router.use(oauth2.template);

// Set Content-Type for all responses for these routes
Expand Down Expand Up @@ -54,7 +53,7 @@ router.get('/', function list (req, res, next) {
// can access this handler.
router.get('/mine', oauth2.required, function list (req, res, next) {
model.listBy(
req.session.profile.id,
req.user.id,
10,
req.query.pageToken,
function (err, entities, cursor, apiResponse) {
Expand Down Expand Up @@ -96,9 +95,9 @@ router.post(
var data = req.body;

// If the user is logged in, set them as the creator of the book.
if (req.session.profile) {
data.createdBy = req.session.profile.displayName;
data.createdById = req.session.profile.id;
if (req.user) {
data.createdBy = req.user.displayName;
data.createdById = req.user.id;
} else {
data.createdBy = 'Anonymous';
}
Expand Down
5 changes: 4 additions & 1 deletion 4-auth/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ nconf
// This is the id of your project in the Google Cloud Developers Console.
GCLOUD_PROJECT: '',

// Connection url for the Memcache instance used to store session data
MEMCACHE_URL: '127.0.0.1:11211',

// MongoDB connection string
// https://docs.mongodb.org/manual/reference/connection-string/
MONGO_URL: 'mongodb://localhost:27017',
Expand All @@ -64,7 +67,7 @@ nconf

OAUTH2_CLIENT_ID: '',
OAUTH2_CLIENT_SECRET: '',
OAUTH2_CALLBACK: 'http://localhost:8080/oauth2callback',
OAUTH2_CALLBACK: 'http://localhost:8080/auth/google/callback',

// Port the HTTP server
PORT: 8080,
Expand Down
Loading

0 comments on commit d8adabf

Please sign in to comment.