This repository has been archived on 2024-06-14. You can view files and clone it, but cannot push or open issues or pull requests.
icynet-admin/components/OAuth2Page/OAuth2Page.tsx

545 lines
16 KiB
TypeScript
Raw Normal View History

2022-09-14 17:31:38 +00:00
import useSWR from 'swr';
2022-08-30 18:08:54 +00:00
import Image from 'next/image';
import {
OAuth2ClientListItem,
OAuth2ClientURL,
OAuth2ClientURLType,
} from '../../lib/types/oauth2-client.interface';
2022-08-30 18:08:54 +00:00
import { PaginatedResponse } from '../../lib/types/paginated-response.interface';
import { Paginator } from '../common/Paginator/Paginator';
import styles from './OAuth2Page.module.scss';
import { UPLOADS_URL } from '../../lib/constants';
import application from '../../public/application.png';
2022-09-14 17:31:38 +00:00
import { ChangeEvent, useEffect, useMemo, useRef, useState } from 'react';
2022-08-30 18:08:54 +00:00
import useUser from '../../lib/hooks/useUser';
import { Container } from '../common/Container/Container';
import { Header } from '../common/Header/Header';
import Modal from '../common/Modal/Modal/Modal';
import ModalHeader from '../common/Modal/ModalHeader/ModalHeader';
import ModalBody from '../common/Modal/ModalBody/ModalBody';
import ModalFooter from '../common/Modal/ModalFooter/ModalFooter';
import ModalService from '../common/Modal/services/ModalService';
import { Dropdown } from '../common/Dropdown/Dropdown';
import { ModalProps } from '../../lib/types/modal.interface';
import { useForm } from '../../lib/hooks/useForm';
import { FormWrapper } from '../common/Form/FormWrapper/FormWrapper';
import { FormControl } from '../common/Form/FormControl/FormControl';
2022-09-01 14:23:11 +00:00
import toast from 'react-hot-toast';
import { Button } from '../common/Button/Button';
import userHasPrivileges from '../../lib/utils/has-privileges';
2022-09-12 19:31:58 +00:00
import { publishJSON } from '../../lib/utils/fetch';
2022-09-14 17:31:38 +00:00
import Head from 'next/head';
2022-09-14 17:31:38 +00:00
const LINK_NAMES: Record<string, string> = {
redirect_uri: 'Redirect URI',
terms: 'Terms of Service',
privacy: 'Privacy Policy',
website: 'Website',
};
2022-09-14 17:31:38 +00:00
const REDIRECT_URI_COUNT = 3;
const LinkEdit = ({
2022-09-14 17:31:38 +00:00
link,
onChange,
onRemove,
}: {
link: Partial<OAuth2ClientURL>;
onChange: () => void;
onRemove: () => void;
}) => {
return (
<FormControl>
<label htmlFor={link.type}>{LINK_NAMES[link.type!]}</label>
<div className={styles.urlWrapper}>
<input
id={link.type}
name={link.type}
value={link.url || ''}
onChange={(e) => {
link.url = e.target.value;
onChange();
}}
/>
<Button variant="link" onClick={() => onRemove()} type="button">
Remove
</Button>
</div>
{link.type === OAuth2ClientURLType.REDIRECT_URI && (
<span>Wildcards are NOT allowed!</span>
)}
</FormControl>
);
};
const LinkEditor = ({
formData,
handleInputChange,
}: {
formData: Partial<OAuth2ClientListItem>;
handleInputChange: (
2022-09-14 17:31:38 +00:00
e?: ChangeEvent,
setValue?: any,
formField?: string
) => void;
}) => {
2022-09-14 17:31:38 +00:00
const [links, setLinks] = useState<Partial<OAuth2ClientURL>[]>(
formData.urls || []
);
const [addNewSelection, setAddNewSelection] = useState<OAuth2ClientURLType>();
const availableTypes = useMemo(
() =>
Object.values(OAuth2ClientURLType).filter((type) =>
type === 'redirect_uri'
? links.filter(
(link) => link.type === OAuth2ClientURLType.REDIRECT_URI
).length < REDIRECT_URI_COUNT
: !links.some((link) => link.type === type)
),
[links]
);
2022-09-14 17:31:38 +00:00
useEffect(() => {
if (
(!addNewSelection ||
availableTypes.indexOf(addNewSelection as OAuth2ClientURLType) ===
-1) &&
availableTypes.length
) {
setAddNewSelection(availableTypes[0]);
}
}, [availableTypes, addNewSelection]);
return (
2022-09-14 17:31:38 +00:00
<>
<h3>Client URLs</h3>
{links.map((link, index) => (
<LinkEdit
key={index}
link={link}
onChange={() => {
setLinks(links);
handleInputChange(undefined, links, 'urls');
}}
onRemove={() => {
const clone = links.slice();
clone.splice(index, 1);
setLinks(clone);
handleInputChange(undefined, clone, 'urls');
}}
/>
))}
<FormControl>
{availableTypes.length > 0 && (
<div className={styles.urlWrapper}>
<select
value={addNewSelection}
onChange={(e) =>
setAddNewSelection(e.target.value as OAuth2ClientURLType)
}
>
{availableTypes.map((value, index) => (
<option value={value} key={index}>
{LINK_NAMES[value]}
</option>
))}
</select>
<Button
variant="default"
type="button"
onClick={() => {
setLinks([
...links,
{
type: addNewSelection,
url: '',
},
]);
}}
>
Add
</Button>
</div>
)}
</FormControl>
</>
);
};
const EditClientModal = ({
close,
modalRef,
client,
2022-09-01 14:23:11 +00:00
isAdmin,
update,
}: ModalProps<{
client?: OAuth2ClientListItem;
update: () => void;
isAdmin: boolean;
}>) => {
const formRef = useRef<HTMLFormElement>(null);
2022-09-09 14:37:42 +00:00
const scopeReq = useSWR<string[]>('/api/admin/oauth2/scopes');
const grantReq = useSWR<string[]>('/api/admin/oauth2/grants');
const { formData, handleInputChange, handleSubmit } = useForm<
Partial<OAuth2ClientListItem>
2022-09-09 14:37:42 +00:00
>(client || { grants: 'authorization_code' }, () => {
2022-09-01 14:23:11 +00:00
toast
.promise(
2022-09-01 18:11:36 +00:00
publishJSON(
2022-09-01 14:23:11 +00:00
client
? `/api/admin/oauth2/clients/${client.id}`
: '/api/admin/oauth2/clients',
2022-09-01 18:11:36 +00:00
client ? 'PATCH' : 'POST',
{ activated: true, ...formData }
),
2022-09-01 14:23:11 +00:00
{
loading: 'Saving client...',
success: 'Client saved!',
error: (err) => `Saving the client failed: ${err.message}`,
}
)
.then((data) => {
if (data) {
close(true);
update();
}
});
return false;
});
2022-08-30 18:08:54 +00:00
return (
<Modal modalRef={modalRef}>
2022-08-30 18:08:54 +00:00
<ModalHeader>
2022-09-01 14:23:11 +00:00
<h3>{client ? 'Edit OAuth2 Client' : 'New OAuth2 Client'}</h3>
2022-08-30 18:08:54 +00:00
</ModalHeader>
<ModalBody>
<FormWrapper>
2022-09-01 14:23:11 +00:00
<form ref={formRef} onSubmit={handleSubmit} autoComplete="off">
<FormControl>
<label htmlFor="title">Title</label>
<input
id="title"
type="text"
name="title"
2022-09-01 14:23:11 +00:00
value={formData.title || ''}
onChange={handleInputChange}
/>
</FormControl>
<FormControl>
<label htmlFor="description">Description</label>
<textarea
id="description"
name="description"
2022-09-01 14:23:11 +00:00
value={formData.description || ''}
onChange={handleInputChange}
/>
</FormControl>
<FormControl>
<label htmlFor="scope">Allowed scopes</label>
<input
id="scope"
name="scope"
2022-09-01 14:23:11 +00:00
value={formData.scope || ''}
onChange={handleInputChange}
/>
2022-09-09 14:37:42 +00:00
{scopeReq.data && (
<span>
<b>Available:</b>{' '}
{scopeReq.data
.filter(
(item) => !formData.scope?.split(' ').includes(item)
)
.join(', ') || 'None'}
</span>
)}
</FormControl>
<FormControl>
<label htmlFor="grants">Allowed grant types</label>
<input
id="grants"
name="grants"
2022-09-01 14:23:11 +00:00
value={formData.grants || ''}
onChange={handleInputChange}
/>
2022-09-09 14:37:42 +00:00
{grantReq.data && (
<span>
<b>Available:</b>{' '}
{grantReq.data
.filter(
(item) => !formData.grants?.split(' ').includes(item)
)
.join(', ') || 'None'}
</span>
)}
</FormControl>
2022-09-01 14:23:11 +00:00
{isAdmin && (
<>
<FormControl inline={true}>
<label htmlFor="activated">Activated</label>
<input
id="activated"
name="activated"
type="checkbox"
checked={formData.activated ?? true}
onChange={handleInputChange}
/>
</FormControl>
<FormControl inline={true}>
<label htmlFor="verified">Verified</label>
<input
id="verified"
name="verified"
type="checkbox"
checked={formData.verified ?? false}
onChange={handleInputChange}
/>
</FormControl>
</>
)}
2022-09-14 17:31:38 +00:00
<LinkEditor
2022-09-01 14:23:11 +00:00
formData={formData}
handleInputChange={handleInputChange}
2022-09-14 17:31:38 +00:00
></LinkEditor>
</form>
</FormWrapper>
2022-08-30 18:08:54 +00:00
</ModalBody>
<ModalFooter>
2022-09-01 14:23:11 +00:00
<Button onClick={() => close(true)}>Cancel</Button>
{client && (
<Button
onClick={() =>
toast
.promise(
2022-09-01 18:11:36 +00:00
publishJSON(
`/api/admin/oauth2/clients/${client!.id}/new-secret`,
'POST'
),
2022-09-01 14:23:11 +00:00
{
loading: 'Generating new secret...',
success: 'New secret generated.',
error: 'Failed to generate new secret.',
}
)
.then(() => update())
}
>
New secret
</Button>
)}
<Button onClick={() => handleSubmit()} variant="primary">
Submit
</Button>
2022-08-30 18:08:54 +00:00
</ModalFooter>
</Modal>
);
};
2022-09-01 14:23:11 +00:00
const OAuth2ClientCard = ({
client,
isAdmin,
update,
}: {
client: OAuth2ClientListItem;
isAdmin: boolean;
update: () => void;
}) => (
2022-08-30 18:08:54 +00:00
<div className={styles.clientCard}>
<div className={styles.pictureWrapper}>
{client.picture ? (
<Image
src={`${UPLOADS_URL}/${client.picture.file}`}
width={128}
height={128}
alt=""
/>
) : (
<Image src={application} alt="" width={128} height={128} />
)}
</div>
<div className={styles.clientInfo}>
<div className={styles.titleWrap}>
<h2>{client.title}</h2>
<Dropdown opens="right">
<button
onClick={() =>
2022-09-01 14:23:11 +00:00
ModalService.open(EditClientModal, {
2022-09-09 14:37:42 +00:00
client,
2022-09-01 14:23:11 +00:00
isAdmin,
update,
})
}
>
Edit client
</button>
2022-09-01 14:23:11 +00:00
<button
onClick={() => {
toast.promise(
navigator.clipboard.writeText(client.client_secret),
{
loading: 'Copying',
success: 'Copied to clipboard',
error: 'Copying to clipboard failed.',
}
);
}}
>
Copy secret
</button>
{(!client.activated || !isAdmin) && (
2022-09-09 14:37:42 +00:00
<button
onClick={() => {
toast
.promise(
publishJSON(
`/api/admin/oauth2/clients/${client.id}`,
'DELETE'
),
{
loading: 'Deleting client...',
success: 'Client deleted!',
error: (err) =>
`Deleting the client failed: ${err.message}`,
}
)
.then((data) => {
if (data) {
update();
}
});
}}
>
Delete client
</button>
)}
</Dropdown>
</div>
2022-08-30 18:08:54 +00:00
<span className={styles.clientDescription}>{client.description}</span>
<dl>
<dt>Client ID</dt>
<dd>{client.client_id}</dd>
<dt>Allowed scopes</dt>
<dd>{client.scope?.split(' ').join(', ')}</dd>
<dt>Allowed grant types</dt>
<dd>{client.grants?.split(' ').join(', ')}</dd>
2022-09-01 14:23:11 +00:00
{isAdmin && (
<>
<dt>Activated</dt>
<dd>{client.activated ? 'Yes' : <b>NOT ACTIVATED</b>}</dd>
<dt>Verified</dt>
<dd>{client.verified ? 'Yes' : 'No'}</dd>
</>
)}
{client.owner ? (
<>
<dt>Owner</dt>
<dd>
{client.owner.uuid} ({client.owner.username})
</dd>
</>
) : undefined}
2022-08-30 18:08:54 +00:00
<dt>Created</dt>
<dd>{new Date(client.created_at).toDateString()}</dd>
2022-09-01 14:23:11 +00:00
{client.urls?.length ? (
<>
<dt>URLs</dt>
<dd>
<div className={styles.urls}>
{client.urls.map((url) => (
<a
key={url.id}
href={url.url}
target="_blank"
rel="noreferrer"
>
{LINK_NAMES[url.type]} &lt;{url.url}&gt;
</a>
))}
</div>
</dd>
</>
) : undefined}
2022-08-30 18:08:54 +00:00
</dl>
</div>
</div>
);
2022-09-10 09:54:45 +00:00
const OAuth2ClientList = ({ isAdmin }: { isAdmin: boolean }) => {
const [pageIndex, setPageIndex] = useState(1);
2022-09-14 17:31:38 +00:00
const [clientCount, setClientCount] = useState<number>();
2022-09-10 09:54:45 +00:00
const [searchTerm, setSearchTerm] = useState('');
2022-09-01 14:23:11 +00:00
const { data, mutate } = useSWR<PaginatedResponse<OAuth2ClientListItem>>(
2022-08-30 18:08:54 +00:00
`/api/admin/oauth2/clients?page=${pageIndex}${
searchTerm ? `&q=${searchTerm}` : ''
2022-09-10 09:54:45 +00:00
}&pageSize=8`
2022-08-30 18:08:54 +00:00
);
2022-09-14 17:31:38 +00:00
useEffect(() => {
if (data?.pagination?.rowCount) {
setClientCount(data?.pagination.rowCount);
}
}, [data]);
2022-09-10 09:54:45 +00:00
return (
2022-08-30 18:08:54 +00:00
<>
2022-09-01 14:23:11 +00:00
<div className={styles.header}>
2022-09-14 17:31:38 +00:00
<h1>OAuth2 clients{clientCount && ` (${clientCount})`}</h1>
2022-09-01 14:23:11 +00:00
<Button
onClick={() =>
ModalService.open(EditClientModal, { isAdmin, update: mutate })
}
>
Create new
</Button>
</div>
2022-09-10 09:54:45 +00:00
<FormWrapper>
<FormControl>
<input
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
placeholder="Search titles, descriptions, IDs.."
2022-09-01 14:23:11 +00:00
/>
2022-09-10 09:54:45 +00:00
</FormControl>
</FormWrapper>
<br />
{data ? (
<div className={styles.clientList}>
{data.list.map((client) => (
<OAuth2ClientCard
client={client}
key={client.client_id}
isAdmin={isAdmin}
update={mutate}
/>
))}
{data?.pagination && (
<Paginator
setPage={setPageIndex}
pagination={data.pagination}
></Paginator>
)}
</div>
) : (
<span>Nothing found</span>
)}
2022-08-30 18:08:54 +00:00
</>
);
};
export const OAuth2Page = () => {
const { user } = useUser({ redirectTo: '/login' });
2022-09-01 14:23:11 +00:00
const isAdmin = userHasPrivileges(user, 'admin:oauth2');
2022-08-30 18:08:54 +00:00
return (
<>
2022-09-14 17:31:38 +00:00
<Head>
<title>OAuth2 | Icy Network Administration</title>
</Head>
2022-08-30 18:08:54 +00:00
<Header user={user}></Header>
<Container>
2022-09-10 09:54:45 +00:00
<OAuth2ClientList isAdmin={isAdmin} />
2022-08-30 18:08:54 +00:00
</Container>
</>
);
};