aboutsummaryrefslogtreecommitdiff
path: root/src/modules
diff options
context:
space:
mode:
authorZyb Jared Valdez <[email protected]>2024-10-24 05:28:05 +0800
committerGitHub <[email protected]>2024-10-23 23:28:05 +0200
commite3858f221bb3b6e1278933c0b8e0d98da6854e52 (patch)
tree0ca600d9311b2fe0d203be983ad66fdc1f7cd79d /src/modules
parent48931a56b761352a35d2f7f6c1240231a5df5efd (diff)
downloadfaker-e3858f221bb3b6e1278933c0b8e0d98da6854e52.tar.xz
faker-e3858f221bb3b6e1278933c0b8e0d98da6854e52.zip
feat(internet): add jwt method (#2936)
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/internet/index.ts103
1 files changed, 103 insertions, 0 deletions
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}`;
+ }
}