diff --git a/README.md b/README.md index 2bea9f0..a3d272b 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ This package provides an implementation for the `@aws-appsync/utils` package tha ## Changelog: +- v0.1.10: `util.authType()` now reports `User Pool Authorization` for the user pool identity of an Event API request, which carries no `defaultAuthStrategy` unlike the GraphQL one - v0.1.9: add `runtime.earlyReturn()`, importable from `@aws-appsync/utils`; it validates the `skipTo` return option and hands the value and target to the host, which decides which handlers still run - v0.1.8: `util.authType()` now reports `Lambda Authorization` for the identity of a Lambda-authorized Event API request, which carries what the authorizer returned as `handlerContext` rather than the `resolverContext` a GraphQL API uses - v0.1.7: complete the `@aws-appsync/utils/dynamodb` module with `query`, `sync`, the batch and transact helpers, the set helpers, `operations.remove` and `operations.updateListItem`, and the arguments the existing helpers dropped (`filter`, `projection`, `condition`, `consistentRead`, `_version` and the rest); add `util.transform.toSubscriptionFilter`; `util.dynamodb.fromS3ObjectJson` now parses instead of throwing diff --git a/__tests__/authType.test.js b/__tests__/authType.test.js index 4445101..c24d1ba 100644 --- a/__tests__/authType.test.js +++ b/__tests__/authType.test.js @@ -69,6 +69,16 @@ const withoutKey = (identity, key) => { return rest; }; +// the same user pool on an Event API, which leaves out `defaultAuthStrategy` +const EVENT_USER_POOL_IDENTITY = withoutKey(USER_POOL_IDENTITY, "defaultAuthStrategy"); + +// the `ctx.info` of an Event API handler, the only one naming a channel namespace +const EVENT_INFO = { + channel: { path: "/default/channel", segments: ["default", "channel"] }, + channelNamespace: { name: "default" }, + operation: "PUBLISH", +}; + describe("util.authType", () => { describe("the mode of an identity", () => { test("a request carrying no identity is authorized by an API key", async () => { @@ -141,14 +151,15 @@ describe("util.authType", () => { // mock context against the GraphQL identity keys and rejects one carrying `handlerContext` ("The // following properties in your mock context object failed validation: ctx.identity"), so // `checkResolverValid` cannot reach this shape even with `TEST_TARGET=AWS_CLOUD`. The expected - // values were instead read off responses recorded from a real Lambda-authorized Event API on - // AWS, where `ctx.identity` is `{handlerContext: {...}}` and `util.authType()` is `Lambda + // values were instead read off responses recorded from real Lambda- and user-pool-authorized + // Event APIs on AWS, where `ctx.identity` is `{handlerContext: {...}}` and `util.authType()` is + // `Lambda Authorization`, or the user pool identity without `defaultAuthStrategy` and `User Pool // Authorization`. describe("the identity of an Event API request", () => { afterEach(() => setResolverContext(null)); const authTypeForEventIdentity = (identity) => { - setResolverContext({ identity }); + setResolverContext({ identity, info: EVENT_INFO }); return util.authType(); }; @@ -160,6 +171,27 @@ describe("util.authType", () => { test("an identity whose handler context is empty comes from a Lambda authorizer", () => { expect(authTypeForEventIdentity({ handlerContext: {} })).toBe("Lambda Authorization"); }); + + // unlike the GraphQL user pool identity, it carries no `defaultAuthStrategy` + test("a user pool identity is user pool authorized", () => { + expect(authTypeForEventIdentity(EVENT_USER_POOL_IDENTITY)).toBe("User Pool Authorization"); + }); + + test("a user pool identity without groups is user pool authorized", () => { + expect(authTypeForEventIdentity(withoutKey(EVENT_USER_POOL_IDENTITY, "groups"))).toBe( + "User Pool Authorization", + ); + }); + + test("a user pool identity missing its source IP falls back to an API key", () => { + expect(authTypeForEventIdentity(withoutKey(EVENT_USER_POOL_IDENTITY, "sourceIp"))).toBe( + "API Key Authorization", + ); + }); + + test("an OIDC identity is OIDC authorized", () => { + expect(authTypeForEventIdentity(OIDC_IDENTITY)).toBe("Open ID Connect Authorization"); + }); }); // NOT compared against AWS: `setResolverContext` is the seam a host uses to hand the request to diff --git a/index.js b/index.js index 4fda064..4b4249c 100644 --- a/index.js +++ b/index.js @@ -176,6 +176,18 @@ const IDENTITY_SCHEMAS = [ }, ]; +// An Event API hands its handlers a user pool identity without `defaultAuthStrategy`, a shape the +// GraphQL schemas above read as an API key request. Read off the responses of a real +// user-pool-authorized Event API, like the `handlerContext` shape described below. +const EVENT_API_IDENTITY_SCHEMAS = [ + { + authType: AUTH_TYPE_USER_POOL, + keys: ["sourceIp", "username", "groups", "sub", "issuer", "claims"], + required: ["sourceIp", "username", "sub", "issuer", "claims"], + }, + ...IDENTITY_SCHEMAS, +]; + // Recorded from `EvaluateCode` against every identity shape in the AppSync resolver context // reference plus the ambiguous mixtures: an identity belongs to a mode when all of its keys are // keys of that mode and every key the mode requires is present and not null. Foreign keys rule a @@ -188,12 +200,12 @@ const IDENTITY_SCHEMAS = [ // `EvaluateCode` validates its mock context against the GraphQL identity keys alone and rejects // `handlerContext` before the resolver runs. It was read off the responses of a real // Lambda-authorized Event API instead. -function authTypeFromIdentity(identity) { +function authTypeFromIdentity(identity, schemas) { if (identity === null || typeof identity !== "object" || Array.isArray(identity)) { return AUTH_TYPE_API_KEY; } const keys = Object.keys(identity); - for (const schema of IDENTITY_SCHEMAS) { + for (const schema of schemas) { const noForeignKeys = keys.every((key) => schema.keys.includes(key)); const hasRequired = schema.required.every((key) => identity[key] !== null && identity[key] !== undefined); if (noForeignKeys && hasRequired) { @@ -234,7 +246,12 @@ export const util = { throw new AppSyncUserError("Unauthorized", "UnauthorizedException") }, authType: function() { - return authTypeFromIdentity(resolverContext?.identity); + // only an Event API handler's `ctx.info` names a channel namespace + const isEventApi = resolverContext?.info?.channelNamespace != null; + return authTypeFromIdentity( + resolverContext?.identity, + isEventApi ? EVENT_API_IDENTITY_SCHEMAS : IDENTITY_SCHEMAS, + ); }, time: { nowFormatted: function(pattern) { diff --git a/package.json b/package.json index de0161a..b91cb27 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@localstack/appsync-utils", - "version": "0.1.9", + "version": "0.1.10", "description": "Implementation of the AppSync utils helpers", "type": "module", "main": "index.js",