mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-23 18:26:32 -05:00
Refactored OpenID to typescript
Added OpenID in auth Signed-off-by: Yannick Bungers <git@innay.de> Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
4e74998372
commit
83ef1de792
3 changed files with 61 additions and 61 deletions
|
@ -13,7 +13,7 @@ import { LdapMiddleware } from './ldap'
|
|||
import { SamlMiddleware } from './saml'
|
||||
import oauth2 from './oauth2'
|
||||
import { EmailMiddleware } from './email'
|
||||
import openid from './openid'
|
||||
import {OPenIDMiddleware } from './openid'
|
||||
|
||||
const AuthRouter = Router()
|
||||
|
||||
|
@ -53,7 +53,7 @@ if (config.isLDAPEnable) AuthRouter.use(LdapMiddleware.getMiddleware())
|
|||
if (config.isSAMLEnable) AuthRouter.use(SamlMiddleware.getMiddleware())
|
||||
if (config.isOAuth2Enable) AuthRouter.use(oauth2)
|
||||
if (config.isEmailEnable) AuthRouter.use(EmailMiddleware.getMiddleware())
|
||||
if (config.isOpenIDEnable) AuthRouter.use(openid)
|
||||
if (config.isOpenIDEnable) AuthRouter.use(OPenIDMiddleware.getMiddleware())
|
||||
|
||||
// logout
|
||||
AuthRouter.get('/logout', function (req: Request, res: Response) {
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
'use strict'
|
||||
|
||||
const Router = require('express').Router
|
||||
const passport = require('passport')
|
||||
const OpenIDStrategy = require('@passport-next/passport-openid').Strategy
|
||||
const config = require('../../../config')
|
||||
const models = require('../../../models')
|
||||
const logger = require('../../../logger')
|
||||
const { urlencodedParser } = require('../../utils')
|
||||
|
||||
let openIDAuth = module.exports = Router()
|
||||
|
||||
passport.use(new OpenIDStrategy({
|
||||
returnURL: config.serverURL + '/auth/openid/callback',
|
||||
realm: config.serverURL,
|
||||
profile: true
|
||||
}, function (openid, profile, done) {
|
||||
var stringifiedProfile = JSON.stringify(profile)
|
||||
models.User.findOrCreate({
|
||||
where: {
|
||||
profileid: openid
|
||||
},
|
||||
defaults: {
|
||||
profile: stringifiedProfile
|
||||
}
|
||||
}).spread(function (user, created) {
|
||||
if (user) {
|
||||
var 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)
|
||||
})
|
||||
|
||||
// openID auth callback
|
||||
openIDAuth.get('/auth/openid/callback',
|
||||
passport.authenticate('openid', {
|
||||
successReturnToOrRedirect: config.serverURL + '/',
|
||||
failureRedirect: config.serverURL + '/'
|
||||
})
|
||||
)
|
59
lib/web/auth/openid/index.ts
Normal file
59
lib/web/auth/openid/index.ts
Normal file
|
@ -0,0 +1,59 @@
|
|||
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
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue