Last active
August 29, 2015 14:05
-
-
Save geilt/f1121194d012d74ead20 to your computer and use it in GitHub Desktop.
Sails Dropbox oAuth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var passport = require('passport'), | |
| qs = require('querystring'), | |
| request = require('request-promise'); | |
| module.exports = { | |
| authenticate: passport.authenticate('dropbox'), | |
| callback: passport.authenticate('dropbox', { | |
| failureRedirect: '/options?' + qs.stringify({success: 'Dropbox failed authentication.'}), | |
| successRedirect: '/options?' + qs.stringify({success: 'Dropbox connected.'}) | |
| }), | |
| remove: function(req, res){ | |
| Option.findOne({name: 'dropbox'}).then( function(option){ | |
| if(option.value.token !== req.param('id')) return res.redirect('/options?' + qs.stringify({error: 'Invalid token'})); | |
| DropboxService.authenticate().then(function(oauth){ | |
| request({url: 'https://api.dropbox.com/1/disable_access_token', json: {}, oauth: oauth }) | |
| .then( function(result){ | |
| if( !_.isEmpty(result) ) return res.redirect('/options?' + qs.stringify({error: 'Dropbox API error: ' + result.ErrorMessage + '.'})); | |
| Option.destroy({name: 'dropbox'}).then( | |
| function(destroyed){ | |
| return res.redirect('/options?' + qs.stringify({success: 'Dropbox token revoked and connection removed.'})); | |
| }, | |
| function(err){ | |
| return res.redirect('/options?' + qs.stringify({error: 'Dropbox token revoked, but could not remove connection.'})); | |
| }); | |
| }, function(err){ | |
| return res.redirect('/options?' + qs.stringify({error: 'Could not communicate with Dropbox API.'})); | |
| }); | |
| }, function(){ | |
| return res.redirect('/options?' + qs.stringify({error: 'Could not generate oauth header.'})); | |
| }); | |
| }, function(err){ | |
| return res.redirect('/options?' + qs.stringify({error: 'No token found.'})); | |
| }); | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Option.js | |
| * | |
| * @description :: TODO: You might write a short summary of how this model works and what it represents here. | |
| * @docs :: http://sailsjs.org/#!documentation/models | |
| */ | |
| module.exports = { | |
| attributes: { | |
| name: { | |
| type: 'string', | |
| unique: true | |
| }, | |
| value: { | |
| type: 'object', | |
| defaultsTo: {} | |
| } | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Dropbox Service | |
| */ | |
| var passport = require('passport'), | |
| DropboxStrategy = require('passport-dropbox').Strategy; | |
| module.exports = { | |
| /** | |
| * [authenticate description] | |
| * @return {promise} | |
| */ | |
| authenticate: function() { | |
| return Option.findOne({name: 'dropbox'}).then( function(option){ | |
| return { | |
| consumer_key: sails.config.dropbox.appKey, | |
| consumer_secret: sails.config.dropbox.appSecret, | |
| token: option.value.token, | |
| token_secret: option.value.tokenSecret | |
| }; | |
| }); | |
| } | |
| }; | |
| /** | |
| * Enable Dropbox oAuth Passport Strategy. | |
| */ | |
| passport.use(new DropboxStrategy({ | |
| consumerKey: sails.config.dropbox.appKey, | |
| consumerSecret: sails.config.dropbox.appSecret, | |
| callbackURL: sails.config.dropbox.callbackURL, | |
| passReqToCallback: true | |
| }, | |
| function(req, token, tokenSecret, profile, done) { | |
| Option.create({name: 'dropbox', value: { token: token, tokenSecret: tokenSecret, profile: profile }}) | |
| .then( function(option){ | |
| return done(null, req.user); | |
| }, function(err) { | |
| return done(null, false, {message: err}); | |
| }); | |
| } | |
| )); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = { | |
| dropbox: { | |
| appKey: '', | |
| appSecret: '', | |
| callbackURL: "http://mydomain.com/oauth/dropbox/callback" | |
| } | |
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports.routes = { | |
| 'GET /oauth/dropbox': { | |
| controller: 'DropboxController', | |
| action: 'authenticate' | |
| }, | |
| 'GET /oauth/dropbox/callback': { | |
| controller: 'DropboxController', | |
| action: 'callback' | |
| }, | |
| 'GET /oauth/dropbox/remove/:id': { | |
| controller: 'DropboxController', | |
| action: 'remove' | |
| }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment