Skip to content

Commit

Permalink
fix: Some fixes for misskey-dev#12850
Browse files Browse the repository at this point in the history
- refinement the error message when trueMail validation fails
- the settings of trueMail are not displayed after saving
- changing how `Active Email Validation` is saved
  • Loading branch information
MarryDream committed Jan 2, 2024
1 parent 9c5559a commit a2d2027
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 45 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

## 202x.x.x (Unreleased)

### General
- Feat: Add support for TrueMail

### Client
- Enhance: ハッシュタグ入力時に、本文の末尾の行に何も書かれていない場合は新たにスペースを追加しないように

Expand Down
76 changes: 51 additions & 25 deletions packages/backend/src/core/EmailService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class EmailService {
@bindThis
public async validateEmailForAccount(emailAddress: string): Promise<{
available: boolean;
reason: null | 'used' | 'format' | 'disposable' | 'mx' | 'smtp' | 'banned' | 'network' | 'blacklist';
reason: null | string;
}> {
const meta = await this.metaService.fetch();

Expand All @@ -165,10 +165,17 @@ export class EmailService {
email: emailAddress,
});

if (exist !== 0) {
return {
available: false,
reason: 'used',
};
}

let validated: {
valid: boolean,
reason?: string | null,
};
} = { valid: true, reason: null };

if (meta.enableActiveEmailValidation) {
if (meta.enableVerifymailApi && meta.verifymailAuthKey != null) {
Expand All @@ -185,33 +192,38 @@ export class EmailService {
validateSMTP: false, // 日本だと25ポートが殆どのプロバイダーで塞がれていてタイムアウトになるので
});
}
} else {
validated = { valid: true, reason: null };
}

if (!validated.valid) {
let reason = validated.reason ?? null;
if (reason === 'regex') {
reason = 'format';
}
return {
available: false,
reason,
};
}

const emailDomain: string = emailAddress.split('@')[1];
const isBanned = this.utilityService.isBlockedHost(meta.bannedEmailDomains, emailDomain);

const available = exist === 0 && validated.valid && !isBanned;
if (isBanned) {
return {
available: false,
reason: 'banned',
};
}

return {
available,
reason: available ? null :
exist !== 0 ? 'used' :
isBanned ? 'banned' :
validated.reason === 'regex' ? 'format' :
validated.reason === 'disposable' ? 'disposable' :
validated.reason === 'mx' ? 'mx' :
validated.reason === 'smtp' ? 'smtp' :
validated.reason === 'network' ? 'network' :
validated.reason === 'blacklist' ? 'blacklist' :
null,
available: true,
reason: null,
};
}

private async verifyMail(emailAddress: string, verifymailAuthKey: string): Promise<{
valid: boolean;
reason: 'used' | 'format' | 'disposable' | 'mx' | 'smtp' | null;
reason: 'used' | 'format' | 'disposable' | 'mx' | 'smtp' | 'key' | null;
}> {
const endpoint = 'https://verifymail.io/api/' + emailAddress + '?key=' + verifymailAuthKey;
const res = await this.httpRequestService.send(endpoint, {
Expand All @@ -223,6 +235,7 @@ export class EmailService {
});

const json = (await res.json()) as {
message?: string;
block: boolean;
catch_all: boolean;
deliverable_email: boolean;
Expand All @@ -239,6 +252,12 @@ export class EmailService {
related_domains: string[];
};

if (json.message) {
return {
valid: false,
reason: 'key',
};
}
if (json.email_address === undefined) {
return {
valid: false,
Expand Down Expand Up @@ -272,7 +291,7 @@ export class EmailService {

private async trueMail<T>(truemailInstance: string, emailAddress: string, truemailAuthKey: string): Promise<{
valid: boolean;
reason: 'used' | 'format' | 'blacklist' | 'mx' | 'smtp' | 'network' | T | null;
reason: 'key' | 'used' | 'format' | 'blacklist' | 'mx' | 'smtp' | 'network' | T | null;
}> {
const endpoint = truemailInstance + '?email=' + emailAddress;
try {
Expand All @@ -284,22 +303,29 @@ export class EmailService {
Authorization: truemailAuthKey
},
});

const json = (await res.json()) as {
email: string;
success: boolean;
errors?: {
error?: string;
errors?: {
list_match?: string;
regex?: string;
mx?: string;
smtp?: string;
} | null;
};
if (json.email === undefined || (json.email !== undefined && json.errors?.regex)) {

if (json.error) {
return {
valid: false,
reason: 'format',
valid: false,
reason: 'key',
};
}
if (json.email === undefined || json.errors?.regex) {
return {
valid: false,
reason: 'format',
};
}
if (json.errors?.smtp) {
Expand All @@ -320,7 +346,7 @@ export class EmailService {
reason: json.errors?.list_match as T || 'blacklist',
};
}

return {
valid: true,
reason: null,
Expand Down
18 changes: 11 additions & 7 deletions packages/frontend/src/pages/admin/security.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,28 @@ SPDX-License-Identifier: AGPL-3.0-only

<div class="_gaps_m">
<span>{{ i18n.ts.activeEmailValidationDescription }}</span>
<MkSwitch v-model="enableActiveEmailValidation" @update:modelValue="save">
<MkSwitch v-model="enableActiveEmailValidation">
<template #label>Enable</template>
</MkSwitch>
<MkSwitch v-model="enableVerifymailApi" @update:modelValue="save">
<MkSwitch v-model="enableVerifymailApi">
<template #label>Use Verifymail.io API</template>
</MkSwitch>
<MkInput v-model="verifymailAuthKey" @update:modelValue="save">
<MkInput v-model="verifymailAuthKey">
<template #prefix><i class="ti ti-key"></i></template>
<template #label>Verifymail.io API Auth Key</template>
</MkInput>
<MkSwitch v-model="enableTruemailApi" @update:modelValue="save">
<MkSwitch v-model="enableTruemailApi">
<template #label>Use TrueMail API</template>
</MkSwitch>
<MkInput v-model="truemailInstance" @update:modelValue="save">
<MkInput v-model="truemailInstance">
<template #prefix><i class="ti ti-key"></i></template>
<template #label>TrueMail API Instance</template>
</MkInput>
<MkInput v-model="truemailAuthKey" @update:modelValue="save">
<MkInput v-model="truemailAuthKey">
<template #prefix><i class="ti ti-key"></i></template>
<template #label>TrueMail API Auth Key</template>
</MkInput>
<MkButton primary @click="save"><i class="ti ti-device-floppy"></i> {{ i18n.ts.save }}</MkButton>
</div>
</MkFolder>

Expand Down Expand Up @@ -188,7 +189,10 @@ async function init() {
enableActiveEmailValidation.value = meta.enableActiveEmailValidation;
enableVerifymailApi.value = meta.enableVerifymailApi;
verifymailAuthKey.value = meta.verifymailAuthKey;
bannedEmailDomains.value = meta.bannedEmailDomains.join('\n');
enableTruemailApi.value = meta.enableTruemailApi;
truemailInstance.value = meta.truemailInstance;
truemailAuthKey.value = meta.truemailAuthKey;
bannedEmailDomains.value = meta.bannedEmailDomains?.join('\n') || "";
}

function save() {
Expand Down
5 changes: 2 additions & 3 deletions packages/misskey-js/src/autogen/apiClientJSDoc.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* version: 2023.12.0
* generatedAt: 2023-12-26T23:35:09.494Z
* version: 2023.12.2
* generatedAt: 2024-01-02T13:38:55.847Z
*/

import type { SwitchCaseResponseType } from '../api.js';
Expand Down Expand Up @@ -33,7 +33,6 @@ declare module '../api.js' {
/**
* No description provided.
*
* **Internal Endpoint**: This endpoint is an API for the misskey mainframe and is not intended for use by third parties.
* **Credential required**: *No*
*/
request<E extends 'admin/accounts/create', P extends Endpoints[E]['req']>(
Expand Down
4 changes: 2 additions & 2 deletions packages/misskey-js/src/autogen/endpoint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* version: 2023.12.0
* generatedAt: 2023-12-26T23:35:09.491Z
* version: 2023.12.2
* generatedAt: 2024-01-02T13:38:55.835Z
*/

import type {
Expand Down
4 changes: 2 additions & 2 deletions packages/misskey-js/src/autogen/entities.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* version: 2023.12.0
* generatedAt: 2023-12-26T23:35:09.489Z
* version: 2023.12.2
* generatedAt: 2024-01-02T13:38:55.831Z
*/

import { operations } from './types.js';
Expand Down
4 changes: 2 additions & 2 deletions packages/misskey-js/src/autogen/models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* version: 2023.12.0
* generatedAt: 2023-12-26T23:35:09.485Z
* version: 2023.12.2
* generatedAt: 2024-01-02T13:38:55.818Z
*/

import { components } from './types.js';
Expand Down
12 changes: 8 additions & 4 deletions packages/misskey-js/src/autogen/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/* eslint @typescript-eslint/no-explicit-any: 0 */

/*
* version: 2023.12.0
* generatedAt: 2023-12-26T23:35:09.389Z
* version: 2023.12.2
* generatedAt: 2024-01-02T13:38:55.207Z
*/

/**
Expand Down Expand Up @@ -40,7 +40,6 @@ export type paths = {
* admin/accounts/create
* @description No description provided.
*
* **Internal Endpoint**: This endpoint is an API for the misskey mainframe and is not intended for use by third parties.
* **Credential required**: *No*
*/
post: operations['admin/accounts/create'];
Expand Down Expand Up @@ -4456,6 +4455,9 @@ export type operations = {
enableActiveEmailValidation: boolean;
enableVerifymailApi: boolean;
verifymailAuthKey: string | null;
enableTruemailApi: boolean;
truemailInstance: string | null;
truemailAuthKey: string | null;
enableChartsForRemoteUser: boolean;
enableChartsForFederatedInstances: boolean;
enableServerMachineStats: boolean;
Expand Down Expand Up @@ -4620,7 +4622,6 @@ export type operations = {
* admin/accounts/create
* @description No description provided.
*
* **Internal Endpoint**: This endpoint is an API for the misskey mainframe and is not intended for use by third parties.
* **Credential required**: *No*
*/
'admin/accounts/create': {
Expand Down Expand Up @@ -8247,6 +8248,9 @@ export type operations = {
enableActiveEmailValidation?: boolean;
enableVerifymailApi?: boolean;
verifymailAuthKey?: string | null;
enableTruemailApi?: boolean;
truemailInstance?: string | null;
truemailAuthKey?: string | null;
enableChartsForRemoteUser?: boolean;
enableChartsForFederatedInstances?: boolean;
enableServerMachineStats?: boolean;
Expand Down

0 comments on commit a2d2027

Please sign in to comment.