History

Preserve OAuth scopes on spec refresh

Rhys Sullivan committed 3 months ago

Commit 1d2c8a51 parent 63ea0283 files · +115 −8Browse files

Unified · Split

3 files
  1. .changeset/preserve-google-refresh-scopes.md +5 −0
  2. packages/plugins/openapi/src/providers/google/spec-format-adapter.test.ts +105 −0
  3. packages/plugins/openapi/src/sdk/plugin.ts +5 −8
.changeset/preserve-google-refresh-scopes.md+5−0
@@ -0,0 +1,5 @@
1+---
2+"@executor-js/plugin-openapi": patch
3+---
4+
5+Preserve an integration's selected OAuth consent scopes when refreshing converted API specifications, so Google Gmail refreshes do not restore operations that require broader scopes.
packages/plugins/openapi/src/providers/google/spec-format-adapter.test.ts+105−0
@@ -10,6 +10,9 @@ import { deriveGoogleDiscoveryIdentity, googleDiscoveryAdapter } from "./spec-fo · 9 unmodified lines
1010 import { googleCatalog } from "./presets";
1111
1212 const TASKS_URL = "https://www.googleapis.com/discovery/v1/apis/tasks/v1/rest";
13+const GMAIL_URL = "https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest";
14+const GMAIL_MODIFY_SCOPE = "https://www.googleapis.com/auth/gmail.modify";
15+const GMAIL_FULL_SCOPE = "https://mail.google.com/";
1316
1417 const tasksDiscoveryDoc = {
1518 name: "tasks",
@@ -70,6 +73,66 @@ const discoveryHttpClientLayer = Layer.succeed(HttpClient.HttpClient)( · 54 unmodified lines
7073 ),
7174 );
7275
76+const gmailDiscoveryDoc = {
77+ name: "gmail",
78+ version: "v1",
79+ title: "Gmail API",
80+ rootUrl: "https://gmail.googleapis.com/",
81+ servicePath: "",
82+ auth: {
83+ oauth2: {
84+ scopes: {
85+ [GMAIL_MODIFY_SCOPE]: { description: "Read and modify Gmail" },
86+ [GMAIL_FULL_SCOPE]: { description: "Full Gmail access" },
87+ },
88+ },
89+ },
90+ resources: {
91+ users: {
92+ resources: {
93+ messages: {
94+ methods: {
95+ list: {
96+ id: "gmail.users.messages.list",
97+ httpMethod: "GET",
98+ path: "gmail/v1/users/{userId}/messages",
99+ scopes: [GMAIL_MODIFY_SCOPE, GMAIL_FULL_SCOPE],
100+ parameters: {
101+ userId: { location: "path", required: true, type: "string" },
102+ },
103+ },
104+ delete: {
105+ id: "gmail.users.messages.delete",
106+ httpMethod: "DELETE",
107+ path: "gmail/v1/users/{userId}/messages/{id}",
108+ scopes: [GMAIL_FULL_SCOPE],
109+ parameters: {
110+ userId: { location: "path", required: true, type: "string" },
111+ id: { location: "path", required: true, type: "string" },
112+ },
113+ },
114+ },
115+ },
116+ },
117+ },
118+ },
119+ schemas: {},
120+};
121+
122+const gmailDiscoveryHttpClientLayer = Layer.succeed(HttpClient.HttpClient)(
123+ HttpClient.make((request: HttpClientRequest.HttpClientRequest) =>
124+ Effect.succeed(
125+ HttpClientResponse.fromWeb(
126+ request,
127+ new Response(JSON.stringify(gmailDiscoveryDoc), {
128+ status: request.url === GMAIL_URL ? 200 : 404,
129+ headers: { "content-type": "application/json" },
130+ }),
131+ ),
132+ ),
133+ ),
134+);
135+
73136 it.effect("fetches and converts a Google Discovery document", () =>
74137 Effect.gen(function* () {
75138 const converted = yield* googleDiscoveryAdapter.fetch({
@@ -165,3 +228,45 @@ it.effect( · 89 unmodified lines
165228 );
166229 }),
167230 );
231+
232+it.effect("preserves a Google preset's consent scope boundary when refreshing", () =>
233+ Effect.gen(function* () {
234+ const gmailPreset = googleCatalog.find((preset) => preset.id === "google-gmail")!;
235+ const authTemplate: readonly AuthenticationInput[] = (gmailPreset.authTemplate ?? []).flatMap(
236+ (template) => (template.kind === "oauth2" ? [template] : []),
237+ );
238+ const executor = yield* createExecutor(
239+ makeTestConfig({
240+ plugins: [
241+ openApiPlugin({
242+ httpClientLayer: gmailDiscoveryHttpClientLayer,
243+ presets: [gmailPreset],
244+ specFormats: [googleDiscoveryAdapter],
245+ }),
246+ memoryCredentialsPlugin(),
247+ ],
248+ }),
249+ );
250+
251+ const added = yield* executor.openapi.addSpec({
252+ spec: { kind: "url", url: GMAIL_URL },
253+ slug: gmailPreset.defaultSlug,
254+ specFormat: gmailPreset.specFormat,
255+ family: gmailPreset.family,
256+ authenticationTemplate: authTemplate,
257+ });
258+ expect(added.toolCount).toBe(1);
259+
260+ const updated = yield* executor.openapi.updateSpec("google_gmail");
261+
262+ const config = yield* executor.openapi.getConfig("google_gmail");
263+ const oauthTemplate = config?.authenticationTemplate?.find(
264+ (template) => template.kind === "oauth2",
265+ );
266+ expect(updated.toolCount).toBe(1);
267+ expect(updated.addedTools).not.toContain("gmail.users.messages.delete");
268+ expect(oauthTemplate?.kind === "oauth2" ? oauthTemplate.scopes : undefined).toContain(
269+ GMAIL_MODIFY_SCOPE,
270+ );
271+ }),
272+);
packages/plugins/openapi/src/sdk/plugin.ts+5−8
@@ -646,14 +646,10 @@ export const openApiPlugin = definePlugin< · 645 unmodified lines
646646 const resolveSpecForInput = (
647647 config: Pick<
648648 OpenApiSpecConfig,
649− | "spec"
650− | "specFormat"
651− | "specOverrides"
652− | "headers"
653− | "queryParams"
654− | "baseUrl"
655− | "authenticationTemplate"
656− >,
649+ "spec" | "specFormat" | "specOverrides" | "headers" | "queryParams" | "baseUrl"
650+ > & {
651+ readonly authenticationTemplate?: readonly (Authentication | AuthenticationInput)[];
652+ },
657653 httpClientLayer: Layer.Layer<HttpClient.HttpClient, never, never>,
658654 ): Effect.Effect<
659655 ResolvedSpec,
@@ -989,6 +985,7 @@ export const openApiPlugin = definePlugin< · 329 unmodified lines
989985 headers: current.headers,
990986 queryParams: current.queryParams,
991987 baseUrl: current.baseUrl,
988+ authenticationTemplate: current.authenticationTemplate,
992989 },
993990 httpClientLayer,
994991 );

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t