Clean up server init and save sso settings

This commit is contained in:
advplyr 2022-03-04 16:55:38 -06:00
parent 61bf30e08a
commit 4616ef91e6
5 changed files with 88 additions and 2912 deletions

View file

@ -82,17 +82,17 @@ export default {
user: { user: {
createNewUser: false, createNewUser: false,
isActive: true, isActive: true,
settings: { // settings: {
mobileOrderBy: 'recent', // mobileOrderBy: 'recent',
mobileOrderDesc: true, // mobileOrderDesc: true,
mobileFilterBy: 'all', // mobileFilterBy: 'all',
orderBy: 'book.title', // orderBy: 'book.title',
orderDesc: false, // orderDesc: false,
filterBy: 'all', // filterBy: 'all',
playbackRate: 1, // playbackRate: 1,
bookshelfCoverSize: 120, // bookshelfCoverSize: 120,
collapseSeries: false // collapseSeries: false
}, // },
permissions: { permissions: {
download: false, download: false,
update: false, update: false,
@ -106,7 +106,6 @@ export default {
}, },
watch: { watch: {
SSOSettings(newVal, oldVal) { SSOSettings(newVal, oldVal) {
console.log('SSO Settings set', newVal, oldVal)
if (newVal && !oldVal) { if (newVal && !oldVal) {
this.initSSOSettings() this.initSSOSettings()
} }
@ -160,8 +159,14 @@ export default {
methods: { methods: {
saveSSOSettings() { saveSSOSettings() {
this.updatingSSOSettings = true this.updatingSSOSettings = true
var user = JSON.parse(JSON.stringify(this.user))
var updatePayload = {
oidc: { ...this.oidc },
user
}
this.$store this.$store
.dispatch('settings/updateSSOSettings', { oidc: this.oidc, user: this.user }) .dispatch('settings/updateSSOSettings', updatePayload)
.then((payload) => { .then((payload) => {
console.log('Update SSO settings success', payload) console.log('Update SSO settings success', payload)
this.updatingSSOSettings = false this.updatingSSOSettings = false

View file

@ -92,21 +92,23 @@ export default {
this.processing = false this.processing = false
}, },
getCookies() { getCookies() {
return document.cookie.split(";").map(c => c.split("=")).reduce((acc, val)=> { return document.cookie
.split(';')
.map((c) => c.split('='))
.reduce((acc, val) => {
return { return {
...acc, ...acc,
[val[0]]: val[1] [val[0]]: val[1]
} }
}, {}) }, {})
}, },
deleteCookie(name, path="/", domain=document.location.host) { deleteCookie(name, path = '/', domain = document.location.host) {
document.cookie = name + "=" + document.cookie = name + '=' + (path ? ';path=' + path : '') + (domain ? ';domain=' + domain : '') + ';expires=Thu, 01 Jan 1970 00:00:01 GMT'
((path) ? ";path="+path:"")+
((domain)?";domain="+domain:"") +
";expires=Thu, 01 Jan 1970 00:00:01 GMT";
}, },
checkAuth() { checkAuth() {
if (this.getCookies()["sso"]) { console.log('Raw cookie', document.cookie)
if (this.getCookies()['sso']) {
this.processing = true this.processing = true
this.$axios this.$axios
@ -117,10 +119,10 @@ export default {
}) })
.catch((error) => { .catch((error) => {
console.error('Authorize error', error) console.error('Authorize error', error)
this.deleteCookie("sso") this.deleteCookie('sso')
this.processing = false this.processing = false
}) })
return; return
} }
if (localStorage.getItem('token')) { if (localStorage.getItem('token')) {
let token = localStorage.getItem('token') let token = localStorage.getItem('token')
@ -146,7 +148,8 @@ export default {
} }
}, },
async checkSSO() { async checkSSO() {
const res = await fetch("/oidc/login", {mode: "no-cors"}) const res = await fetch('/oidc/login', { mode: 'no-cors' })
console.log('res', res)
if (res.status >= 400 && res.status < 600) { if (res.status >= 400 && res.status < 600) {
this.ssoAvailable = false this.ssoAvailable = false
return return

2857
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -153,25 +153,22 @@ class Server {
this.watcher.initWatcher(this.libraries) this.watcher.initWatcher(this.libraries)
this.watcher.on('files', this.filesChanged.bind(this)) this.watcher.on('files', this.filesChanged.bind(this))
} }
this.passportInit()
} }
passportInit() { passportInit() {
if (this.db.SSOSettings.isOIDCConfigured) { Logger.debug(`[Server] passportInit OIDC is configured - init`)
Logger.debug(`[Server] passportInit OIDC is configured - init`) passport.serializeUser((user, next) => {
passport.serializeUser((user, next) => { console.log('Testing serialize user', user)
next(null, {userId: user.id}); next(null, { userId: user.id })
}) })
passport.deserializeUser((obj, next) => { passport.deserializeUser((obj, next) => {
this.db.users.find(u => u.id === obj.userId) console.log('Testing deserialize user', obj)
next(null, obj); var user = this.db.users.find(u => u.id === obj.userId)
}) next(null, user)
})
// Initialize passport OIDC verification // Initialize passport OIDC verification
passport.use(new OidcStrategy(this.db.SSOSettings.getOIDCSettings(), this.auth.handleOIDCVerification.bind(this.auth))) passport.use(new OidcStrategy(this.db.SSOSettings.getOIDCSettings(), this.auth.handleOIDCVerification.bind(this.auth)))
} else {
Logger.debug(`[Server] passportInit OIDC not configured`)
}
} }
async start() { async start() {
@ -183,19 +180,33 @@ class Server {
this.server = http.createServer(app) this.server = http.createServer(app)
// TEMP testing OIDC
app.use((req, res, next) => {
if (req.url.startsWith('/oidc/callback')) {
console.log('OIDC CALLBACK', req.headers)
}
next()
})
app.use(this.auth.cors) app.use(this.auth.cors)
app.use(fileUpload()) app.use(fileUpload())
app.use(express.urlencoded({ extended: true })); app.use(express.urlencoded({ extended: true }));
app.use(express.json()) app.use(express.json())
app.use(cookieParser()); app.use(cookieParser())
app.use(session({ app.use(session({
secret: process.env.TOKEN_SECRET, secret: process.env.TOKEN_SECRET,
resave: false, resave: false,
saveUninitialized: true saveUninitialized: true
})); }));
app.use(passport.initialize());
app.use(passport.session());
if (this.db.SSOSettings.isOIDCConfigured) {
this.passportInit()
app.use(passport.initialize())
app.use(passport.session())
} else {
Logger.debug(`[Server] passportInit OIDC not configured`)
}
/* /*
if (req.method === 'GET' && req.query && req.query.token) { if (req.method === 'GET' && req.query && req.query.token) {
token = req.query.token token = req.query.token
@ -283,6 +294,8 @@ class Server {
app.get("/oidc/login", passport.authenticate('openidconnect')) app.get("/oidc/login", passport.authenticate('openidconnect'))
app.get("/oidc/callback", passport.authenticate('openidconnect', { failureRedirect: '/login', failureMessage: true }), app.get("/oidc/callback", passport.authenticate('openidconnect', { failureRedirect: '/login', failureMessage: true }),
async (req, res) => { async (req, res) => {
// Never reached
console.log('OIDC CALLBACK RECEIVED', req)
res.cookie('sso', true, { httpOnly: false /* TODO: Set secure: true */ }); res.cookie('sso', true, { httpOnly: false /* TODO: Set secure: true */ });
res.redirect('/'); res.redirect('/');
} }

View file

@ -4,23 +4,23 @@ const { isObject } = require('../utils')
const { difference } = require('../utils/string') const { difference } = require('../utils/string')
const defaultSettings = { const defaultSettings = {
createNewUser: false,
user: {
createNewUser: false, createNewUser: false,
user: { isActive: true,
createNewUser: false, settings: {
isActive: true, mobileOrderBy: 'recent',
settings: { mobileOrderDesc: true,
mobileOrderBy: 'recent', mobileFilterBy: 'all',
mobileOrderDesc: true, orderBy: 'book.title',
mobileFilterBy: 'all', orderDesc: false,
orderBy: 'book.title', filterBy: 'all',
orderDesc: false, playbackRate: 1,
filterBy: 'all', bookshelfCoverSize: 120,
playbackRate: 1, collapseSeries: false
bookshelfCoverSize: 120, },
collapseSeries: false permissions: User.getDefaultUserPermissions('guest')
}, }
permissions: User.getDefaultUserPermissions('guest')
}
} }
class SSOSettings { class SSOSettings {