-
-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added IAuditUser to request with middleware (#6857)
Adds a middleware which adds our needed audit info as a separate object to the request.
- Loading branch information
Showing
8 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { auditAccessMiddleware } from './audit-middleware'; | ||
import { createTestConfig } from '../../test/config/test-config'; | ||
import express from 'express'; | ||
import noAuthentication from './no-authentication'; | ||
import type { IAuthRequest } from '../routes/unleash-types'; | ||
import type { IAuditUser } from '../types'; | ||
import supertest from 'supertest'; | ||
|
||
const config = createTestConfig(); | ||
|
||
describe('auditMiddleware testing', () => { | ||
test('Adds username and id from an IAuthRequest', async () => { | ||
const middleware = auditAccessMiddleware(config); | ||
const app = express(); | ||
noAuthentication('', app); | ||
app.use('', middleware); | ||
let audit: IAuditUser | undefined; | ||
app.get('/api/admin/test', (req: IAuthRequest, res) => { | ||
audit = req.audit; | ||
res.status(200).end(); | ||
}); | ||
const request = supertest(app); | ||
await request.get('/api/admin/test').expect(200); | ||
expect(audit).toBeDefined(); | ||
expect(audit!.id).toBe(-1); | ||
expect(audit!.username).toBe('unknown'); | ||
expect(audit!.ip).toBe('::ffff:127.0.0.1'); | ||
}); | ||
test('If no auth in place, does not add the audit object', async () => { | ||
const middleware = auditAccessMiddleware(config); | ||
const app = express(); | ||
app.use('', middleware); | ||
let audit: IAuditUser | undefined; | ||
app.get('/api/admin/test', (req: IAuthRequest, res) => { | ||
audit = req.audit; | ||
res.status(200).end(); | ||
}); | ||
const request = supertest(app); | ||
await request.get('/api/admin/test').expect(200); | ||
expect(audit).toBeUndefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { IUnleashConfig } from '../types'; | ||
import type { IApiRequest, IAuthRequest } from '../routes/unleash-types'; | ||
import { extractAuditInfo } from '../util'; | ||
|
||
export const auditAccessMiddleware = ({ | ||
getLogger, | ||
}: Pick<IUnleashConfig, 'getLogger'>): any => { | ||
const logger = getLogger('/middleware/audit-middleware.ts'); | ||
return (req: IAuthRequest | IApiRequest, _res, next) => { | ||
if (!req.user) { | ||
logger.info('Could not find user'); | ||
} else { | ||
req.audit = extractAuditInfo(req); | ||
} | ||
next(); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters