Skip to content

Commit bd8db7d

Browse files
committed
Change impl to check for truthy values.
1 parent 38a632f commit bd8db7d

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

spec/v2/providers/https.spec.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ describe("onCall", () => {
543543
expect(noClaimResp.status).to.equal(403);
544544
});
545545

546-
it("should check single claim with default value (true)", async () => {
546+
it("should check single claim with default value (truthy)", async () => {
547547
const func = https.onCall(
548548
{
549549
authPolicy: https.hasClaim("admin"),
@@ -553,12 +553,15 @@ describe("onCall", () => {
553553
const validResp = await runHandler(func, request({ auth: { admin: true } }));
554554
expect(validResp.status).to.equal(200);
555555

556-
const wrongTypeResp = await runHandler(func, request({ auth: { admin: "true" } }));
557-
expect(wrongTypeResp.status).to.equal(403);
556+
const truthyResp = await runHandler(func, request({ auth: { admin: "true" } }));
557+
expect(truthyResp.status).to.equal(200);
558558

559559
const falseResp = await runHandler(func, request({ auth: { admin: false } }));
560560
expect(falseResp.status).to.equal(403);
561561

562+
const falseStrResp = await runHandler(func, request({ auth: { admin: "false" } }));
563+
expect(falseStrResp.status).to.equal(403);
564+
562565
const noClaimResp = await runHandler(func, request({ auth: {} }));
563566
expect(noClaimResp.status).to.equal(403);
564567
});

src/v2/providers/https.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ export function hasClaim(
248248
let claimsToCheck: Record<string, unknown> = {};
249249

250250
if (typeof claimOrClaims === "string") {
251-
claimsToCheck[claimOrClaims] = value ?? true;
251+
claimsToCheck[claimOrClaims] = value;
252252
} else if (Array.isArray(claimOrClaims)) {
253253
for (const claim of claimOrClaims) {
254254
claimsToCheck[claim] = true;
@@ -265,8 +265,15 @@ export function hasClaim(
265265
if (!(claim in auth.token)) {
266266
throw new Error(`Missing claim '${claim}'`);
267267
}
268-
if (auth.token[claim] !== claimsToCheck[claim]) {
269-
throw new Error(`Missing claim '${claim}' with value '${value}'`);
268+
const expectedValue = claimsToCheck[claim];
269+
const actualValue = auth.token[claim];
270+
271+
if (expectedValue === undefined) {
272+
if (!actualValue || actualValue === "false") {
273+
return false;
274+
}
275+
} else if (actualValue !== expectedValue) {
276+
return false;
270277
}
271278
}
272279
return true;

0 commit comments

Comments
 (0)