@axinom/mosaic-id-guard 0.18.0-rc.9 → 0.18.0

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.
Files changed (49) hide show
  1. package/dist/common/helpers/guard-authorization.d.ts +22 -0
  2. package/dist/common/helpers/guard-authorization.d.ts.map +1 -0
  3. package/dist/common/helpers/guard-authorization.js +49 -0
  4. package/dist/common/helpers/guard-authorization.js.map +1 -0
  5. package/dist/common/id-guard-error.d.ts +1 -0
  6. package/dist/common/id-guard-error.d.ts.map +1 -1
  7. package/dist/common/id-guard-error.js +7 -1
  8. package/dist/common/id-guard-error.js.map +1 -1
  9. package/dist/common/id-guard-errors.d.ts +8 -0
  10. package/dist/common/id-guard-errors.d.ts.map +1 -1
  11. package/dist/common/id-guard-errors.js +8 -0
  12. package/dist/common/id-guard-errors.js.map +1 -1
  13. package/dist/graphql/ax-guard-plugin.d.ts +23 -0
  14. package/dist/graphql/ax-guard-plugin.d.ts.map +1 -0
  15. package/dist/graphql/ax-guard-plugin.js +29 -0
  16. package/dist/graphql/ax-guard-plugin.js.map +1 -0
  17. package/dist/graphql/enforce-strict-permissions.plugin.d.ts +1 -1
  18. package/dist/graphql/enforce-strict-permissions.plugin.d.ts.map +1 -1
  19. package/dist/graphql/index.d.ts +3 -2
  20. package/dist/graphql/index.d.ts.map +1 -1
  21. package/dist/graphql/index.js +3 -2
  22. package/dist/graphql/index.js.map +1 -1
  23. package/dist/graphql/{guard-plugin.d.ts → query-mutation-guard-plugin.d.ts} +2 -2
  24. package/dist/graphql/query-mutation-guard-plugin.d.ts.map +1 -0
  25. package/dist/graphql/{guard-plugin.js → query-mutation-guard-plugin.js} +5 -26
  26. package/dist/graphql/query-mutation-guard-plugin.js.map +1 -0
  27. package/dist/graphql/subscription-guard-plugin.d.ts +20 -0
  28. package/dist/graphql/subscription-guard-plugin.d.ts.map +1 -0
  29. package/dist/graphql/subscription-guard-plugin.js +81 -0
  30. package/dist/graphql/subscription-guard-plugin.js.map +1 -0
  31. package/package.json +9 -10
  32. package/src/common/helpers/guard-authorization.ts +76 -0
  33. package/src/common/id-guard-error.ts +13 -0
  34. package/src/common/id-guard-errors.ts +10 -0
  35. package/src/graphql/ax-guard-plugin.ts +29 -0
  36. package/src/graphql/enforce-strict-permissions.plugin.ts +1 -1
  37. package/src/graphql/index.ts +3 -2
  38. package/src/graphql/{guard-plugin.spec.ts → query-mutation-guard-plugin.spec.ts} +3 -3
  39. package/src/graphql/{guard-plugin.ts → query-mutation-guard-plugin.ts} +12 -36
  40. package/src/graphql/subscription-guard-plugin.spec.ts +257 -0
  41. package/src/graphql/subscription-guard-plugin.ts +112 -0
  42. package/dist/graphql/guard-plugin.d.ts.map +0 -1
  43. package/dist/graphql/guard-plugin.js.map +0 -1
  44. package/dist/graphql/subscription-authorization-hook-factory.d.ts +0 -13
  45. package/dist/graphql/subscription-authorization-hook-factory.d.ts.map +0 -1
  46. package/dist/graphql/subscription-authorization-hook-factory.js +0 -182
  47. package/dist/graphql/subscription-authorization-hook-factory.js.map +0 -1
  48. package/src/graphql/subscription-authorization-hook-factory.spec.ts +0 -749
  49. package/src/graphql/subscription-authorization-hook-factory.ts +0 -286
@@ -1,182 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.subscriptionAuthorizationHookFactory = void 0;
7
- const mosaic_service_common_1 = require("@axinom/mosaic-service-common");
8
- const graphql_tag_1 = __importDefault(require("graphql-tag"));
9
- const graphql_ws_1 = require("graphql-ws");
10
- const common_1 = require("../common");
11
- const guard_utils_1 = require("../common/guard-utils");
12
- const handle_end_user_authorization_1 = require("../common/handle-end-user-authorization");
13
- const handle_management_user_authorization_1 = require("../common/handle-management-user-authorization");
14
- /**
15
- * This is a hook plugin generator function that will use the PermissionDefinition object in PostGraphileOptions
16
- * to authorize the Subscription operation done over a web socket.
17
- * It checks if the authenticated user which initiates the subscription has required permissions.
18
- * The actual plugin is built using this function when the build() function is called in PostgraphileOptionsBuilder.
19
- * This function reference can be passed into addHookPluginGenerator() to activate authorization of subscriptions.
20
- * @param PostGraphileOptions object containing permissions.
21
- * @returns PostGraphilePlugin
22
- */
23
- const subscriptionAuthorizationHookFactory = (buildOptions) => {
24
- return {
25
- // Handles subscription requests coming through protocol `graphql-ws`
26
- // This is maintained for backwards compatibility. Managed services use `graphql-transport-ws` protocol (implemented below)
27
- 'postgraphile:ws:onOperation': (params) => {
28
- var _a;
29
- // Extract GQL query and build an AST
30
- // Need to determine the Subscription that's being called.
31
- const gqlQuery = (0, graphql_tag_1.default)(params.query.toString());
32
- const operationDefinitions = gqlQuery.definitions;
33
- const subscription = getSubscription(operationDefinitions, params.operationName);
34
- if (buildOptions !== undefined &&
35
- buildOptions.graphileBuildOptions !== undefined &&
36
- subscription !== undefined) {
37
- // Get permissionDefinition from graphileBuildOptions
38
- const permissionDefinition = buildOptions === null || buildOptions === void 0 ? void 0 : buildOptions.graphileBuildOptions.permissionDefinition;
39
- const endUserAuthorizationConfig = buildOptions === null || buildOptions === void 0 ? void 0 : buildOptions.graphileBuildOptions.endUserAuthorizationConfig;
40
- const ensureOnlyAuthentication = buildOptions === null || buildOptions === void 0 ? void 0 : buildOptions.graphileBuildOptions.ensureOnlyAuthentication;
41
- const options = {
42
- operation: subscription.name.value,
43
- permissionDefinition,
44
- serviceId: params.context.config.serviceId,
45
- endUserAuthorizationConfig,
46
- };
47
- const error = undefined;
48
- const subject = params.context.subject;
49
- (0, common_1.assertGenericAuthenticatedSubject)(subject);
50
- // If the authorization fails, throw error
51
- if ((0, guard_utils_1.isAuthenticatedManagementSubject)(subject)) {
52
- (0, handle_management_user_authorization_1.handleManagementUserAuthorization)(options, ensureOnlyAuthentication, subject, error);
53
- return params;
54
- }
55
- else if ((0, guard_utils_1.isAuthenticatedEndUser)(subject)) {
56
- (0, handle_end_user_authorization_1.handleEndUserAuthorization)(options, ensureOnlyAuthentication, subject, error);
57
- return params;
58
- }
59
- else {
60
- throw new common_1.IdGuardError({
61
- message: 'User is not authorized for this subscription.',
62
- code: common_1.IdGuardErrors.UserNotAuthorized.code,
63
- details: {
64
- user: (_a = params.context.subject) === null || _a === void 0 ? void 0 : _a.sub,
65
- serviceId: options.serviceId,
66
- },
67
- });
68
- }
69
- }
70
- throw new Error('Error in subscription setup. This is likely caused by a development time issue.');
71
- },
72
- // Handles subscription requests coming through protocol `graphql-transport-ws`
73
- // Managed services use this protocol for subscriptions.
74
- 'postgraphile:ws:onSubscribe': (args, params) => {
75
- var _a;
76
- // If there were any Authentication Errors, close the web socket.
77
- if (params.context.extra.request.authContext
78
- .authErrorInfo !== undefined) {
79
- params.context.extra.socket.close(graphql_ws_1.CloseCode.Forbidden, common_1.IdGuardErrors.AccessTokenInvalid.code);
80
- return args;
81
- }
82
- const definitionNodes = args.document
83
- .definitions;
84
- const subscription = getSubscription(definitionNodes, (_a = args.operationName) !== null && _a !== void 0 ? _a : undefined);
85
- if (buildOptions !== undefined &&
86
- buildOptions.graphileBuildOptions !== undefined &&
87
- subscription !== undefined) {
88
- // Get permissionDefinition from graphileBuildOptions
89
- const permissionDefinition = buildOptions === null || buildOptions === void 0 ? void 0 : buildOptions.graphileBuildOptions.permissionDefinition;
90
- const endUserAuthorizationConfig = buildOptions === null || buildOptions === void 0 ? void 0 : buildOptions.graphileBuildOptions.endUserAuthorizationConfig;
91
- const ensureOnlyAuthentication = buildOptions === null || buildOptions === void 0 ? void 0 : buildOptions.graphileBuildOptions.ensureOnlyAuthentication;
92
- const options = {
93
- operation: subscription.name.value,
94
- permissionDefinition,
95
- serviceId: args.contextValue.config.serviceId,
96
- endUserAuthorizationConfig,
97
- };
98
- const error = undefined;
99
- const subject = args.contextValue.subject;
100
- (0, common_1.assertGenericAuthenticatedSubject)(subject);
101
- // If the authorization fails, throw error
102
- // Authorization is only checked for Management tokens
103
- if ((0, guard_utils_1.isAuthenticatedManagementSubject)(subject)) {
104
- try {
105
- (0, handle_management_user_authorization_1.handleManagementUserAuthorization)(options, ensureOnlyAuthentication, subject, error);
106
- }
107
- catch (error) {
108
- assertIdGuardError(error);
109
- /**
110
- * If the token has expired, we close the connection with CloseCode.Forbidden, and allow the client to automatically re-establish
111
- * the connection with a new token.
112
- */
113
- if (error.code === common_1.IdGuardErrors.AccessTokenExpired.code) {
114
- /**
115
- * `CloseCode.Forbidden` is used so that the client will automatically start retry process with a newer token.
116
- * Any other CloseCode will result in a client exception and the client will not automatically retry.
117
- */
118
- params.context.extra.socket.close(graphql_ws_1.CloseCode.Forbidden, common_1.IdGuardErrors.AccessTokenExpired.code);
119
- }
120
- else {
121
- throw error;
122
- }
123
- }
124
- return args;
125
- }
126
- else if ((0, guard_utils_1.isAuthenticatedEndUser)(subject)) {
127
- try {
128
- (0, handle_end_user_authorization_1.handleEndUserAuthorization)(options, ensureOnlyAuthentication, subject, error);
129
- }
130
- catch (error) {
131
- assertIdGuardError(error);
132
- /**
133
- * If the token has expired, we close the connection with CloseCode.Forbidden, and allow the client to automatically re-establish
134
- * the connection with a new token.
135
- */
136
- if (error.code === common_1.IdGuardErrors.AccessTokenExpired.code) {
137
- /**
138
- * `CloseCode.Forbidden` is used so that the client will automatically start retry process with a newer token.
139
- * Any other CloseCode will result in a client exception and the client will not automatically retry.
140
- */
141
- params.context.extra.socket.close(graphql_ws_1.CloseCode.Forbidden, common_1.IdGuardErrors.AccessTokenExpired.code);
142
- }
143
- else {
144
- throw error;
145
- }
146
- }
147
- return args;
148
- }
149
- }
150
- /**
151
- * `graphql-transport-ws` does not propagate thrown exceptions to the client.
152
- * So if either `graphileBuildOptions` or `subscription` is undefined, we close the web socket connection with BadRequest error.
153
- * The client will have to explicitly re-establish the web socket connection.
154
- */
155
- params.context.extra.socket.close(graphql_ws_1.CloseCode.BadRequest, 'Error in subscription setup. This is likely caused by a development time issue.');
156
- return args;
157
- },
158
- };
159
- };
160
- exports.subscriptionAuthorizationHookFactory = subscriptionAuthorizationHookFactory;
161
- /**
162
- * Extract Subscription from the AST.
163
- */
164
- const getSubscription = (operationDefinitions, operationName) => {
165
- // In a subscription request query, there can be only one subscription,
166
- // and only one operation with the same name.
167
- // We extract the AST for the Subscription using the operationName sent in params
168
- const subscriptionDefinition = operationDefinitions.filter((opDefs) => { var _a; return ((_a = opDefs.name) === null || _a === void 0 ? void 0 : _a.value) === operationName; })[0];
169
- if (subscriptionDefinition !== undefined) {
170
- // If the called operation is found in the query,
171
- // proceed to extract the subscription from the GQL Operation definition.
172
- // There can only be one subscription in a Subscription operation, hence getting the [0]th element.
173
- const subscription = subscriptionDefinition.selectionSet.selections[0];
174
- return subscription;
175
- }
176
- };
177
- const assertIdGuardError = (error) => {
178
- if (!(error instanceof common_1.IdGuardError)) {
179
- throw new mosaic_service_common_1.NonMosaicError(`A caught error is not an instance of an IdGuardError class.`);
180
- }
181
- };
182
- //# sourceMappingURL=subscription-authorization-hook-factory.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"subscription-authorization-hook-factory.js","sourceRoot":"","sources":["../../src/graphql/subscription-authorization-hook-factory.ts"],"names":[],"mappings":";;;;;;AAIA,yEAAgF;AAQhF,8DAA8B;AAC9B,2CAAkE;AAQlE,sCAImB;AACnB,uDAI+B;AAC/B,2FAAqF;AACrF,yGAAmG;AAGnG;;;;;;;;GAQG;AACI,MAAM,oCAAoC,GAAG,CAClD,YAAgE,EAC5C,EAAE;IACtB,OAAO;QACL,qEAAqE;QACrE,2HAA2H;QAC3H,6BAA6B,EAAE,CAAC,MAAuB,EAAE,EAAE;;YACzD,qCAAqC;YACrC,0DAA0D;YAC1D,MAAM,QAAQ,GAAG,IAAA,qBAAG,EAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC;YAC9C,MAAM,oBAAoB,GACxB,QAAQ,CAAC,WAAwC,CAAC;YAEpD,MAAM,YAAY,GAAG,eAAe,CAClC,oBAAoB,EACpB,MAAM,CAAC,aA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