Cleanup frontend to only pass the required description field

This commit is contained in:
Dane Everitt 2021-07-28 21:13:49 -07:00
parent 374910d73a
commit dfff8ad667
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
4 changed files with 30 additions and 27 deletions

View file

@ -13,18 +13,6 @@ class StoreApiKeyRequest extends ClientApiRequest
return [ return [
'description' => $rules['memo'], 'description' => $rules['memo'],
'allowed_ips' => $rules['allowed_ips'],
'allowed_ips.*' => 'ip',
];
}
/**
* @return array|string[]
*/
public function messages()
{
return [
'allowed_ips.*' => 'All of the IP addresses entered must be valid IPv4 addresses.',
]; ];
} }
} }

View file

@ -9,6 +9,11 @@ class PersonalAccessToken extends Model implements HasAbilities
{ {
public const RESOURCE_NAME = 'personal_access_token'; public const RESOURCE_NAME = 'personal_access_token';
/**
* The length of the raw API token.
*/
public const TOKEN_LENGTH = 32;
/** /**
* @var string[] * @var string[]
*/ */
@ -28,6 +33,16 @@ class PersonalAccessToken extends Model implements HasAbilities
'abilities', 'abilities',
]; ];
/**
* @var array
*/
public static array $validationRules = [
'token' => 'required|string',
'token_id' => 'required|string|size:16',
'description' => 'required|nullable|string|max:500',
'last_used_at' => 'nullable|date',
];
/** /**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/ */
@ -87,4 +102,14 @@ class PersonalAccessToken extends Model implements HasAbilities
return static::where('token_id', $id)->where('token', hash('sha256', $token))->first(); return static::where('token_id', $id)->where('token', hash('sha256', $token))->first();
} }
/**
* Generates a new identifier for a personal access token.
*
* @return string
*/
public static function generateTokenIdentifier(): string
{
return 'ptdl_' . Str::random(11);
}
} }

View file

@ -35,8 +35,8 @@ trait HasAccessTokens
$token = $this->tokens()->create([ $token = $this->tokens()->create([
'user_id' => $this->id, 'user_id' => $this->id,
'description' => $description, 'description' => $description,
'token' => hash('sha256', $plain = Str::random(36)), 'token' => hash('sha256', $plain = Str::random(PersonalAccessToken::TOKEN_LENGTH)),
'token_id' => 'ptdl_' . Str::random(11), 'token_id' => PersonalAccessToken::generateTokenIdentifier(),
'abilities' => $abilities, 'abilities' => $abilities,
]); ]);

View file

@ -8,9 +8,9 @@ import { ApplicationStore } from '@/state';
import { httpErrorToHuman } from '@/api/http'; import { httpErrorToHuman } from '@/api/http';
import SpinnerOverlay from '@/components/elements/SpinnerOverlay'; import SpinnerOverlay from '@/components/elements/SpinnerOverlay';
import { ApiKey } from '@/api/account/getApiKeys'; import { ApiKey } from '@/api/account/getApiKeys';
import tw, { styled } from 'twin.macro'; import tw from 'twin.macro';
import Button from '@/components/elements/Button'; import Button from '@/components/elements/Button';
import Input, { Textarea } from '@/components/elements/Input'; import Input from '@/components/elements/Input';
import ApiKeyModal from '@/components/dashboard/ApiKeyModal'; import ApiKeyModal from '@/components/dashboard/ApiKeyModal';
interface Values { interface Values {
@ -18,8 +18,6 @@ interface Values {
allowedIps: string; allowedIps: string;
} }
const CustomTextarea = styled(Textarea)`${tw`h-32`}`;
export default ({ onKeyCreated }: { onKeyCreated: (key: ApiKey) => void }) => { export default ({ onKeyCreated }: { onKeyCreated: (key: ApiKey) => void }) => {
const [ apiKey, setApiKey ] = useState(''); const [ apiKey, setApiKey ] = useState('');
const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes); const { addError, clearFlashes } = useStoreActions((actions: Actions<ApplicationStore>) => actions.flashes);
@ -52,7 +50,6 @@ export default ({ onKeyCreated }: { onKeyCreated: (key: ApiKey) => void }) => {
onSubmit={submit} onSubmit={submit}
initialValues={{ description: '', allowedIps: '' }} initialValues={{ description: '', allowedIps: '' }}
validationSchema={object().shape({ validationSchema={object().shape({
allowedIps: string(),
description: string().required().min(4), description: string().required().min(4),
})} })}
> >
@ -62,18 +59,11 @@ export default ({ onKeyCreated }: { onKeyCreated: (key: ApiKey) => void }) => {
<FormikFieldWrapper <FormikFieldWrapper
label={'Description'} label={'Description'}
name={'description'} name={'description'}
description={'A description of this API key.'} description={'This API key will be able to act on your behalf against this Panel\'s API.'}
css={tw`mb-6`} css={tw`mb-6`}
> >
<Field name={'description'} as={Input}/> <Field name={'description'} as={Input}/>
</FormikFieldWrapper> </FormikFieldWrapper>
<FormikFieldWrapper
label={'Allowed IPs'}
name={'allowedIps'}
description={'Leave blank to allow any IP address to use this API key, otherwise provide each IP address on a new line.'}
>
<Field name={'allowedIps'} as={CustomTextarea}/>
</FormikFieldWrapper>
<div css={tw`flex justify-end mt-6`}> <div css={tw`flex justify-end mt-6`}>
<Button>Create</Button> <Button>Create</Button>
</div> </div>