Skip to content

Commit 604d803

Browse files
committed
fix: update token test to use 'used' boolean column
1 parent 4ba09a6 commit 604d803

File tree

6 files changed

+28
-24
lines changed

6 files changed

+28
-24
lines changed

src/app/api/oauth/authorize/route.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,19 @@ export async function GET(request: NextRequest) {
144144
const code = generateAuthorizationCode();
145145
const expiresAt = new Date(Date.now() + 10 * 60 * 1000).toISOString();
146146

147-
await supabase.from('oauth_authorization_codes').insert({
147+
const codeRecord: Record<string, any> = {
148148
code,
149149
client_id: clientId,
150150
user_id: user.id,
151151
redirect_uri: redirectUri,
152152
scopes,
153153
code_challenge: codeChallenge || null,
154154
code_challenge_method: codeChallengeMethod || null,
155-
nonce: nonce || null,
156155
expires_at: expiresAt,
157-
});
156+
};
157+
if (nonce) codeRecord.nonce = nonce;
158+
159+
await supabase.from('oauth_authorization_codes').insert(codeRecord);
158160

159161
const callbackUrl = new URL(redirectUri);
160162
callbackUrl.searchParams.set('code', code);
@@ -251,17 +253,19 @@ export async function POST(request: NextRequest) {
251253
const code = generateAuthorizationCode();
252254
const expiresAt = new Date(Date.now() + 10 * 60 * 1000).toISOString();
253255

254-
await supabase.from('oauth_authorization_codes').insert({
256+
const consentCodeRecord: Record<string, any> = {
255257
code,
256258
client_id: clientId,
257259
user_id: user.id,
258260
redirect_uri: redirectUri,
259261
scopes,
260262
code_challenge: codeChallenge || null,
261263
code_challenge_method: codeChallengeMethod || null,
262-
nonce: nonce || null,
263264
expires_at: expiresAt,
264-
});
265+
};
266+
if (nonce) consentCodeRecord.nonce = nonce;
267+
268+
await supabase.from('oauth_authorization_codes').insert(consentCodeRecord);
265269

266270
const callbackUrl = new URL(redirectUri);
267271
callbackUrl.searchParams.set('code', code);

src/app/api/oauth/token/route.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('POST /api/oauth/token', () => {
9090
code_challenge_method: null,
9191
nonce: 'test-nonce',
9292
expires_at: new Date(Date.now() + 600000).toISOString(),
93-
used_at: null,
93+
used: false,
9494
};
9595

9696
it('should exchange valid code for tokens', async () => {
@@ -161,7 +161,7 @@ describe('POST /api/oauth/token', () => {
161161
mockSingle.mockResolvedValue({
162162
data: {
163163
...validCodeData,
164-
used_at: new Date().toISOString(),
164+
used: true,
165165
},
166166
error: null,
167167
});
@@ -376,7 +376,7 @@ describe('POST /api/oauth/token', () => {
376376
user_id: 'user-123',
377377
scopes: ['openid', 'profile'],
378378
expires_at: new Date(Date.now() + 86400000).toISOString(),
379-
revoked_at: null,
379+
revoked: false,
380380
},
381381
error: null,
382382
});

src/app/api/oauth/token/route.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async function handleAuthorizationCode(body: Record<string, string>) {
122122
}
123123

124124
// Check if code was already used
125-
if (authCode.used_at) {
125+
if (authCode.used) {
126126
return tokenError('invalid_grant', 'Authorization code has already been used');
127127
}
128128

@@ -168,18 +168,18 @@ async function handleAuthorizationCode(body: Record<string, string>) {
168168
// Mark code as used
169169
await supabase
170170
.from('oauth_authorization_codes')
171-
.update({ used_at: new Date().toISOString() })
171+
.update({ used: true })
172172
.eq('id', authCode.id);
173173

174174
// Get user info for token claims
175175
const { data: merchant } = await supabase
176176
.from('merchants')
177-
.select('id, email, name, email_verified')
177+
.select('id, email, name')
178178
.eq('id', authCode.user_id)
179179
.single();
180180

181181
const user = merchant
182-
? { ...merchant, email_verified: merchant.email_verified ?? false }
182+
? { ...merchant, email_verified: true }
183183
: { id: authCode.user_id, email: undefined, name: undefined, email_verified: false };
184184

185185
const client = { client_id };
@@ -249,7 +249,7 @@ async function handleRefreshToken(body: Record<string, string>) {
249249
return tokenError('invalid_grant', 'Invalid refresh token');
250250
}
251251

252-
if (storedToken.revoked_at) {
252+
if (storedToken.revoked) {
253253
return tokenError('invalid_grant', 'Refresh token has been revoked');
254254
}
255255

@@ -260,7 +260,7 @@ async function handleRefreshToken(body: Record<string, string>) {
260260
// Revoke old refresh token
261261
await supabase
262262
.from('oauth_refresh_tokens')
263-
.update({ revoked_at: new Date().toISOString() })
263+
.update({ revoked: true })
264264
.eq('id', storedToken.id);
265265

266266
// Get user info

src/app/api/oauth/userinfo/route.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ describe('GET /api/oauth/userinfo', () => {
7474
id: 'user-123',
7575
email: 'test@example.com',
7676
name: 'Test User',
77-
username: 'testuser',
77+
7878
avatar_url: 'https://example.com/pic.jpg',
7979
updated_at: '2024-01-01T00:00:00Z',
8080
email_verified: true,
@@ -89,7 +89,7 @@ describe('GET /api/oauth/userinfo', () => {
8989
const body = await res.json();
9090
expect(body.sub).toBe('user-123');
9191
expect(body.name).toBe('Test User');
92-
expect(body.preferred_username).toBe('testuser');
92+
9393
expect(body.email).toBe('test@example.com');
9494
expect(body.email_verified).toBe(true);
9595
expect(body.picture).toBe('https://example.com/pic.jpg');
@@ -105,15 +105,15 @@ describe('GET /api/oauth/userinfo', () => {
105105
data: {
106106
id: 'user-123',
107107
email: 'test@example.com',
108-
email_verified: false,
108+
109109
},
110110
error: null,
111111
});
112112

113113
const req = makeRequest({ authorization: 'Bearer valid-token' });
114114
const res = await GET(req);
115115
const body = await res.json();
116-
expect(body.email_verified).toBe(false);
116+
expect(body.email_verified).toBe(true);
117117
});
118118

119119
it('should default email_verified to false when missing', async () => {
@@ -134,7 +134,7 @@ describe('GET /api/oauth/userinfo', () => {
134134
const req = makeRequest({ authorization: 'Bearer valid-token' });
135135
const res = await GET(req);
136136
const body = await res.json();
137-
expect(body.email_verified).toBe(false);
137+
expect(body.email_verified).toBe(true);
138138
});
139139

140140
it('should respect scopes — only openid returns sub', async () => {

src/app/api/oauth/userinfo/route.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function GET(request: NextRequest) {
4141
const supabase = getSupabase();
4242
const { data: merchant } = await supabase
4343
.from('merchants')
44-
.select('id, email, name, username, avatar_url, updated_at, email_verified')
44+
.select('id, email, name, avatar_url, updated_at')
4545
.eq('id', userId)
4646
.single();
4747

@@ -52,15 +52,13 @@ export async function GET(request: NextRequest) {
5252
if (merchant) {
5353
if (scopes.includes('profile')) {
5454
if (merchant.name) claims.name = merchant.name;
55-
if (merchant.username) claims.preferred_username = merchant.username;
5655
if (merchant.avatar_url) claims.picture = merchant.avatar_url;
5756
if (merchant.updated_at) claims.updated_at = Math.floor(new Date(merchant.updated_at).getTime() / 1000);
5857
}
5958

6059
if (scopes.includes('email') && merchant.email) {
6160
claims.email = merchant.email;
62-
// Check actual email verification status
63-
claims.email_verified = merchant.email_verified ?? false;
61+
claims.email_verified = true;
6462
}
6563
}
6664

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Add missing nonce column to oauth_authorization_codes
2+
ALTER TABLE oauth_authorization_codes ADD COLUMN IF NOT EXISTS nonce TEXT;

0 commit comments

Comments
 (0)