Skip to content

Commit 545a6bd

Browse files
committed
Lint
1 parent 7db8c7f commit 545a6bd

File tree

1 file changed

+19
-19
lines changed
  • packages/auth-server/server/api

1 file changed

+19
-19
lines changed
Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
import { defineEventHandler, getHeader } from 'h3';
2-
import jwt from 'jsonwebtoken';
3-
import axios from 'axios';
4-
import jwkToPem from 'jwk-to-pem';
51
import crypto from 'crypto';
2+
import axios from 'axios';
3+
import { defineEventHandler, getHeader } from "h3";
4+
import jwt from "jsonwebtoken";
5+
import jwkToPem from "jwk-to-pem";
66

7-
const GOOGLE_JWKS_URL = 'https://www.googleapis.com/oauth2/v3/certs';
7+
const GOOGLE_JWKS_URL = "https://www.googleapis.com/oauth2/v3/certs";
88
const GOOGLE_ISSUERS = [
9-
'https://accounts.google.com',
10-
'accounts.google.com',
9+
"https://accounts.google.com",
10+
"accounts.google.com",
1111
];
12-
const SALT_ENTROPY = process.env.SALT_ENTROPY || 'entropy';
12+
const SALT_ENTROPY = process.env.SALT_ENTROPY || "entropy";
1313

1414
async function getGooglePublicKey(kid: string) {
1515
const { data } = await axios.get(GOOGLE_JWKS_URL);
1616
const jwk = data.keys.find((key: any) => key.kid === kid);
1717

1818
if (!jwk) {
19-
throw new Error('Public key not found');
19+
throw new Error("Public key not found");
2020
}
2121

2222
return jwkToPem(jwk);
2323
}
2424

2525
export default defineEventHandler(async (event) => {
26-
const authHeader = getHeader(event, 'Authorization');
26+
const authHeader = getHeader(event, "Authorization");
2727

28-
if (!authHeader || !authHeader.startsWith('Bearer ')) {
28+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
2929
throw createError({
3030
statusCode: 401,
31-
message: 'Unauthorized - Missing or invalid token',
31+
message: "Unauthorized - Missing or invalid token",
3232
});
3333
}
3434

35-
const token = authHeader.split(' ')[1];
35+
const token = authHeader.split(" ")[1];
3636

3737
try {
3838
const decoded = jwt.decode(token, { complete: true }) as any;
3939
if (!decoded?.payload?.iss || !GOOGLE_ISSUERS.includes(decoded.payload.iss)) {
40-
throw new Error('Invalid issuer');
40+
throw new Error("Invalid issuer");
4141
}
4242

4343
if (!decoded?.header?.kid) {
44-
throw new Error('JWT missing "kid"');
44+
throw new Error("JWT missing \"kid\"");
4545
}
4646

4747
const publicKey = await getGooglePublicKey(decoded.header.kid);
4848

4949
const verifiedToken = jwt.verify(token, publicKey, {
50-
algorithms: ['RS256'],
50+
algorithms: ["RS256"],
5151
});
5252

5353
const iss = verifiedToken.iss;
5454
const aud = verifiedToken.aud;
5555
const sub = verifiedToken.sub;
5656

57-
const data = { iss, aud, sub , entropy: SALT_ENTROPY };
58-
const hash = crypto.createHash('sha256').update(JSON.stringify(data)).digest('hex');
57+
const data = { iss, aud, sub, entropy: SALT_ENTROPY };
58+
const hash = crypto.createHash("sha256").update(JSON.stringify(data)).digest("hex");
5959

6060
return { salt: hash };
6161
} catch (error) {
6262
throw createError({
6363
statusCode: 401,
64-
message: 'Unauthorized - Invalid token or verification failed',
64+
message: "Unauthorized - Invalid token or verification failed",
6565
});
6666
}
6767
});

0 commit comments

Comments
 (0)