Skip to content

Commit

Permalink
Admin endpoint and initial permissions integration tests (#485)
Browse files Browse the repository at this point in the history
* added admin endpoint with available bitmaps and sample documents

* added first integration test for permissions

* import
  • Loading branch information
theorm authored Jan 10, 2025
1 parent 5608bc0 commit 2d5d21f
Show file tree
Hide file tree
Showing 70 changed files with 1,666 additions and 705 deletions.
2 changes: 1 addition & 1 deletion .mocharc-integration.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"exit": true,
"recursive": true,
"extension": ["ts", "js"],
"watch-files": ["src/**/*.js", "test/**/*.js"],
"watch-files": ["src/**/*.{js,ts}", "test/**/*.{js,ts}"],
"ignore": ["node_modules"]
}
4 changes: 2 additions & 2 deletions .mocharc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"package": "./package.json",
"reporter": "spec",
"ui": "bdd",
"recursive": true,
"exit": true,
"recursive": true,
"extension": ["ts", "js"],
"watch-files": ["src/**/*.{js,ts}", "test/**/*.{js,ts}"],
"ignore": ["node_modules", "test/integration/**/*"]
"ignore": ["node_modules", "test/integration/**/*.{js,ts}"]
}
80 changes: 69 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"copy-files": "tscp",
"test": "mocha",
"test-watch": "mocha --watch 'test/**/*.test.{js,ts}'",
"integration-test": "NODE_ENV=test mocha --config ./.mocharc-integration.json 'test/integration/**/*.test.js'",
"integration-test": "mocha --inspect --config ./.mocharc-integration.json",
"lintfix": "eslint src/. --config .eslintrc.js --fix",
"lint": "eslint src/. --config .eslintrc.js",
"lint-api-spec": "spectral lint http://localhost:3030/swagger.json ",
Expand Down Expand Up @@ -55,6 +55,7 @@
"async": "^2.6.1",
"aws-sdk": "^2.395.0",
"axios": "^1.6.7",
"bigint-buffer": "^1.1.5",
"body-parser": "^1.18.3",
"bufferutil": "4.0.8",
"cache-manager": "^6.3.1",
Expand Down Expand Up @@ -91,6 +92,7 @@
"helmet": "^3.21.1",
"http-proxy-middleware": "^2.0.1",
"impresso-jscommons": "https://github.com/impresso/impresso-jscommons/tarball/v1.4.3",
"json-bigint": "^1.0.0",
"json2csv": "^4.3.3",
"jsonpath-plus": "^10.0.1",
"jsonschema": "^1.4.1",
Expand Down Expand Up @@ -134,6 +136,7 @@
"@types/cache-manager": "^2.10.3",
"@types/generic-pool": "^3.1.9",
"@types/ioredis": "^4.28.5",
"@types/json-bigint": "^1.0.4",
"@types/mocha": "^10.0.6",
"@types/node": "^22.5.5",
"@types/node-fetch": "^2.5.6",
Expand All @@ -150,7 +153,7 @@
"prettier": "3.2.5",
"sinon": "^19.0.2",
"tsx": "^4.19.2",
"typescript": "5.6.3",
"typescript": "5.7.3",
"typescript-cp": "0.1.9"
}
}
21 changes: 10 additions & 11 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import express, { Application, static as staticMiddleware } from '@feathersjs/express'
import { feathers } from '@feathersjs/feathers'
import bodyParser from 'body-parser'
import compress from 'compression'
import path from 'path'
import appHooksFactory from './app.hooks'
import authentication from './authentication'
import cache from './cache'
import celery, { init as initCelery } from './celery'
import channels from './channels'
import configuration from './configuration'
import configuration, { Configuration } from './configuration'
import { init as simpleSolrClient } from './internalServices/simpleSolr'
import { startupJobs } from './jobs'
import middleware from './middleware'
import errorHandling from './middleware/errorHandling'
import openApiValidator, { init as initOpenApiValidator } from './middleware/openApiValidator'
import swagger from './middleware/swagger'
import transport from './middleware/transport'
import multer from './multer'
import redis, { init as initRedis } from './redis'
import sequelize from './sequelize'
import services from './services'
import rateLimiter from './services/internal/rateLimiter/redis'
import media from './services/media'
import proxy from './services/proxy'
import schemas from './services/schemas'
import { ImpressoApplication } from './types'
import { init as simpleSolrClient } from './internalServices/simpleSolr'
import path from 'path'
import compress from 'compression'
import middleware from './middleware'
import multer from './multer'
import { AppServices, ImpressoApplication } from './types'
import { customJsonMiddleware } from './util/express'

const helmet = require('helmet')
const cookieParser = require('cookie-parser')

const app: ImpressoApplication & Application = express(feathers())
const app: ImpressoApplication & Application<AppServices, Configuration> = express(feathers())

// Load app configuration
app.configure(configuration)
Expand All @@ -45,8 +45,7 @@ app.configure(simpleSolrClient)
app.use(helmet())
app.use(compress())
app.use(cookieParser())
// needed to access body in non-feathers middlewares, like openapi validator
app.use(bodyParser.json({ limit: '50mb' }))
app.use(customJsonMiddleware()) // JSON body parser / serializer

// configure local multer service.
app.configure(multer)
Expand Down
19 changes: 13 additions & 6 deletions src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ import User from './models/users.model'
import { docs } from './services/authentication/authentication.schema'
import { ImpressoApplication } from './types'
import { BufferUserPlanGuest } from './models/user-bitmap.model'
import { bigIntToBuffer, bufferToBigInt } from './util/bigint'

const debug = initDebug('impresso/authentication')

type AuthPayload = Omit<SlimUser, 'uid' | 'id' | 'bitmap'> & { userId: string; bitmap: number }
/**
* Using base64 for the bitmap to keep the size
* of the JWT token as small as possible.
*/
type AuthPayload = Omit<SlimUser, 'uid' | 'id' | 'bitmap'> & {
userId: string
bitmap: string // bigint as a base64 string
}

class CustomisedAuthenticationService extends AuthenticationService {
async getPayload(authResult: AuthenticationResult, params: AuthenticationParams) {
Expand All @@ -31,7 +39,9 @@ class CustomisedAuthenticationService extends AuthenticationService {
payload.groups = user.groups.map(d => d.name)
}
payload.isStaff = user.isStaff
payload.bitmap = Number(user.bitmap != null ? BigInt(user.bitmap) : BufferUserPlanGuest)
payload.bitmap = bigIntToBuffer(user.bitmap != null ? BigInt(user.bitmap) : BufferUserPlanGuest).toString(
'base64'
)
}
return payload
}
Expand Down Expand Up @@ -64,9 +74,6 @@ export interface SlimUser {
uid: string
id: number
isStaff: boolean
/**
* Bitmap as number Number(BigInt)
*/
bitmap: bigint
groups: string[]
}
Expand Down Expand Up @@ -99,7 +106,7 @@ class NoDBJWTStrategy extends JWTStrategy {
const slimUser: SlimUser = {
uid: payload.userId,
id: parseInt(payload.sub),
bitmap: payload.bitmap != null ? BigInt(payload.bitmap) : BufferUserPlanGuest,
bitmap: payload.bitmap != null ? bufferToBigInt(Buffer.from(payload.bitmap, 'base64')) : BufferUserPlanGuest,
isStaff: payload.isStaff ?? false,
groups: payload.groups ?? [],
}
Expand Down
Loading

0 comments on commit 2d5d21f

Please sign in to comment.