Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions backend/src/auth/auth-refresh-token.regression.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { UnauthorizedException } from "@nestjs/common";
import { AuthService } from "./auth.service";

describe("AuthService refresh-token claim compatibility", () => {
const user = {
id: "user-123",
walletAddress: "GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
isArtist: false,
};

function createService(payload: Record<string, unknown>) {
const service = Object.create(AuthService.prototype) as any;
service.jwtService = {
verify: jest.fn().mockReturnValue(payload),
sign: jest.fn().mockReturnValue("new-access-token"),
};
service.authRedisService = {
validateRefreshToken: jest.fn().mockResolvedValue(true),
};
service.userRepository = {
findOne: jest.fn().mockResolvedValue(user),
};
service.logger = { debug: jest.fn(), error: jest.fn() };
return service;
}

it('accepts the "sub" claim used by tokens issued by AuthService', async () => {
const service = createService({ sub: "user-123", tokenId: "refresh-123" });

await expect(service.refreshAccessToken("signed-refresh-token")).resolves.toEqual({
accessToken: "new-access-token",
});

expect(service.authRedisService.validateRefreshToken).toHaveBeenCalledWith(
"refresh-123",
"user-123",
);
expect(service.userRepository.findOne).toHaveBeenCalledWith({
where: { id: "user-123" },
});
});

it("rejects refresh tokens without a subject or token ID", async () => {
const service = createService({ tokenId: "refresh-123" });

await expect(
service.refreshAccessToken("malformed-refresh-token"),
).rejects.toBeInstanceOf(UnauthorizedException);
expect(service.authRedisService.validateRefreshToken).not.toHaveBeenCalled();
});
});
14 changes: 10 additions & 4 deletions backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ interface Challenge {
}

interface RefreshTokenPayload {
userId: string;
/** JWT subject: the user ID placed in the token when it is issued. */
sub: string;
tokenId: string;
}

Expand Down Expand Up @@ -234,22 +235,27 @@ export class AuthService implements OnModuleInit, OnModuleDestroy {
try {
const payload = this.jwtService.verify<RefreshTokenPayload>(refreshToken);

if (!payload.tokenId || !payload.userId) {
// Refresh tokens are issued with the standard JWT "sub" claim. The
// previous implementation incorrectly required a "userId" claim that
// was never added to the token, causing every freshly issued refresh
// token to be rejected.
const userId = payload.sub;
if (!payload.tokenId || !userId) {
throw new UnauthorizedException("Invalid refresh token");
}

// Verify token exists in our store
const isValidToken = await this.authRedisService.validateRefreshToken(
payload.tokenId,
payload.userId,
userId,
);
if (!isValidToken) {
throw new UnauthorizedException("Refresh token not found or invalid");
}

// Get user
const user = await this.userRepository.findOne({
where: { id: payload.userId },
where: { id: userId },
});

if (!user) {
Expand Down
Loading