Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 35 additions & 3 deletions __tests__/authType.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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();
};

Expand All @@ -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
Expand Down
23 changes: 20 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Loading