Skip to content

Commit 7d0fcd0

Browse files
authored
refactor: auth validation logic (#1129)
1 parent d2402d3 commit 7d0fcd0

File tree

4 files changed

+74
-17
lines changed

4 files changed

+74
-17
lines changed

src/routes/LoginWithOAuthApp.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import { Button } from '../components/fields/Button';
1515
import { FieldInput } from '../components/fields/FieldInput';
1616
import { AppContext } from '../context/App';
1717
import type { AuthOptions } from '../types';
18-
import { getNewOAuthAppURL } from '../utils/auth';
18+
import {
19+
getNewOAuthAppURL,
20+
isValidClientId,
21+
isValidHostname,
22+
isValidToken,
23+
} from '../utils/auth';
1924
import Constants from '../utils/constants';
2025

2126
interface IValues {
@@ -35,25 +40,19 @@ export const validate = (values: IValues): IFormErrors => {
3540

3641
if (!values.hostname) {
3742
errors.hostname = 'Required';
38-
} else if (
39-
!/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/i.test(
40-
values.hostname,
41-
)
42-
) {
43+
} else if (!isValidHostname(values.hostname)) {
4344
errors.hostname = 'Invalid hostname.';
4445
}
4546

4647
if (!values.clientId) {
47-
// 20
4848
errors.clientId = 'Required';
49-
} else if (!/^[A-Z0-9]{20}$/i.test(values.clientId)) {
49+
} else if (!isValidClientId(values.clientId)) {
5050
errors.clientId = 'Invalid client id.';
5151
}
5252

5353
if (!values.clientSecret) {
54-
// 40
5554
errors.clientSecret = 'Required';
56-
} else if (!/^[A-Z0-9]{40}$/i.test(values.clientSecret)) {
55+
} else if (!isValidToken(values.clientSecret)) {
5756
errors.clientSecret = 'Invalid client secret.';
5857
}
5958

src/routes/LoginWithPersonalAccessToken.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import type { AuthTokenOptions } from '../types';
1515
import { Constants } from '../utils/constants';
1616

1717
import { Button } from '../components/fields/Button';
18-
import { getNewTokenURL } from '../utils/auth';
18+
import { getNewTokenURL, isValidHostname, isValidToken } from '../utils/auth';
1919

2020
interface IValues {
2121
token?: string;
@@ -32,17 +32,13 @@ export const validate = (values: IValues): IFormErrors => {
3232

3333
if (!values.hostname) {
3434
errors.hostname = 'Required';
35-
} else if (
36-
!/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/i.test(
37-
values.hostname,
38-
)
39-
) {
35+
} else if (!isValidHostname(values.hostname)) {
4036
errors.hostname = 'Invalid hostname.';
4137
}
4238

4339
if (!values.token) {
4440
errors.token = 'Required';
45-
} else if (!/^[A-Z0-9_]{40}$/i.test(values.token)) {
41+
} else if (!isValidToken(values.token)) {
4642
errors.token = 'Invalid token.';
4743
}
4844

src/utils/auth.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,52 @@ describe('utils/auth.tsx', () => {
167167
).toBeTruthy();
168168
});
169169
});
170+
171+
describe('isValidHostname', () => {
172+
it('should validate hostname - github cloud', () => {
173+
expect(auth.isValidHostname('github.com')).toBeTruthy();
174+
});
175+
176+
it('should validate hostname - github enterprise server', () => {
177+
expect(auth.isValidHostname('github.gitify.io')).toBeTruthy();
178+
});
179+
180+
it('should invalidate hostname - empty', () => {
181+
expect(auth.isValidHostname('')).toBeFalsy();
182+
});
183+
184+
it('should invalidate hostname - invalid', () => {
185+
expect(auth.isValidHostname('github')).toBeFalsy();
186+
});
187+
});
188+
189+
describe('isValidClientId', () => {
190+
it('should validate client id - valid', () => {
191+
expect(auth.isValidClientId('1234567890_ASDFGHJKL')).toBeTruthy();
192+
});
193+
194+
it('should validate client id - empty', () => {
195+
expect(auth.isValidClientId('')).toBeFalsy();
196+
});
197+
198+
it('should validate client id - invalid', () => {
199+
expect(auth.isValidClientId('1234567890asdfg')).toBeFalsy();
200+
});
201+
});
202+
203+
describe('isValidToken', () => {
204+
it('should validate token - valid', () => {
205+
expect(
206+
auth.isValidToken('1234567890_asdfghjklPOIUYTREWQ0987654321'),
207+
).toBeTruthy();
208+
});
209+
210+
it('should validate token - empty', () => {
211+
expect(auth.isValidToken('')).toBeFalsy();
212+
});
213+
214+
it('should validate token - invalid', () => {
215+
expect(auth.isValidToken('1234567890asdfg')).toBeFalsy();
216+
});
217+
});
170218
});

src/utils/auth.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,17 @@ export function getNewOAuthAppURL(hostname: string): string {
167167

168168
return newOAuthAppURL.toString();
169169
}
170+
171+
export function isValidHostname(hostname: string) {
172+
return /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/i.test(
173+
hostname,
174+
);
175+
}
176+
177+
export function isValidClientId(clientId: string) {
178+
return /^[A-Z0-9_]{20}$/i.test(clientId);
179+
}
180+
181+
export function isValidToken(token: string) {
182+
return /^[A-Z0-9_]{40}$/i.test(token);
183+
}

0 commit comments

Comments
 (0)