File size: 39,568 Bytes
62cfde8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 | diff --git a/src/server/auth-client.test.ts b/src/server/auth-client.test.ts
index 459088b4..097b90fd 100644
--- a/src/server/auth-client.test.ts
+++ b/src/server/auth-client.test.ts
@@ -496,7 +496,9 @@ ca/T0LLtgmbMmxSv/MmzIg==
createdAt: Math.floor(Date.now() / 1000)
}
};
- const sessionCookie = await encrypt(session, secret);
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const sessionCookie = await encrypt(session, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__session=${sessionCookie}`);
const request = new NextRequest(
@@ -516,7 +518,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
updatedSessionCookie!.value,
secret
);
- expect(updatedSessionCookieValue).toEqual({
+ expect(updatedSessionCookieValue).toEqual(expect.objectContaining({
user: {
sub: DEFAULT.sub
},
@@ -529,7 +531,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
sid: DEFAULT.sid,
createdAt: expect.any(Number)
}
- });
+ }));
// assert that the session expiry has been extended by the inactivity duration
expect(updatedSessionCookie?.maxAge).toEqual(1800);
@@ -868,13 +870,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
);
expect(transactionCookie).toBeDefined();
expect((await decrypt(transactionCookie!.value, secret)).payload).toEqual(
- {
+ expect.objectContaining({
nonce: authorizationUrl.searchParams.get("nonce"),
codeVerifier: expect.any(String),
responseType: "code",
state: authorizationUrl.searchParams.get("state"),
returnTo: "/"
- }
+ })
);
});
@@ -1023,13 +1025,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
expect(transactionCookie).toBeDefined();
expect(
(await decrypt(transactionCookie!.value, secret)).payload
- ).toEqual({
+ ).toEqual(expect.objectContaining({
nonce: authorizationUrl.searchParams.get("nonce"),
codeVerifier: expect.any(String),
responseType: "code",
state: authorizationUrl.searchParams.get("state"),
returnTo: "/"
- });
+ }));
});
it("should forward the configured authorization parameters to the authorization server", async () => {
@@ -1356,14 +1358,14 @@ ca/T0LLtgmbMmxSv/MmzIg==
);
expect(transactionCookie).toBeDefined();
expect((await decrypt(transactionCookie!.value, secret)).payload).toEqual(
- {
+ expect.objectContaining({
nonce: authorizationUrl.searchParams.get("nonce"),
maxAge: 3600,
codeVerifier: expect.any(String),
responseType: "code",
state: authorizationUrl.searchParams.get("state"),
returnTo: "/"
- }
+ })
);
});
@@ -1403,13 +1405,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
);
expect(transactionCookie).toBeDefined();
expect((await decrypt(transactionCookie!.value, secret)).payload).toEqual(
- {
+ expect.objectContaining({
nonce: authorizationUrl.searchParams.get("nonce"),
codeVerifier: expect.any(String),
responseType: "code",
state: authorizationUrl.searchParams.get("state"),
returnTo: "https://example.com/dashboard"
- }
+ })
);
});
@@ -1449,13 +1451,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
);
expect(transactionCookie).toBeDefined();
expect((await decrypt(transactionCookie!.value, secret)).payload).toEqual(
- {
+ expect.objectContaining({
nonce: authorizationUrl.searchParams.get("nonce"),
codeVerifier: expect.any(String),
responseType: "code",
state: authorizationUrl.searchParams.get("state"),
returnTo: "/"
- }
+ })
);
});
@@ -1583,13 +1585,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
expect(transactionCookie).toBeDefined();
expect(
(await decrypt(transactionCookie!.value, secret)).payload
- ).toEqual({
+ ).toEqual(expect.objectContaining({
nonce: expect.any(String),
codeVerifier: expect.any(String),
responseType: "code",
state,
returnTo: "/"
- });
+ }));
});
describe("custom parameters to the authorization server", async () => {
@@ -1662,13 +1664,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
expect(transactionCookie).toBeDefined();
expect(
(await decrypt(transactionCookie!.value, secret)).payload
- ).toEqual({
+ ).toEqual(expect.objectContaining({
nonce: expect.any(String),
codeVerifier: expect.any(String),
responseType: "code",
state,
returnTo: "/"
- });
+ }));
});
it("should forward custom parameters set in the configuration to the authorization server", async () => {
@@ -1742,13 +1744,13 @@ ca/T0LLtgmbMmxSv/MmzIg==
expect(transactionCookie).toBeDefined();
expect(
(await decrypt(transactionCookie!.value, secret)).payload
- ).toEqual({
+ ).toEqual(expect.objectContaining({
nonce: expect.any(String),
codeVerifier: expect.any(String),
responseType: "code",
state,
returnTo: "/"
- });
+ }));
});
});
});
@@ -1838,7 +1840,9 @@ ca/T0LLtgmbMmxSv/MmzIg==
createdAt: Math.floor(Date.now() / 1000)
}
};
- const sessionCookie = await encrypt(session, secret);
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const sessionCookie = await encrypt(session, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__session=${sessionCookie}`);
const request = new NextRequest(
@@ -1911,7 +1915,9 @@ ca/T0LLtgmbMmxSv/MmzIg==
createdAt: Math.floor(Date.now() / 1000)
}
};
- const sessionCookie = await encrypt(session, secret);
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const sessionCookie = await encrypt(session, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__session=${sessionCookie}`);
@@ -2173,7 +2179,9 @@ ca/T0LLtgmbMmxSv/MmzIg==
createdAt: Math.floor(Date.now() / 1000)
}
};
- const sessionCookie = await encrypt(session, secret);
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const sessionCookie = await encrypt(session, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__session=${sessionCookie}`);
const request = new NextRequest(
@@ -2268,9 +2276,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -2288,7 +2298,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
const sessionCookie = response.cookies.get("__session");
expect(sessionCookie).toBeDefined();
const { payload: session } = await decrypt(sessionCookie!.value, secret);
- expect(session).toEqual({
+ expect(session).toEqual(expect.objectContaining({
user: {
sub: DEFAULT.sub
},
@@ -2302,7 +2312,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
sid: expect.any(String),
createdAt: expect.any(Number)
}
- });
+ }));
// validate the transaction cookie has been removed
const transactionCookie = response.cookies.get(`__txn_${state}`);
@@ -2377,9 +2387,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -2397,7 +2409,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
const sessionCookie = response.cookies.get("__session");
expect(sessionCookie).toBeDefined();
const { payload: session } = await decrypt(sessionCookie!.value, secret);
- expect(session).toEqual({
+ expect(session).toEqual(expect.objectContaining({
user: {
sub: DEFAULT.sub
},
@@ -2411,7 +2423,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
sid: expect.any(String),
createdAt: expect.any(Number)
}
- });
+ }));
// validate the transaction cookie has been removed
const transactionCookie = response.cookies.get(`__txn_${state}`);
@@ -2496,9 +2508,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_does-not-exist=${await encrypt(transactionState, secret)}`
+ `__txn_does-not-exist=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -2548,9 +2562,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -2607,9 +2623,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -2663,9 +2681,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -2726,9 +2746,12 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -2773,7 +2796,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
sessionCookie!.value,
secret
);
- expect(session).toEqual(expectedSession);
+ expect(session).toEqual(expect.objectContaining(expectedSession));
});
it("should be called with an error if the state parameter is missing", async () => {
@@ -2881,9 +2904,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_non-existent-state=${await encrypt(transactionState, secret)}`
+ `__txn_non-existent-state=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -2956,9 +2981,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -3040,9 +3067,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -3124,9 +3153,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -3204,9 +3235,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -3227,7 +3260,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
sessionCookie!.value,
secret
);
- expect(session).toEqual({
+ expect(session).toEqual(expect.objectContaining({
user: {
sub: DEFAULT.sub,
name: "John Doe",
@@ -3244,7 +3277,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
sid: expect.any(String),
createdAt: expect.any(Number)
}
- });
+ }));
});
it("should not call the hook if the session is not established", async () => {
@@ -3334,9 +3367,11 @@ ca/T0LLtgmbMmxSv/MmzIg==
state: state,
returnTo: "/dashboard"
};
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
headers.set(
"cookie",
- `__txn_${state}=${await encrypt(transactionState, secret)}`
+ `__txn_${state}=${await encrypt(transactionState, secret, expiration)}`
);
const request = new NextRequest(url, {
method: "GET",
@@ -3357,7 +3392,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
sessionCookie!.value,
secret
);
- expect(session).toEqual({
+ expect(session).toEqual(expect.objectContaining({
user: {
sub: DEFAULT.sub,
name: "John Doe",
@@ -3374,7 +3409,7 @@ ca/T0LLtgmbMmxSv/MmzIg==
sid: expect.any(String),
createdAt: expect.any(Number)
}
- });
+ }));
});
});
});
@@ -3432,7 +3467,9 @@ ca/T0LLtgmbMmxSv/MmzIg==
createdAt: Math.floor(Date.now() / 1000)
}
};
- const sessionCookie = await encrypt(session, secret);
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const sessionCookie = await encrypt(session, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__session=${sessionCookie}`);
const request = new NextRequest(
@@ -3543,7 +3580,9 @@ ca/T0LLtgmbMmxSv/MmzIg==
createdAt: Math.floor(Date.now() / 1000)
}
};
- const sessionCookie = await encrypt(session, secret);
+ const maxAge = 60 * 60; // 1 hour
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const sessionCookie = await encrypt(session, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__session=${sessionCookie}`);
const request = new NextRequest(
diff --git a/src/server/cookies.test.ts b/src/server/cookies.test.ts
index f943b9aa..0f895d4e 100644
--- a/src/server/cookies.test.ts
+++ b/src/server/cookies.test.ts
@@ -9,28 +9,47 @@ describe("encrypt/decrypt", async () => {
it("should encrypt/decrypt a payload with the correct secret", async () => {
const payload = { key: "value" };
- const encrypted = await encrypt(payload, secret);
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const encrypted = await encrypt(payload, secret, expiration);
const decrypted = await decrypt(encrypted, secret);
- expect(decrypted.payload).toEqual(payload);
+ expect(decrypted.payload).toEqual(expect.objectContaining(payload));
});
it("should fail to decrypt a payload with the incorrect secret", async () => {
const payload = { key: "value" };
- const encrypted = await encrypt(payload, secret);
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const encrypted = await encrypt(payload, secret, expiration);
await expect(() =>
decrypt(encrypted, incorrectSecret)
).rejects.toThrowError();
});
+ it("should fail to decrypt when expired", async () => {
+ const payload = { key: "value" };
+ const expiration = Math.floor(Date.now() / 1000 - 60); // 60 seconds in the past
+ const encrypted = await encrypt(payload, secret, expiration);
+ await expect(() =>
+ decrypt(encrypted, secret)
+ ).rejects.toThrowError(`"exp" claim timestamp check failed`);
+ });
+
it("should fail to encrypt if a secret is not provided", async () => {
const payload = { key: "value" };
- await expect(() => encrypt(payload, "")).rejects.toThrowError();
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+
+ await expect(() => encrypt(payload, "", expiration)).rejects.toThrowError();
});
it("should fail to decrypt if a secret is not provided", async () => {
const payload = { key: "value" };
- const encrypted = await encrypt(payload, secret);
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+
+ const encrypted = await encrypt(payload, secret, expiration);
await expect(() => decrypt(encrypted, "")).rejects.toThrowError();
});
});
diff --git a/src/server/cookies.ts b/src/server/cookies.ts
index be31bbe7..757bf947 100644
--- a/src/server/cookies.ts
+++ b/src/server/cookies.ts
@@ -15,6 +15,7 @@ const ENCRYPTION_INFO = "JWE CEK";
export async function encrypt(
payload: jose.JWTPayload,
secret: string,
+ expiration: number,
additionalHeaders?: {
iat: number;
uat: number;
@@ -31,6 +32,7 @@ export async function encrypt(
const encryptedCookie = await new jose.EncryptJWT(payload)
.setProtectedHeader({ enc: ENC, alg: ALG, ...additionalHeaders })
+ .setExpirationTime(expiration)
.encrypt(encryptionSecret);
return encryptedCookie.toString();
diff --git a/src/server/session/stateful-session-store.test.ts b/src/server/session/stateful-session-store.test.ts
index 6e690756..d8551656 100644
--- a/src/server/session/stateful-session-store.test.ts
+++ b/src/server/session/stateful-session-store.test.ts
@@ -34,11 +34,14 @@ describe("Stateful Session Store", async () => {
set: vi.fn(),
delete: vi.fn()
};
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
const encryptedCookieValue = await encrypt(
{
id: sessionId
},
- secret
+ secret,
+ expiration,
);
const headers = new Headers();
@@ -99,11 +102,14 @@ describe("Stateful Session Store", async () => {
set: vi.fn(),
delete: vi.fn()
};
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
const encryptedCookieValue = await encrypt(
{
id: sessionId
},
- secret
+ secret,
+ expiration,
);
const headers = new Headers();
@@ -464,12 +470,14 @@ describe("Stateful Session Store", async () => {
set: vi.fn(),
delete: vi.fn()
};
-
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
const encryptedCookieValue = await encrypt(
{
id: sessionId
},
- secret
+ secret,
+ expiration,
);
const headers = new Headers();
headers.append("cookie", `__session=${encryptedCookieValue}`);
@@ -750,11 +758,14 @@ describe("Stateful Session Store", async () => {
set: vi.fn(),
delete: vi.fn()
};
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
const encryptedCookieValue = await encrypt(
{
id: sessionId
},
- secret
+ secret,
+ expiration,
);
const headers = new Headers();
headers.append("cookie", `__session=${encryptedCookieValue}`);
diff --git a/src/server/session/stateful-session-store.ts b/src/server/session/stateful-session-store.ts
index 04206981..802cebdc 100644
--- a/src/server/session/stateful-session-store.ts
+++ b/src/server/session/stateful-session-store.ts
@@ -130,15 +130,17 @@ export class StatefulSessionStore extends AbstractSessionStore {
if (!sessionId) {
sessionId = generateId();
}
-
+
+ const maxAge = this.calculateMaxAge(session.internal.createdAt);
+ const expiration = Date.now() / 1000 + maxAge;
const jwe = await cookies.encrypt(
{
id: sessionId
},
- this.secret
+ this.secret,
+ expiration,
);
- const maxAge = this.calculateMaxAge(session.internal.createdAt);
-
+
resCookies.set(this.sessionCookieName, jwe.toString(), {
...this.cookieConfig,
maxAge
diff --git a/src/server/session/stateless-session-store.test.ts b/src/server/session/stateless-session-store.test.ts
index 23f853d7..45bf7e58 100644
--- a/src/server/session/stateless-session-store.test.ts
+++ b/src/server/session/stateless-session-store.test.ts
@@ -22,7 +22,9 @@ describe("Stateless Session Store", async () => {
createdAt: Math.floor(Date.now() / 1000)
}
};
- const encryptedCookieValue = await encrypt(session, secret);
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const encryptedCookieValue = await encrypt(session, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__session=${encryptedCookieValue}`);
@@ -32,7 +34,7 @@ describe("Stateless Session Store", async () => {
secret
});
- expect(await sessionStore.get(requestCookies)).toEqual(session);
+ expect(await sessionStore.get(requestCookies)).toEqual(expect.objectContaining(session));
});
it("should return null if no session cookie exists", async () => {
@@ -65,9 +67,12 @@ describe("Stateless Session Store", async () => {
uat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000)
};
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
const encryptedCookieValue = await encrypt(
legacySession,
secret,
+ expiration,
legacyHeader
);
@@ -106,9 +111,12 @@ describe("Stateless Session Store", async () => {
uat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000)
};
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
const encryptedCookieValue = await encrypt(
legacySession,
secret,
+ expiration,
legacyHeader
);
@@ -153,9 +161,12 @@ describe("Stateless Session Store", async () => {
uat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000)
};
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
const encryptedCookieValue = await encrypt(
legacySession,
secret,
+ expiration,
legacyHeader
);
@@ -206,7 +217,9 @@ describe("Stateless Session Store", async () => {
}
]
};
- const encryptedCookieValue = await encrypt(session, secret);
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const encryptedCookieValue = await encrypt(session, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__session=${encryptedCookieValue}`);
@@ -216,7 +229,7 @@ describe("Stateless Session Store", async () => {
secret
});
- expect(await sessionStore.get(requestCookies)).toEqual(session);
+ expect(await sessionStore.get(requestCookies)).toEqual(expect.objectContaining(session));
});
});
@@ -264,7 +277,7 @@ describe("Stateless Session Store", async () => {
const cookie = responseCookies.get("__session");
expect(cookie).toBeDefined();
- expect((await decrypt(cookie!.value, secret)).payload).toEqual(session);
+ expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session));
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
expect(cookie?.sameSite).toEqual("lax");
@@ -306,12 +319,10 @@ describe("Stateless Session Store", async () => {
const cookie = responseCookies.get("__session");
expect(cookie).toBeDefined();
- expect((await decrypt(cookie!.value, secret)).payload).toEqual(session);
- expect(cookie?.path).toEqual("/");
- expect(cookie?.httpOnly).toEqual(true);
- expect(cookie?.sameSite).toEqual("lax");
- expect(cookie?.maxAge).toEqual(0); // cookie should expire immediately
- expect(cookie?.secure).toEqual(false);
+
+ await expect(
+ decrypt(cookie!.value, secret)
+ ).rejects.toThrow(`"exp" claim timestamp check failed`);
});
it("should delete the legacy cookie if it exists", async () => {
@@ -413,7 +424,7 @@ describe("Stateless Session Store", async () => {
const cookie = responseCookies.get("__session");
expect(cookie).toBeDefined();
- expect((await decrypt(cookie!.value, secret)).payload).toEqual(session);
+ expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session));
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
expect(cookie?.sameSite).toEqual("lax");
@@ -453,7 +464,7 @@ describe("Stateless Session Store", async () => {
const cookie = responseCookies.get("__session");
expect(cookie).toBeDefined();
- expect((await decrypt(cookie!.value, secret)).payload).toEqual(session);
+ expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session));
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
expect(cookie?.sameSite).toEqual("lax");
@@ -492,7 +503,7 @@ describe("Stateless Session Store", async () => {
const cookie = responseCookies.get("__session");
expect(cookie).toBeDefined();
- expect((await decrypt(cookie!.value, secret)).payload).toEqual(session);
+ expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session));
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
expect(cookie?.sameSite).toEqual("strict");
@@ -528,7 +539,7 @@ describe("Stateless Session Store", async () => {
const cookie = responseCookies.get("__session");
expect(cookie).toBeDefined();
- expect((await decrypt(cookie!.value, secret)).payload).toEqual(session);
+ expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session));
expect(cookie?.path).toEqual("/custom-path");
});
@@ -563,7 +574,7 @@ describe("Stateless Session Store", async () => {
const cookie = responseCookies.get("custom-session");
expect(cookie).toBeDefined();
- expect((await decrypt(cookie!.value, secret)).payload).toEqual(session);
+ expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(session));
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
expect(cookie?.sameSite).toEqual("lax");
diff --git a/src/server/session/stateless-session-store.ts b/src/server/session/stateless-session-store.ts
index 2e804830..29310b81 100644
--- a/src/server/session/stateless-session-store.ts
+++ b/src/server/session/stateless-session-store.ts
@@ -87,8 +87,9 @@ export class StatelessSessionStore extends AbstractSessionStore {
session: SessionData
) {
const { connectionTokenSets, ...originalSession } = session;
- const jwe = await cookies.encrypt(originalSession, this.secret);
const maxAge = this.calculateMaxAge(session.internal.createdAt);
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const jwe = await cookies.encrypt(originalSession, this.secret, expiration);
const cookieValue = jwe.toString();
const options: CookieOptions = {
...this.cookieConfig,
@@ -141,7 +142,8 @@ export class StatelessSessionStore extends AbstractSessionStore {
cookieName: string,
maxAge: number
) {
- const jwe = await cookies.encrypt(session, this.secret);
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const jwe = await cookies.encrypt(session, this.secret, expiration);
const cookieValue = jwe.toString();
diff --git a/src/server/transaction-store.test.ts b/src/server/transaction-store.test.ts
index 94a3ea56..c6fd0a85 100644
--- a/src/server/transaction-store.test.ts
+++ b/src/server/transaction-store.test.ts
@@ -20,7 +20,9 @@ describe("Transaction Store", async () => {
state,
returnTo: "/dashboard"
};
- const encryptedCookieValue = await encrypt(transactionState, secret);
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const encryptedCookieValue = await encrypt(transactionState, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__txn_${state}=${encryptedCookieValue}`);
@@ -32,7 +34,7 @@ describe("Transaction Store", async () => {
expect(
(await transactionStore.get(requestCookies, state))?.payload
- ).toEqual(transactionState);
+ ).toEqual(expect.objectContaining(transactionState));
});
it("should return null if no transaction cookie with a matching state exists", async () => {
@@ -48,7 +50,9 @@ describe("Transaction Store", async () => {
state,
returnTo: "/dashboard"
};
- const encryptedCookieValue = await encrypt(transactionState, secret);
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+ const encryptedCookieValue = await encrypt(transactionState, secret, expiration);
const headers = new Headers();
headers.append("cookie", `__txn_incorrect-state=${encryptedCookieValue}`);
@@ -89,7 +93,7 @@ describe("Transaction Store", async () => {
expect(cookie).toBeDefined();
expect((await decrypt(cookie!.value, secret)).payload).toEqual(
- transactionState
+ expect.objectContaining(transactionState)
);
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
@@ -152,7 +156,7 @@ describe("Transaction Store", async () => {
expect(cookie).toBeDefined();
expect((await decrypt(cookie!.value, secret)).payload).toEqual(
- transactionState
+ expect.objectContaining(transactionState)
);
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
@@ -190,7 +194,7 @@ describe("Transaction Store", async () => {
expect(cookie).toBeDefined();
expect((await decrypt(cookie!.value, secret)).payload).toEqual(
- transactionState
+ expect.objectContaining(transactionState)
);
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
@@ -228,7 +232,7 @@ describe("Transaction Store", async () => {
expect(cookie).toBeDefined();
expect((await decrypt(cookie!.value, secret)).payload).toEqual(
- transactionState
+ expect.objectContaining(transactionState)
);
expect(cookie?.path).toEqual("/custom-path");
});
@@ -261,9 +265,9 @@ describe("Transaction Store", async () => {
const cookie = responseCookies.get(cookieName);
expect(cookie).toBeDefined();
- expect((await decrypt(cookie!.value, secret)).payload).toEqual(
+ expect((await decrypt(cookie!.value, secret)).payload).toEqual(expect.objectContaining(
transactionState
- );
+ ));
expect(cookie?.path).toEqual("/");
expect(cookie?.httpOnly).toEqual(true);
expect(cookie?.sameSite).toEqual("lax");
diff --git a/src/server/transaction-store.ts b/src/server/transaction-store.ts
index 9a20ea4e..45ff2de0 100644
--- a/src/server/transaction-store.ts
+++ b/src/server/transaction-store.ts
@@ -79,7 +79,9 @@ export class TransactionStore {
resCookies: cookies.ResponseCookies,
transactionState: TransactionState
) {
- const jwe = await cookies.encrypt(transactionState, this.secret);
+
+ const expiration = Math.floor(Date.now() / 1000 + this.cookieConfig.maxAge!);
+ const jwe = await cookies.encrypt(transactionState, this.secret, expiration);
if (!transactionState.state) {
throw new Error("Transaction state is required");
diff --git a/src/testing/generate-session-cookie.test.ts b/src/testing/generate-session-cookie.test.ts
index a3a83040..8e433a0e 100644
--- a/src/testing/generate-session-cookie.test.ts
+++ b/src/testing/generate-session-cookie.test.ts
@@ -29,7 +29,7 @@ describe("generateSessionCookie", async () => {
};
const sessionCookie = await generateSessionCookie(session, config);
expect(sessionCookie).toEqual(expect.any(String));
- expect((await decrypt(sessionCookie, secret)).payload).toEqual({
+ expect((await decrypt(sessionCookie, secret)).payload).toEqual(expect.objectContaining({
user: {
sub: "user_123"
},
@@ -42,7 +42,7 @@ describe("generateSessionCookie", async () => {
sid: "auth0-sid",
createdAt: createdAt
}
- });
+ }));
});
it("should populate the internal property if it was not provided", async () => {
@@ -60,7 +60,7 @@ describe("generateSessionCookie", async () => {
};
const sessionCookie = await generateSessionCookie(session, config);
expect(sessionCookie).toEqual(expect.any(String));
- expect((await decrypt(sessionCookie, secret)).payload).toEqual({
+ expect((await decrypt(sessionCookie, secret)).payload).toEqual(expect.objectContaining({
user: {
sub: "user_123"
},
@@ -73,7 +73,7 @@ describe("generateSessionCookie", async () => {
sid: "auth0-sid",
createdAt: expect.any(Number)
}
- });
+ }));
});
it("should not populate the internal property if a null was provided", async () => {
@@ -93,20 +93,12 @@ describe("generateSessionCookie", async () => {
};
const sessionCookie = await generateSessionCookie(session, config);
expect(sessionCookie).toEqual(expect.any(String));
- expect((await decrypt(sessionCookie, secret)).payload).toEqual({
- user: {
- sub: "user_123"
- },
- tokenSet: {
- accessToken: "at_123",
- refreshToken: "rt_123",
- expiresAt: 123456
- },
- internal: null
- });
+ expect((await decrypt(sessionCookie, secret)).payload).not.toEqual(expect.objectContaining({
+ internal: expect.anything()
+ }));
});
- it("should not populate the internal property if a undefine was provided", async () => {
+ it("should not populate the internal property if a undefined was provided", async () => {
const session: Partial<SessionData> = {
user: { sub: "user_123" },
tokenSet: {
@@ -122,16 +114,8 @@ describe("generateSessionCookie", async () => {
};
const sessionCookie = await generateSessionCookie(session, config);
expect(sessionCookie).toEqual(expect.any(String));
- expect((await decrypt(sessionCookie, secret)).payload).toEqual({
- user: {
- sub: "user_123"
- },
- tokenSet: {
- accessToken: "at_123",
- refreshToken: "rt_123",
- expiresAt: 123456
- },
- internal: undefined
- });
+ expect((await decrypt(sessionCookie, secret)).payload).not.toEqual(expect.objectContaining({
+ internal: expect.anything()
+ }));
});
});
diff --git a/src/testing/generate-session-cookie.ts b/src/testing/generate-session-cookie.ts
index 371a3aec..0e4ba05f 100644
--- a/src/testing/generate-session-cookie.ts
+++ b/src/testing/generate-session-cookie.ts
@@ -21,5 +21,8 @@ export const generateSessionCookie = async (
};
}
- return encrypt(session, config.secret);
+ const maxAge = 60 * 60; // 1 hour in seconds
+ const expiration = Math.floor(Date.now() / 1000 + maxAge);
+
+ return encrypt(session, config.secret, expiration);
};
|