fix: auth
This commit is contained in:
parent
107a57ffe6
commit
72636d96c8
2 changed files with 82 additions and 1 deletions
|
|
@ -247,7 +247,6 @@ export class WorkOsAuthProvider implements AuthenticationProvider, Disposable {
|
|||
if (
|
||||
error.message?.includes("401") ||
|
||||
error.message?.includes("Invalid refresh token") ||
|
||||
error.message?.includes("400") ||
|
||||
error.message?.includes("Unauthorized") ||
|
||||
attempt >= maxAttempts
|
||||
) {
|
||||
|
|
|
|||
|
|
@ -481,6 +481,88 @@ it("should remove session if token refresh fails with authentication error", asy
|
|||
}
|
||||
});
|
||||
|
||||
it("should remove session if token refresh returns Unauthorized error message", async () => {
|
||||
// Setup existing sessions with a valid token
|
||||
const validToken = createJwt({ expired: false });
|
||||
const mockSession = {
|
||||
id: "test-id",
|
||||
accessToken: validToken,
|
||||
refreshToken: "invalid-refresh-token",
|
||||
expiresInMs: 300000, // 5 minutes
|
||||
account: { label: "Test User", id: "user@example.com" },
|
||||
scopes: [],
|
||||
loginNeeded: false,
|
||||
};
|
||||
|
||||
// Setup fetch mock
|
||||
const fetchMock = fetch as any;
|
||||
fetchMock.mockClear();
|
||||
|
||||
// Setup refresh to return an error containing "Unauthorized" in the message
|
||||
// Status code doesn't matter here, what matters is the error message text
|
||||
fetchMock.mockResolvedValueOnce({
|
||||
ok: false,
|
||||
status: 403, // Could be any error code
|
||||
text: async () => "Unauthorized",
|
||||
});
|
||||
|
||||
// Create a mock UriHandler
|
||||
const mockUriHandler = {
|
||||
event: new EventEmitter(),
|
||||
handleCallback: vi.fn(),
|
||||
};
|
||||
|
||||
// Create a mock ExtensionContext
|
||||
const mockContext = {
|
||||
secrets: {
|
||||
store: vi.fn(),
|
||||
get: vi.fn(),
|
||||
},
|
||||
subscriptions: [],
|
||||
};
|
||||
|
||||
// Mock setInterval to prevent continuous refreshes
|
||||
const originalSetInterval = global.setInterval;
|
||||
global.setInterval = vi.fn().mockReturnValue(123 as any);
|
||||
|
||||
// Set up our SecretStorage mock to return the session
|
||||
mockSecretStorageGet.mockResolvedValue(JSON.stringify([mockSession]));
|
||||
mockSecretStorageStore.mockClear();
|
||||
|
||||
// Import WorkOsAuthProvider after setting up all mocks
|
||||
const { WorkOsAuthProvider } = await import("./WorkOsAuthProvider");
|
||||
|
||||
// Create provider instance - this will automatically call refreshSessions
|
||||
const provider = new WorkOsAuthProvider(mockContext, mockUriHandler);
|
||||
|
||||
// Wait for all promises to resolve, including any nested promise chains
|
||||
await new Promise(process.nextTick);
|
||||
|
||||
// Verify that the token refresh endpoint was called
|
||||
expect(fetchMock).toHaveBeenCalledWith(
|
||||
expect.any(URL),
|
||||
expect.objectContaining({
|
||||
method: "POST",
|
||||
body: expect.stringContaining("invalid-refresh-token"),
|
||||
}),
|
||||
);
|
||||
|
||||
// Verify sessions were removed due to Unauthorized error message
|
||||
expect(mockSecretStorageStore).toHaveBeenCalledWith(
|
||||
"workos.sessions", // Use the hard-coded key that matches our mock
|
||||
expect.stringMatching(/\[\]/),
|
||||
);
|
||||
|
||||
// Restore setInterval
|
||||
global.setInterval = originalSetInterval;
|
||||
|
||||
// Clean up
|
||||
if (provider._refreshInterval) {
|
||||
clearInterval(provider._refreshInterval);
|
||||
provider._refreshInterval = null;
|
||||
}
|
||||
});
|
||||
|
||||
it("should preserve valid tokens during network errors by retrying", async () => {
|
||||
// Mock Date.now to return a fixed timestamp for token validation
|
||||
const originalDateNow = Date.now;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue