Skip to content

Commit 90411e2

Browse files
committed
Better OAuth error logging
1 parent 1245390 commit 90411e2

3 files changed

Lines changed: 16 additions & 2 deletions

File tree

  • apps/backend/src
    • app/api/latest/connected-accounts/[user_id]/[provider_id]/access-token
    • oauth/providers
  • packages/stack-shared/src/utils

apps/backend/src/app/api/latest/connected-accounts/[user_id]/[provider_id]/access-token/crud.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ export const connectedAccountAccessTokenCrudHandlers = createLazyProxy(() => cre
111111
scope: data.scope,
112112
});
113113
} catch (error) {
114-
captureError('oauth-access-token-refresh-error', {
114+
captureError('oauth-access-token-refresh-error', new StackAssertionError('Error refreshing access token — this might be nothing bad and the refresh token might just be expired, but we should instead of throwing an error check whether this is a legit error or not', {
115115
error,
116116
tenancyId: auth.tenancy.id,
117117
providerId: params.provider_id,
118118
userId: params.user_id,
119119
refreshToken: token.refreshToken,
120120
scope: data.scope,
121-
});
121+
}));
122122

123123
// mark the token as invalid
124124
await prisma.oAuthToken.update({

apps/backend/src/oauth/providers/github.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { getEnvVariable } from "@stackframe/stack-shared/dist/utils/env";
22
import { StackAssertionError, StatusError } from "@stackframe/stack-shared/dist/utils/errors";
3+
import { getJwtInfo } from "@stackframe/stack-shared/dist/utils/jwt";
34
import { OAuthUserInfo, validateUserInfo } from "../utils";
45
import { OAuthBaseProvider, TokenSet } from "./base";
56

@@ -42,6 +43,7 @@ export class GithubProvider extends OAuthBaseProvider {
4243
hasAccessToken: !!tokenSet.accessToken,
4344
hasRefreshToken: !!tokenSet.refreshToken,
4445
accessTokenExpiredAt: tokenSet.accessTokenExpiredAt,
46+
jwtInfo: await getJwtInfo({ jwt: tokenSet.accessToken }),
4547
});
4648
}
4749
const rawUserInfo = await rawUserInfoRes.json();

packages/stack-shared/src/utils/jwt.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getEnvVariable } from "./env";
77
import { StackAssertionError } from "./errors";
88
import { globalVar } from "./globals";
99
import { pick } from "./objects";
10+
import { Result } from "./results";
1011

1112
function getStackServerSecret() {
1213
const STACK_SERVER_SECRET = getEnvVariable("STACK_SERVER_SECRET");
@@ -18,6 +19,17 @@ function getStackServerSecret() {
1819
return STACK_SERVER_SECRET;
1920
}
2021

22+
export async function getJwtInfo(options: {
23+
jwt: string,
24+
}) {
25+
try {
26+
const decodedJwt = jose.decodeJwt(options.jwt);
27+
return Result.ok({ payload: decodedJwt });
28+
} catch (e) {
29+
return Result.error(e);
30+
}
31+
}
32+
2133
export async function signJWT(options: {
2234
issuer: string,
2335
audience: string,

0 commit comments

Comments
 (0)