Skip to content

Commit abcb2af

Browse files
committed
fixed isJWKMetadata function
1 parent 2806c5c commit abcb2af

File tree

4 files changed

+31
-36
lines changed

4 files changed

+31
-36
lines changed

src/jwk-fetcher.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { JsonWebKeyWithKid } from './jwt-decoder';
22
import type { KeyStorer } from './key-store';
3-
import { isArray, isNonNullObject, isURL } from './validator';
3+
import { isNonNullObject, isObject, isURL } from './validator';
44

55
export interface KeyFetcher {
66
fetchPublicKeys(): Promise<Array<JsonWebKeyWithKid>>;
@@ -10,8 +10,19 @@ interface JWKMetadata {
1010
keys: Array<JsonWebKeyWithKid>;
1111
}
1212

13-
export const isJWKMetadata = (value: any): value is JWKMetadata =>
14-
isNonNullObject(value) && !!value.keys && isArray(value.keys);
13+
export const isJWKMetadata = (value: any): value is JWKMetadata => {
14+
if (!isNonNullObject(value) || !value.keys) {
15+
return false
16+
}
17+
const keys = value.keys
18+
if (!Array.isArray(keys)) {
19+
return false
20+
}
21+
const filtered = keys.filter((key): key is JsonWebKeyWithKid =>
22+
isObject(key) && !!key.kid && typeof key.kid === 'string'
23+
)
24+
return keys.length === filtered.length
25+
}
1526

1627
/**
1728
* Class to fetch public keys from a client certificates URL.

src/validator.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,6 @@ export function isNonEmptyString(value: any): value is string {
7070
return isString(value) && value !== '';
7171
}
7272

73-
/**
74-
* Validates that a value is an array.
75-
*
76-
* @param value - The value to validate.
77-
* @returns Whether the value is an array or not.
78-
*/
79-
export function isArray<T>(value: any): value is T[] {
80-
return Array.isArray(value);
81-
}
82-
8373
/**
8474
*
8575
/**
@@ -89,7 +79,7 @@ export function isArray<T>(value: any): value is T[] {
8979
* @returns Whether the value is an object or not.
9080
*/
9181
export function isObject(value: any): boolean {
92-
return typeof value === 'object' && !isArray(value);
82+
return typeof value === 'object' && !Array.isArray(value);
9383
}
9484

9585
/**

tests/jwk-fetcher.test.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,20 @@ describe('isJWKMetadata', () => {
186186
expect(isJWKMetadata(invalidJWKMetadata)).toBe(false);
187187
});
188188

189-
it('should return false for object with non-array keys property', () => {
190-
const invalidJWKMetadata = {
191-
keys: 'notArray',
192-
};
193-
expect(isJWKMetadata(invalidJWKMetadata)).toBe(false);
189+
it('returns false if keys is not an array', () => {
190+
expect(isJWKMetadata({ keys: {} })).toBe(false);
191+
expect(isJWKMetadata({ keys: 'string' })).toBe(false);
192+
});
193+
194+
it('returns false if keys is an array but its elements do not have a kid property', () => {
195+
expect(isJWKMetadata({ keys: [{}] })).toBe(false);
196+
});
197+
198+
it('returns false if keys is an array but kid is not a string', () => {
199+
expect(isJWKMetadata({ keys: [{ kid: 123 }] })).toBe(false);
200+
});
201+
202+
it('returns false if only some keys have a kid property that is a string', () => {
203+
expect(isJWKMetadata({ keys: [{ kid: 'string' }, {}] })).toBe(false);
194204
});
195205
});

tests/validator.test.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it, expect } from 'vitest';
2-
import { isArray, isNonEmptyString, isNonNullObject, isNumber, isObject, isString, isURL } from '../src/validator';
2+
import { isNonEmptyString, isNonNullObject, isNumber, isObject, isString, isURL } from '../src/validator';
33

44
describe('validator', () => {
55
describe('isURL', () => {
@@ -76,22 +76,6 @@ describe('validator', () => {
7676
});
7777
});
7878

79-
describe('isArray', () => {
80-
describe('non-array', () => {
81-
const nonArrays = [undefined, null, NaN, 0, 1, '', 'a', true, false, {}, { a: 1 }];
82-
nonArrays.forEach(v => {
83-
it(`${v}`, () => expect(isArray(v)).toBeFalsy());
84-
});
85-
});
86-
87-
describe('array', () => {
88-
const arrays = [[], [1, 2, 3], [], [1, 2, 3]];
89-
arrays.forEach(v => {
90-
it(`${v}`, () => expect(isArray(v)).toBeTruthy());
91-
});
92-
});
93-
});
94-
9579
describe('isObject', () => {
9680
describe('non-object', () => {
9781
const nonObjects = [undefined, NaN, 0, 1, true, false, '', 'a', [], ['a']];

0 commit comments

Comments
 (0)