@inixiative/integrations 0.4.0 → 0.4.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inixiative/integrations",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "The provider catalog behind Kingdom integrations: each provider's operations, events, resource kinds and scopes, with the request, webhook and credential code that reaches it.",
5
5
  "type": "module",
6
6
  "main": "src/index.ts",
@@ -0,0 +1,12 @@
1
+ import type { IntegrationFamily, Provider } from '../contracts';
2
+ import { findProvider } from './findProvider';
3
+ import { integrationFamilyDefinition } from './integrationFamilyDefinition';
4
+
5
+ export const refreshTokenRotates = (
6
+ owner: { provider: Provider } | { integrationFamily: IntegrationFamily },
7
+ ) => {
8
+ // why: an unknown refresh outcome may have spent a rotating token, so only a non-rotating one is safe to retry.
9
+ if ('integrationFamily' in owner)
10
+ return integrationFamilyDefinition(owner.integrationFamily)?.oauth?.refreshTokenRotates ?? true;
11
+ return findProvider(owner.provider)?.refreshTokenRotates ?? true;
12
+ };
package/src/contracts.ts CHANGED
@@ -83,7 +83,9 @@ export const integrationFamilyDefinitionSchema = z
83
83
  name: z.string(),
84
84
  documentationUrl: z.url(),
85
85
  authMethods: z.array(credentialMethodSchema),
86
- oauth: z.object({ authorizeUrl: z.url(), tokenUrl: z.url() }).optional(),
86
+ oauth: z
87
+ .object({ authorizeUrl: z.url(), tokenUrl: z.url(), refreshTokenRotates: z.boolean() })
88
+ .optional(),
87
89
  })
88
90
  .refine((definition) => !definition.authMethods.includes('oauth2') || definition.oauth, {
89
91
  message: 'an integration family with oauth2 auth needs its OAuth endpoints',
@@ -102,6 +104,7 @@ export const providerDefinitionSchema = z
102
104
  subscription: z.boolean().optional(),
103
105
  integrationFamily: integrationFamilySchema.optional(),
104
106
  oauthScopes: z.array(z.string()).optional(),
107
+ refreshTokenRotates: z.boolean().optional(),
105
108
  })
106
109
  .refine(
107
110
  (definition) =>
@@ -20,14 +20,14 @@ const mintedSecret = (clientCredentials: ClientCredentials, data: Record<string,
20
20
  permissions: z.record(z.string(), z.string()).default({}),
21
21
  })
22
22
  .safeParse(data);
23
- if (!minted.success) throw new ApiError('renewal_outcome_unknown', 409);
23
+ if (!minted.success) throw new ApiError('renewal_answer_unreadable', 409);
24
24
  const expiresIn = z
25
25
  .number()
26
26
  .int()
27
27
  .positive()
28
28
  .max(31536000)
29
29
  .safeParse(Math.floor((Date.parse(minted.data.expires_at) - Date.now()) / 1000));
30
- if (!expiresIn.success) throw new ApiError('renewal_outcome_unknown', 409);
30
+ if (!expiresIn.success) throw new ApiError('renewal_answer_unreadable', 409);
31
31
  return {
32
32
  secret: credentialSecretSchema.parse({ token: minted.data.token, clientCredentials }),
33
33
  expiresIn: expiresIn.data,
@@ -27,7 +27,7 @@ import { requestOauthToken } from './requestOauthToken';
27
27
 
28
28
  const secondsUntilJwtExpiry = (token: string) => {
29
29
  const exp = z.number().int().positive().safeParse(jwtClaims(token)?.exp);
30
- if (!exp.success) throw new ApiError('renewal_outcome_unknown', 409);
30
+ if (!exp.success) throw new ApiError('renewal_answer_unreadable', 409);
31
31
  return z
32
32
  .number()
33
33
  .int()
@@ -137,7 +137,7 @@ const refreshedSecret = (
137
137
  ...(typeof data.id_token === 'string' ? { idToken: data.id_token } : {}),
138
138
  },
139
139
  });
140
- if (!refreshed.success) throw new ApiError('renewal_outcome_unknown', 409);
140
+ if (!refreshed.success) throw new ApiError('renewal_answer_unreadable', 409);
141
141
  if (
142
142
  [
143
143
  'slack',
@@ -151,14 +151,14 @@ const refreshedSecret = (
151
151
  ].includes(provider) &&
152
152
  !data.refresh_token
153
153
  )
154
- throw new ApiError('renewal_outcome_unknown', 409);
154
+ throw new ApiError('renewal_answer_unreadable', 409);
155
155
  const expiresIn =
156
156
  provider === 'notion' && data.expires_in === undefined
157
157
  ? null
158
158
  : provider === 'codex' && data.expires_in === undefined
159
159
  ? secondsUntilJwtExpiry(refreshed.data.token)
160
160
  : z.number().int().positive().max(31536000).safeParse(data.expires_in).data;
161
- if (expiresIn === undefined) throw new ApiError('renewal_outcome_unknown', 409);
161
+ if (expiresIn === undefined) throw new ApiError('renewal_answer_unreadable', 409);
162
162
  return { secret: refreshed.data, expiresIn };
163
163
  };
164
164
 
@@ -186,6 +186,6 @@ export const refreshProviderCredential = async (
186
186
  return refreshedSecret(provider, secret, data);
187
187
  } catch (error) {
188
188
  if (error instanceof ApiError) throw error;
189
- throw new ApiError('renewal_outcome_unknown', 409);
189
+ throw new ApiError('renewal_answer_unreadable', 409);
190
190
  }
191
191
  };
@@ -7,5 +7,6 @@ export const renewalError = (error: unknown) => {
7
7
  if (error.code === 'provider_rate_limited' || status >= 500)
8
8
  return new ApiError('renewal_temporarily_unavailable', 503);
9
9
  if (status >= 400) return new ApiError('renewal_rejected', 409);
10
- return new ApiError('renewal_outcome_unknown', 409);
10
+ // why: the provider answered, so a token that rotates may already be spent; one that doesn't is not worth retrying.
11
+ return new ApiError('renewal_answer_unreadable', 409);
11
12
  };
@@ -8,5 +8,6 @@ export const atlassianIntegrationFamily: IntegrationFamilyDefinition = {
8
8
  oauth: {
9
9
  authorizeUrl: 'https://auth.atlassian.com/authorize',
10
10
  tokenUrl: 'https://auth.atlassian.com/oauth/token',
11
+ refreshTokenRotates: true,
11
12
  },
12
13
  };
@@ -8,5 +8,6 @@ export const githubIntegrationFamily: IntegrationFamilyDefinition = {
8
8
  oauth: {
9
9
  authorizeUrl: 'https://github.com/login/oauth/authorize',
10
10
  tokenUrl: 'https://github.com/login/oauth/access_token',
11
+ refreshTokenRotates: true,
11
12
  },
12
13
  };
@@ -8,5 +8,6 @@ export const googleIntegrationFamily: IntegrationFamilyDefinition = {
8
8
  oauth: {
9
9
  authorizeUrl: 'https://accounts.google.com/o/oauth2/v2/auth',
10
10
  tokenUrl: 'https://oauth2.googleapis.com/token',
11
+ refreshTokenRotates: false,
11
12
  },
12
13
  };