Updating authenticated middleware to typescript and updating the TTL once per minute rather than every API request.

This commit is contained in:
mike12345567 2022-08-05 15:21:12 +01:00
parent 29c0e07b7e
commit ba0e423fb9
1 changed files with 38 additions and 25 deletions

View File

@ -1,28 +1,39 @@
const { Cookies, Headers } = require("../constants") import { Cookies, Headers } from "../constants"
const { getCookie, clearCookie, openJwt } = require("../utils") import { getCookie, clearCookie, openJwt } from "../utils"
const { getUser } = require("../cache/user") import { getUser } from "../cache/user"
const { getSession, updateSessionTTL } = require("../security/sessions") import { getSession, updateSessionTTL } from "../security/sessions"
const { buildMatcherRegex, matches } = require("./matchers") import { buildMatcherRegex, matches } from "./matchers"
const env = require("../environment") import { SEPARATOR } from "../db/constants"
const { SEPARATOR } = require("../db/constants") import { ViewNames } from "../db/utils"
const { ViewNames } = require("../db/utils") import { queryGlobalView } from "../db/views"
const { queryGlobalView } = require("../db/views") import { getGlobalDB, doInTenant } from "../tenancy"
const { getGlobalDB, doInTenant } = require("../tenancy") import { decrypt } from "../security/encryption"
const { decrypt } = require("../security/encryption")
const identity = require("../context/identity") const identity = require("../context/identity")
const env = require("../environment")
function finalise( const ONE_MINUTE = 60 * 1000
ctx,
{ authenticated, user, internal, version, publicEndpoint } = {} interface FinaliseOpts {
) { authenticated?: boolean
ctx.publicEndpoint = publicEndpoint || false internal?: boolean
ctx.isAuthenticated = authenticated || false publicEndpoint?: boolean
ctx.user = user version?: string
ctx.internal = internal || false user?: any
ctx.version = version
} }
async function checkApiKey(apiKey, populateUser) { function timeMinusOneMinute() {
return new Date(Date.now() - ONE_MINUTE).toISOString()
}
function finalise(ctx: any, opts: FinaliseOpts = {}) {
ctx.publicEndpoint = opts.publicEndpoint || false
ctx.isAuthenticated = opts.authenticated || false
ctx.user = opts.user
ctx.internal = opts.internal || false
ctx.version = opts.version
}
async function checkApiKey(apiKey: string, populateUser?: Function) {
if (apiKey === env.INTERNAL_API_KEY) { if (apiKey === env.INTERNAL_API_KEY) {
return { valid: true } return { valid: true }
} }
@ -56,10 +67,12 @@ async function checkApiKey(apiKey, populateUser) {
*/ */
module.exports = ( module.exports = (
noAuthPatterns = [], noAuthPatterns = [],
opts = { publicAllowed: false, populateUser: null } opts: { publicAllowed: boolean; populateUser?: Function } = {
publicAllowed: false,
}
) => { ) => {
const noAuthOptions = noAuthPatterns ? buildMatcherRegex(noAuthPatterns) : [] const noAuthOptions = noAuthPatterns ? buildMatcherRegex(noAuthPatterns) : []
return async (ctx, next) => { return async (ctx: any, next: any) => {
let publicEndpoint = false let publicEndpoint = false
const version = ctx.request.headers[Headers.API_VER] const version = ctx.request.headers[Headers.API_VER]
// the path is not authenticated // the path is not authenticated
@ -103,7 +116,7 @@ module.exports = (
console.error("Auth Error", error) console.error("Auth Error", error)
// remove the cookie as the user does not exist anymore // remove the cookie as the user does not exist anymore
clearCookie(ctx, Cookies.Auth) clearCookie(ctx, Cookies.Auth)
} else { } else if (session?.lastAccessedAt < timeMinusOneMinute()) {
// make sure we denote that the session is still in use // make sure we denote that the session is still in use
await updateSessionTTL(session) await updateSessionTTL(session)
} }
@ -142,7 +155,7 @@ module.exports = (
} else { } else {
return next() return next()
} }
} catch (err) { } catch (err: any) {
// invalid token, clear the cookie // invalid token, clear the cookie
if (err && err.name === "JsonWebTokenError") { if (err && err.name === "JsonWebTokenError") {
clearCookie(ctx, Cookies.Auth) clearCookie(ctx, Cookies.Auth)