Skip to content

Commit 41d89ce

Browse files
lazy migration of issuerUrl
1 parent 077c646 commit 41d89ce

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

docs/docs/features/permission-syncing.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,6 @@ These flags are useful when you want different enforcement behavior across conne
176176
}
177177
```
178178

179-
<Warning>
180-
If you set `enforcePermissionsForPublicRepos` to true on an existing deployment, users who are already signed in will need to sign out and sign back in before the flag takes effect for their account.
181-
</Warning>
182-
183179
The table below shows when permissions are enforced based on the combination of `PERMISSION_SYNC_ENABLED`, `enforcePermissions`, and `enforcePermissionsForPublicRepos`:
184180

185181
| `PERMISSION_SYNC_ENABLED` | `enforcePermissions` | `enforcePermissionsForPublicRepos` | Private repos enforced? | Public repos enforced? |

packages/web/src/auth.ts

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,13 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
159159
// This is necessary to update the access token when the user
160160
// re-authenticates.
161161
// NOTE: Tokens are encrypted before storage for security
162-
if (account && account.provider && account.provider !== 'credentials' && account.providerAccountId) {
163-
164-
// Find the matching provider for the account
165-
const providers = getProviders();
166-
const matchingProvider = providers.find((provider) => {
167-
if (typeof provider.provider === "function") {
168-
const providerInfo = provider.provider();
169-
return providerInfo.id === account.provider;
170-
} else {
171-
return provider.provider.id === account.provider;
172-
}
173-
});
162+
if (
163+
account &&
164+
account.provider &&
165+
account.provider !== 'credentials' &&
166+
account.providerAccountId
167+
) {
168+
const issuerUrl = await getIssuerUrlForAccount(account);
174169

175170
await prisma.account.update({
176171
where: {
@@ -186,7 +181,7 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
186181
token_type: account.token_type,
187182
scope: account.scope,
188183
id_token: account.id_token,
189-
issuerUrl: matchingProvider?.issuerUrl,
184+
issuerUrl,
190185
})
191186
})
192187
}
@@ -233,6 +228,34 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
233228
token.userId = user.id;
234229
}
235230

231+
// @note The following performs a lazy migration of the issuerUrl
232+
// in the user's accounts. The issuerUrl was introduced in v4.15.4
233+
// and will not be present for accounts created prior to this version.
234+
//
235+
// @see https://github.com/sourcebot-dev/sourcebot/pull/993
236+
if (token.userId) {
237+
const accountsWithoutIssuerUrl = await prisma.account.findMany({
238+
where: {
239+
userId: token.userId,
240+
issuerUrl: null,
241+
},
242+
});
243+
244+
for (const account of accountsWithoutIssuerUrl) {
245+
const issuerUrl = await getIssuerUrlForAccount(account);
246+
if (issuerUrl) {
247+
await prisma.account.update({
248+
where: {
249+
id: account.id,
250+
},
251+
data: {
252+
issuerUrl,
253+
},
254+
});
255+
}
256+
}
257+
}
258+
236259
// Refresh expiring tokens and capture any errors.
237260
if (hasEntitlement('sso') && token.userId) {
238261
const errors = await refreshLinkedAccountTokens(token.userId);
@@ -265,3 +288,19 @@ export const { handlers, signIn, signOut, auth } = NextAuth({
265288
// verifyRequest: "/login/verify",
266289
}
267290
});
291+
292+
/**
293+
* Returns the issuer URL for a given auth.js account
294+
*/
295+
const getIssuerUrlForAccount = async (account: { provider: string; }) => {
296+
const providers = getProviders();
297+
const matchingProvider = providers.find((provider) => {
298+
if (typeof provider.provider === "function") {
299+
const providerInfo = provider.provider();
300+
return providerInfo.id === account.provider;
301+
} else {
302+
return provider.provider.id === account.provider;
303+
}
304+
});
305+
return matchingProvider?.issuerUrl;
306+
}

0 commit comments

Comments
 (0)