Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: appId argument can be set to Client ID string #606

Merged
merged 7 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@octokit/request-error": "^6.1.1",
"@octokit/types": "^13.4.1",
"lru-cache": "^10.0.0",
"universal-github-app-jwt": "^2.0.6",
"universal-github-app-jwt": "^2.2.0",
"universal-user-agent": "^7.0.0"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions src/get-app-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ export async function getAppAuthentication({
appId,
privateKey,
timeDifference,
}: State & { timeDifference?: number }): Promise<AppAuthentication> {
}: State & {
timeDifference?: number;
}): Promise<AppAuthentication> {
try {
const appAuthentication = await githubAppJwt({
id: +appId,
id: appId,
privateKey,
now: timeDifference && Math.floor(Date.now() / 1000) + timeDifference,
});
Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ export function createAppAuth(options: StrategyOptions): AuthInterface {
if (!options.appId) {
throw new Error("[@octokit/auth-app] appId option is required");
}
if (!Number.isFinite(+options.appId)) {
throw new Error(
"[@octokit/auth-app] appId option must be a number or numeric string",
);
}

if (!options.privateKey) {
throw new Error("[@octokit/auth-app] privateKey option is required");
}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export type UTC_TIMESTAMP = string;
export type AppAuthentication = {
type: APP_TYPE;
token: JWT;
appId: number;
appId: number | string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a later date, maybe we could use template literal strings to better define the format

Suggested change
appId: number | string;
appId: number | `Iv1.${string}`;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, this would imply a change in universal-github-app-jwt first. Do you want me to open an issue there first?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

appId: number | Iv1.${string};

I wouldn't do that, because there is a chance that future Client IDs will start with lv2.... and I also think there are old apps that still have a client ID without the lv1 prefix

expiresAt: string;
};

Expand Down
6 changes: 2 additions & 4 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2311,12 +2311,10 @@ it("throws helpful error if `appId` is not set (#184)", async () => {
it("throws helpful error if `appId` is not set to a numeric value", async () => {
expect(() => {
createAppAuth({
appId: "not-a-number",
appId: "Iv1.0123456789abcdef",
privateKey: PRIVATE_KEY,
});
}).toThrowError(
"[@octokit/auth-app] appId option must be a number or numeric string",
);
}).not.toThrow();
});

it("throws helpful error if `privateKey` is not set properly (#184)", async () => {
Expand Down
42 changes: 42 additions & 0 deletions test/typescript-validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// THIS CODE IS NOT EXECUTED. IT IS JUST FOR TYPECHECKING
// ************************************************************

import { getAppAuthentication } from "../src/get-app-authentication.js";
import { request } from "@octokit/request";
import { createAppAuth } from "../src/index.js";
import { State, StrategyOptions } from "../src/types.js";
import { getCache } from "../src/cache.js";
import { createOAuthAppAuth } from "@octokit/auth-oauth-app";
function isString(what: string) {}

export async function readmeExample() {
Expand Down Expand Up @@ -31,3 +36,40 @@ export async function issue282() {

isString(authentication.token);
}

// https://github.com/octokit/auth-app.js/issues/603
export async function issue603() {
createAppAuth({
appId: "Iv1.0123456789abcdef",
privateKey: "",
});

const options: StrategyOptions = {
appId: "Iv1.0123456789abcdef",
privateKey: "",
};

const state: State = Object.assign(
{
request,
cache: getCache(),
},
options,
options.installationId
? { installationId: Number(options.installationId) }
: {},
{
log: {
warn: console.warn.bind(console),
},
oauthApp: createOAuthAppAuth({
clientType: "github-app",
clientId: options.clientId || "",
clientSecret: options.clientSecret || "",
request,
}),
},
);

getAppAuthentication(state);
}