Skip to content

Commit a3fba2c

Browse files
committed
refactor(auth): migrate to octokit/oauth-method library
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent 86a51c3 commit a3fba2c

13 files changed

Lines changed: 189 additions & 459 deletions

File tree

jest.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ const config: Config = {
1414
},
1515
// Allow transforming specific ESM packages in node_modules that ship untranspiled ESM.
1616
// @primer/* libraries rely on lit and @lit-labs/react internally for some components.
17+
// @octokit/* libraries rely on universal-user-agent internally.
1718
// We also include GitHub web components that ship ESM-only builds.
1819
transformIgnorePatterns: [
19-
'node_modules/(?!(?:@primer/react|@primer/primitives|@primer/octicons-react|@lit-labs/react|lit|@github/relative-time-element|@github/tab-container-element)/)',
20+
'node_modules/(?!(?:@primer/react|@primer/primitives|@primer/octicons-react|@lit-labs/react|lit|@github/relative-time-element|@github/tab-container-element|@octokit/oauth-methods|@octokit/oauth-authorization-url|@octokit/request|@octokit/request-error|@octokit/endpoint|universal-user-agent)/)',
2021
],
2122
moduleNameMapper: {
2223
// Force CommonJS build for http adapter to be available.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@
8484
"@electron/notarize": "3.1.1",
8585
"@graphql-codegen/cli": "6.1.0",
8686
"@graphql-codegen/schema-ast": "5.0.0",
87+
"@octokit/oauth-methods": "6.0.2",
8788
"@octokit/openapi-types": "27.0.0",
89+
"@octokit/request": "10.0.7",
8890
"@parcel/watcher": "2.5.1",
8991
"@primer/css": "22.1.0",
9092
"@primer/octicons-react": "19.21.1",
@@ -149,4 +151,4 @@
149151
"*": "biome check --no-errors-on-unmatched",
150152
"*.{js,ts,tsx}": "pnpm test --findRelatedTests --passWithNoTests --updateSnapshot"
151153
}
152-
}
154+
}

pnpm-lock.yaml

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

src/renderer/constants.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ClientID, ClientSecret, Hostname, Link } from './types';
2+
import type { LoginOAuthAppOptions } from './utils/auth/types';
23

34
export const Constants = {
45
STORAGE_KEY: 'gitify-storage',
@@ -13,8 +14,9 @@ export const Constants = {
1314
hostname: 'github.com' as Hostname,
1415
clientId: process.env.OAUTH_CLIENT_ID as ClientID,
1516
clientSecret: process.env.OAUTH_CLIENT_SECRET as ClientSecret,
16-
},
17+
} satisfies LoginOAuthAppOptions,
1718

19+
GITHUB_BASE_URL: 'https://github.com',
1820
GITHUB_API_BASE_URL: 'https://api.github.com',
1921
GITHUB_API_GRAPHQL_URL: 'https://api.github.com/graphql',
2022

src/renderer/context/App.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ import type {
3737
} from '../utils/auth/types';
3838
import {
3939
addAccount,
40-
authGitHub,
40+
exchangeAuthCodeForAccessToken,
4141
getAccountUUID,
42-
getToken,
4342
hasAccounts,
43+
performGitHubOAuth,
4444
refreshAccount,
4545
removeAccount,
4646
} from '../utils/auth/utils';
@@ -405,22 +405,36 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
405405
return hasAccounts(auth);
406406
}, [auth]);
407407

408+
/**
409+
* Note: although we call this "Login with GitHub App", this function actually
410+
* authenticates via a predefined "Gitify" GitHub OAuth App.
411+
*/
408412
const loginWithGitHubApp = useCallback(async () => {
409-
const { authCode } = await authGitHub();
410-
const { token } = await getToken(authCode);
413+
const { authCode } = await performGitHubOAuth(
414+
Constants.DEFAULT_AUTH_OPTIONS,
415+
);
416+
const token = await exchangeAuthCodeForAccessToken(authCode);
411417
const hostname = Constants.DEFAULT_AUTH_OPTIONS.hostname;
412418

413419
const updatedAuth = await addAccount(auth, 'GitHub App', token, hostname);
414420

415421
persistAuth(updatedAuth);
416422
}, [auth, persistAuth]);
417423

424+
/**
425+
* Login with custom GitHub OAuth App
426+
*/
418427
const loginWithOAuthApp = useCallback(
419428
async (data: LoginOAuthAppOptions) => {
420-
const { authOptions, authCode } = await authGitHub(data);
421-
const { token, hostname } = await getToken(authCode, authOptions);
429+
const { authOptions, authCode } = await performGitHubOAuth(data);
430+
const token = await exchangeAuthCodeForAccessToken(authCode, authOptions);
422431

423-
const updatedAuth = await addAccount(auth, 'OAuth App', token, hostname);
432+
const updatedAuth = await addAccount(
433+
auth,
434+
'OAuth App',
435+
token,
436+
authOptions.hostname,
437+
);
424438

425439
persistAuth(updatedAuth);
426440
},

0 commit comments

Comments
 (0)