Add support for storing SSH keys on user accounts
This commit is contained in:
parent
5705d7dbdd
commit
97280a62a2
20 changed files with 678 additions and 6 deletions
28
resources/scripts/api/account/ssh-keys.ts
Normal file
28
resources/scripts/api/account/ssh-keys.ts
Normal file
|
@ -0,0 +1,28 @@
|
|||
import useSWR, { ConfigInterface } from 'swr';
|
||||
import useUserSWRContentKey from '@/plugins/useUserSWRContentKey';
|
||||
import http, { FractalResponseList } from '@/api/http';
|
||||
import { SSHKey, Transformers } from '@definitions/user';
|
||||
import { AxiosError } from 'axios';
|
||||
|
||||
const useSSHKeys = (config?: ConfigInterface<SSHKey[], AxiosError>) => {
|
||||
const key = useUserSWRContentKey([ 'account', 'ssh-keys' ]);
|
||||
|
||||
return useSWR(key, async () => {
|
||||
const { data } = await http.get('/api/client/account/ssh-keys');
|
||||
|
||||
return (data as FractalResponseList).data.map((datum: any) => {
|
||||
return Transformers.toSSHKey(datum.attributes);
|
||||
});
|
||||
}, { revalidateOnMount: false, ...(config || {}) });
|
||||
};
|
||||
|
||||
const createSSHKey = async (name: string, publicKey: string): Promise<SSHKey> => {
|
||||
const { data } = await http.post('/api/client/account/ssh-keys', { name, public_key: publicKey });
|
||||
|
||||
return Transformers.toSSHKey(data.attributes);
|
||||
};
|
||||
|
||||
const deleteSSHKey = async (fingerprint: string): Promise<void> =>
|
||||
await http.delete(`/api/client/account/ssh-keys/${fingerprint}`);
|
||||
|
||||
export { useSSHKeys, createSSHKey, deleteSSHKey };
|
|
@ -1,2 +1,8 @@
|
|||
// empty export
|
||||
export type _T = string;
|
||||
import { Model } from '@/api/definitions';
|
||||
|
||||
interface SSHKey extends Model {
|
||||
name: string;
|
||||
publicKey: string;
|
||||
fingerprint: string;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,14 @@
|
|||
import { SSHKey } from '@definitions/user/models';
|
||||
|
||||
export default class Transformers {
|
||||
static toSSHKey (data: Record<any, any>): SSHKey {
|
||||
return {
|
||||
name: data.name,
|
||||
publicKey: data.public_key,
|
||||
fingerprint: data.fingerprint,
|
||||
createdAt: new Date(data.created_at),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export class MetaTransformers {
|
||||
|
|
Reference in a new issue