|
|
@ -2,10 +2,19 @@ import { Handler } from 'express'; |
|
|
|
|
|
|
|
|
|
|
|
import { stdout } from './shell'; |
|
|
|
import { stdout } from './shell'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type HandlerParameters = Parameters<Handler>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type AssertAuthenticationFailFunction = ( |
|
|
|
|
|
|
|
returnTo?: string, |
|
|
|
|
|
|
|
...args: HandlerParameters |
|
|
|
|
|
|
|
) => void; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type AssertAuthenticationSucceedFunction = (...args: HandlerParameters) => void; |
|
|
|
|
|
|
|
|
|
|
|
type AssertAuthenticationOptions = { |
|
|
|
type AssertAuthenticationOptions = { |
|
|
|
fail?: string | ((...args: Parameters<Handler>) => void); |
|
|
|
fail?: string | AssertAuthenticationFailFunction; |
|
|
|
failReturnTo?: boolean | string; |
|
|
|
failReturnTo?: boolean | string; |
|
|
|
succeed?: string | ((...args: Parameters<Handler>) => void); |
|
|
|
succeed?: string | AssertAuthenticationSucceedFunction; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
type AssertAuthenticationFunction = ( |
|
|
|
type AssertAuthenticationFunction = ( |
|
|
@ -13,42 +22,47 @@ type AssertAuthenticationFunction = ( |
|
|
|
) => Handler; |
|
|
|
) => Handler; |
|
|
|
|
|
|
|
|
|
|
|
export const assertAuthentication: AssertAuthenticationFunction = ({ |
|
|
|
export const assertAuthentication: AssertAuthenticationFunction = ({ |
|
|
|
fail: initFail = (request, response) => response.status(404).send(), |
|
|
|
fail: initFail = (rt, rq, response) => response.status(404).send(), |
|
|
|
failReturnTo, |
|
|
|
failReturnTo, |
|
|
|
succeed: initSucceed = (request, response, next) => next(), |
|
|
|
succeed: initSucceed = (request, response, next) => next(), |
|
|
|
}: AssertAuthenticationOptions = {}) => { |
|
|
|
}: AssertAuthenticationOptions = {}) => { |
|
|
|
const fail: (...args: Parameters<Handler>) => void = |
|
|
|
let getReturnTo: ((...args: HandlerParameters) => string) | undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (failReturnTo === true) { |
|
|
|
|
|
|
|
getReturnTo = ({ path }) => path; |
|
|
|
|
|
|
|
} else if (typeof failReturnTo === 'string') { |
|
|
|
|
|
|
|
getReturnTo = () => failReturnTo; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fail: AssertAuthenticationFailFunction = |
|
|
|
typeof initFail === 'string' |
|
|
|
typeof initFail === 'string' |
|
|
|
? (request, response) => response.redirect(initFail) |
|
|
|
? (returnTo, rq, response) => |
|
|
|
|
|
|
|
response.redirect(returnTo ? `${initFail}?rt=${returnTo}` : initFail) |
|
|
|
: initFail; |
|
|
|
: initFail; |
|
|
|
|
|
|
|
|
|
|
|
const succeed: (...args: Parameters<Handler>) => void = |
|
|
|
const succeed: AssertAuthenticationSucceedFunction = |
|
|
|
typeof initSucceed === 'string' |
|
|
|
typeof initSucceed === 'string' |
|
|
|
? (request, response) => response.redirect(initSucceed) |
|
|
|
? (request, response) => response.redirect(initSucceed) |
|
|
|
: initSucceed; |
|
|
|
: initSucceed; |
|
|
|
|
|
|
|
|
|
|
|
let getReturnTo: ((...args: Parameters<Handler>) => string) | undefined; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (failReturnTo === true) { |
|
|
|
|
|
|
|
getReturnTo = ({ originalUrl, url }) => originalUrl || url; |
|
|
|
|
|
|
|
} else if (typeof failReturnTo === 'string') { |
|
|
|
|
|
|
|
getReturnTo = () => failReturnTo; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return (...args) => { |
|
|
|
return (...args) => { |
|
|
|
const { 0: request } = args; |
|
|
|
const { 0: request } = args; |
|
|
|
const { originalUrl, session } = request; |
|
|
|
const { path, session } = request; |
|
|
|
const { passport } = session; |
|
|
|
const { passport } = session; |
|
|
|
|
|
|
|
|
|
|
|
if (passport?.user) return succeed(...args); |
|
|
|
if (passport?.user) return succeed(...args); |
|
|
|
|
|
|
|
|
|
|
|
session.returnTo = getReturnTo?.call(null, ...args); |
|
|
|
const rt = getReturnTo?.call(null, ...args); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
stdout(`Unauthenticated access to ${path}`); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (rt) { |
|
|
|
|
|
|
|
stdout(`Set session.returnTo=${rt}`); |
|
|
|
|
|
|
|
|
|
|
|
stdout( |
|
|
|
session.returnTo = rt; |
|
|
|
`Unauthenticated access to ${originalUrl}; set return to ${session.returnTo}`, |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return fail(...args); |
|
|
|
return fail(rt, ...args); |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|