Pull out logic into module

This commit is contained in:
James Allen 2017-08-09 11:50:05 +02:00
parent eac0ce8353
commit 17e849792c
5 changed files with 1 additions and 62 deletions

View file

@ -1,33 +0,0 @@
logger = require("logger-sharelatex")
settings = require "settings-sharelatex"
{User} = require "../../models/User"
UserCreator = require "../User/UserCreator"
AuthenticationController = require "./AuthenticationController"
passport = require "passport"
module.exports = OverleafAuthenticationController =
passportLogin: passport.authenticate("oauth2")
passportCallback: passport.authenticate("oauth2")
afterPassportLogin: (req, res, next) ->
logger.log {user: req.user}, "successful log in!"
AuthenticationController.afterLoginSessionSetup req, req.user, (err) ->
return next(err) if err?
res.redirect("/")
doPassportLogin: (accessToken, refreshToken, profile, cb) ->
logger.log {accessToken, refreshToken, profile}, "authing user via overleaf oauth"
OverleafAuthenticationController._findOrCreateUser profile, (err, user) ->
return cb(err) if err?
user.overleaf.accessToken = accessToken
user.save (err) ->
return cb(err) if err?
return cb(null, user)
_findOrCreateUser: (profile, cb) ->
User.findOne { "overleaf.id": profile.id }, (err, user) ->
return cb(err) if err?
return cb(null, user) if user?
UserCreator.createNewUser { overleaf: { id: profile.id }, email: profile.email }, cb

View file

@ -25,7 +25,6 @@ sessionStore = new RedisStore(client:sessionsRedisClient)
passport = require('passport')
LocalStrategy = require('passport-local').Strategy
OAuth2Strategy = require('passport-oauth2').Strategy
Mongoose = require("./Mongoose")
@ -39,7 +38,6 @@ Modules = require "./Modules"
ErrorController = require "../Features/Errors/ErrorController"
UserSessionsManager = require "../Features/User/UserSessionsManager"
AuthenticationController = require "../Features/Authentication/AuthenticationController"
OverleafAuthenticationController = require "../Features/Authentication/OverleafAuthenticationController"
metrics.event_loop?.monitor(logger)
@ -106,29 +104,6 @@ passport.use(new LocalStrategy(
passport.serializeUser(AuthenticationController.serializeUser)
passport.deserializeUser(AuthenticationController.deserializeUser)
overleafOAuth2Strategy = new OAuth2Strategy(
{
authorizationURL: 'http://localhost:5000/oauth/authorize',
tokenURL: 'http://localhost:5000/oauth/token',
clientID: "0479498de20727971b5f40f86dc558264fe7a5021ae74c3e0e03f7dccfeaf0ab",
clientSecret: "ecb446d53bb9a1555fecd74b5e3faabefe1345ca6a9228da0c1fbdac2338c502",
callbackURL: "http://www.sharelatex.dev:3000/overleaf/callback"
},
OverleafAuthenticationController.doPassportLogin
)
overleafOAuth2Strategy.userProfile = (accessToken, cb) ->
require("request").get {
url: "http://localhost:5000/api/v1/sharelatex/profile"
json: true
headers:
Authorization: "Bearer #{accessToken}"
}, (err, response, body) ->
console.log {err, response, body}
return cb(err) if err?
cb(null, body)
passport.use(overleafOAuth2Strategy)
Modules.hooks.fire 'passportSetup', passport, (err) ->
if err?
logger.err {err}, "error setting up passport in modules"

View file

@ -62,6 +62,7 @@ UserSchema = new Schema
overleaf:
id: { type: Number }
accessToken: { type: String }
refreshToken: { type: String }
conn = mongoose.createConnection(Settings.mongo.url, server: poolSize: 10)

View file

@ -12,7 +12,6 @@ UploadsRouter = require './Features/Uploads/UploadsRouter'
metrics = require('metrics-sharelatex')
ReferalController = require('./Features/Referal/ReferalController')
AuthenticationController = require('./Features/Authentication/AuthenticationController')
OverleafAuthenticationController = require('./Features/Authentication/OverleafAuthenticationController')
TagsController = require("./Features/Tags/TagsController")
NotificationsController = require("./Features/Notifications/NotificationsController")
CollaboratorsRouter = require('./Features/Collaborators/CollaboratorsRouter')
@ -59,9 +58,6 @@ module.exports = class Router
AuthenticationController.addEndpointToLoginWhitelist '/login'
webRouter.post '/login', AuthenticationController.passportLogin
webRouter.get '/overleaf/login', OverleafAuthenticationController.passportLogin
webRouter.get '/overleaf/callback', OverleafAuthenticationController.passportCallback, OverleafAuthenticationController.afterPassportLogin
webRouter.get '/logout', UserController.logout
webRouter.get '/restricted', AuthorizationMiddlewear.restricted