Skip to content

Commit 4d86a2d

Browse files
authored
fix: set expiresAt with token if present. Add GitHubAppAuthenticationWithExpirationEnabled, GitHubAppAuthenticationWithExpirationDisabled, GitHubAppAuthenticationWithRefreshToken types. Deprecates GitHubAppAuthentication, GitHubAppAuthenticationWithExpiration (#50)
1 parent 8f3b8b8 commit 4d86a2d

12 files changed

+16558
-66
lines changed

package-lock.json

Lines changed: 16412 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/check-token.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { request as defaultRequest } from "@octokit/request";
22
import { RequestInterface, Endpoints } from "@octokit/types";
33
import btoa from "btoa-lite";
44

5-
import { OAuthAppAuthentication, GitHubAppAuthentication } from "./types";
5+
import {
6+
OAuthAppAuthentication,
7+
GitHubAppAuthenticationWithExpirationEnabled,
8+
GitHubAppAuthenticationWithExpirationDisabled,
9+
} from "./types";
610

711
export type CheckTokenOAuthAppOptions = {
812
clientType: "oauth-app";
@@ -25,7 +29,9 @@ export type CheckTokenOAuthAppResponse =
2529
};
2630
export type CheckTokenGitHubAppResponse =
2731
Endpoints["POST /applications/{client_id}/token"]["response"] & {
28-
authentication: GitHubAppAuthentication;
32+
authentication:
33+
| GitHubAppAuthenticationWithExpirationEnabled
34+
| GitHubAppAuthenticationWithExpirationDisabled;
2935
};
3036

3137
export async function checkToken(
@@ -61,6 +67,9 @@ export async function checkToken(
6167
scopes: response.data.scopes,
6268
};
6369

70+
if (response.data.expires_at)
71+
authentication.expiresAt = response.data.expires_at;
72+
6473
if (options.clientType === "github-app") {
6574
delete authentication.scopes;
6675
}

src/exchange-device-code.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { OctokitResponse, RequestInterface } from "@octokit/types";
33

44
import {
55
OAuthAppAuthentication,
6-
GitHubAppAuthentication,
7-
GitHubAppAuthenticationWithExpiration,
6+
GitHubAppAuthenticationWithExpirationEnabled,
7+
GitHubAppAuthenticationWithExpirationDisabled,
8+
GitHubAppAuthenticationWithRefreshToken,
89
OAuthAppCreateTokenResponseData,
910
GitHubAppCreateTokenResponseData,
1011
GitHubAppCreateTokenWithExpirationResponseData,
@@ -43,11 +44,12 @@ type OAuthAppAuthenticationWithoutClientSecret = Omit<
4344
"clientSecret"
4445
>;
4546
type GitHubAppAuthenticationWithoutClientSecret = Omit<
46-
GitHubAppAuthentication,
47+
| GitHubAppAuthenticationWithExpirationEnabled
48+
| GitHubAppAuthenticationWithExpirationDisabled,
4749
"clientSecret"
4850
>;
4951
type GitHubAppAuthenticationWithExpirationWithoutClientSecret = Omit<
50-
GitHubAppAuthenticationWithExpiration,
52+
GitHubAppAuthenticationWithRefreshToken,
5153
"clientSecret"
5254
>;
5355

@@ -66,8 +68,9 @@ export type ExchangeDeviceCodeGitHubAppResponse = OctokitResponse<
6668
| GitHubAppCreateTokenWithExpirationResponseData
6769
> & {
6870
authentication:
69-
| GitHubAppAuthentication
70-
| GitHubAppAuthenticationWithExpiration;
71+
| GitHubAppAuthenticationWithExpirationEnabled
72+
| GitHubAppAuthenticationWithExpirationDisabled
73+
| GitHubAppAuthenticationWithRefreshToken;
7174
};
7275

7376
export type ExchangeDeviceCodeGitHubAppResponseWithoutClientSecret =

src/exchange-web-flow-code.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { OctokitResponse, RequestInterface } from "@octokit/types";
33

44
import {
55
OAuthAppAuthentication,
6-
GitHubAppAuthentication,
7-
GitHubAppAuthenticationWithExpiration,
6+
GitHubAppAuthenticationWithExpirationEnabled,
7+
GitHubAppAuthenticationWithExpirationDisabled,
8+
GitHubAppAuthenticationWithRefreshToken,
89
OAuthAppCreateTokenResponseData,
910
GitHubAppCreateTokenResponseData,
1011
GitHubAppCreateTokenWithExpirationResponseData,
@@ -39,8 +40,9 @@ export type ExchangeWebFlowCodeGitHubAppResponse = OctokitResponse<
3940
| GitHubAppCreateTokenWithExpirationResponseData
4041
> & {
4142
authentication:
42-
| GitHubAppAuthentication
43-
| GitHubAppAuthenticationWithExpiration;
43+
| GitHubAppAuthenticationWithExpirationEnabled
44+
| GitHubAppAuthenticationWithExpirationDisabled
45+
| GitHubAppAuthenticationWithRefreshToken;
4446
};
4547

4648
/**

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ export * from "./delete-token";
1111
export * from "./delete-authorization";
1212
export {
1313
OAuthAppAuthentication,
14+
GitHubAppAuthenticationWithExpirationDisabled,
15+
GitHubAppAuthenticationWithExpirationEnabled,
16+
GitHubAppAuthenticationWithRefreshToken,
1417
GitHubAppAuthentication,
1518
GitHubAppAuthenticationWithExpiration,
1619
} from "./types";

src/refresh-token.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { request as defaultRequest } from "@octokit/request";
22
import { OctokitResponse, RequestInterface } from "@octokit/types";
33

44
import {
5-
GitHubAppAuthenticationWithExpiration,
5+
GitHubAppAuthenticationWithRefreshToken,
66
GitHubAppCreateTokenWithExpirationResponseData,
77
} from "./types";
88
import { oauthRequest } from "./utils";
@@ -17,7 +17,7 @@ export type RefreshTokenOptions = {
1717

1818
export type RefreshTokenResponse =
1919
OctokitResponse<GitHubAppCreateTokenWithExpirationResponseData> & {
20-
authentication: GitHubAppAuthenticationWithExpiration;
20+
authentication: GitHubAppAuthenticationWithRefreshToken;
2121
};
2222

2323
export async function refreshToken(
@@ -40,7 +40,7 @@ export async function refreshToken(
4040
);
4141

4242
const apiTimeInMs = new Date(response.headers.date as string).getTime();
43-
const authentication: GitHubAppAuthenticationWithExpiration = {
43+
const authentication: GitHubAppAuthenticationWithRefreshToken = {
4444
clientType: "github-app",
4545
clientId: options.clientId,
4646
clientSecret: options.clientSecret,

src/reset-token.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { request as defaultRequest } from "@octokit/request";
22
import { RequestInterface, Endpoints } from "@octokit/types";
33
import btoa from "btoa-lite";
44

5-
import { OAuthAppAuthentication, GitHubAppAuthentication } from "./types";
5+
import {
6+
OAuthAppAuthentication,
7+
GitHubAppAuthenticationWithExpirationEnabled,
8+
GitHubAppAuthenticationWithExpirationDisabled,
9+
} from "./types";
610

711
export type ResetTokenOAuthAppOptions = {
812
clientType: "oauth-app";
@@ -25,7 +29,9 @@ export type ResetTokenOAuthAppResponse =
2529
};
2630
export type ResetTokenGitHubAppResponse =
2731
Endpoints["PATCH /applications/{client_id}/token"]["response"] & {
28-
authentication: GitHubAppAuthentication;
32+
authentication:
33+
| GitHubAppAuthenticationWithExpirationEnabled
34+
| GitHubAppAuthenticationWithExpirationDisabled;
2935
};
3036

3137
export async function resetToken(
@@ -60,6 +66,9 @@ export async function resetToken(
6066
scopes: response.data.scopes,
6167
};
6268

69+
if (response.data.expires_at)
70+
authentication.expiresAt = response.data.expires_at;
71+
6372
if (options.clientType === "github-app") {
6473
delete authentication.scopes;
6574
}

src/scope-token.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { request as defaultRequest } from "@octokit/request";
22
import { RequestInterface, Endpoints } from "@octokit/types";
33
import btoa from "btoa-lite";
44

5-
import { GitHubAppAuthentication } from "./types";
5+
import {
6+
GitHubAppAuthenticationWithExpirationEnabled,
7+
GitHubAppAuthenticationWithExpirationDisabled,
8+
} from "./types";
69

710
type CommonOptions = {
811
clientType: "github-app";
@@ -35,7 +38,9 @@ export type ScopeTokenOptions =
3538
| (CommonOptions & TargetIdOption & RepositoryIdsOption);
3639

3740
export type ScopeTokenResponse = Endpoint["response"] & {
38-
authentication: GitHubAppAuthentication;
41+
authentication:
42+
| GitHubAppAuthenticationWithExpirationEnabled
43+
| GitHubAppAuthenticationWithExpirationDisabled;
3944
};
4045

4146
export async function scopeToken(
@@ -62,12 +67,17 @@ export async function scopeToken(
6267
...requestOptions,
6368
});
6469

65-
const authentication: GitHubAppAuthentication = {
66-
clientType,
67-
clientId,
68-
clientSecret,
69-
token: response.data.token,
70-
};
70+
const authentication:
71+
| GitHubAppAuthenticationWithExpirationEnabled
72+
| GitHubAppAuthenticationWithExpirationDisabled = Object.assign(
73+
{
74+
clientType,
75+
clientId,
76+
clientSecret,
77+
token: response.data.token,
78+
},
79+
response.data.expires_at ? { expiresAt: response.data.expires_at } : {}
80+
);
7181

7282
return { ...response, authentication };
7383
}

src/types.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,36 @@ export type OAuthAppAuthentication = {
66
scopes: string[];
77
};
88

9+
export type GitHubAppAuthenticationWithExpirationDisabled = {
10+
clientType: "github-app";
11+
clientId: string;
12+
clientSecret: string;
13+
token: string;
14+
};
15+
16+
export type GitHubAppAuthenticationWithExpirationEnabled =
17+
GitHubAppAuthenticationWithExpirationDisabled & { expiresAt: string };
18+
19+
export type GitHubAppAuthenticationWithRefreshToken =
20+
GitHubAppAuthenticationWithExpirationEnabled & {
21+
refreshToken: string;
22+
refreshTokenExpiresAt: string;
23+
};
24+
25+
/**
26+
* @deprecated Use `GitHubAppAuthenticationWithExpirationDisabled` or
27+
* `GitHubAppAuthenticationWithExpirationEnabled` instead.
28+
*/
929
export type GitHubAppAuthentication = {
1030
clientType: "github-app";
1131
clientId: string;
1232
clientSecret: string;
1333
token: string;
1434
};
1535

36+
/**
37+
* @deprecated Use `GitHubAppAuthenticationWithRefreshToken` instead.
38+
*/
1639
export type GitHubAppAuthenticationWithExpiration = {
1740
clientType: "github-app";
1841
clientId: string;

test/check-token.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ describe("checkToken()", () => {
6262
const mock = fetchMock.sandbox().postOnce(
6363
"https://api.github.com/applications/lv1.1234567890abcdef/token",
6464
{
65+
expires_at: "2021-10-06T17:26:27Z",
6566
scopes: [],
6667
},
6768
{
@@ -95,6 +96,7 @@ describe("checkToken()", () => {
9596

9697
expect(data).toMatchInlineSnapshot(`
9798
Object {
99+
"expires_at": "2021-10-06T17:26:27Z",
98100
"scopes": Array [],
99101
}
100102
`);
@@ -103,6 +105,7 @@ describe("checkToken()", () => {
103105
"clientId": "lv1.1234567890abcdef",
104106
"clientSecret": "1234567890abcdef12347890abcdef12345678",
105107
"clientType": "github-app",
108+
"expiresAt": "2021-10-06T17:26:27Z",
106109
"token": "token123",
107110
}
108111
`);

0 commit comments

Comments
 (0)