Skip to content

Supported cookie verification #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixed isJWKMetadata function
  • Loading branch information
Code-Hex committed Feb 17, 2024
commit abcb2afe4c44e053acf72a6624cbe9a5bc8c3c95
17 changes: 14 additions & 3 deletions src/jwk-fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { JsonWebKeyWithKid } from './jwt-decoder';
import type { KeyStorer } from './key-store';
import { isArray, isNonNullObject, isURL } from './validator';
import { isNonNullObject, isObject, isURL } from './validator';

export interface KeyFetcher {
fetchPublicKeys(): Promise<Array<JsonWebKeyWithKid>>;
Expand All @@ -10,8 +10,19 @@ interface JWKMetadata {
keys: Array<JsonWebKeyWithKid>;
}

export const isJWKMetadata = (value: any): value is JWKMetadata =>
isNonNullObject(value) && !!value.keys && isArray(value.keys);
export const isJWKMetadata = (value: any): value is JWKMetadata => {
if (!isNonNullObject(value) || !value.keys) {
return false
}
const keys = value.keys
if (!Array.isArray(keys)) {
return false
}
const filtered = keys.filter((key): key is JsonWebKeyWithKid =>
isObject(key) && !!key.kid && typeof key.kid === 'string'
)
return keys.length === filtered.length
}

/**
* Class to fetch public keys from a client certificates URL.
Expand Down
12 changes: 1 addition & 11 deletions src/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,6 @@ export function isNonEmptyString(value: any): value is string {
return isString(value) && value !== '';
}

/**
* Validates that a value is an array.
*
* @param value - The value to validate.
* @returns Whether the value is an array or not.
*/
export function isArray<T>(value: any): value is T[] {
return Array.isArray(value);
}

/**
*
/**
Expand All @@ -89,7 +79,7 @@ export function isArray<T>(value: any): value is T[] {
* @returns Whether the value is an object or not.
*/
export function isObject(value: any): boolean {
return typeof value === 'object' && !isArray(value);
return typeof value === 'object' && !Array.isArray(value);
}

/**
Expand Down
20 changes: 15 additions & 5 deletions tests/jwk-fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,20 @@ describe('isJWKMetadata', () => {
expect(isJWKMetadata(invalidJWKMetadata)).toBe(false);
});

it('should return false for object with non-array keys property', () => {
const invalidJWKMetadata = {
keys: 'notArray',
};
expect(isJWKMetadata(invalidJWKMetadata)).toBe(false);
it('returns false if keys is not an array', () => {
expect(isJWKMetadata({ keys: {} })).toBe(false);
expect(isJWKMetadata({ keys: 'string' })).toBe(false);
});

it('returns false if keys is an array but its elements do not have a kid property', () => {
expect(isJWKMetadata({ keys: [{}] })).toBe(false);
});

it('returns false if keys is an array but kid is not a string', () => {
expect(isJWKMetadata({ keys: [{ kid: 123 }] })).toBe(false);
});

it('returns false if only some keys have a kid property that is a string', () => {
expect(isJWKMetadata({ keys: [{ kid: 'string' }, {}] })).toBe(false);
});
});
18 changes: 1 addition & 17 deletions tests/validator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { isArray, isNonEmptyString, isNonNullObject, isNumber, isObject, isString, isURL } from '../src/validator';
import { isNonEmptyString, isNonNullObject, isNumber, isObject, isString, isURL } from '../src/validator';

describe('validator', () => {
describe('isURL', () => {
Expand Down Expand Up @@ -76,22 +76,6 @@ describe('validator', () => {
});
});

describe('isArray', () => {
describe('non-array', () => {
const nonArrays = [undefined, null, NaN, 0, 1, '', 'a', true, false, {}, { a: 1 }];
nonArrays.forEach(v => {
it(`${v}`, () => expect(isArray(v)).toBeFalsy());
});
});

describe('array', () => {
const arrays = [[], [1, 2, 3], [], [1, 2, 3]];
arrays.forEach(v => {
it(`${v}`, () => expect(isArray(v)).toBeTruthy());
});
});
});

describe('isObject', () => {
describe('non-object', () => {
const nonObjects = [undefined, NaN, 0, 1, true, false, '', 'a', [], ['a']];
Expand Down