mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-12-18 04:42:44 -05:00
83ef1de792
Added OpenID in auth Signed-off-by: Yannick Bungers <git@innay.de> Signed-off-by: David Mehren <dmehren1@gmail.com>
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { Router } from 'express'
|
|
import passport from 'passport'
|
|
import * as OpenID from '@passport-next/passport-openid'
|
|
import { config } from '../../../config'
|
|
import { User } from '../../../models'
|
|
import { logger } from '../../../logger'
|
|
import { urlencodedParser } from '../../utils'
|
|
import { AuthMiddleware } from '../interface'
|
|
|
|
const openIDAuth = Router()
|
|
export const OPenIDMiddleware: AuthMiddleware = {
|
|
getMiddleware (): Router {
|
|
passport.use(new OpenID.Strategy({
|
|
returnURL: config.serverURL + '/auth/openid/callback',
|
|
realm: config.serverURL,
|
|
profile: true
|
|
}, function (openid, profile, done) {
|
|
const stringifiedProfile = JSON.stringify(profile)
|
|
User.findOrCreate({
|
|
where: {
|
|
profileid: openid
|
|
},
|
|
defaults: {
|
|
profile: stringifiedProfile
|
|
}
|
|
}).then(function ([user, _]) {
|
|
if (user) {
|
|
let needSave = false
|
|
if (user.profile !== stringifiedProfile) {
|
|
user.profile = stringifiedProfile
|
|
needSave = true
|
|
}
|
|
if (needSave) {
|
|
user.save().then(function () {
|
|
logger.debug(`user login: ${user.id}`)
|
|
return done(null, user)
|
|
})
|
|
} else {
|
|
logger.debug(`user login: ${user.id}`)
|
|
return done(null, user)
|
|
}
|
|
}
|
|
}).catch(function (err) {
|
|
logger.error('auth callback failed: ' + err)
|
|
return done(err, null)
|
|
})
|
|
}))
|
|
openIDAuth.post('/auth/openid', urlencodedParser, function (req, res, next) {
|
|
passport.authenticate('openid')(req, res, next)
|
|
})
|
|
openIDAuth.get('/auth/openid/callback',
|
|
passport.authenticate('openid', {
|
|
successReturnToOrRedirect: config.serverURL + '/',
|
|
failureRedirect: config.serverURL + '/'
|
|
})
|
|
)
|
|
return openIDAuth
|
|
}
|
|
}
|