Skip to content

Commit 38acece

Browse files
authored
NONE: decrease TTL of the token (#2611)
1 parent d8caaf7 commit 38acece

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

src/github/client/app-token-holder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AsymmetricAlgorithm, encodeAsymmetric } from "atlassian-jwt";
2-
import { AuthToken, ONE_MINUTE, TEN_MINUTES } from "./auth-token";
2+
import { AuthToken, ONE_MINUTE, NINE_MINUTES_MSEC } from "./auth-token";
33
import LRUCache from "lru-cache";
44
import { InstallationId } from "./installation-id";
55
import { keyLocator } from "~/src/github/client/key-locator";
@@ -33,7 +33,7 @@ export class AppTokenHolder {
3333
*/
3434
public static createAppJwt(key: string, appId: string): AuthToken {
3535

36-
const expirationDate = new Date(Date.now() + TEN_MINUTES);
36+
const expirationDate = new Date(Date.now() + NINE_MINUTES_MSEC);
3737

3838
const jwtPayload = {
3939
// "issued at" date, 60 seconds into the past to allow for some time drift

src/github/client/auth-token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const TEN_MINUTES = 10 * 60 * 1000;
1+
export const NINE_MINUTES_MSEC = 9 * 60 * 1000;
22
export const ONE_MINUTE = 60 * 1000;
33

44
export class AuthToken {

src/github/client/installation-token-cache.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { InstallationTokenCache } from "./installation-token-cache";
2-
import { AuthToken, ONE_MINUTE, TEN_MINUTES } from "./auth-token";
2+
import { AuthToken, ONE_MINUTE, NINE_MINUTES_MSEC } from "./auth-token";
33

44
jest.unmock("lru-cache");
55

66
describe("InstallationTokenCache", () => {
77

88
const now = new Date(2021, 10, 25, 10, 0);
99
const in5Minutes = new Date(now.getTime() + 5 * ONE_MINUTE);
10-
const in10Minutes = new Date(now.getTime() + TEN_MINUTES);
11-
const in20Minutes = new Date(now.getTime() + 2 * TEN_MINUTES);
10+
const in9Minutes = new Date(now.getTime() + NINE_MINUTES_MSEC);
11+
const in18Minutes = new Date(now.getTime() + 2 * NINE_MINUTES_MSEC);
1212

1313
beforeAll(() => {
1414
jest.useFakeTimers("modern");
@@ -22,8 +22,8 @@ describe("InstallationTokenCache", () => {
2222

2323
const GITHUB_INSTALLATION_ID = 1;
2424
jest.setSystemTime(now);
25-
const token1 = new AuthToken("token1", in10Minutes);
26-
const token2 = new AuthToken("token2", in10Minutes);
25+
const token1 = new AuthToken("token1", in9Minutes);
26+
const token2 = new AuthToken("token2", in9Minutes);
2727

2828
const cache1 = InstallationTokenCache.getInstance();
2929
const cache2 = InstallationTokenCache.getInstance();
@@ -40,8 +40,8 @@ describe("InstallationTokenCache", () => {
4040
const GITHUB_INSTALLATION_ID = 1;
4141
const GITHUB_APP_ID = 1;
4242
jest.setSystemTime(now);
43-
const token1 = new AuthToken("token1", in10Minutes);
44-
const token2 = new AuthToken("token2", in10Minutes);
43+
const token1 = new AuthToken("token1", in9Minutes);
44+
const token2 = new AuthToken("token2", in9Minutes);
4545

4646
const cache1 = InstallationTokenCache.getInstance();
4747
const cache2 = InstallationTokenCache.getInstance();
@@ -58,8 +58,8 @@ describe("InstallationTokenCache", () => {
5858
const GITHUB_INSTALLATION_ID_1 = 21;
5959
const GITHUB_INSTALLATION_ID_2 = 22;
6060
jest.setSystemTime(now);
61-
const token1 = new AuthToken("token1", in10Minutes);
62-
const token2 = new AuthToken("token2", in10Minutes);
61+
const token1 = new AuthToken("token1", in9Minutes);
62+
const token2 = new AuthToken("token2", in9Minutes);
6363

6464
const cache1 = InstallationTokenCache.getInstance();
6565
const cache2 = InstallationTokenCache.getInstance();
@@ -77,8 +77,8 @@ describe("InstallationTokenCache", () => {
7777
const GITHUB_APP_ID_1 = 31;
7878
const GITHUB_APP_ID_2 = 32;
7979
jest.setSystemTime(now);
80-
const token1 = new AuthToken("token1", in10Minutes);
81-
const token2 = new AuthToken("token2", in10Minutes);
80+
const token1 = new AuthToken("token1", in9Minutes);
81+
const token2 = new AuthToken("token2", in9Minutes);
8282

8383
const cache1 = InstallationTokenCache.getInstance();
8484
const cache2 = InstallationTokenCache.getInstance();
@@ -95,8 +95,8 @@ describe("InstallationTokenCache", () => {
9595
const CONFLICTIN_GITHUB_INSTALLATION_ID = 41;
9696
const GITHUB_APP_ID = 41;
9797
jest.setSystemTime(now);
98-
const token1 = new AuthToken("token1", in10Minutes);
99-
const token2 = new AuthToken("token2", in10Minutes);
98+
const token1 = new AuthToken("token1", in9Minutes);
99+
const token2 = new AuthToken("token2", in9Minutes);
100100

101101
const cache1 = InstallationTokenCache.getInstance();
102102
const cache2 = InstallationTokenCache.getInstance();
@@ -109,10 +109,10 @@ describe("InstallationTokenCache", () => {
109109
});
110110

111111
it("Re-generates expired tokens", async () => {
112-
const initialInstallationToken = new AuthToken("initial installation token", in10Minutes);
112+
const initialInstallationToken = new AuthToken("initial installation token", in9Minutes);
113113
const generateInitialInstallationToken = jest.fn().mockImplementation(() => Promise.resolve(initialInstallationToken));
114114

115-
const freshInstallationToken = new AuthToken("fresh installation token", in20Minutes);
115+
const freshInstallationToken = new AuthToken("fresh installation token", in18Minutes);
116116
const generateFreshInstallationToken = jest.fn().mockImplementation(() => Promise.resolve(freshInstallationToken));
117117

118118
const githubInstallationId = 123456;
@@ -132,7 +132,7 @@ describe("InstallationTokenCache", () => {
132132
expect(generateFreshInstallationToken).toHaveBeenCalledTimes(0);
133133

134134
// after 10 minutes we expect a new token because the old one has expired
135-
jest.setSystemTime(in10Minutes);
135+
jest.setSystemTime(in9Minutes);
136136
const token3 = await installationTokenCache.getInstallationToken(githubInstallationId, undefined, generateFreshInstallationToken);
137137
expect(token3).toEqual(freshInstallationToken);
138138
expect(generateInitialInstallationToken).toHaveBeenCalledTimes(1);

src/github/client/token-cache.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { InstallationTokenCache } from "./installation-token-cache";
2-
import { AuthToken, TEN_MINUTES } from "./auth-token";
2+
import { AuthToken, NINE_MINUTES_MSEC } from "./auth-token";
33
import { AppTokenHolder } from "./app-token-holder";
44
import { getInstallationId } from "./installation-id";
55
import { keyLocator } from "./key-locator";
@@ -15,7 +15,7 @@ jest.mock("~/src/config/feature-flags");
1515
describe("InstallationTokenCache & AppTokenHolder", () => {
1616
const githubInstallationId = 123456;
1717
const date = new Date(2021, 10, 25, 10, 0);
18-
const in10Minutes = new Date(date.getTime() + TEN_MINUTES);
18+
const in10Minutes = new Date(date.getTime() + NINE_MINUTES_MSEC);
1919

2020
beforeEach(() => {
2121
jest.useFakeTimers("modern");

0 commit comments

Comments
 (0)