Skip to content

Commit 054282d

Browse files
committed
address bugbot comment
1 parent 7a65ab4 commit 054282d

4 files changed

Lines changed: 43 additions & 38 deletions

File tree

apps/sim/app/api/auth/oauth/utils.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,14 @@ import { createLogger } from '@sim/logger'
44
import { and, desc, eq, inArray } from 'drizzle-orm'
55
import { getSession } from '@/lib/auth'
66
import { refreshOAuthToken } from '@/lib/oauth'
7+
import {
8+
getMicrosoftRefreshTokenExpiry,
9+
isMicrosoftProvider,
10+
PROACTIVE_REFRESH_THRESHOLD_DAYS,
11+
} from '@/lib/oauth/microsoft'
712

813
const logger = createLogger('OAuthUtilsAPI')
914

10-
const MICROSOFT_REFRESH_TOKEN_LIFETIME_DAYS = 90
11-
const PROACTIVE_REFRESH_THRESHOLD_DAYS = 7
12-
13-
const MICROSOFT_PROVIDERS = new Set([
14-
'microsoft-excel',
15-
'microsoft-planner',
16-
'microsoft-teams',
17-
'outlook',
18-
'onedrive',
19-
'sharepoint',
20-
])
21-
22-
function isMicrosoftProvider(providerId: string): boolean {
23-
return MICROSOFT_PROVIDERS.has(providerId)
24-
}
25-
26-
function getMicrosoftRefreshTokenExpiry(): Date {
27-
return new Date(Date.now() + MICROSOFT_REFRESH_TOKEN_LIFETIME_DAYS * 24 * 60 * 60 * 1000)
28-
}
29-
3015
interface AccountInsertData {
3116
id: string
3217
userId: string
@@ -264,6 +249,10 @@ export async function refreshAccessTokenIfNeeded(
264249
userId: credential.userId,
265250
hasRefreshToken: !!credential.refreshToken,
266251
})
252+
if (!accessTokenNeedsRefresh && accessToken) {
253+
logger.info(`[${requestId}] Proactive refresh failed but access token still valid`)
254+
return accessToken
255+
}
267256
return null
268257
}
269258

@@ -297,6 +286,10 @@ export async function refreshAccessTokenIfNeeded(
297286
credentialId,
298287
userId: credential.userId,
299288
})
289+
if (!accessTokenNeedsRefresh && accessToken) {
290+
logger.info(`[${requestId}] Proactive refresh failed but access token still valid`)
291+
return accessToken
292+
}
300293
return null
301294
}
302295
} else if (!accessToken) {
@@ -351,6 +344,10 @@ export async function refreshTokenIfNeeded(
351344

352345
if (!refreshResult) {
353346
logger.error(`[${requestId}] Failed to refresh token for credential`)
347+
if (!accessTokenNeedsRefresh && credential.accessToken) {
348+
logger.info(`[${requestId}] Proactive refresh failed but access token still valid`)
349+
return { accessToken: credential.accessToken, refreshed: false }
350+
}
354351
throw new Error('Failed to refresh token')
355352
}
356353

@@ -393,6 +390,11 @@ export async function refreshTokenIfNeeded(
393390
}
394391
}
395392

393+
if (!accessTokenNeedsRefresh && credential.accessToken) {
394+
logger.info(`[${requestId}] Proactive refresh failed but access token still valid`)
395+
return { accessToken: credential.accessToken, refreshed: false }
396+
}
397+
396398
logger.error(`[${requestId}] Refresh failed and no valid token found in DB`, error)
397399
throw error
398400
}

apps/sim/lib/auth/auth.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,7 @@ import { SSO_TRUSTED_PROVIDERS } from './sso/constants'
6464

6565
const logger = createLogger('Auth')
6666

67-
const MICROSOFT_REFRESH_TOKEN_LIFETIME_DAYS = 90
68-
69-
const MICROSOFT_PROVIDERS = new Set([
70-
'microsoft-excel',
71-
'microsoft-planner',
72-
'microsoft-teams',
73-
'outlook',
74-
'onedrive',
75-
'sharepoint',
76-
])
77-
78-
function isMicrosoftProvider(providerId: string): boolean {
79-
return MICROSOFT_PROVIDERS.has(providerId)
80-
}
81-
82-
function getMicrosoftRefreshTokenExpiry(): Date {
83-
return new Date(Date.now() + MICROSOFT_REFRESH_TOKEN_LIFETIME_DAYS * 24 * 60 * 60 * 1000)
84-
}
67+
import { getMicrosoftRefreshTokenExpiry, isMicrosoftProvider } from '@/lib/oauth/microsoft'
8568

8669
const validStripeKey = env.STRIPE_SECRET_KEY
8770

apps/sim/lib/oauth/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './microsoft'
12
export * from './oauth'
23
export * from './types'
34
export * from './utils'

apps/sim/lib/oauth/microsoft.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const MICROSOFT_REFRESH_TOKEN_LIFETIME_DAYS = 90
2+
export const PROACTIVE_REFRESH_THRESHOLD_DAYS = 7
3+
4+
export const MICROSOFT_PROVIDERS = new Set([
5+
'microsoft-excel',
6+
'microsoft-planner',
7+
'microsoft-teams',
8+
'outlook',
9+
'onedrive',
10+
'sharepoint',
11+
])
12+
13+
export function isMicrosoftProvider(providerId: string): boolean {
14+
return MICROSOFT_PROVIDERS.has(providerId)
15+
}
16+
17+
export function getMicrosoftRefreshTokenExpiry(): Date {
18+
return new Date(Date.now() + MICROSOFT_REFRESH_TOKEN_LIFETIME_DAYS * 24 * 60 * 60 * 1000)
19+
}

0 commit comments

Comments
 (0)