@aws-amplify/graphql-auth-transformer 0.10.0 → 0.10.1-alpha.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/lib/accesscontrol/acm.d.ts +2 -2
- package/lib/accesscontrol/acm.d.ts.map +1 -1
- package/lib/accesscontrol/acm.js +22 -25
- package/lib/accesscontrol/acm.js.map +1 -1
- package/lib/graphql-auth-transformer.d.ts +4 -4
- package/lib/graphql-auth-transformer.d.ts.map +1 -1
- package/lib/graphql-auth-transformer.js +115 -91
- package/lib/graphql-auth-transformer.js.map +1 -1
- package/lib/resolvers/helpers.d.ts.map +1 -1
- package/lib/resolvers/helpers.js +12 -10
- package/lib/resolvers/helpers.js.map +1 -1
- package/lib/resolvers/mutation.create.js +1 -1
- package/lib/resolvers/mutation.create.js.map +1 -1
- package/lib/resolvers/mutation.delete.js +1 -1
- package/lib/resolvers/mutation.delete.js.map +1 -1
- package/lib/resolvers/mutation.update.js +1 -1
- package/lib/resolvers/mutation.update.js.map +1 -1
- package/lib/resolvers/query.d.ts.map +1 -1
- package/lib/resolvers/query.js +4 -1
- package/lib/resolvers/query.js.map +1 -1
- package/lib/utils/constants.d.ts +1 -0
- package/lib/utils/constants.d.ts.map +1 -1
- package/lib/utils/constants.js +3 -2
- package/lib/utils/constants.js.map +1 -1
- package/lib/utils/definitions.d.ts +2 -3
- package/lib/utils/definitions.d.ts.map +1 -1
- package/lib/utils/definitions.js +5 -0
- package/lib/utils/definitions.js.map +1 -1
- package/lib/utils/index.d.ts +1 -3
- package/lib/utils/index.d.ts.map +1 -1
- package/lib/utils/index.js +56 -45
- package/lib/utils/index.js.map +1 -1
- package/lib/utils/schema.d.ts.map +1 -1
- package/lib/utils/schema.js +19 -23
- package/lib/utils/schema.js.map +1 -1
- package/package.json +10 -10
|
@@ -32,7 +32,9 @@ const graphql_transformer_common_1 = require("graphql-transformer-common");
|
|
|
32
32
|
const iam = __importStar(require("@aws-cdk/aws-iam"));
|
|
33
33
|
const cdk = __importStar(require("@aws-cdk/core"));
|
|
34
34
|
const graphql_relational_transformer_1 = require("@aws-amplify/graphql-relational-transformer");
|
|
35
|
+
const graphql_transformer_core_2 = require("@aws-amplify/graphql-transformer-core");
|
|
35
36
|
const resolvers_1 = require("./resolvers");
|
|
37
|
+
const field_1 = require("./resolvers/field");
|
|
36
38
|
const accesscontrol_1 = require("./accesscontrol");
|
|
37
39
|
const utils_1 = require("./utils");
|
|
38
40
|
const warnings_1 = require("./utils/warnings");
|
|
@@ -90,7 +92,7 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
90
92
|
const modelDirective = (_a = parent.directives) === null || _a === void 0 ? void 0 : _a.find(dir => dir.name.value === 'model');
|
|
91
93
|
const typeName = parent.name.value;
|
|
92
94
|
const fieldName = field.name.value;
|
|
93
|
-
const rules = (0, utils_1.getAuthDirectiveRules)(new graphql_transformer_core_1.DirectiveWrapper(directive));
|
|
95
|
+
const rules = (0, utils_1.getAuthDirectiveRules)(new graphql_transformer_core_1.DirectiveWrapper(directive), true);
|
|
94
96
|
(0, utils_1.validateFieldRules)(new graphql_transformer_core_1.DirectiveWrapper(directive), isParentTypeBuiltinType, modelDirective !== undefined, field.name.value);
|
|
95
97
|
(0, utils_1.validateRules)(rules, this.configuredAuthProviders, field.name.value);
|
|
96
98
|
this.setAuthPolicyFlag(rules);
|
|
@@ -118,10 +120,10 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
118
120
|
const typeFieldName = `${typeName}:${fieldName}`;
|
|
119
121
|
const acm = new accesscontrol_1.AccessControlMatrix({
|
|
120
122
|
name: typeFieldName,
|
|
121
|
-
operations: ['
|
|
123
|
+
operations: ['list', 'get', 'search', 'listen', 'sync'],
|
|
122
124
|
resources: [typeFieldName],
|
|
123
125
|
});
|
|
124
|
-
this.convertRulesToRoles(acm, staticRules, false, typeFieldName, ['
|
|
126
|
+
this.convertRulesToRoles(acm, staticRules, false, typeFieldName, ['list', 'get', 'search', 'listen', 'sync']);
|
|
125
127
|
this.authNonModelConfig.set(typeFieldName, acm);
|
|
126
128
|
}
|
|
127
129
|
};
|
|
@@ -135,7 +137,7 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
135
137
|
this.authModelConfig.forEach((acm, modelName) => {
|
|
136
138
|
const def = context.output.getObject(modelName);
|
|
137
139
|
const modelHasSearchable = def.directives.some(dir => dir.name.value === 'searchable');
|
|
138
|
-
|
|
140
|
+
this.addFieldsToObject(context, modelName, getOwnerFields(acm));
|
|
139
141
|
const providers = this.getAuthProviders(acm.getRoles());
|
|
140
142
|
const directives = this.getServiceDirectives(providers, providers.length === 0 ? this.shouldAddDefaultServiceDirective() : false);
|
|
141
143
|
if (modelHasSearchable) {
|
|
@@ -170,8 +172,6 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
170
172
|
const def = context.output.getObject(modelName);
|
|
171
173
|
const modelNameConfig = this.modelDirectiveConfig.get(modelName);
|
|
172
174
|
const searchableDirective = def.directives.find(dir => dir.name.value === 'searchable');
|
|
173
|
-
const readRoles = acm.getRolesPerOperation('read');
|
|
174
|
-
const roleDefinitions = readRoles.map(role => this.roleMap.get(role));
|
|
175
175
|
const queryFields = (0, utils_1.getQueryFieldNames)(this.modelDirectiveConfig.get(modelName));
|
|
176
176
|
queryFields.forEach(query => {
|
|
177
177
|
switch (query.type) {
|
|
@@ -198,11 +198,12 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
198
198
|
const config = (0, utils_1.getSearchableConfig)(searchableDirective, modelName);
|
|
199
199
|
this.protectSearchResolver(context, def, context.output.getQueryTypeName(), config.queries.search, acm);
|
|
200
200
|
}
|
|
201
|
-
const
|
|
201
|
+
const readRoles = [...new Set(...utils_1.READ_MODEL_OPERATIONS.map(op => acm.getRolesPerOperation(op)))];
|
|
202
|
+
const modelFields = (_b = (_a = def.fields) === null || _a === void 0 ? void 0 : _a.filter((f) => acm.hasResource(f.name.value))) !== null && _b !== void 0 ? _b : [];
|
|
202
203
|
const errorFields = new Array();
|
|
203
|
-
modelFields.forEach(field => {
|
|
204
|
-
const fieldReadRoles =
|
|
205
|
-
const allowedRoles = fieldReadRoles.filter(r => acm.isAllowed(r, field.name.value,
|
|
204
|
+
modelFields.forEach((field) => {
|
|
205
|
+
const fieldReadRoles = getReadRolesForField(acm, readRoles, field.name.value);
|
|
206
|
+
const allowedRoles = fieldReadRoles.filter(r => utils_1.READ_MODEL_OPERATIONS.some(op => acm.isAllowed(r, field.name.value, op)));
|
|
206
207
|
const needsFieldResolver = allowedRoles.length < fieldReadRoles.length;
|
|
207
208
|
if (needsFieldResolver && field.type.kind === graphql_1.Kind.NON_NULL_TYPE) {
|
|
208
209
|
errorFields.push(field.name.value);
|
|
@@ -235,12 +236,15 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
235
236
|
}
|
|
236
237
|
});
|
|
237
238
|
const subscriptionFieldNames = (0, utils_1.getSubscriptionFieldNames)(this.modelDirectiveConfig.get(modelName));
|
|
238
|
-
const subscriptionRoles =
|
|
239
|
+
const subscriptionRoles = acm
|
|
240
|
+
.getRolesPerOperation('listen')
|
|
241
|
+
.map(role => this.roleMap.get(role))
|
|
239
242
|
.filter(roleDef => { var _a; return (roleDef.strategy === 'owner' && !(0, utils_1.fieldIsList)((_a = def.fields) !== null && _a !== void 0 ? _a : [], roleDef.entity)) || roleDef.static; });
|
|
240
243
|
subscriptionFieldNames.forEach(subscription => {
|
|
241
244
|
this.protectSubscriptionResolver(context, subscription.typeName, subscription.fieldName, subscriptionRoles);
|
|
242
245
|
});
|
|
243
246
|
if (context.featureFlags.getBoolean('useSubUsernameForDefaultIdentityClaim')) {
|
|
247
|
+
const roleDefinitions = acm.getRoles().map(role => this.roleMap.get(role));
|
|
244
248
|
roleDefinitions.forEach(role => {
|
|
245
249
|
var _a, _b;
|
|
246
250
|
const hasMultiClaims = ((_b = (_a = role.claim) === null || _a === void 0 ? void 0 : _a.split(utils_1.IDENTITY_CLAIM_DELIMITER)) === null || _b === void 0 ? void 0 : _b.length) > 1;
|
|
@@ -285,42 +289,43 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
285
289
|
this.addOperationToResourceReferences(typeName, operationName, acm.getRoles());
|
|
286
290
|
}
|
|
287
291
|
};
|
|
288
|
-
addServiceDirective(ctx.output.getQueryTypeName(), '
|
|
289
|
-
addServiceDirective(ctx.output.getQueryTypeName(), '
|
|
290
|
-
addServiceDirective(ctx.output.getQueryTypeName(), '
|
|
292
|
+
addServiceDirective(ctx.output.getQueryTypeName(), 'get', (_a = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.queries) === null || _a === void 0 ? void 0 : _a.get);
|
|
293
|
+
addServiceDirective(ctx.output.getQueryTypeName(), 'list', (_b = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.queries) === null || _b === void 0 ? void 0 : _b.list);
|
|
294
|
+
addServiceDirective(ctx.output.getQueryTypeName(), 'sync', (_c = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.queries) === null || _c === void 0 ? void 0 : _c.sync);
|
|
291
295
|
addServiceDirective(ctx.output.getMutationTypeName(), 'create', (_d = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.mutations) === null || _d === void 0 ? void 0 : _d.create);
|
|
292
296
|
addServiceDirective(ctx.output.getMutationTypeName(), 'update', (_e = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.mutations) === null || _e === void 0 ? void 0 : _e.update);
|
|
293
297
|
addServiceDirective(ctx.output.getMutationTypeName(), 'delete', (_f = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.mutations) === null || _f === void 0 ? void 0 : _f.delete);
|
|
294
298
|
if (ctx.metadata.has(indexKeyName)) {
|
|
295
299
|
ctx.metadata.get(indexKeyName).forEach(index => {
|
|
296
|
-
addServiceDirective(ctx.output.getQueryTypeName(), '
|
|
300
|
+
addServiceDirective(ctx.output.getQueryTypeName(), 'list', index.split(':')[1]);
|
|
301
|
+
addServiceDirective(ctx.output.getQueryTypeName(), 'get', index.split(':')[1]);
|
|
297
302
|
});
|
|
298
303
|
}
|
|
299
304
|
if (searchableDirective) {
|
|
300
305
|
const config = (0, utils_1.getSearchableConfig)(searchableDirective, def.name.value);
|
|
301
|
-
addServiceDirective(ctx.output.getQueryTypeName(), '
|
|
306
|
+
addServiceDirective(ctx.output.getQueryTypeName(), 'search', config.queries.search);
|
|
302
307
|
}
|
|
303
308
|
const subscriptions = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.subscriptions;
|
|
304
309
|
if ((subscriptions === null || subscriptions === void 0 ? void 0 : subscriptions.level) === graphql_model_transformer_1.SubscriptionLevel.on) {
|
|
305
310
|
const subscriptionArguments = acm
|
|
306
|
-
.getRolesPerOperation('
|
|
311
|
+
.getRolesPerOperation('listen')
|
|
307
312
|
.map(role => this.roleMap.get(role))
|
|
308
313
|
.filter(roleDef => { var _a; return roleDef.strategy === 'owner' && !(0, utils_1.fieldIsList)((_a = def.fields) !== null && _a !== void 0 ? _a : [], roleDef.entity); });
|
|
309
314
|
if (subscriptions.onCreate && ((_g = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.mutations) === null || _g === void 0 ? void 0 : _g.create)) {
|
|
310
315
|
subscriptions.onCreate.forEach(onCreateSub => {
|
|
311
|
-
addServiceDirective(ctx.output.getSubscriptionTypeName(), '
|
|
316
|
+
addServiceDirective(ctx.output.getSubscriptionTypeName(), 'listen', onCreateSub);
|
|
312
317
|
(0, utils_1.addSubscriptionArguments)(ctx, onCreateSub, subscriptionArguments);
|
|
313
318
|
});
|
|
314
319
|
}
|
|
315
320
|
if (subscriptions.onUpdate && ((_h = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.mutations) === null || _h === void 0 ? void 0 : _h.update)) {
|
|
316
321
|
subscriptions.onUpdate.forEach(onUpdateSub => {
|
|
317
|
-
addServiceDirective(ctx.output.getSubscriptionTypeName(), '
|
|
322
|
+
addServiceDirective(ctx.output.getSubscriptionTypeName(), 'listen', onUpdateSub);
|
|
318
323
|
(0, utils_1.addSubscriptionArguments)(ctx, onUpdateSub, subscriptionArguments);
|
|
319
324
|
});
|
|
320
325
|
}
|
|
321
326
|
if (subscriptions.onDelete && ((_j = modelConfig === null || modelConfig === void 0 ? void 0 : modelConfig.mutations) === null || _j === void 0 ? void 0 : _j.delete)) {
|
|
322
327
|
subscriptions.onDelete.forEach(onDeleteSub => {
|
|
323
|
-
addServiceDirective(ctx.output.getSubscriptionTypeName(), '
|
|
328
|
+
addServiceDirective(ctx.output.getSubscriptionTypeName(), 'listen', onDeleteSub);
|
|
324
329
|
(0, utils_1.addSubscriptionArguments)(ctx, onDeleteSub, subscriptionArguments);
|
|
325
330
|
});
|
|
326
331
|
}
|
|
@@ -329,7 +334,7 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
329
334
|
this.protectGetResolver = (ctx, def, typeName, fieldName, acm) => {
|
|
330
335
|
var _a;
|
|
331
336
|
const resolver = ctx.resolvers.getResolver(typeName, fieldName);
|
|
332
|
-
const roleDefinitions = acm.getRolesPerOperation('
|
|
337
|
+
const roleDefinitions = acm.getRolesPerOperation('get').map(r => this.roleMap.get(r));
|
|
333
338
|
const tableKeySchema = (0, graphql_transformer_core_1.getTable)(ctx, def).keySchema;
|
|
334
339
|
const primaryFields = tableKeySchema.map(att => att.attributeName);
|
|
335
340
|
const primaryKey = (0, utils_1.getPartitionKey)(tableKeySchema);
|
|
@@ -339,7 +344,7 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
339
344
|
this.protectListResolver = (ctx, def, typeName, fieldName, acm, indexName) => {
|
|
340
345
|
var _a;
|
|
341
346
|
const resolver = ctx.resolvers.getResolver(typeName, fieldName);
|
|
342
|
-
const roleDefinitions = acm.getRolesPerOperation('
|
|
347
|
+
const roleDefinitions = acm.getRolesPerOperation('list').map(r => this.roleMap.get(r));
|
|
343
348
|
let primaryFields;
|
|
344
349
|
let partitionKey;
|
|
345
350
|
const table = (0, graphql_transformer_core_1.getTable)(ctx, def);
|
|
@@ -362,15 +367,21 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
362
367
|
var _a, _b, _c;
|
|
363
368
|
let fieldAuthExpression;
|
|
364
369
|
let relatedAuthExpression;
|
|
365
|
-
const relatedModelObject =
|
|
370
|
+
const relatedModelObject = this.getRelatedModelObject(ctx, (0, graphql_transformer_common_1.getBaseType)(field.type));
|
|
366
371
|
if (this.authModelConfig.has(relatedModelObject.name.value)) {
|
|
367
372
|
const acm = this.authModelConfig.get(relatedModelObject.name.value);
|
|
368
|
-
const roleDefinitions =
|
|
373
|
+
const roleDefinitions = [...new Set([
|
|
374
|
+
...acm.getRolesPerOperation('get'),
|
|
375
|
+
...acm.getRolesPerOperation('list'),
|
|
376
|
+
...acm.getRolesPerOperation('sync'),
|
|
377
|
+
...acm.getRolesPerOperation('search'),
|
|
378
|
+
...acm.getRolesPerOperation('listen'),
|
|
379
|
+
])].map(r => this.roleMap.get(r));
|
|
369
380
|
const relationalPrimaryMap = (0, utils_1.getRelationalPrimaryMap)(ctx, def, field, relatedModelObject);
|
|
370
381
|
relatedAuthExpression = (0, resolvers_1.generateAuthExpressionForRelationQuery)(this.configuredAuthProviders, roleDefinitions, (_a = relatedModelObject.fields) !== null && _a !== void 0 ? _a : [], relationalPrimaryMap);
|
|
371
382
|
}
|
|
372
383
|
else {
|
|
373
|
-
relatedAuthExpression = (0,
|
|
384
|
+
relatedAuthExpression = (0, field_1.generateSandboxExpressionForField)(ctx.sandboxModeEnabled);
|
|
374
385
|
}
|
|
375
386
|
if (fieldRoles) {
|
|
376
387
|
const roleDefinitions = fieldRoles.map(r => this.roleMap.get(r));
|
|
@@ -390,7 +401,7 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
390
401
|
var _a;
|
|
391
402
|
if (ctx.isProjectUsingDataStore()) {
|
|
392
403
|
const resolver = ctx.resolvers.getResolver(typeName, fieldName);
|
|
393
|
-
const roleDefinitions = acm.getRolesPerOperation('
|
|
404
|
+
const roleDefinitions = acm.getRolesPerOperation('sync').map(r => this.roleMap.get(r));
|
|
394
405
|
const primaryFields = (0, graphql_transformer_core_1.getTable)(ctx, def).keySchema.map(att => att.attributeName);
|
|
395
406
|
const authExpression = (0, resolvers_1.generateAuthExpressionForQueries)(this.configuredAuthProviders, roleDefinitions, (_a = def.fields) !== null && _a !== void 0 ? _a : [], primaryFields);
|
|
396
407
|
resolver.addToSlot('auth', graphql_transformer_core_1.MappingTemplate.s3MappingTemplateFromString(authExpression, `${typeName}.${fieldName}.{slotName}.{slotIndex}.req.vtl`));
|
|
@@ -403,8 +414,8 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
403
414
|
const allowedAggFields = modelFields.map(f => f.name.value).filter(f => !acmFields.includes(f));
|
|
404
415
|
let leastAllowedFields = acmFields;
|
|
405
416
|
const resolver = ctx.resolvers.getResolver(typeName, fieldName);
|
|
406
|
-
const readRoleDefinitions = acm.getRolesPerOperation('
|
|
407
|
-
const allowedFields = acmFields.filter(resource => acm.isAllowed(role, resource, '
|
|
417
|
+
const readRoleDefinitions = acm.getRolesPerOperation('search').map(role => {
|
|
418
|
+
const allowedFields = acmFields.filter(resource => acm.isAllowed(role, resource, 'search'));
|
|
408
419
|
const roleDefinition = this.roleMap.get(role);
|
|
409
420
|
if (allowedFields.length !== acmFields.length || !roleDefinition.static) {
|
|
410
421
|
roleDefinition.allowedFields = allowedFields;
|
|
@@ -489,6 +500,26 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
489
500
|
const authExpression = (0, resolvers_1.generateAuthExpressionForSubscriptions)(this.configuredAuthProviders, subscriptionRoles);
|
|
490
501
|
resolver.addToSlot('auth', graphql_transformer_core_1.MappingTemplate.s3MappingTemplateFromString(authExpression, `${typeName}.${fieldName}.{slotName}.{slotIndex}.req.vtl`));
|
|
491
502
|
};
|
|
503
|
+
this.getRelatedModelObject = (ctx, typeName) => {
|
|
504
|
+
const modelObjectName = graphql_transformer_common_1.ModelResourceIDs.IsModelConnectionType(typeName)
|
|
505
|
+
? graphql_transformer_common_1.ModelResourceIDs.GetModelFromConnectionType(typeName)
|
|
506
|
+
: typeName;
|
|
507
|
+
if (!ctx.output.hasType(modelObjectName)) {
|
|
508
|
+
throw new graphql_transformer_core_1.TransformerContractError(`Could not find type: ${modelObjectName}`);
|
|
509
|
+
}
|
|
510
|
+
else {
|
|
511
|
+
return ctx.output.getObject(modelObjectName);
|
|
512
|
+
}
|
|
513
|
+
};
|
|
514
|
+
this.addFieldsToObject = (ctx, modelName, ownerFields) => {
|
|
515
|
+
const modelObject = ctx.output.getObject(modelName);
|
|
516
|
+
const existingFields = (0, utils_1.collectFieldNames)(modelObject);
|
|
517
|
+
const ownerFieldsToAdd = ownerFields.filter(field => !existingFields.includes(field));
|
|
518
|
+
ownerFieldsToAdd.forEach(ownerField => {
|
|
519
|
+
modelObject.fields.push((0, graphql_transformer_common_1.makeField)(ownerField, [], (0, graphql_transformer_common_1.makeNamedType)('String')));
|
|
520
|
+
});
|
|
521
|
+
ctx.output.putType(modelObject);
|
|
522
|
+
};
|
|
492
523
|
this.addAutoGeneratedFields = (ctx, def, allowedFields, fields) => {
|
|
493
524
|
const allowedFieldsSet = new Set(allowedFields);
|
|
494
525
|
this.addAutoGeneratedRelationalFields(ctx, def, allowedFieldsSet, fields);
|
|
@@ -498,8 +529,8 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
498
529
|
};
|
|
499
530
|
this.addAutoGeneratedRelationalFields = (ctx, def, allowedFields, fields) => {
|
|
500
531
|
const typeDefinitions = ctx.inputDocument.definitions.filter(it => it.kind === 'ObjectTypeDefinition');
|
|
501
|
-
this.addAutoGeneratedHasManyFields(typeDefinitions, def, allowedFields);
|
|
502
|
-
|
|
532
|
+
this.addAutoGeneratedHasManyFields(ctx, typeDefinitions, def, allowedFields);
|
|
533
|
+
this.addAutoGeneratedHasOneFields(ctx, typeDefinitions, fields, def, allowedFields);
|
|
503
534
|
};
|
|
504
535
|
this.addAutoGeneratedIndexFields = (definition, allowedFields) => {
|
|
505
536
|
var _a;
|
|
@@ -514,13 +545,34 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
514
545
|
}
|
|
515
546
|
});
|
|
516
547
|
};
|
|
517
|
-
this.addAutoGeneratedHasManyFields = (typeDefinitions, def, allowedFields) => {
|
|
548
|
+
this.addAutoGeneratedHasManyFields = (ctx, typeDefinitions, def, allowedFields) => {
|
|
518
549
|
const hasManyRelatedFields = typeDefinitions
|
|
519
|
-
.map(it => it.fields.map(field => ({ ...field, relatedType: it
|
|
550
|
+
.map(it => it.fields.map(field => ({ ...field, relatedType: it })))
|
|
520
551
|
.flat()
|
|
521
552
|
.filter(it => { var _a; return (0, graphql_transformer_common_1.getBaseType)(it.type) === def.name.value && ((_a = it.directives) === null || _a === void 0 ? void 0 : _a.some(d => d.name.value === 'hasMany')); });
|
|
522
553
|
hasManyRelatedFields.forEach(relatedField => {
|
|
523
|
-
allowedFields.add((0, graphql_relational_transformer_1.getConnectionAttributeName)(relatedField.relatedType, relatedField.name.value));
|
|
554
|
+
allowedFields.add((0, graphql_relational_transformer_1.getConnectionAttributeName)(ctx.featureFlags, relatedField.relatedType.name.value, relatedField.name.value, (0, graphql_relational_transformer_1.getObjectPrimaryKey)(relatedField.relatedType).name.value));
|
|
555
|
+
(0, graphql_transformer_core_2.getSortKeyFieldNames)(relatedField.relatedType).forEach(sortKeyFieldName => {
|
|
556
|
+
allowedFields.add((0, graphql_relational_transformer_1.getSortKeyConnectionAttributeName)(relatedField.relatedType.name.value, relatedField.name.value, sortKeyFieldName));
|
|
557
|
+
});
|
|
558
|
+
});
|
|
559
|
+
};
|
|
560
|
+
this.addAutoGeneratedHasOneFields = (ctx, typeDefinitions, fields, def, allowedFields) => {
|
|
561
|
+
fields.forEach(field => {
|
|
562
|
+
var _a;
|
|
563
|
+
const modelField = def.fields.find(it => it.name.value === field);
|
|
564
|
+
const directives = (_a = modelField.directives) === null || _a === void 0 ? void 0 : _a.filter(dir => { var _a; return !((_a = dir.arguments) === null || _a === void 0 ? void 0 : _a.some(it => it.name.value === 'fields')) && (dir.name.value === 'hasOne' || dir.name.value === 'belongsTo'); });
|
|
565
|
+
directives.forEach(directive => {
|
|
566
|
+
const relatedType = typeDefinitions.find(it => it.name.value === (0, graphql_transformer_common_1.getBaseType)(modelField.type));
|
|
567
|
+
if (directive.name.value === 'hasOne' ||
|
|
568
|
+
(directive.name.value === 'belongsTo' &&
|
|
569
|
+
relatedType.fields.some(f => { var _a; return (0, graphql_transformer_common_1.getBaseType)(f.type) === def.name.value && ((_a = f.directives) === null || _a === void 0 ? void 0 : _a.some(d => d.name.value === 'hasOne')); }))) {
|
|
570
|
+
allowedFields.add((0, graphql_relational_transformer_1.getConnectionAttributeName)(ctx.featureFlags, def.name.value, field, (0, graphql_relational_transformer_1.getObjectPrimaryKey)(relatedType).name.value));
|
|
571
|
+
(0, graphql_transformer_core_2.getSortKeyFieldNames)(def).forEach(sortKeyFieldName => {
|
|
572
|
+
allowedFields.add((0, graphql_relational_transformer_1.getSortKeyConnectionAttributeName)(def.name.value, field, sortKeyFieldName));
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
});
|
|
524
576
|
});
|
|
525
577
|
};
|
|
526
578
|
this.addAutoGeneratedDataStoreFields = (ctx, allowedFields) => {
|
|
@@ -643,26 +695,6 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
643
695
|
}
|
|
644
696
|
return Array.from(providers);
|
|
645
697
|
}
|
|
646
|
-
static getRelatedModelObject(ctx, typeName) {
|
|
647
|
-
const modelObjectName = graphql_transformer_common_1.ModelResourceIDs.IsModelConnectionType(typeName)
|
|
648
|
-
? graphql_transformer_common_1.ModelResourceIDs.GetModelFromConnectionType(typeName)
|
|
649
|
-
: typeName;
|
|
650
|
-
if (!ctx.output.hasType(modelObjectName)) {
|
|
651
|
-
throw new graphql_transformer_core_1.TransformerContractError(`Could not find type: ${modelObjectName}`);
|
|
652
|
-
}
|
|
653
|
-
else {
|
|
654
|
-
return ctx.output.getObject(modelObjectName);
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
static addFieldsToObject(ctx, modelName, ownerFields) {
|
|
658
|
-
const modelObject = ctx.output.getObject(modelName);
|
|
659
|
-
const existingFields = (0, utils_1.collectFieldNames)(modelObject);
|
|
660
|
-
const ownerFieldsToAdd = ownerFields.filter(field => !existingFields.includes(field));
|
|
661
|
-
ownerFieldsToAdd.forEach(ownerField => {
|
|
662
|
-
modelObject.fields.push((0, graphql_transformer_common_1.makeField)(ownerField, [], (0, graphql_transformer_common_1.makeNamedType)('String')));
|
|
663
|
-
});
|
|
664
|
-
ctx.output.putType(modelObject);
|
|
665
|
-
}
|
|
666
698
|
propagateAuthDirectivesToNestedTypes(ctx, def, providers) {
|
|
667
699
|
const nonModelTypePredicate = (fieldType) => {
|
|
668
700
|
if (fieldType) {
|
|
@@ -736,14 +768,14 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
736
768
|
const authPolicyDocuments = (0, utils_1.createPolicyDocumentForManagedPolicy)(this.authPolicyResources);
|
|
737
769
|
const { rootStack } = ctx.stackManager;
|
|
738
770
|
const iamAuthRoleArn = iam.Role.fromRoleArn(rootStack, 'auth-role-name', `arn:aws:iam::${cdk.Stack.of(rootStack).account}:role/${authRoleParameter}`);
|
|
739
|
-
|
|
771
|
+
authPolicyDocuments.forEach((authPolicyDocument, i) => {
|
|
740
772
|
const paddedIndex = `${i + 1}`.padStart(2, '0');
|
|
741
773
|
const resourceName = `${graphql_transformer_common_1.ResourceConstants.RESOURCES.AuthRolePolicy}${paddedIndex}`;
|
|
742
774
|
new iam.ManagedPolicy(rootStack, resourceName, {
|
|
743
|
-
document: iam.PolicyDocument.fromJson(
|
|
775
|
+
document: iam.PolicyDocument.fromJson(authPolicyDocument),
|
|
744
776
|
roles: [iamAuthRoleArn],
|
|
745
777
|
});
|
|
746
|
-
}
|
|
778
|
+
});
|
|
747
779
|
}
|
|
748
780
|
}
|
|
749
781
|
if (this.generateIAMPolicyForUnauthRole) {
|
|
@@ -754,35 +786,27 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
754
786
|
const unauthPolicyDocuments = (0, utils_1.createPolicyDocumentForManagedPolicy)(this.unauthPolicyResources);
|
|
755
787
|
const { rootStack } = ctx.stackManager;
|
|
756
788
|
const iamUnauthRoleArn = iam.Role.fromRoleArn(rootStack, 'unauth-role-name', `arn:aws:iam::${cdk.Stack.of(rootStack).account}:role/${unauthRoleParameter}`);
|
|
757
|
-
|
|
789
|
+
unauthPolicyDocuments.forEach((unauthPolicyDocument, i) => {
|
|
758
790
|
const paddedIndex = `${i + 1}`.padStart(2, '0');
|
|
759
791
|
const resourceName = `${graphql_transformer_common_1.ResourceConstants.RESOURCES.UnauthRolePolicy}${paddedIndex}`;
|
|
760
792
|
new iam.ManagedPolicy(ctx.stackManager.rootStack, resourceName, {
|
|
761
|
-
document: iam.PolicyDocument.fromJson(
|
|
793
|
+
document: iam.PolicyDocument.fromJson(unauthPolicyDocument),
|
|
762
794
|
roles: [iamUnauthRoleArn],
|
|
763
795
|
});
|
|
764
|
-
}
|
|
796
|
+
});
|
|
765
797
|
}
|
|
766
798
|
}
|
|
767
799
|
setAuthPolicyFlag(rules) {
|
|
768
800
|
if (rules.length === 0 || this.generateIAMPolicyForAuthRole === true) {
|
|
769
801
|
return;
|
|
770
802
|
}
|
|
771
|
-
rules.
|
|
772
|
-
if ((rule.allow === 'private' || rule.allow === 'public') && rule.provider === 'iam') {
|
|
773
|
-
this.generateIAMPolicyForAuthRole = true;
|
|
774
|
-
}
|
|
775
|
-
});
|
|
803
|
+
this.generateIAMPolicyForAuthRole = rules.some(rule => (rule.allow === 'private' || rule.allow === 'public') && rule.provider === 'iam');
|
|
776
804
|
}
|
|
777
805
|
setUnauthPolicyFlag(rules) {
|
|
778
806
|
if (rules.length === 0 || this.generateIAMPolicyForUnauthRole === true) {
|
|
779
807
|
return;
|
|
780
808
|
}
|
|
781
|
-
rules.
|
|
782
|
-
if (rule.allow === 'public' && rule.provider === 'iam') {
|
|
783
|
-
this.generateIAMPolicyForUnauthRole = true;
|
|
784
|
-
}
|
|
785
|
-
});
|
|
809
|
+
this.generateIAMPolicyForUnauthRole = rules.some(rule => (rule.allow === 'public' && rule.provider === 'iam'));
|
|
786
810
|
}
|
|
787
811
|
addOperationToResourceReferences(operationName, fieldName, roles) {
|
|
788
812
|
const iamPublicRolesExist = roles.some(r => this.roleMap.get(r).provider === 'iam' && this.roleMap.get(r).strategy === 'public');
|
|
@@ -817,28 +841,28 @@ class AuthTransformer extends graphql_transformer_core_1.TransformerAuthBase {
|
|
|
817
841
|
this.authPolicyResources.add(`${typeName}/${fieldName}`);
|
|
818
842
|
}
|
|
819
843
|
}
|
|
820
|
-
static addAutoGeneratedHasOneFields(typeDefinitions, fields, def, allowedFields) {
|
|
821
|
-
fields.forEach(field => {
|
|
822
|
-
var _a;
|
|
823
|
-
const modelField = def.fields.find(it => it.name.value === field);
|
|
824
|
-
const directives = (_a = modelField.directives) === null || _a === void 0 ? void 0 : _a.filter(dir => { var _a; return !((_a = dir.arguments) === null || _a === void 0 ? void 0 : _a.some(it => it.name.value === 'fields')) && (dir.name.value === 'hasOne' || dir.name.value === 'belongsTo'); });
|
|
825
|
-
directives.forEach(directive => {
|
|
826
|
-
if (directive.name.value === 'hasOne') {
|
|
827
|
-
allowedFields.add((0, graphql_relational_transformer_1.getConnectionAttributeName)(def.name.value, field));
|
|
828
|
-
}
|
|
829
|
-
else if (directive.name.value === 'belongsTo') {
|
|
830
|
-
const relatedType = typeDefinitions.find(it => {
|
|
831
|
-
var _a;
|
|
832
|
-
return it.name.value === (0, graphql_transformer_common_1.getBaseType)(modelField.type)
|
|
833
|
-
&& ((_a = it.fields) === null || _a === void 0 ? void 0 : _a.some(f => { var _a; return (0, graphql_transformer_common_1.getBaseType)(f.type) === def.name.value && ((_a = f.directives) === null || _a === void 0 ? void 0 : _a.some(d => d.name.value === 'hasOne')); }));
|
|
834
|
-
});
|
|
835
|
-
if (relatedType) {
|
|
836
|
-
allowedFields.add((0, graphql_relational_transformer_1.getConnectionAttributeName)(def.name.value, field));
|
|
837
|
-
}
|
|
838
|
-
}
|
|
839
|
-
});
|
|
840
|
-
});
|
|
841
|
-
}
|
|
842
844
|
}
|
|
843
845
|
exports.AuthTransformer = AuthTransformer;
|
|
846
|
+
const getReadRolesForField = (acm, readRoles, fieldName) => {
|
|
847
|
+
const hasCognitoPrivateRole = readRoles.some(r => r === 'userPools:private')
|
|
848
|
+
&& acm.isAllowed('userPools:private', fieldName, 'get')
|
|
849
|
+
&& acm.isAllowed('userPools:private', fieldName, 'list')
|
|
850
|
+
&& acm.isAllowed('userPools:private', fieldName, 'sync')
|
|
851
|
+
&& acm.isAllowed('userPools:private', fieldName, 'search')
|
|
852
|
+
&& acm.isAllowed('userPools:private', fieldName, 'listen');
|
|
853
|
+
const hasOIDCPrivateRole = readRoles.some(r => r === 'oidc:private')
|
|
854
|
+
&& acm.isAllowed('oidc:private', fieldName, 'get')
|
|
855
|
+
&& acm.isAllowed('oidc:private', fieldName, 'list')
|
|
856
|
+
&& acm.isAllowed('oidc:private', fieldName, 'sync')
|
|
857
|
+
&& acm.isAllowed('oidc:private', fieldName, 'search')
|
|
858
|
+
&& acm.isAllowed('oidc:private', fieldName, 'listen');
|
|
859
|
+
let allowedRoles = [...readRoles];
|
|
860
|
+
if (hasCognitoPrivateRole) {
|
|
861
|
+
allowedRoles = allowedRoles.filter(r => !(r.startsWith('userPools:') && r !== 'userPools:private'));
|
|
862
|
+
}
|
|
863
|
+
if (hasOIDCPrivateRole) {
|
|
864
|
+
allowedRoles = allowedRoles.filter(r => !(r.startsWith('oidc:') && r !== 'oidc:private'));
|
|
865
|
+
}
|
|
866
|
+
return allowedRoles;
|
|
867
|
+
};
|
|
844
868
|
//# sourceMappingURL=graphql-auth-transformer.js.map
|