From aa36ad9fbb64496a482eda155e5115c6452cc108 Mon Sep 17 00:00:00 2001 From: Karl Jakober Date: Wed, 5 Feb 2014 16:37:48 +0000 Subject: [PATCH] Adds Steam auth and API --- README.md | 7 +++++++ app.js | 6 ++++++ config/passport.js | 48 +++++++++++++++++++++++++++++++++++++++++++- controllers/api.js | 24 ++++++++++++++++++++++ models/User.js | 4 +++- package.json | 4 +++- views/api/index.jade | 4 ++++ views/api/steam.jade | 23 +++++++++++++++++++++ 8 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 views/api/steam.jade diff --git a/README.md b/README.md index 205f57b13fc..8849cd1a95b 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,13 @@ Obtaining API Keys - Click **✔Register** - Copy and paste *OAuth consumer key* and *OAuth consumer secret* keys into `config/secrets.js` +
+ + +- Go to http://steamcommunity.com/dev/apikey +- Once signed in, enter your domainm agree to terms, and click **Register** +- Copy and paste *key* into `config.secrets.js` + Project Structure ----------------- diff --git a/app.js b/app.js index 229f85f9d7f..a185a4ec12c 100755 --- a/app.js +++ b/app.js @@ -118,6 +118,8 @@ app.get('/api/aviary', apiController.getAviary); app.get('/api/paypal', apiController.getPayPal); app.get('/api/paypal/success', apiController.getPayPalSuccess); app.get('/api/paypal/cancel', apiController.getPayPalCancel); +app.get('/api/steam', passportConf.isAuthorized, passportConf.isAuthorized, apiController.getSteam); + /** * OAuth routes for sign-in. @@ -144,6 +146,10 @@ app.get('/auth/tumblr', passport.authorize('tumblr')); app.get('/auth/tumblr/callback', passport.authorize('tumblr', { failureRedirect: '/api' }), function(req, res) { res.redirect('/api/tumblr'); }); +app.get('/auth/steam', passport.authenticate('steam')); +app.get('/auth/steam/callback', passport.authenticate('steam', { failureRedirect: '/login' }), function(req, res) { + res.redirect('/api/steam'); +}); app.listen(app.get('port'), function() { console.log("✔ Express server listening on port %d in %s mode", app.get('port'), app.settings.env); diff --git a/config/passport.js b/config/passport.js index 518e89a3fa1..584eaa1aef9 100755 --- a/config/passport.js +++ b/config/passport.js @@ -6,6 +6,7 @@ var FacebookStrategy = require('passport-facebook').Strategy; var TwitterStrategy = require('passport-twitter').Strategy; var GitHubStrategy = require('passport-github').Strategy; var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy; +var SteamStrategy = require('passport-steam').Strategy; var User = require('../models/User'); var secrets = require('./secrets'); var _ = require('underscore'); @@ -37,7 +38,7 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, passw * Sign in with Facebook. */ -passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken, refreshToken, profile, done) { +passport.use(new FacebookStrategy(secrets.facebook, function(req, accessToken, refreshToken, profile, done) { if (req.user) { User.findOne({ $or: [{ facebook: profile.id }, { email: profile.email }] }, function(err, existingUser) { if (existingUser) { @@ -201,6 +202,47 @@ passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refre } })); +/** + * Sign in with Steam. + */ + +passport.use(new SteamStrategy(secrets.steam, function(req, identifier, profile, done) { + identifier = identifier.match('http://steamcommunity.com/openid/id/([0-9]{17,25})'); + var steam_id = identifier[1]; + if (req.user) { + User.findOne({ steam: steam_id }, function(err, existingUser) { + if (existingUser) { + req.flash('errors', { msg: 'There is already a Steam account that belongs to you. Sign in with that account or delete it, then link it with your current account.' }); + done(err); + } else { + User.findById(req.user.id, function(err, user) { + user.steam = steam_id; + user.tokens.push({ kind: 'steam', accessToken: steam_id }); + user.save(function(err) { + req.flash('info', { msg: 'Steam account has been linked.' }); + done(err, user); + }); + }); + } + }); + + } else { + User.findOne({ steam: steam_id }, function(err, existingUser) { + if (existingUser) return done(null, existingUser); + var user = new User(); + user.steam = steam_id; + user.tokens.push({ kind: 'steam', steam_id: steam_id }); + user.save(function(err) { + done(err, user); + }); + }); + } +})); + +/** + * Sign in with Tumblr. + */ + passport.use('tumblr', new OAuthStrategy({ requestTokenURL: 'http://www.tumblr.com/oauth/request_token', accessTokenURL: 'http://www.tumblr.com/oauth/access_token', @@ -220,6 +262,10 @@ passport.use('tumblr', new OAuthStrategy({ } )); +/** + * Sign in with Foursquare. + */ + passport.use('foursquare', new OAuth2Strategy({ authorizationURL: 'https://foursquare.com/oauth2/authorize', tokenURL: 'https://foursquare.com/oauth2/access_token', diff --git a/controllers/api.js b/controllers/api.js index a9db5a60453..c33837b36e0 100644 --- a/controllers/api.js +++ b/controllers/api.js @@ -12,6 +12,9 @@ var foursquare = require('node-foursquare')({ secrets: secrets.foursquare }); var Github = require('github-api'); var Twit = require('twit'); var paypal = require('paypal-rest-sdk'); +var steam = require('steam-web'); + + /** * GET /api @@ -331,4 +334,25 @@ exports.getPayPalCancel = function(req, res, next) { result: true, canceled: true }); +}; + +/** + * GET /api/steam + * Steam API example. + */ + +exports.getSteam = function(req, res) { + var S = new steam({ + apiKey: secrets.steam.apiKey + }); + + S.getPlayerSummaries({ + steamids: [ req.user.steam ], + callback: function(err, data) { + res.render('api/steam', { + title: 'Steam Web API', + players: data.response.players, + }); + } + }); }; \ No newline at end of file diff --git a/models/User.js b/models/User.js index 734b942f284..9ac347580e2 100644 --- a/models/User.js +++ b/models/User.js @@ -2,13 +2,15 @@ var mongoose = require('mongoose'); var bcrypt = require('bcrypt-nodejs'); var userSchema = new mongoose.Schema({ - email: { type: String, unique: true }, + email: { type: String, unique: true, sparse: true }, password: String, facebook: { type: String, unique: true, sparse: true }, twitter: { type: String, unique: true, sparse: true }, google: { type: String, unique: true, sparse: true }, github: { type: String, unique: true, sparse: true }, + steam: { type: String, unique: true, sparse: true }, + tokens: Array, profile: { diff --git a/package.json b/package.json index 3d71ab59ed9..a003f88b033 100755 --- a/package.json +++ b/package.json @@ -28,6 +28,8 @@ "twit": "~1.1.12", "underscore": "~1.5.2", "paypal-rest-sdk": "~0.6.4", - "connect-mongo": "~0.4.0" + "connect-mongo": "~0.4.0", + "passport-steam": "~0.1.3", + "steam-web": "~0.2.1" } } diff --git a/views/api/index.jade b/views/api/index.jade index 97e2775dea2..9c340a36fb6 100644 --- a/views/api/index.jade +++ b/views/api/index.jade @@ -35,3 +35,7 @@ block content a(href='/api/scraping') Web Scraping li a(href='/api/paypal') PayPal + li + a(href='/api/steam') + i.fa.fa-lock + | Steam diff --git a/views/api/steam.jade b/views/api/steam.jade new file mode 100644 index 00000000000..da0f9fc49b8 --- /dev/null +++ b/views/api/steam.jade @@ -0,0 +1,23 @@ +extends ../layout + +block content + .page-header + h2 + i.fa.fa-gamepad + | Steam Web API + + .btn-group.btn-group-justified + a.btn.btn-primary(href='https://developer.valvesoftware.com/wiki/Steam_Web_API', target='_blank') + i.fa.fa-check-square-o + | Overview + + h4 Steam Players + + ul.list-unstyled + for player in players + li + .panel.panel-default + .panel-body + .clearfix + img.pull-left(src='#{player.avatarfull}', style='margin-right: 10px') + =player.personaname \ No newline at end of file