feat(xo-server): passport strategies can be unregistered (#4702)
Now all authentication plugins can be unloaded.
This commit is contained in:
@@ -19,6 +19,7 @@ export const configurationSchema = {
|
||||
|
||||
class AuthGitHubXoPlugin {
|
||||
constructor(xo) {
|
||||
this._unregisterPassportStrategy = undefined
|
||||
this._xo = xo
|
||||
}
|
||||
|
||||
@@ -29,7 +30,7 @@ class AuthGitHubXoPlugin {
|
||||
load() {
|
||||
const { _xo: xo } = this
|
||||
|
||||
xo.registerPassportStrategy(
|
||||
this._unregisterPassportStrategy = xo.registerPassportStrategy(
|
||||
new Strategy(
|
||||
this._conf,
|
||||
async (accessToken, refreshToken, profile, done) => {
|
||||
@@ -42,6 +43,10 @@ class AuthGitHubXoPlugin {
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
unload() {
|
||||
this._unregisterPassportStrategy()
|
||||
}
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
||||
@@ -31,6 +31,7 @@ export const configurationSchema = {
|
||||
class AuthGoogleXoPlugin {
|
||||
constructor({ xo }) {
|
||||
this._conf = null
|
||||
this._unregisterPassportStrategy = undefined
|
||||
this._xo = xo
|
||||
}
|
||||
|
||||
@@ -42,7 +43,7 @@ class AuthGoogleXoPlugin {
|
||||
const conf = this._conf
|
||||
const xo = this._xo
|
||||
|
||||
xo.registerPassportStrategy(
|
||||
this._unregisterPassportStrategy = xo.registerPassportStrategy(
|
||||
new Strategy(conf, async (accessToken, refreshToken, profile, done) => {
|
||||
try {
|
||||
done(
|
||||
@@ -60,6 +61,10 @@ class AuthGoogleXoPlugin {
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
unload() {
|
||||
this._unregisterPassportStrategy()
|
||||
}
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
||||
@@ -48,6 +48,7 @@ You should try \`http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddr
|
||||
class AuthSamlXoPlugin {
|
||||
constructor({ xo }) {
|
||||
this._conf = null
|
||||
this._unregisterPassportStrategy = undefined
|
||||
this._usernameField = null
|
||||
this._xo = xo
|
||||
}
|
||||
@@ -66,7 +67,7 @@ class AuthSamlXoPlugin {
|
||||
load() {
|
||||
const xo = this._xo
|
||||
|
||||
xo.registerPassportStrategy(
|
||||
this._unregisterPassportStrategy = xo.registerPassportStrategy(
|
||||
new Strategy(this._conf, async (profile, done) => {
|
||||
const name = profile[this._usernameField]
|
||||
if (!name) {
|
||||
@@ -83,6 +84,10 @@ class AuthSamlXoPlugin {
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
unload() {
|
||||
this._unregisterPassportStrategy()
|
||||
}
|
||||
}
|
||||
|
||||
// ===================================================================
|
||||
|
||||
@@ -15,7 +15,7 @@ import serveStatic from 'serve-static'
|
||||
import stoppable from 'stoppable'
|
||||
import WebServer from 'http-server-plus'
|
||||
import WebSocket from 'ws'
|
||||
import { forOwn, map } from 'lodash'
|
||||
import { forOwn, map, once } from 'lodash'
|
||||
import { URL } from 'url'
|
||||
|
||||
import { compile as compilePug } from 'pug'
|
||||
@@ -125,10 +125,14 @@ async function setUpPassport(express, xo, { authentication: authCfg }) {
|
||||
{ label = strategy.label, name = strategy.name } = {}
|
||||
) => {
|
||||
passport.use(name, strategy)
|
||||
|
||||
if (name !== 'local') {
|
||||
strategies[name] = label ?? name
|
||||
}
|
||||
|
||||
return once(() => {
|
||||
passport.unuse(name)
|
||||
delete strategies[name]
|
||||
})
|
||||
}
|
||||
|
||||
// Registers the sign in form.
|
||||
|
||||
Reference in New Issue
Block a user