mgebbe@3: # Most of the routes in the application mgebbe@3: # mgebbe@3: # Copyright 2013, E14N (https://e14n.com/) mgebbe@3: # all changes Copyright 2014, Intevation GmbH (https://intevation.org) mgebbe@3: # mgebbe@3: # Licensed under the Apache License, Version 2.0 (the "License"); mgebbe@3: # you may not use this file except in compliance with the License. mgebbe@3: # You may obtain a copy of the License at mgebbe@3: # mgebbe@3: # http://www.apache.org/licenses/LICENSE-2.0 mgebbe@3: # mgebbe@3: # Unless required by applicable law or agreed to in writing, software mgebbe@3: # distributed under the License is distributed on an "AS IS" BASIS, mgebbe@3: # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. mgebbe@3: # See the License for the specific language governing permissions and mgebbe@3: # limitations under the License. mgebbe@3: mgebbe@3: async = require("async") mgebbe@3: _ = require("underscore") mgebbe@3: PumpIOClientApp = require("pump.io-client-app") mgebbe@3: RequestToken = PumpIOClientApp.RequestToken mgebbe@3: userAuth = PumpIOClientApp.userAuth mgebbe@3: userOptional = PumpIOClientApp.userOptional mgebbe@3: userRequired = PumpIOClientApp.userRequired mgebbe@3: noUser = PumpIOClientApp.noUser mgebbe@3: Routes = require("./routes") mgebbe@3: Config = require("./config") mgebbe@3: config = Config.config mgebbe@3: mgebbe@3: addRoutes = (app) -> mgebbe@3: mgebbe@3: Twitter = require("./twitter")(config) mgebbe@3: mgebbe@3: addAccount = (req, res, next) -> mgebbe@3: Twitter.getRequestToken req.site, (err, rt) -> mgebbe@3: if err mgebbe@3: next err mgebbe@3: else mgebbe@3: res.redirect Twitter.authorizeURL(rt) mgebbe@3: return mgebbe@3: mgebbe@3: return mgebbe@3: mgebbe@3: authorizedForTwitter = (req, res, next) -> mgebbe@3: hostname = "twitter.com" mgebbe@3: token = req.query.oauth_token mgebbe@3: verifier = req.query.oauth_verifier mgebbe@3: problem = req.query.oauth_problem mgebbe@3: user = req.user mgebbe@3: rt = undefined mgebbe@3: fuser = undefined mgebbe@3: access_token = undefined mgebbe@3: token_secret = undefined mgebbe@3: id = undefined mgebbe@3: object = undefined mgebbe@3: newUser = false mgebbe@3: unless token mgebbe@3: next new Error("No token returned.") mgebbe@3: return mgebbe@3: async.waterfall [ mgebbe@3: (callback) -> mgebbe@3: RequestToken.get RequestToken.key(hostname, token), callback mgebbe@3: (results, callback) -> mgebbe@3: rt = results mgebbe@3: Twitter.getAccessToken req.site, rt, verifier, callback mgebbe@3: (token, secret, extra, callback) -> mgebbe@3: access_token = token mgebbe@3: token_secret = secret mgebbe@3: async.parallel [ mgebbe@3: (callback) -> mgebbe@3: rt.del callback mgebbe@3: (callback) -> mgebbe@3: Twitter.whoami req.site, access_token, token_secret, callback mgebbe@3: ], callback mgebbe@3: (results, callback) -> mgebbe@3: object = results[1] mgebbe@3: object = JSON.stringify(object) mgebbe@3: object = JSON.parse(object) mgebbe@3: res.clearCookie('twitteruser') mgebbe@3: res.clearCookie('twitterid') mgebbe@3: res.cookie('twitterid',object.id, { maxAge: 900000, httpOnly: false }) mgebbe@3: res.cookie('twitteruser',object.screen_name, { maxAge: 900000, httpOnly: false }) mgebbe@3: Routes.saveUsermap(user.id ,object.id + "@twitter" ,access_token, token_secret, callback) mgebbe@3: ], (err) -> mgebbe@3: if err mgebbe@3: next err mgebbe@3: else mgebbe@3: res.redirect "/" mgebbe@3: return mgebbe@3: mgebbe@3: return mgebbe@17: mgebbe@3: # Routes mgebbe@3: console.log "Initializing Twitter routes" mgebbe@3: app.get "/add-account", userAuth, userRequired, addAccount mgebbe@3: app.get "/authorized-for-twitter", userAuth, userRequired, authorizedForTwitter mgebbe@3: return mgebbe@3: mgebbe@3: exports.addRoutes = addRoutes