From 5a8a0f2c64857ba8e0db000e95895c13eccf79dd Mon Sep 17 00:00:00 2001 From: guqing Date: Tue, 23 Apr 2024 17:03:43 +0800 Subject: [PATCH 1/5] feat: require password verification for email updates --- .../core/extension/endpoint/UserEndpoint.java | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java b/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java index 0f7c71baec..9748840614 100644 --- a/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java +++ b/application/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java @@ -258,26 +258,38 @@ private Mono verifyEmail(ServerRequest request) { .switchIfEmpty(Mono.error( () -> new ServerWebInputException("Request body is required.")) ) - .flatMap(verifyEmailRequest -> ReactiveSecurityContextHolder.getContext() - .map(SecurityContext::getAuthentication) - .map(Principal::getName) - .map(username -> Tuples.of(username, verifyEmailRequest.code())) - ) - .flatMap(tuple2 -> { - var username = tuple2.getT1(); - var code = tuple2.getT2(); - return Mono.just(username) - .transformDeferred(verificationEmailRateLimiter(username)) - .flatMap(name -> emailVerificationService.verify(username, code)) - .onErrorMap(RequestNotPermitted.class, RateLimitExceededException::new); - }) + .flatMap(this::doVerifyCode) .then(ServerResponse.ok().build()); } + private Mono doVerifyCode(VerifyCodeRequest verifyCodeRequest) { + return ReactiveSecurityContextHolder.getContext() + .map(SecurityContext::getAuthentication) + .map(Principal::getName) + .flatMap(username -> verifyPasswordAndCode(username, verifyCodeRequest)); + } + + private Mono verifyPasswordAndCode(String username, VerifyCodeRequest verifyCodeRequest) { + return userService.confirmPassword(username, verifyCodeRequest.password()) + .filter(Boolean::booleanValue) + .switchIfEmpty(Mono.error(new UnsatisfiedAttributeValueException( + "Password is incorrect.", "problemDetail.user.password.notMatch", null))) + .flatMap(verified -> verifyEmailCode(username, verifyCodeRequest.code())); + } + + private Mono verifyEmailCode(String username, String code) { + return Mono.just(username) + .transformDeferred(verificationEmailRateLimiter(username)) + .flatMap(name -> emailVerificationService.verify(username, code)) + .onErrorMap(RequestNotPermitted.class, RateLimitExceededException::new); + } + public record EmailVerifyRequest(@Schema(requiredMode = REQUIRED) String email) { } - public record VerifyCodeRequest(@Schema(requiredMode = REQUIRED, minLength = 1) String code) { + public record VerifyCodeRequest( + @Schema(requiredMode = REQUIRED) String password, + @Schema(requiredMode = REQUIRED, minLength = 1) String code) { } private Mono sendEmailVerificationCode(ServerRequest request) { From d80e4e50055e4a219aab3cdc9b6ca74472406277 Mon Sep 17 00:00:00 2001 From: Ryan Wang Date: Tue, 23 Apr 2024 17:46:33 +0800 Subject: [PATCH 2/5] Refine ui Signed-off-by: Ryan Wang --- api-docs/openapi/v3_0/aggregated.json | 6 +++- .../api-client/src/.openapi-generator/FILES | 1 + .../api-client/src/models/comment-status.ts | 6 ++++ ui/packages/api-client/src/models/index.ts | 1 + .../api-client/src/models/plugin-status.ts | 3 +- .../api-client/src/models/reply-status.ts | 30 +++++++++++++++++++ ui/packages/api-client/src/models/reply.ts | 9 ++++++ .../src/models/verify-code-request.ts | 6 ++++ ui/src/locales/en.yaml | 3 ++ ui/src/locales/zh-CN.yaml | 3 ++ ui/src/locales/zh-TW.yaml | 3 ++ .../profile/components/EmailVerifyModal.vue | 24 +++++++++------ 12 files changed, 83 insertions(+), 12 deletions(-) create mode 100644 ui/packages/api-client/src/models/reply-status.ts diff --git a/api-docs/openapi/v3_0/aggregated.json b/api-docs/openapi/v3_0/aggregated.json index 5e1d7e49ff..57935394c3 100644 --- a/api-docs/openapi/v3_0/aggregated.json +++ b/api-docs/openapi/v3_0/aggregated.json @@ -19969,13 +19969,17 @@ }, "VerifyCodeRequest": { "required": [ - "code" + "code", + "password" ], "type": "object", "properties": { "code": { "minLength": 1, "type": "string" + }, + "password": { + "type": "string" } } }, diff --git a/ui/packages/api-client/src/.openapi-generator/FILES b/ui/packages/api-client/src/.openapi-generator/FILES index 1219e3ee4b..bb8c1cdc40 100644 --- a/ui/packages/api-client/src/.openapi-generator/FILES +++ b/ui/packages/api-client/src/.openapi-generator/FILES @@ -240,6 +240,7 @@ models/register-verify-email-request.ts models/reply-list.ts models/reply-request.ts models/reply-spec.ts +models/reply-status.ts models/reply-vo-list.ts models/reply-vo.ts models/reply.ts diff --git a/ui/packages/api-client/src/models/comment-status.ts b/ui/packages/api-client/src/models/comment-status.ts index 1f565a51be..86df5b65a5 100644 --- a/ui/packages/api-client/src/models/comment-status.ts +++ b/ui/packages/api-client/src/models/comment-status.ts @@ -32,6 +32,12 @@ export interface CommentStatus { * @memberof CommentStatus */ 'lastReplyTime'?: string; + /** + * + * @type {number} + * @memberof CommentStatus + */ + 'observedVersion'?: number; /** * * @type {number} diff --git a/ui/packages/api-client/src/models/index.ts b/ui/packages/api-client/src/models/index.ts index b0acf1a96b..fb7893f6f1 100644 --- a/ui/packages/api-client/src/models/index.ts +++ b/ui/packages/api-client/src/models/index.ts @@ -158,6 +158,7 @@ export * from './reply'; export * from './reply-list'; export * from './reply-request'; export * from './reply-spec'; +export * from './reply-status'; export * from './reply-vo'; export * from './reply-vo-list'; export * from './reset-password-request'; diff --git a/ui/packages/api-client/src/models/plugin-status.ts b/ui/packages/api-client/src/models/plugin-status.ts index 59dd538c90..13ae2787ff 100644 --- a/ui/packages/api-client/src/models/plugin-status.ts +++ b/ui/packages/api-client/src/models/plugin-status.ts @@ -79,8 +79,7 @@ export const PluginStatusLastProbeStateEnum = { Resolved: 'RESOLVED', Started: 'STARTED', Stopped: 'STOPPED', - Failed: 'FAILED', - Unloaded: 'UNLOADED' + Failed: 'FAILED' } as const; export type PluginStatusLastProbeStateEnum = typeof PluginStatusLastProbeStateEnum[keyof typeof PluginStatusLastProbeStateEnum]; diff --git a/ui/packages/api-client/src/models/reply-status.ts b/ui/packages/api-client/src/models/reply-status.ts new file mode 100644 index 0000000000..d50500be32 --- /dev/null +++ b/ui/packages/api-client/src/models/reply-status.ts @@ -0,0 +1,30 @@ +/* tslint:disable */ +/* eslint-disable */ +/** + * Halo Next API + * No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator) + * + * The version of the OpenAPI document: 2.0.0 + * + * + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + + + +/** + * + * @export + * @interface ReplyStatus + */ +export interface ReplyStatus { + /** + * + * @type {number} + * @memberof ReplyStatus + */ + 'observedVersion'?: number; +} + diff --git a/ui/packages/api-client/src/models/reply.ts b/ui/packages/api-client/src/models/reply.ts index 8ce8856e1d..6ec10ecd86 100644 --- a/ui/packages/api-client/src/models/reply.ts +++ b/ui/packages/api-client/src/models/reply.ts @@ -19,6 +19,9 @@ import { Metadata } from './metadata'; // May contain unused imports in some cases // @ts-ignore import { ReplySpec } from './reply-spec'; +// May contain unused imports in some cases +// @ts-ignore +import { ReplyStatus } from './reply-status'; /** * @@ -50,5 +53,11 @@ export interface Reply { * @memberof Reply */ 'spec': ReplySpec; + /** + * + * @type {ReplyStatus} + * @memberof Reply + */ + 'status': ReplyStatus; } diff --git a/ui/packages/api-client/src/models/verify-code-request.ts b/ui/packages/api-client/src/models/verify-code-request.ts index 4939463e5a..5e8c6ae42e 100644 --- a/ui/packages/api-client/src/models/verify-code-request.ts +++ b/ui/packages/api-client/src/models/verify-code-request.ts @@ -26,5 +26,11 @@ export interface VerifyCodeRequest { * @memberof VerifyCodeRequest */ 'code': string; + /** + * + * @type {string} + * @memberof VerifyCodeRequest + */ + 'password': string; } diff --git a/ui/src/locales/en.yaml b/ui/src/locales/en.yaml index 5a3378babc..9797ae2c56 100644 --- a/ui/src/locales/en.yaml +++ b/ui/src/locales/en.yaml @@ -1161,6 +1161,9 @@ core: label: Email address new_email: label: New email address + password: + label: Password + help: The login password for the current account operations: send_code: buttons: diff --git a/ui/src/locales/zh-CN.yaml b/ui/src/locales/zh-CN.yaml index 2e0fbc727e..eb596887a4 100644 --- a/ui/src/locales/zh-CN.yaml +++ b/ui/src/locales/zh-CN.yaml @@ -1106,6 +1106,9 @@ core: label: 电子邮箱 code: label: 验证码 + password: + label: 验证密码 + help: 当前账号的登录密码 operations: send_code: buttons: diff --git a/ui/src/locales/zh-TW.yaml b/ui/src/locales/zh-TW.yaml index 910b144d23..36264c3ae4 100644 --- a/ui/src/locales/zh-TW.yaml +++ b/ui/src/locales/zh-TW.yaml @@ -1083,6 +1083,9 @@ core: label: 電子郵件信箱 new_email: label: 新電子郵件信箱 + password: + label: 驗證密碼 + help: 目前帳號的登入密碼 operations: send_code: buttons: diff --git a/ui/uc-src/modules/profile/components/EmailVerifyModal.vue b/ui/uc-src/modules/profile/components/EmailVerifyModal.vue index 2d283e26c7..818ce8ddc8 100644 --- a/ui/uc-src/modules/profile/components/EmailVerifyModal.vue +++ b/ui/uc-src/modules/profile/components/EmailVerifyModal.vue @@ -1,13 +1,11 @@ @@ -147,6 +146,13 @@ function handleVerify(data: { code: string }) { +