2020-04-13 07:18:31 -04:00
|
|
|
import { NextFunction, Request, Response, Router } from 'express'
|
2020-04-13 06:55:08 -04:00
|
|
|
import passport from 'passport'
|
2020-05-16 09:51:36 -04:00
|
|
|
import { Strategy as DropboxStrategy } from 'passport-dropbox-oauth2'
|
2020-04-13 07:18:31 -04:00
|
|
|
import { config } from '../../../config'
|
2020-05-16 09:52:11 -04:00
|
|
|
import { User } from '../../../models'
|
2020-04-13 07:09:49 -04:00
|
|
|
import { AuthMiddleware } from '../interface'
|
2020-04-13 07:18:31 -04:00
|
|
|
import { passportGeneralCallback } from '../utils'
|
2017-04-11 17:41:14 -04:00
|
|
|
|
2020-04-13 06:55:08 -04:00
|
|
|
export const dropboxAuth = Router()
|
2017-04-11 17:41:14 -04:00
|
|
|
|
2020-04-13 07:18:31 -04:00
|
|
|
export const DropboxMiddleware: AuthMiddleware = {
|
2020-04-13 07:09:49 -04:00
|
|
|
getMiddleware (): Router {
|
|
|
|
passport.use(new DropboxStrategy({
|
|
|
|
apiVersion: '2',
|
|
|
|
clientID: config.dropbox.clientID,
|
|
|
|
clientSecret: config.dropbox.clientSecret,
|
|
|
|
callbackURL: config.serverURL + '/auth/dropbox/callback'
|
2020-05-16 09:52:11 -04:00
|
|
|
}, (
|
|
|
|
accessToken: string,
|
|
|
|
refreshToken: string,
|
2020-05-24 12:38:24 -04:00
|
|
|
profile,
|
2020-05-16 09:52:11 -04:00
|
|
|
done: (err?: Error | null, user?: User) => void
|
|
|
|
): void => {
|
|
|
|
// the Dropbox plugin wraps the email addresses in an object
|
|
|
|
// see https://github.com/florianheinemann/passport-dropbox-oauth2/blob/master/lib/passport-dropbox-oauth2/strategy.js#L146
|
|
|
|
profile.emails = profile.emails.map(element => element.value)
|
|
|
|
passportGeneralCallback(accessToken, refreshToken, profile, done)
|
|
|
|
}))
|
2017-04-11 17:41:14 -04:00
|
|
|
|
2020-04-13 07:09:49 -04:00
|
|
|
dropboxAuth.get('/auth/dropbox', function (req: Request, res: Response, next: NextFunction) {
|
|
|
|
passport.authenticate('dropbox-oauth2')(req, res, next)
|
|
|
|
})
|
|
|
|
dropboxAuth.get('/auth/dropbox/callback',
|
|
|
|
passport.authenticate('dropbox-oauth2', {
|
|
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
|
|
failureRedirect: config.serverURL + '/'
|
|
|
|
})
|
|
|
|
)
|
|
|
|
return dropboxAuth
|
|
|
|
}
|
|
|
|
}
|