Skip to content

Commit 132ac7c

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

3 files changed

Lines changed: 18 additions & 14 deletions

File tree

src/renderer/context/App.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,9 +410,7 @@ export const AppProvider = ({ children }: { children: ReactNode }) => {
410410
* authenticates via a predefined "Gitify" GitHub OAuth App.
411411
*/
412412
const loginWithGitHubApp = useCallback(async () => {
413-
const { authCode } = await performGitHubOAuth(
414-
Constants.DEFAULT_AUTH_OPTIONS,
415-
);
413+
const { authCode } = await performGitHubOAuth();
416414
const token = await exchangeAuthCodeForAccessToken(authCode);
417415
const hostname = Constants.DEFAULT_AUTH_OPTIONS.hostname;
418416

src/renderer/utils/auth/utils.test.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,17 @@ import type { AuthMethod } from './types';
2020
import * as authUtils from './utils';
2121
import { getNewOAuthAppURL, getNewTokenURL } from './utils';
2222

23-
const exchangeWebFlowCodeMock = jest.fn();
2423
jest.mock('@octokit/oauth-methods', () => ({
2524
...jest.requireActual('@octokit/oauth-methods'),
26-
exchangeWebFlowCode: () => exchangeWebFlowCodeMock,
25+
exchangeWebFlowCode: jest.fn(),
2726
}));
2827

28+
import { exchangeWebFlowCode } from '@octokit/oauth-methods';
29+
30+
const exchangeWebFlowCodeMock = exchangeWebFlowCode as jest.MockedFunction<
31+
typeof exchangeWebFlowCode
32+
>;
33+
2934
describe('renderer/utils/auth/utils.ts', () => {
3035
describe('authGitHub', () => {
3136
jest.spyOn(logger, 'rendererLogInfo').mockImplementation();
@@ -44,9 +49,7 @@ describe('renderer/utils/auth/utils.ts', () => {
4449
callback('gitify://auth?code=123-456');
4550
});
4651

47-
const res = await authUtils.performGitHubOAuth(
48-
Constants.DEFAULT_AUTH_OPTIONS,
49-
);
52+
const res = await authUtils.performGitHubOAuth();
5053

5154
expect(openExternalLinkSpy).toHaveBeenCalledTimes(1);
5255
expect(openExternalLinkSpy).toHaveBeenCalledWith(
@@ -103,8 +106,7 @@ describe('renderer/utils/auth/utils.ts', () => {
103106
});
104107

105108
await expect(
106-
async () =>
107-
await authUtils.performGitHubOAuth(Constants.DEFAULT_AUTH_OPTIONS),
109+
async () => await authUtils.performGitHubOAuth(),
108110
).rejects.toEqual(
109111
new Error(
110112
"Oops! Something went wrong and we couldn't log you in using GitHub. Please try again. Reason: The redirect_uri is missing or invalid. Docs: https://docs.github.com/en/developers/apps/troubleshooting-oauth-errors",
@@ -133,15 +135,19 @@ describe('renderer/utils/auth/utils.ts', () => {
133135
authentication: {
134136
token: 'this-is-a-token',
135137
},
136-
});
138+
} as any);
137139

138-
const res = await authUtils.exchangeAuthCodeForAccessToken(authCode);
140+
const res = await authUtils.exchangeAuthCodeForAccessToken(
141+
authCode,
142+
Constants.DEFAULT_AUTH_OPTIONS,
143+
);
139144

140145
expect(exchangeWebFlowCodeMock).toHaveBeenCalledWith({
141146
clientType: 'oauth-app',
142147
clientId: 'FAKE_CLIENT_ID_123',
143148
clientSecret: 'FAKE_CLIENT_SECRET_123',
144149
code: '123-456',
150+
request: expect.any(Function),
145151
});
146152
expect(res).toBe('this-is-a-token');
147153
});

src/renderer/utils/auth/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { rendererLogError, rendererLogInfo, rendererLogWarn } from '../logger';
2626
import type { AuthMethod, AuthResponse, LoginOAuthAppOptions } from './types';
2727

2828
export function performGitHubOAuth(
29-
authOptions: LoginOAuthAppOptions,
29+
authOptions: LoginOAuthAppOptions = Constants.DEFAULT_AUTH_OPTIONS,
3030
): Promise<AuthResponse> {
3131
return new Promise((resolve, reject) => {
3232
const { url } = getWebFlowAuthorizationUrl({
@@ -80,7 +80,7 @@ export function performGitHubOAuth(
8080

8181
export async function exchangeAuthCodeForAccessToken(
8282
authCode: AuthCode,
83-
authOptions = Constants.DEFAULT_AUTH_OPTIONS,
83+
authOptions: LoginOAuthAppOptions = Constants.DEFAULT_AUTH_OPTIONS,
8484
): Promise<Token> {
8585
const { authentication } = await exchangeWebFlowCode({
8686
clientType: 'oauth-app',

0 commit comments

Comments
 (0)