diff options
| author | Zyb Jared Valdez <[email protected]> | 2024-10-24 05:28:05 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-10-23 23:28:05 +0200 |
| commit | e3858f221bb3b6e1278933c0b8e0d98da6854e52 (patch) | |
| tree | 0ca600d9311b2fe0d203be983ad66fdc1f7cd79d /src | |
| parent | 48931a56b761352a35d2f7f6c1240231a5df5efd (diff) | |
| download | faker-e3858f221bb3b6e1278933c0b8e0d98da6854e52.tar.xz faker-e3858f221bb3b6e1278933c0b8e0d98da6854e52.zip | |
feat(internet): add jwt method (#2936)
Diffstat (limited to 'src')
| -rw-r--r-- | src/definitions/internet.ts | 2 | ||||
| -rw-r--r-- | src/internal/base64.ts | 23 | ||||
| -rw-r--r-- | src/locales/base/internet/index.ts | 2 | ||||
| -rw-r--r-- | src/locales/base/internet/jwt_algorithm.ts | 15 | ||||
| -rw-r--r-- | src/modules/internet/index.ts | 103 |
5 files changed, 145 insertions, 0 deletions
diff --git a/src/definitions/internet.ts b/src/definitions/internet.ts index e4687534..fdcd6a0f 100644 --- a/src/definitions/internet.ts +++ b/src/definitions/internet.ts @@ -29,4 +29,6 @@ export type InternetDefinition = LocaleEntry<{ * List of some HTTP status codes. */ http_status_code: Record<HTTPStatusCodeType, number[]>; + + jwt_algorithm: string[]; }>; diff --git a/src/internal/base64.ts b/src/internal/base64.ts index e2702355..c1f309fb 100644 --- a/src/internal/base64.ts +++ b/src/internal/base64.ts @@ -23,3 +23,26 @@ export const toBase64: (input: string) => string = return btoa(binaryString); } : (input) => Buffer.from(input).toString('base64'); + +/** + * This works the same as `Buffer.from(input).toString('base64url')` + * to work on both Node.js and browser environment. + * + * @internal + * + * @param input The string to encode to Base64 URL. + * + * @returns Base64 URL encoded string. + * + * @see https://datatracker.ietf.org/doc/html/rfc4648 + * + * @example const encodedHeader = toBase64Url(JSON.stringify(header)); + */ +export const toBase64Url: (input: string) => string = + typeof Buffer === 'undefined' + ? (input) => + toBase64(input) + .replaceAll('+', '-') + .replaceAll('/', '_') + .replaceAll(/=+$/g, '') + : (input) => Buffer.from(input).toString('base64url'); diff --git a/src/locales/base/internet/index.ts b/src/locales/base/internet/index.ts index 127e78c9..55709025 100644 --- a/src/locales/base/internet/index.ts +++ b/src/locales/base/internet/index.ts @@ -5,10 +5,12 @@ import type { InternetDefinition } from '../../..'; import emoji from './emoji'; import http_status_code from './http_status_code'; +import jwt_algorithm from './jwt_algorithm'; const internet: InternetDefinition = { emoji, http_status_code, + jwt_algorithm, }; export default internet; diff --git a/src/locales/base/internet/jwt_algorithm.ts b/src/locales/base/internet/jwt_algorithm.ts new file mode 100644 index 00000000..1cad0bae --- /dev/null +++ b/src/locales/base/internet/jwt_algorithm.ts @@ -0,0 +1,15 @@ +export default [ + 'HS256', + 'HS384', + 'HS512', + 'RS256', + 'RS384', + 'RS512', + 'ES256', + 'ES384', + 'ES512', + 'PS256', + 'PS384', + 'PS512', + 'none', +]; diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 3e7759f2..58c6e586 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -1,4 +1,5 @@ import { FakerError } from '../../errors/faker-error'; +import { toBase64Url } from '../../internal/base64'; import { deprecated } from '../../internal/deprecated'; import { ModuleBase } from '../../internal/module-base'; import { charMapping } from './char-mappings'; @@ -1019,4 +1020,106 @@ export class InternetModule extends ModuleBase { this.faker.definitions.internet.emoji[emojiType] ); } + + /** + * Generates a random JWT (JSON Web Token) Algorithm. + * + * @see faker.internet.jwt(): For generating random JWT (JSON Web Token). + * + * @example + * faker.internet.jwtAlgorithm() // 'HS256' + * faker.internet.jwtAlgorithm() // 'RS512' + * + * @since 9.1.0 + */ + jwtAlgorithm(): string { + return this.faker.helpers.arrayElement( + this.faker.definitions.internet.jwt_algorithm + ); + } + + /** + * Generates a random JWT (JSON Web Token). + * + * Please note that this method generates a random signature instead of a valid one. + * + * @param options The optional options object. + * @param options.header The Header to use for the token. Defaults to a random object with the following fields: `alg` and `typ`. + * @param options.payload The Payload to use for the token. Defaults to a random object with the following fields: `iat`, `exp`, `nbf`, `iss`, `sub`, `aud`, and `jti`. + * @param options.refDate The date to use as reference point for the newly generated date. + * + * @see https://datatracker.ietf.org/doc/html/rfc7519 + * @see faker.internet.jwtAlgorithm(): For generating random JWT (JSON Web Token) Algorithm. + * + * @example + * faker.internet.jwt() + * faker.internet.jwt({ header: { alg: 'HS256' }}) // 'eyJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE3MTg2MTM3MTIsImV4cCI6MTcxODYzMzY3OSwibmJmIjoxNjk3MjYzNjMwLCJpc3MiOiJEb3lsZSBhbmQgU29ucyIsInN1YiI6IjYxYWRkYWFmLWY4MjktNDkzZS1iNTI1LTJjMGJkNjkzOTdjNyIsImF1ZCI6IjczNjcyMjVjLWIwMWMtNGE1My1hYzQyLTYwOWJkZmI1MzBiOCIsImp0aSI6IjU2Y2ZkZjAxLWRhMzMtNGUxNi04MzJiLTFlYTk3ZGY1MTQ2YSJ9.5iUgaCaFVPZ8d1QD0xMjoeJbmPVyUfKfoRQ6Njzm5MLp5F4UMh5REbPCrW70fAkr' + * faker.internet.jwt({ payload: { iss: 'Acme' }}) // 'eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBY21lIn0.syUt0GBukNac8Cn1AGKFq2SWAXWy1YIfl0uOYiwg6TZ3omAW0c7FGWY6bC7ZOFSt' + * faker.internet.jwt({ refDate: '2020-01-01T00:00:00.000Z' }) // 'eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1Nzc4MDY4NDUsImV4cCI6MTU3Nzg0NjI4MCwibmJmIjoxNTgxNTQyMDYwLCJpc3MiOiJLcmVpZ2VyLCBBbHRlbndlcnRoIGFuZCBQYXVjZWsiLCJzdWIiOiI5NzVjMjMyOS02MDlhLTRjYTYtYjBkZi05ZmY4MGZiNDUwN2QiLCJhdWQiOiI0ODQxZWYwNi01OWYwLTQzMWEtYmFmZi0xMjkxZmRhZDdhNjgiLCJqdGkiOiJmNDBjZTJiYi00ZWYyLTQ1MjMtOGIxMy1kN2Q4NTA5N2M2ZTUifQ.cuClEZQ0CyPIMVS5uxrMwWXz0wcqFFdt0oNne3PMryyly0jghkxVurss2TapMC3C' + * + * @since 9.1.0 + */ + jwt( + options: { + /** + * The header to use for the token. If present, it will replace any default values. + * + * @default + * { + * alg: faker.internet.jwtAlgorithm(), + * typ: 'JWT' + * } + */ + header?: Record<string, unknown>; + /** + * The payload to use for the token. If present, it will replace any default values. + * + * @default + * { + * iat: faker.date.recent(), + * exp: faker.date.soon(), + * nbf: faker.date.anytime(), + * iss: faker.company.name(), + * sub: faker.string.uuid(), + * aud: faker.string.uuid(), + * jti: faker.string.uuid() + * } + */ + payload?: Record<string, unknown>; + /** + * The date to use as reference point for the newly generated date. + * + * @default faker.defaultRefDate() + */ + refDate?: string | Date | number; + } = {} + ): string { + const { refDate = this.faker.defaultRefDate() } = options; + + const iatDefault = this.faker.date.recent({ refDate }); + + const { + header = { + alg: this.jwtAlgorithm(), + typ: 'JWT', + }, + payload = { + iat: Math.round(iatDefault.valueOf() / 1000), + exp: Math.round( + this.faker.date.soon({ refDate: iatDefault }).valueOf() / 1000 + ), + nbf: Math.round(this.faker.date.anytime({ refDate }).valueOf() / 1000), + iss: this.faker.company.name(), + sub: this.faker.string.uuid(), + aud: this.faker.string.uuid(), + jti: this.faker.string.uuid(), + }, + } = options; + + const encodedHeader = toBase64Url(JSON.stringify(header)); + const encodedPayload = toBase64Url(JSON.stringify(payload)); + const signature = this.faker.string.alphanumeric(64); + + return `${encodedHeader}.${encodedPayload}.${signature}`; + } } |
