Skip to content

refactor: auth validation logic #1129

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

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 9 additions & 10 deletions src/routes/LoginWithOAuthApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { Button } from '../components/fields/Button';
import { FieldInput } from '../components/fields/FieldInput';
import { AppContext } from '../context/App';
import type { AuthOptions } from '../types';
import { getNewOAuthAppURL } from '../utils/auth';
import {
getNewOAuthAppURL,
isValidClientId,
isValidHostname,
isValidToken,
} from '../utils/auth';
import Constants from '../utils/constants';

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

if (!values.hostname) {
errors.hostname = 'Required';
} else if (
!/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/i.test(
values.hostname,
)
) {
} else if (!isValidHostname(values.hostname)) {
errors.hostname = 'Invalid hostname.';
}

if (!values.clientId) {
// 20
errors.clientId = 'Required';
} else if (!/^[A-Z0-9]{20}$/i.test(values.clientId)) {
} else if (!isValidClientId(values.clientId)) {
errors.clientId = 'Invalid client id.';
}

if (!values.clientSecret) {
// 40
errors.clientSecret = 'Required';
} else if (!/^[A-Z0-9]{40}$/i.test(values.clientSecret)) {
} else if (!isValidToken(values.clientSecret)) {
errors.clientSecret = 'Invalid client secret.';
}

Expand Down
10 changes: 3 additions & 7 deletions src/routes/LoginWithPersonalAccessToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { AuthTokenOptions } from '../types';
import { Constants } from '../utils/constants';

import { Button } from '../components/fields/Button';
import { getNewTokenURL } from '../utils/auth';
import { getNewTokenURL, isValidHostname, isValidToken } from '../utils/auth';

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

if (!values.hostname) {
errors.hostname = 'Required';
} else if (
!/^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/i.test(
values.hostname,
)
) {
} else if (!isValidHostname(values.hostname)) {
errors.hostname = 'Invalid hostname.';
}

if (!values.token) {
errors.token = 'Required';
} else if (!/^[A-Z0-9_]{40}$/i.test(values.token)) {
} else if (!isValidToken(values.token)) {
errors.token = 'Invalid token.';
}

Expand Down
48 changes: 48 additions & 0 deletions src/utils/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,52 @@ describe('utils/auth.tsx', () => {
).toBeTruthy();
});
});

describe('isValidHostname', () => {
it('should validate hostname - github cloud', () => {
expect(auth.isValidHostname('github.com')).toBeTruthy();
});

it('should validate hostname - github enterprise server', () => {
expect(auth.isValidHostname('github.gitify.io')).toBeTruthy();
});

it('should invalidate hostname - empty', () => {
expect(auth.isValidHostname('')).toBeFalsy();
});

it('should invalidate hostname - invalid', () => {
expect(auth.isValidHostname('github')).toBeFalsy();
});
});

describe('isValidClientId', () => {
it('should validate client id - valid', () => {
expect(auth.isValidClientId('1234567890_ASDFGHJKL')).toBeTruthy();
});

it('should validate client id - empty', () => {
expect(auth.isValidClientId('')).toBeFalsy();
});

it('should validate client id - invalid', () => {
expect(auth.isValidClientId('1234567890asdfg')).toBeFalsy();
});
});

describe('isValidToken', () => {
it('should validate token - valid', () => {
expect(
auth.isValidToken('1234567890_asdfghjklPOIUYTREWQ0987654321'),
).toBeTruthy();
});

it('should validate token - empty', () => {
expect(auth.isValidToken('')).toBeFalsy();
});

it('should validate token - invalid', () => {
expect(auth.isValidToken('1234567890asdfg')).toBeFalsy();
});
});
});
14 changes: 14 additions & 0 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,17 @@ export function getNewOAuthAppURL(hostname: string): string {

return newOAuthAppURL.toString();
}

export function isValidHostname(hostname: string) {
return /^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$/i.test(
hostname,
);
}

export function isValidClientId(clientId: string) {
return /^[A-Z0-9_]{20}$/i.test(clientId);
}

export function isValidToken(token: string) {
return /^[A-Z0-9_]{40}$/i.test(token);
}