Skip to content

Commit

Permalink
fix: set expiresAt with token if present. Add `GitHubAppAuthentic…
Browse files Browse the repository at this point in the history
…ationWithExpirationEnabled`, `GitHubAppAuthenticationWithExpirationDisabled`, `GitHubAppAuthenticationWithRefreshToken` types. Deprecates `GitHubAppAuthentication`, `GitHubAppAuthenticationWithExpiration` (#50)
  • Loading branch information
baoshan committed Oct 5, 2021
1 parent 8f3b8b8 commit 4d86a2d
Show file tree
Hide file tree
Showing 12 changed files with 16,558 additions and 66 deletions.
16,453 changes: 16,412 additions & 41 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 11 additions & 2 deletions src/check-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { request as defaultRequest } from "@octokit/request";
import { RequestInterface, Endpoints } from "@octokit/types";
import btoa from "btoa-lite";

import { OAuthAppAuthentication, GitHubAppAuthentication } from "./types";
import {
OAuthAppAuthentication,
GitHubAppAuthenticationWithExpirationEnabled,
GitHubAppAuthenticationWithExpirationDisabled,
} from "./types";

export type CheckTokenOAuthAppOptions = {
clientType: "oauth-app";
Expand All @@ -25,7 +29,9 @@ export type CheckTokenOAuthAppResponse =
};
export type CheckTokenGitHubAppResponse =
Endpoints["POST /applications/{client_id}/token"]["response"] & {
authentication: GitHubAppAuthentication;
authentication:
| GitHubAppAuthenticationWithExpirationEnabled
| GitHubAppAuthenticationWithExpirationDisabled;
};

export async function checkToken(
Expand Down Expand Up @@ -61,6 +67,9 @@ export async function checkToken(
scopes: response.data.scopes,
};

if (response.data.expires_at)
authentication.expiresAt = response.data.expires_at;

if (options.clientType === "github-app") {
delete authentication.scopes;
}
Expand Down
15 changes: 9 additions & 6 deletions src/exchange-device-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { OctokitResponse, RequestInterface } from "@octokit/types";

import {
OAuthAppAuthentication,
GitHubAppAuthentication,
GitHubAppAuthenticationWithExpiration,
GitHubAppAuthenticationWithExpirationEnabled,
GitHubAppAuthenticationWithExpirationDisabled,
GitHubAppAuthenticationWithRefreshToken,
OAuthAppCreateTokenResponseData,
GitHubAppCreateTokenResponseData,
GitHubAppCreateTokenWithExpirationResponseData,
Expand Down Expand Up @@ -43,11 +44,12 @@ type OAuthAppAuthenticationWithoutClientSecret = Omit<
"clientSecret"
>;
type GitHubAppAuthenticationWithoutClientSecret = Omit<
GitHubAppAuthentication,
| GitHubAppAuthenticationWithExpirationEnabled
| GitHubAppAuthenticationWithExpirationDisabled,
"clientSecret"
>;
type GitHubAppAuthenticationWithExpirationWithoutClientSecret = Omit<
GitHubAppAuthenticationWithExpiration,
GitHubAppAuthenticationWithRefreshToken,
"clientSecret"
>;

Expand All @@ -66,8 +68,9 @@ export type ExchangeDeviceCodeGitHubAppResponse = OctokitResponse<
| GitHubAppCreateTokenWithExpirationResponseData
> & {
authentication:
| GitHubAppAuthentication
| GitHubAppAuthenticationWithExpiration;
| GitHubAppAuthenticationWithExpirationEnabled
| GitHubAppAuthenticationWithExpirationDisabled
| GitHubAppAuthenticationWithRefreshToken;
};

export type ExchangeDeviceCodeGitHubAppResponseWithoutClientSecret =
Expand Down
10 changes: 6 additions & 4 deletions src/exchange-web-flow-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { OctokitResponse, RequestInterface } from "@octokit/types";

import {
OAuthAppAuthentication,
GitHubAppAuthentication,
GitHubAppAuthenticationWithExpiration,
GitHubAppAuthenticationWithExpirationEnabled,
GitHubAppAuthenticationWithExpirationDisabled,
GitHubAppAuthenticationWithRefreshToken,
OAuthAppCreateTokenResponseData,
GitHubAppCreateTokenResponseData,
GitHubAppCreateTokenWithExpirationResponseData,
Expand Down Expand Up @@ -39,8 +40,9 @@ export type ExchangeWebFlowCodeGitHubAppResponse = OctokitResponse<
| GitHubAppCreateTokenWithExpirationResponseData
> & {
authentication:
| GitHubAppAuthentication
| GitHubAppAuthenticationWithExpiration;
| GitHubAppAuthenticationWithExpirationEnabled
| GitHubAppAuthenticationWithExpirationDisabled
| GitHubAppAuthenticationWithRefreshToken;
};

/**
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ export * from "./delete-token";
export * from "./delete-authorization";
export {
OAuthAppAuthentication,
GitHubAppAuthenticationWithExpirationDisabled,
GitHubAppAuthenticationWithExpirationEnabled,
GitHubAppAuthenticationWithRefreshToken,
GitHubAppAuthentication,
GitHubAppAuthenticationWithExpiration,
} from "./types";
6 changes: 3 additions & 3 deletions src/refresh-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { request as defaultRequest } from "@octokit/request";
import { OctokitResponse, RequestInterface } from "@octokit/types";

import {
GitHubAppAuthenticationWithExpiration,
GitHubAppAuthenticationWithRefreshToken,
GitHubAppCreateTokenWithExpirationResponseData,
} from "./types";
import { oauthRequest } from "./utils";
Expand All @@ -17,7 +17,7 @@ export type RefreshTokenOptions = {

export type RefreshTokenResponse =
OctokitResponse<GitHubAppCreateTokenWithExpirationResponseData> & {
authentication: GitHubAppAuthenticationWithExpiration;
authentication: GitHubAppAuthenticationWithRefreshToken;
};

export async function refreshToken(
Expand All @@ -40,7 +40,7 @@ export async function refreshToken(
);

const apiTimeInMs = new Date(response.headers.date as string).getTime();
const authentication: GitHubAppAuthenticationWithExpiration = {
const authentication: GitHubAppAuthenticationWithRefreshToken = {
clientType: "github-app",
clientId: options.clientId,
clientSecret: options.clientSecret,
Expand Down
13 changes: 11 additions & 2 deletions src/reset-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { request as defaultRequest } from "@octokit/request";
import { RequestInterface, Endpoints } from "@octokit/types";
import btoa from "btoa-lite";

import { OAuthAppAuthentication, GitHubAppAuthentication } from "./types";
import {
OAuthAppAuthentication,
GitHubAppAuthenticationWithExpirationEnabled,
GitHubAppAuthenticationWithExpirationDisabled,
} from "./types";

export type ResetTokenOAuthAppOptions = {
clientType: "oauth-app";
Expand All @@ -25,7 +29,9 @@ export type ResetTokenOAuthAppResponse =
};
export type ResetTokenGitHubAppResponse =
Endpoints["PATCH /applications/{client_id}/token"]["response"] & {
authentication: GitHubAppAuthentication;
authentication:
| GitHubAppAuthenticationWithExpirationEnabled
| GitHubAppAuthenticationWithExpirationDisabled;
};

export async function resetToken(
Expand Down Expand Up @@ -60,6 +66,9 @@ export async function resetToken(
scopes: response.data.scopes,
};

if (response.data.expires_at)
authentication.expiresAt = response.data.expires_at;

if (options.clientType === "github-app") {
delete authentication.scopes;
}
Expand Down
26 changes: 18 additions & 8 deletions src/scope-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { request as defaultRequest } from "@octokit/request";
import { RequestInterface, Endpoints } from "@octokit/types";
import btoa from "btoa-lite";

import { GitHubAppAuthentication } from "./types";
import {
GitHubAppAuthenticationWithExpirationEnabled,
GitHubAppAuthenticationWithExpirationDisabled,
} from "./types";

type CommonOptions = {
clientType: "github-app";
Expand Down Expand Up @@ -35,7 +38,9 @@ export type ScopeTokenOptions =
| (CommonOptions & TargetIdOption & RepositoryIdsOption);

export type ScopeTokenResponse = Endpoint["response"] & {
authentication: GitHubAppAuthentication;
authentication:
| GitHubAppAuthenticationWithExpirationEnabled
| GitHubAppAuthenticationWithExpirationDisabled;
};

export async function scopeToken(
Expand All @@ -62,12 +67,17 @@ export async function scopeToken(
...requestOptions,
});

const authentication: GitHubAppAuthentication = {
clientType,
clientId,
clientSecret,
token: response.data.token,
};
const authentication:
| GitHubAppAuthenticationWithExpirationEnabled
| GitHubAppAuthenticationWithExpirationDisabled = Object.assign(
{
clientType,
clientId,
clientSecret,
token: response.data.token,
},
response.data.expires_at ? { expiresAt: response.data.expires_at } : {}
);

return { ...response, authentication };
}
23 changes: 23 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,36 @@ export type OAuthAppAuthentication = {
scopes: string[];
};

export type GitHubAppAuthenticationWithExpirationDisabled = {
clientType: "github-app";
clientId: string;
clientSecret: string;
token: string;
};

export type GitHubAppAuthenticationWithExpirationEnabled =
GitHubAppAuthenticationWithExpirationDisabled & { expiresAt: string };

export type GitHubAppAuthenticationWithRefreshToken =
GitHubAppAuthenticationWithExpirationEnabled & {
refreshToken: string;
refreshTokenExpiresAt: string;
};

/**
* @deprecated Use `GitHubAppAuthenticationWithExpirationDisabled` or
* `GitHubAppAuthenticationWithExpirationEnabled` instead.
*/
export type GitHubAppAuthentication = {
clientType: "github-app";
clientId: string;
clientSecret: string;
token: string;
};

/**
* @deprecated Use `GitHubAppAuthenticationWithRefreshToken` instead.
*/
export type GitHubAppAuthenticationWithExpiration = {
clientType: "github-app";
clientId: string;
Expand Down
3 changes: 3 additions & 0 deletions test/check-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ describe("checkToken()", () => {
const mock = fetchMock.sandbox().postOnce(
"https://api.github.com/applications/lv1.1234567890abcdef/token",
{
expires_at: "2021-10-06T17:26:27Z",
scopes: [],
},
{
Expand Down Expand Up @@ -95,6 +96,7 @@ describe("checkToken()", () => {

expect(data).toMatchInlineSnapshot(`
Object {
"expires_at": "2021-10-06T17:26:27Z",
"scopes": Array [],
}
`);
Expand All @@ -103,6 +105,7 @@ describe("checkToken()", () => {
"clientId": "lv1.1234567890abcdef",
"clientSecret": "1234567890abcdef12347890abcdef12345678",
"clientType": "github-app",
"expiresAt": "2021-10-06T17:26:27Z",
"token": "token123",
}
`);
Expand Down
3 changes: 3 additions & 0 deletions test/reset-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ describe("resetToken()", () => {
const mock = fetchMock.sandbox().patchOnce(
"https://api.github.com/applications/lv1.1234567890abcdef/token",
{
expires_at: "2021-10-06T17:26:27Z",
scopes: [],
token: "token456",
},
Expand Down Expand Up @@ -98,6 +99,7 @@ describe("resetToken()", () => {

expect(data).toMatchInlineSnapshot(`
Object {
"expires_at": "2021-10-06T17:26:27Z",
"scopes": Array [],
"token": "token456",
}
Expand All @@ -107,6 +109,7 @@ describe("resetToken()", () => {
"clientId": "lv1.1234567890abcdef",
"clientSecret": "1234567890abcdef12347890abcdef12345678",
"clientType": "github-app",
"expiresAt": "2021-10-06T17:26:27Z",
"token": "token456",
}
`);
Expand Down
56 changes: 56 additions & 0 deletions test/scope-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,60 @@ describe("scopeToken()", () => {
}
`);
});

it("passes `expires_at` through", async () => {
const mock = fetchMock
.sandbox()
.postOnce(
"https://api.github.com/applications/lv1.1234567890abcdef/token/scoped",
{
account: {
login: "octokit",
id: 1,
},
expires_at: "2021-10-06T17:26:27Z",
token: "usertoken456",
}
);

const { data, authentication } = await scopeToken({
clientType: "github-app",
clientId: "lv1.1234567890abcdef",
clientSecret: "1234567890abcdef12347890abcdef12345678",
token: "usertoken123",
target: "octokit",
repositories: ["oauth-methods.js"],
permissions: {
issues: "write",
},
request: request.defaults({
headers: {
"user-agent": "test",
},
request: {
fetch: mock,
},
}),
});

expect(data).toMatchInlineSnapshot(`
Object {
"account": Object {
"id": 1,
"login": "octokit",
},
"expires_at": "2021-10-06T17:26:27Z",
"token": "usertoken456",
}
`);
expect(authentication).toMatchInlineSnapshot(`
Object {
"clientId": "lv1.1234567890abcdef",
"clientSecret": "1234567890abcdef12347890abcdef12345678",
"clientType": "github-app",
"expiresAt": "2021-10-06T17:26:27Z",
"token": "usertoken456",
}
`);
});
});

0 comments on commit 4d86a2d

Please sign in to comment.