From aeee052d7906833a23e1cbdb8a29fd318f704d01 Mon Sep 17 00:00:00 2001 From: Tom Ashworth Date: Mon, 17 Jun 2013 16:54:46 +0100 Subject: [PATCH] Add github buttons and routes. --- lib/app.js | 31 ++++++++++++++++++++++++++++++ lib/handlers/session.js | 35 ++++++++++++++++++++++++++++++++++ lib/routes.js | 4 ++++ package.json | 4 +++- public/css/style.css | 37 ++++++++++++++++++++++++++++++++++++ public/images/github-32.png | Bin 0 -> 1714 bytes views/index.html | 12 ++++++++++++ 7 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 public/images/github-32.png diff --git a/lib/app.js b/lib/app.js index 2b3d3a79..18214da1 100644 --- a/lib/app.js +++ b/lib/app.js @@ -14,6 +14,34 @@ var nodemailer = require('nodemailer'), url = require('url'), flattened; +var passport = require('passport'), + GitHubStrategy = require('passport-github').Strategy; + +/** + * Passport configuration + */ + +passport.serializeUser(function(user, done) { + done(null, user); +}); + +passport.deserializeUser(function(obj, done) { + done(null, obj); +}); + +passport.use(new GitHubStrategy({ + clientID: options.github.id, + clientSecret: options.github.secret + }, + function(accessToken, refreshToken, profile, done) { + console.log.apply(console, [].slice.call(arguments)); + return done(null, { + access_token: accessToken, + profile: profile + }); + } +)); + app.store = store; app.mailer = (function (mail) { var mailTransport = null, @@ -147,6 +175,9 @@ app.connect = function (callback) { app.use(middleware.limitContentLength({limit: app.set('max-request-size')})); app.use(express.cookieParser(app.set('session secret'))); app.use(express.cookieSession({key: 'jsbin', cookie: {maxAge: 365 * 24 * 60 * 60 * 1000}})); + // Passport + app.use(passport.initialize()); + app.use(passport.session()); app.use(express.urlencoded()); app.use(express.json()); app.use(middleware.csrf({ ignore: ['/', /^\/api\//] })); diff --git a/lib/handlers/session.js b/lib/handlers/session.js index d1c1c393..3de6c8ae 100644 --- a/lib/handlers/session.js +++ b/lib/handlers/session.js @@ -2,6 +2,9 @@ var utils = require('../utils'), errors = require('../errors'), Observable = utils.Observable; +var passport = require('passport'), + GitHubStrategy = require('passport-github').Strategy; + module.exports = Observable.extend({ constructor: function SessionHandler(sandbox) { Observable.apply(this, arguments); @@ -354,5 +357,37 @@ module.exports = Observable.extend({ } return url; + }, + + /** + * Github Auth + */ + + github: passport.authenticate('github'), + + githubPassportCallback: passport.authenticate('github', { failureRedirect: '/' }), + + githubCallback: function(req, res, next) { + var githubUser = req.user; + this.models.user.load(req.user.profile.username, function (err, user) { + if (err) return next(err); + + // if user but current user not signed in, don't allow them to connect github + // if user and the current user is signed in then link the accounts + // if no user then this is a new user, create them an account + // this.models.user.create({ + // name: githubUser.profile.username, + // }, function (err, id) {}); + console.log(); + console.log(); + console.log.apply(console, [].slice.call(arguments)); + console.log(); + console.log(); + console.log(req.session.user); + console.log(); + console.log(); + res.redirect('/'); + }.bind(this)); } + }); diff --git a/lib/routes.js b/lib/routes.js index e1bc5fa7..071379b5 100644 --- a/lib/routes.js +++ b/lib/routes.js @@ -76,6 +76,10 @@ module.exports = function (app) { app.get('/forgot', sessionHandler.requestToken); app.get('/reset', sessionHandler.resetPassword); + // GitHub auth + app.get('/auth/github', sessionHandler.github); + app.get('/auth/github/callback', sessionHandler.githubPassportCallback, sessionHandler.githubCallback); + // List app.get('/list/:user', binHandler.getUserBins); app.get('/list', binHandler.getUserBins); diff --git a/package.json b/package.json index e987f63c..6b81183f 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,9 @@ "async": "~0.1.22", "lynx": "0.0.11", "file-db": "0.0.2", - "underscore": "~1.4.4" + "underscore": "~1.4.4", + "passport": "~0.1.17", + "passport-github": "~0.1.5" }, "devDependencies": { "grunt": "~0.4.1", diff --git a/public/css/style.css b/public/css/style.css index 3161f2db..970d84c9 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -54,6 +54,43 @@ textarea:focus { outline: none; } +.btn-github { + font-size: 1em; + background: #f0f0f0; + background: linear-gradient(0deg, #f0f0f0, #fefefe); + box-shadow: 0 1px 2px #eee; + border: 1px solid #aaa; + border-radius: 4px; + padding: 6px 10px 5px; + cursor: pointer; + display: inline; + -moz-box-sizing: border-box; + box-sizing: border-box; + text-align: center; + text-decoration: none; +} +.btn-github:hover { + background: linear-gradient(0deg, #e8e8e8, #f8f8f8); +} + +.btn-github img { + vertical-align: middle; + top: -1px; + position: relative; + margin-right: 5px; +} + +.login .btn-github { + display: block; + width: 100%; +} + +.login-splitter { + display: block; + font-style: italic; + text-align: center; +} + [hidden], .hidden { display: none !important; diff --git a/public/images/github-32.png b/public/images/github-32.png new file mode 100644 index 0000000000000000000000000000000000000000..8b25551a97921681334176ee143b41510a117d86 GIT binary patch literal 1714 zcmaJ?X;2eq7*4oFu!ne{XxAht2qc?8LXr|_LPCfTpaBK7K$c{I0Ld=NLIOeuC;@2) zZ$K%a)k+m-s0>xHmKxL%0V&0TRzzznhgyqrIC$F)0{WwLXLrBvd*^wc_uSc%h%m9E z{W5z3f#4_!7RvAyFh6!S_*<8qJ%KOIm?#E|L=rJQq=gB5C6WLG5;c?r%V0>EmEH#X z5eSwPRa6WXBMs#$5H%GtW2go-in9p>zW@UYDNNWc^XOXZQ? z1QjEV00I#$3^1wQUJ8&-2UsjB-G|9y(LDhMNN3PM{APL4eYi{(m*ERcUnJa{R+-3^ z34^A6;U^v`8N*O6ji%S@sd{fJqD`XFIUJ5zgTe5^5nj414F(y!G&=H(f)Lgzv?>%+ zAsWD}2qhpH7>|TU`X&W6IxDNuO_vET7|j5oG&&VDr!)hUO8+0KR?nh!m<)a!?|%yG zqOwq!CWCcIhE{<$E|F|@g>nP6FoYr6C<8>D?ID9%&5J(4oSbR1I^byW*g@__U z4QsF&uJSEcFeleM3~ChjEQGbHOjsGDMbyAl(p=Ttv9RaVo8~I#js@@Y9C^_2U})yn zzSHU%6FxuY?d;&65MyR({^lU*3$z$ZllDb(o&<7d;A_`h2U+3~BJ2Hv`{W}KEU801#cv_B|9Cm!ynR{S`AMsSn z;7E=B;mb!wx$L;S>yGXG^6=&WlQn9$s?&L%Y1D8TI^MlKB1DqsEng$>f4=xYWBoPI z_S1p!sJ#d2?YI4kPA{k}Eby?F=f-J9zIc`YDl^pzjVm~9ebE?Hn?t0Nx+la|D0MB; z9)2xv1G>a1|A9kQ>~DV<=X3-4yC&n!m8-3K#P z{X@0zRuQsy$+N ziSCoLJU{Z$nQy4A4Y5UJ07$5FA~qL2%Q+cLaqDU?Lz3?=BC5;Nk6BbTmmceEaM>-Z zi>O&-dSE=%ex;vcvCOk{*JQ5^_4M z4lW7%l9IqY(z7pV(?I@@8=KPFO82)O{VDI18-*d-k$YmI^XiuPs_LuFw<^ZcD}yP5 c*NrbeloN*74g`U%%F6r~k%+>C^#XapzmV0H-2eap literal 0 HcmV?d00001 diff --git a/views/index.html b/views/index.html index 00dcc352..13f5fe8b 100644 --- a/views/index.html +++ b/views/index.html @@ -96,6 +96,10 @@ Logout +
@@ -138,6 +142,10 @@ Reset password +
@@ -162,6 +170,10 @@
+