Skip to content

Commit 677ac41

Browse files
fix(auth): encrypt OAuth tokens using encryption utility directly (#144)
* fix(auth): encrypt OAuth tokens using encryption utility directly auth.ts silently stored GitHub OAuth access tokens as plaintext because the encryption check relied on a non-existent `app.encryption` Fastify decorator - the condition always evaluated false, falling back to the raw token. connect.ts called `app.encryption.encrypt()` directly, throwing a TypeError at runtime and breaking the GitHub connect flow entirely. Both routes now import `encrypt()` directly from utils/encryption.ts, consistent with how follow.ts already imports `decrypt()` from the same module. * fix(auth): isolate OAuth token persistence with focused try/catch Wrap the encrypt + oAuthToken.upsert block in its own try/catch so that a transient DB failure during token storage does not abort the login flow. The platform token is supplementary -- authentication (JWT issuance) proceeds even when persistence fails, and the error is logged for observability. Addresses reviewer feedback on PR #144. --------- Signed-off-by: Prashantkumar Khatri <96608160+ShantKhatri@users.noreply.github.com> Co-authored-by: Prashantkumar Khatri <96608160+ShantKhatri@users.noreply.github.com>
1 parent 0b03d68 commit 677ac41

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

apps/backend/src/routes/auth.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
22
import { randomBytes } from 'crypto';
3+
import { encrypt } from '../utils/encryption.js';
34

45
const GITHUB_AUTH_URL = 'https://github.com/login/oauth/authorize';
56
const GITHUB_TOKEN_URL = 'https://github.com/login/oauth/access_token';
@@ -119,6 +120,19 @@ export async function authRoutes(app: FastifyInstance) {
119120
},
120121
});
121122

123+
// Save the authentication token for 'user:email read:user' so we have a basic platform connection.
124+
// Failure here is non-fatal — the user can still authenticate; the token can be reconnected later.
125+
try {
126+
const encryptedToken = encrypt(tokenData.access_token);
127+
await app.prisma.oAuthToken.upsert({
128+
where: { userId_platform: { userId: user.id, platform: 'github' } },
129+
update: { accessToken: encryptedToken, scopes: 'read:user user:email' },
130+
create: { userId: user.id, platform: 'github', accessToken: encryptedToken, scopes: 'read:user user:email' },
131+
});
132+
} catch (err) {
133+
app.log.error({ err, userId: user.id }, 'Failed to persist GitHub OAuth token — authentication proceeds');
134+
}
135+
122136
// Generate JWT
123137
const token = app.jwt.sign(
124138
{ id: user.id, username: user.username },

0 commit comments

Comments
 (0)