This commit is contained in:
sharevb 2024-12-19 12:57:07 +00:00 committed by GitHub
commit 44b4aa15bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 79 additions and 47 deletions

6
components.d.ts vendored
View file

@ -141,8 +141,10 @@ declare module '@vue/runtime-core' {
NLayout: typeof import('naive-ui')['NLayout']
NLayoutSider: typeof import('naive-ui')['NLayoutSider']
NMenu: typeof import('naive-ui')['NMenu']
NSpace: typeof import('naive-ui')['NSpace']
NTable: typeof import('naive-ui')['NTable']
NP: typeof import('naive-ui')['NP']
NScrollbar: typeof import('naive-ui')['NScrollbar']
NSlider: typeof import('naive-ui')['NSlider']
NSwitch: typeof import('naive-ui')['NSwitch']
NumeronymGenerator: typeof import('./src/tools/numeronym-generator/numeronym-generator.vue')['default']
OtpCodeGeneratorAndValidator: typeof import('./src/tools/otp-code-generator-and-validator/otp-code-generator-and-validator.vue')['default']
PasswordStrengthAnalyser: typeof import('./src/tools/password-strength-analyser/password-strength-analyser.vue')['default']

View file

@ -94,5 +94,19 @@ describe('token-generator', () => {
expect(token).toHaveLength(256);
expect(token).toMatch(/^[a-zA-Z]+$/);
});
it('should generate a random string with just numbers except 1 and 2 if only withNumbers is set and deniedChars contains 1 and 2', () => {
const token = createToken({
withLowercase: false,
withUppercase: false,
withNumbers: true,
withSymbols: false,
length: 256,
deniedChars: '12',
});
expect(token).toHaveLength(256);
expect(token).toMatch(/^[03456789]+$/);
});
});
});

View file

@ -5,6 +5,7 @@ export function createToken({
withLowercase = true,
withNumbers = true,
withSymbols = false,
deniedChars = '',
length = 64,
alphabet,
}: {
@ -12,15 +13,17 @@ export function createToken({
withLowercase?: boolean
withNumbers?: boolean
withSymbols?: boolean
deniedChars?: string
length?: number
alphabet?: string
}) {
const allAlphabet = alphabet ?? [
withUppercase ? 'ABCDEFGHIJKLMOPQRSTUVWXYZ' : '',
withLowercase ? 'abcdefghijklmopqrstuvwxyz' : '',
withNumbers ? '0123456789' : '',
withSymbols ? '.,;:!?./-"\'#{([-|\\@)]=}*+' : '',
].join('');
const allAlphabet = (alphabet ?? (
(withUppercase ? 'ABCDEFGHIJKLMOPQRSTUVWXYZ' : '')
+ (withLowercase ? 'abcdefghijklmopqrstuvwxyz' : '')
+ (withNumbers ? '0123456789' : '')
+ (withSymbols ? '.,;:!?./-"\'#{([-|\\@)]=}*+' : '')
)).split('').filter(c => !(deniedChars?.includes(c))).join('');
return shuffleString(allAlphabet.repeat(length)).substring(0, length);
const len = length < 1 ? 1 : length;
return shuffleString(allAlphabet.repeat(len)).substring(0, len);
}

View file

@ -1,75 +1,88 @@
<script setup lang="ts">
import { createToken } from './token-generator.service';
import { useCopy } from '@/composable/copy';
import { useQueryParam } from '@/composable/queryParams';
import { useQueryParamOrStorage } from '@/composable/queryParams';
import { computedRefreshable } from '@/composable/computedRefreshable';
const length = useQueryParam({ name: 'length', defaultValue: 64 });
const withUppercase = useQueryParam({ name: 'uppercase', defaultValue: true });
const withLowercase = useQueryParam({ name: 'lowercase', defaultValue: true });
const withNumbers = useQueryParam({ name: 'numbers', defaultValue: true });
const withSymbols = useQueryParam({ name: 'symbols', defaultValue: false });
const count = useQueryParamOrStorage({ name: 'count', storageName: 'token-generator:count', defaultValue: 1 });
const length = useQueryParamOrStorage({ name: 'length', storageName: 'token-generator:length', defaultValue: 64 });
const withUppercase = useQueryParamOrStorage({ name: 'uppercase', storageName: 'token-generator:uppercase', defaultValue: true });
const withLowercase = useQueryParamOrStorage({ name: 'lowercase', storageName: 'token-generator:lowercase', defaultValue: true });
const withNumbers = useQueryParamOrStorage({ name: 'numbers', storageName: 'token-generator:numbers', defaultValue: true });
const withSymbols = useQueryParamOrStorage({ name: 'symbols', storageName: 'token-generator:symbols', defaultValue: false });
const deniedChars = useQueryParamOrStorage({ name: 'deny', storageName: 'token-generator:deny', defaultValue: '' });
const { t } = useI18n();
const [token, refreshToken] = computedRefreshable(() =>
createToken({
length: length.value,
withUppercase: withUppercase.value,
withLowercase: withLowercase.value,
withNumbers: withNumbers.value,
withSymbols: withSymbols.value,
}),
const [tokens, refreshTokens] = computedRefreshable(() =>
Array.from({ length: count.value < 1 ? 1 : count.value },
() => createToken({
length: length.value,
withUppercase: withUppercase.value,
withLowercase: withLowercase.value,
withNumbers: withNumbers.value,
withSymbols: withSymbols.value,
deniedChars: deniedChars.value,
})).join('\n'),
);
const { copy } = useCopy({ source: token, text: t('tools.token-generator.copied') });
const { copy } = useCopy({ source: tokens, text: t('tools.token-generator.copied') });
</script>
<template>
<div>
<c-card>
<n-form label-placement="left" label-width="140">
<div flex justify-center>
<div>
<n-form-item :label="t('tools.token-generator.uppercase')">
<n-switch v-model:value="withUppercase" />
</n-form-item>
<n-space justify="center">
<n-form-item :label="t('tools.token-generator.uppercase')">
<n-switch v-model:value="withUppercase" />
</n-form-item>
<n-form-item :label="t('tools.token-generator.lowercase')">
<n-switch v-model:value="withLowercase" />
</n-form-item>
</div>
<n-form-item :label="t('tools.token-generator.lowercase')">
<n-switch v-model:value="withLowercase" />
</n-form-item>
<n-form-item :label="t('tools.token-generator.numbers')">
<n-switch v-model:value="withNumbers" />
</n-form-item>
<div>
<n-form-item :label="t('tools.token-generator.numbers')">
<n-switch v-model:value="withNumbers" />
</n-form-item>
<n-form-item :label="t('tools.token-generator.symbols')">
<n-switch v-model:value="withSymbols" />
</n-form-item>
</div>
</div>
<n-form-item :label="t('tools.token-generator.symbols')">
<n-switch v-model:value="withSymbols" />
</n-form-item>
</n-space>
</n-form>
<n-form-item label="Denied Characters (ie, visually similar { oO01lI } or punctuations)" label-placement="top">
<c-input-text
v-model:value="deniedChars"
placeholder="Put characters to deny from token"
/>
</n-form-item>
<n-form-item :label="`${t('tools.token-generator.length')} (${length})`" label-placement="left">
<n-slider v-model:value="length" :step="1" :min="1" :max="512" />
<n-slider v-model:value="length" :step="1" :min="1" :max="512" mr-2 />
<n-input-number v-model:value="length" :min="1" :max="512" size="small" />
</n-form-item>
<n-form-item label="Number of token to generate" label-placement="left">
<n-slider v-model:value="count" :step="1" :min="1" mr-2 />
<n-input-number v-model:value="count" :min="1" size="small" />
</n-form-item>
<c-input-text
v-model:value="token"
v-model:value="tokens"
multiline
:placeholder="t('tools.token-generator.tokenPlaceholder')"
readonly
rows="3"
autosize
class="token-display"
word-wrap
/>
<div mt-5 flex justify-center gap-3>
<c-button @click="copy()">
{{ t('tools.token-generator.button.copy') }}
</c-button>
<c-button @click="refreshToken">
<c-button @click="refreshTokens">
{{ t('tools.token-generator.button.refresh') }}
</c-button>
</div>