Skip to content

Commit

Permalink
fix(#246): re-throw invalid PKCS8 error as AuthError with user-friend…
Browse files Browse the repository at this point in the history
…ly message
  • Loading branch information
awinogrodzki committed Aug 30, 2024
1 parent 9fc3106 commit a7d7a22
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/auth/jwt/sign.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {JWTPayload, SignJWT, base64url, importPKCS8} from 'jose';
import {ALGORITHM_RS256} from '../signature-verifier';
import {fetchAny} from '../utils';
import { JWTPayload, KeyLike, SignJWT, base64url, importPKCS8 } from 'jose';

Check failure on line 1 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Replace `·JWTPayload,·KeyLike,·SignJWT,·base64url,·importPKCS8·` with `JWTPayload,·KeyLike,·SignJWT,·base64url,·importPKCS8`
import { ALGORITHM_RS256 } from '../signature-verifier';

Check failure on line 2 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Replace `·ALGORITHM_RS256·` with `ALGORITHM_RS256`
import { fetchAny } from '../utils';

Check failure on line 3 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Replace `·fetchAny·` with `fetchAny`
import { AuthError, AuthErrorCode } from '../error';

Check failure on line 4 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Replace `·AuthError,·AuthErrorCode·` with `AuthError,·AuthErrorCode`

export type SignOptions = {
readonly payload: JWTPayload;
Expand All @@ -13,10 +14,18 @@ export async function sign({
privateKey,
keyId
}: SignOptions): Promise<string> {
const key = await importPKCS8(privateKey, ALGORITHM_RS256);
let key: KeyLike;

try {
key = await importPKCS8(privateKey, ALGORITHM_RS256);
} catch (e) {
const error = new AuthError(AuthErrorCode.INVALID_ARGUMENT, "It looks like the value provided for `serviceAccount.privateKey` is incorrectly formatted. Please double-check if private key has correct format. See /~https://github.com/awinogrodzki/next-firebase-auth-edge/issues/246#issuecomment-2321559620 for details")

Check failure on line 22 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Replace `AuthErrorCode.INVALID_ARGUMENT,·"It·looks·like·the·value·provided·for·`serviceAccount.privateKey`·is·incorrectly·formatted.·Please·double-check·if·private·key·has·correct·format.·See·/~https://github.com/awinogrodzki/next-firebase-auth-edge/issues/246#issuecomment-2321559620·for·details")` with `⏎······AuthErrorCode.INVALID_ARGUMENT,⏎······'It·looks·like·the·value·provided·for·`serviceAccount.privateKey`·is·incorrectly·formatted.·Please·double-check·if·private·key·has·correct·format.·See·/~https://github.com/awinogrodzki/next-firebase-auth-edge/issues/246#issuecomment-2321559620·for·details'⏎····);`
error.stack = (error?.stack ?? '') + (e as Error)?.stack ?? '';

Check failure on line 23 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Unexpected constant nullishness on the left-hand side of a `??` expression
throw error;
}

return new SignJWT(payload)
.setProtectedHeader({alg: ALGORITHM_RS256, kid: keyId})
.setProtectedHeader({ alg: ALGORITHM_RS256, kid: keyId })

Check failure on line 28 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Replace `·alg:·ALGORITHM_RS256,·kid:·keyId·` with `alg:·ALGORITHM_RS256,·kid:·keyId`
.sign(key);
}

Expand Down Expand Up @@ -52,12 +61,12 @@ export async function signBlob({
headers: {
Authorization: `Bearer ${accessToken}`
},
body: JSON.stringify({payload: base64url.encode(token)})
body: JSON.stringify({ payload: base64url.encode(token) })

Check failure on line 64 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Replace `·payload:·base64url.encode(token)·` with `payload:·base64url.encode(token)`
};
const response = await fetchAny(url, request);
const blob = await response.blob();
const key = await blob.text();
const {signedBlob} = JSON.parse(key);
const { signedBlob } = JSON.parse(key);

Check failure on line 69 in src/auth/jwt/sign.ts

View workflow job for this annotation

GitHub Actions / Lint packages

Replace `·signedBlob·` with `signedBlob`

return `${token}.${formatBase64(signedBlob)}`;
}

0 comments on commit a7d7a22

Please sign in to comment.