-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #441 from impresso/develop
Release v3.0.3
- Loading branch information
Showing
17 changed files
with
353 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,5 +21,6 @@ | |
}, | ||
"[json]": { | ||
"editor.formatOnSave": true | ||
} | ||
}, | ||
"mochaExplorer.files": "test/**/*.test.{ts,js}" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { HookContext, HookFunction } from '@feathersjs/feathers' | ||
import { FindResponse } from '../models/common' | ||
import { ImpressoApplication } from '../types' | ||
import { Redactable, RedactionPolicy, redactObject } from '../util/redaction' | ||
import { SlimUser } from '../authentication' | ||
|
||
export type RedactCondition = (context: HookContext<ImpressoApplication>) => boolean | ||
|
||
/** | ||
* Redact the response object using the provided redaction policy. | ||
* If the condition is provided, the redaction will only be applied if the condition is met. | ||
*/ | ||
export const redactResponse = <S>( | ||
policy: RedactionPolicy, | ||
condition?: (context: HookContext<ImpressoApplication>) => boolean | ||
): HookFunction<ImpressoApplication, S> => { | ||
return context => { | ||
if (context.type != 'after') throw new Error('The redactResponse hook should be used as an after hook only') | ||
|
||
if (condition != null && !condition(context)) return context | ||
|
||
if (context.result != null) { | ||
context.result = redactObject(context.result, policy) | ||
} | ||
return context | ||
} | ||
} | ||
|
||
/** | ||
* Redact the response object using the provided redaction policy. | ||
* Assumes that the response is a FindResponse object (has a `data` field with | ||
* an array of objects). | ||
* If the condition is provided, the redaction will only be applied if the condition is met. | ||
*/ | ||
export const redactResponseDataItem = <S>( | ||
policy: RedactionPolicy, | ||
condition?: (context: HookContext<ImpressoApplication>) => boolean, | ||
dataItemsField?: string | ||
): HookFunction<ImpressoApplication, S> => { | ||
return context => { | ||
if (context.type != 'after') throw new Error('The redactResponseDataItem hook should be used as an after hook only') | ||
|
||
if (condition != null && !condition(context)) return context | ||
|
||
if (context.result != null) { | ||
if (dataItemsField != null) { | ||
const result = context.result as Record<string, any> | ||
result[dataItemsField] = result[dataItemsField].map((item: Redactable) => redactObject(item, policy)) | ||
} else { | ||
const result = context.result as any as FindResponse<Redactable> | ||
result.data = result.data.map(item => redactObject(item, policy)) | ||
} | ||
} | ||
return context | ||
} | ||
} | ||
|
||
/** | ||
* Below are conditions that can be used in the redactResponse hook. | ||
*/ | ||
export const inPublicApi: RedactCondition = context => { | ||
return context.app.get('isPublicApi') == true | ||
} | ||
|
||
/** | ||
* Condition is: | ||
* - user is not authenticated | ||
* - OR user is authenticated and is not in the specified group | ||
*/ | ||
export const notInGroup = | ||
(groupName: string): RedactCondition => | ||
context => { | ||
const user = context.params?.user as any as SlimUser | ||
return user == null || !user.groups.includes(groupName) | ||
} | ||
|
||
const NoRedactionGroup = 'NoRedaction' | ||
|
||
/** | ||
* Default condition we should currently use: | ||
* - running as Public API | ||
* - AND user is not in the NoRedaction group | ||
*/ | ||
export const defaultCondition: RedactCondition = context => { | ||
return inPublicApi(context) && notInGroup(NoRedactionGroup)(context) | ||
} | ||
|
||
export type { RedactionPolicy } |
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,32 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "RedactionPolicy", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string" | ||
}, | ||
"items": { | ||
"type": "array", | ||
"items": { | ||
"$ref": "#/definitions/RedactionPolicyItem" | ||
} | ||
} | ||
}, | ||
"required": ["name", "items"], | ||
"definitions": { | ||
"RedactionPolicyItem": { | ||
"type": "object", | ||
"properties": { | ||
"jsonPath": { | ||
"type": "string" | ||
}, | ||
"valueConverterName": { | ||
"type": "string", | ||
"enum": ["redact", "contextNotAllowedImage", "remove", "emptyArray"] | ||
} | ||
}, | ||
"required": ["jsonPath", "valueConverterName"] | ||
} | ||
} | ||
} |
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
17 changes: 17 additions & 0 deletions
17
src/services/articles/resources/articleRedactionPolicy.yml
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 @@ | ||
# yaml-language-server: $schema=../../../schema/common/redactionPolicy.json | ||
name: artice-redaction-policy | ||
items: | ||
- jsonPath: $.title | ||
valueConverterName: redact | ||
- jsonPath: $.excerpt | ||
valueConverterName: redact | ||
- jsonPath: $.content | ||
valueConverterName: redact | ||
- jsonPath: $.regions | ||
valueConverterName: emptyArray | ||
- jsonPath: $.matches | ||
valueConverterName: emptyArray | ||
- jsonPath: $.pages[*].iiif | ||
valueConverterName: contextNotAllowedImage | ||
- jsonPath: $.pages[*].iiifThumbnail | ||
valueConverterName: contextNotAllowedImage |
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
Oops, something went wrong.