-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
133 lines (115 loc) · 3.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { AccessControlError, verifyAndDecodeToken } from "./utils"
import { addNoise, hash, generalize } from "value-anonymizer";
import { SchemaDirectiveVisitor } from "graphql-tools";
import { defaultFieldResolver, GraphQLDirective, DirectiveLocation } from "graphql";
class AnonymizationSchemaDirectiveVisitor extends SchemaDirectiveVisitor{
//https://www.apollographql.com/blog/graphql/directives/eusable-graphql-schema-directives/
static getDirectiveDeclaration(directiveName) {
return new GraphQLDirective({
name: directiveName,
locations: [
DirectiveLocation.FIELD_DEFINITION,
],
});
}
async getAnonymizedResult(resolve, anonymizationCallback, result, args, context, info){
const res = await resolve.apply(this,[result, args, context, info]);
const token = verifyAndDecodeToken(context);
const role = token.role;
var parameters = this.getAnonymizationParameter(role, result, args, context, info);
if(!parameters) {
throw new AccessControlError("No Parameters given.")
}
return anonymizationCallback(res, parameters);
}
}
export class NoiseDirective extends AnonymizationSchemaDirectiveVisitor{
visitFieldDefinition(field){
const {resolve = defaultFieldResolver} = field;
field.resolve = async function(result, args, context, info){
return this.getAnonymizedResult(resolve, addNoise, result, args, context, info);
}.bind(this);
}
}
export const noiseParameters = {
standardNormal: {
typeOfDistribution: "normal",
distributionParameters:{
mean: 0,
standardDeviation: 1,
},
valueParameters:{
isInt: false,
}
},
laplace: {
typeOfDistribution: "normal",
distributionParameters:{
mean: 0,
standardDeviation: 1,
},
valueParameters:{
isInt: false,
}
},
}
export class HashDirective extends AnonymizationSchemaDirectiveVisitor{
visitFieldDefinition(field){
const {resolve = defaultFieldResolver} = field;
field.resolve = async function(result, args, context, info){
return this.getAnonymizedResult(resolve, hash, result, args, context, info);
}.bind(this);
}
}
export const hashingParameters = {
sha256: {
hashingParameters: {
outputLength: 256
}
},
sha256Hex: {
hashingParameters: {
outputLength: 256
},
convertion: "Hex"
}
}
export class GeneralizationDirective extends AnonymizationSchemaDirectiveVisitor{
visitFieldDefinition(field){
const {resolve = defaultFieldResolver} = field;
field.resolve = async function(result, args, context, info){
return this.getAnonymizedResult(resolve, generalize, result, args, context, info);
}.bind(this);
}
}
export const generalizationParameters = {
hideFromPosition3: {
generalizationParameters: {
hideCharactersFromPosition: 3,
numberOfHideCharacters: 3
}
},
}
export class SuppressDirective extends SchemaDirectiveVisitor{
static getDirectiveDeclaration(directiveName, schema) {
return new GraphQLDirective({
name: directiveName,
locations: [
DirectiveLocation.FIELD_DEFINITION,
]
});
}
visitFieldDefinition(field){
const {resolve = defaultFieldResolver} = field;
field.resolve = async function(result, args, context, info){
const res = await resolve.apply(this,[result, args, context, info]);
const token = verifyAndDecodeToken(context);
const role = token.role;
const allowedRoles = this.getAllowedRoles(info)
if(!allowedRoles || !allowedRoles.includes(role)) {
return null;
}
return res;
}.bind(this);
}
}