-
Notifications
You must be signed in to change notification settings - Fork 588
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api-security): use AsyncLocalStorage in withoutAuthorization
- Loading branch information
Showing
6 changed files
with
115 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/api-security/__tests__/graphql/parallelQueries.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { GraphQLSchemaPlugin } from "@webiny/handler-graphql"; | ||
import { SecurityContext } from "~/types"; | ||
|
||
export const PARALLEL_QUERY = /* GraphQL */ ` | ||
query ParallelQueries { | ||
withoutAuthorization | ||
withAuthorization | ||
security { | ||
listApiKeys { | ||
data { | ||
id | ||
token | ||
} | ||
error { | ||
code | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
export const withoutAuthorizationPlugin = new GraphQLSchemaPlugin<SecurityContext>({ | ||
typeDefs: /* GraphQL*/ ` | ||
extend type Query { | ||
withoutAuthorization: String! | ||
withAuthorization: String! | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
withoutAuthorization(_, args, context) { | ||
return context.security.withoutAuthorization(async () => { | ||
const permissions = await context.security.getPermissions("security.apiKey"); | ||
if (!permissions.length) { | ||
return "NOT_AUTHORIZED"; | ||
} | ||
return "YOUR DATA!"; | ||
}); | ||
}, | ||
async withAuthorization(_, args, context) { | ||
const permissions = await context.security.getPermissions("security.apiKey"); | ||
if (!permissions.length) { | ||
return "NOT_AUTHORIZED"; | ||
} | ||
return "AUTHORIZED"; | ||
} | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import useGqlHandler from "./useGqlHandler"; | ||
import { PARALLEL_QUERY, withoutAuthorizationPlugin } from "./graphql/parallelQueries"; | ||
|
||
describe("Security Parallel Queries", () => { | ||
const { install, invoke } = useGqlHandler({ plugins: [withoutAuthorizationPlugin] }); | ||
|
||
beforeEach(async () => { | ||
await install.install(); | ||
}); | ||
|
||
test("should not disable authorization in parallel queries", async () => { | ||
const [response] = await invoke({ | ||
body: { query: PARALLEL_QUERY }, | ||
// We want to simulate an anonymous user. | ||
headers: { authorization: "anonymous" } | ||
}); | ||
|
||
expect(response.data).toEqual({ | ||
withoutAuthorization: "YOUR DATA!", | ||
withAuthorization: "NOT_AUTHORIZED", | ||
security: { | ||
listApiKeys: { | ||
data: null, | ||
error: { | ||
code: "SECURITY_NOT_AUTHORIZED" | ||
} | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters