mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-05 22:05:47 -05:00
Make personal access tokens soft-deletable; update front-end
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
|
||||
import http, { FractalResponseList } from '@/api/http';
|
||||
import Transformers from '@transformers';
|
||||
import { PersonalAccessToken } from '@models';
|
||||
import { AxiosError } from 'axios';
|
||||
import useUserSWRContextKey from '@/plugins/useUserSWRContextKey';
|
||||
|
||||
const useAPIKeys = (
|
||||
config?: SWRConfiguration<PersonalAccessToken[], AxiosError>,
|
||||
): SWRResponse<PersonalAccessToken[], AxiosError> => {
|
||||
const key = useUserSWRContextKey([ 'account', 'api-keys' ]);
|
||||
|
||||
return useSWR(key, async () => {
|
||||
const { data } = await http.get('/api/client/account/api-keys');
|
||||
|
||||
return (data as FractalResponseList).data.map((datum: any) => {
|
||||
return Transformers.toPersonalAccessToken(datum.attributes);
|
||||
});
|
||||
}, config || { revalidateOnMount: false });
|
||||
};
|
||||
|
||||
const createAPIKey = async (description: string): Promise<[ PersonalAccessToken, string ]> => {
|
||||
const { data } = await http.post('/api/client/account/api-keys', { description });
|
||||
|
||||
const token = Transformers.toPersonalAccessToken(data.attributes);
|
||||
|
||||
return [ token, data.meta?.secret_token || '' ];
|
||||
};
|
||||
|
||||
const deleteAPIKey = async (identifier: string) =>
|
||||
await http.delete(`/api/client/account/api-keys/${identifier}`);
|
||||
|
||||
export { useAPIKeys, createAPIKey, deleteAPIKey };
|
||||
@@ -1,17 +0,0 @@
|
||||
import http from '@/api/http';
|
||||
import { ApiKey, rawDataToApiKey } from '@/api/account/getApiKeys';
|
||||
|
||||
export default (description: string, allowedIps: string): Promise<ApiKey & { secretToken: string }> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.post('/api/client/account/api-keys', {
|
||||
description,
|
||||
allowed_ips: allowedIps.length > 0 ? allowedIps.split('\n') : [],
|
||||
})
|
||||
.then(({ data }) => resolve({
|
||||
...rawDataToApiKey(data.attributes),
|
||||
// eslint-disable-next-line camelcase
|
||||
secretToken: data.meta?.secret_token ?? '',
|
||||
}))
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
||||
@@ -1,9 +0,0 @@
|
||||
import http from '@/api/http';
|
||||
|
||||
export default (identifier: string): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.delete(`/api/client/account/api-keys/${identifier}`)
|
||||
.then(() => resolve())
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
||||
@@ -1,23 +0,0 @@
|
||||
import http from '@/api/http';
|
||||
|
||||
export interface ApiKey {
|
||||
identifier: string;
|
||||
description: string;
|
||||
createdAt: Date | null;
|
||||
lastUsedAt: Date | null;
|
||||
}
|
||||
|
||||
export const rawDataToApiKey = (data: any): ApiKey => ({
|
||||
identifier: data.token_id,
|
||||
description: data.description,
|
||||
createdAt: data.created_at ? new Date(data.created_at) : null,
|
||||
lastUsedAt: data.last_used_at ? new Date(data.last_used_at) : null,
|
||||
});
|
||||
|
||||
export default (): Promise<ApiKey[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.get('/api/client/account/api-keys')
|
||||
.then(({ data }) => resolve((data.data || []).map((d: any) => rawDataToApiKey(d.attributes))))
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
||||
@@ -1,17 +1,17 @@
|
||||
import useSWR, { SWRConfiguration, SWRResponse } from 'swr';
|
||||
import { useStoreState } from '@/state/hooks';
|
||||
import http, { FractalResponseList } from '@/api/http';
|
||||
import Transformers from '@transformers';
|
||||
import { SecurityKey } from '@models';
|
||||
import { AxiosError } from 'axios';
|
||||
import { base64Decode, bufferDecode, bufferEncode, decodeSecurityKeyCredentials } from '@/helpers';
|
||||
import { LoginResponse } from '@/api/auth/login';
|
||||
import useUserSWRContextKey from '@/plugins/useUserSWRContextKey';
|
||||
|
||||
const useSecurityKeys = (config?: SWRConfiguration<SecurityKey[], AxiosError>): SWRResponse<SecurityKey[], AxiosError> => {
|
||||
const uuid = useStoreState(state => state.user.data!.uuid);
|
||||
const key = useUserSWRContextKey([ 'account', 'security-keys' ]);
|
||||
|
||||
return useSWR<SecurityKey[], AxiosError>(
|
||||
[ 'account', uuid, 'security-keys' ],
|
||||
key,
|
||||
async (): Promise<SecurityKey[]> => {
|
||||
const { data } = await http.get('/api/client/account/security-keys');
|
||||
|
||||
|
||||
+8
@@ -9,3 +9,11 @@ interface SecurityKey extends Model {
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
interface PersonalAccessToken extends Model {
|
||||
identifier: string;
|
||||
description: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
lastUsedAt: Date | null;
|
||||
}
|
||||
|
||||
@@ -11,4 +11,14 @@ export default class Transformers {
|
||||
updatedAt: new Date(data.updated_at),
|
||||
};
|
||||
}
|
||||
|
||||
static toPersonalAccessToken (data: Record<string, any>): Models.PersonalAccessToken {
|
||||
return {
|
||||
identifier: data.token_id,
|
||||
description: data.description,
|
||||
createdAt: new Date(data.created_at),
|
||||
updatedAt: new Date(data.updated_at),
|
||||
lastUsedAt: data.last_used_at ? new Date(data.last_used_at) : null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user