From c272b418391197f09eb5e215e8a7735d645f3dc3 Mon Sep 17 00:00:00 2001 From: trydofor Date: Tue, 12 Dec 2023 11:22:26 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20autoreg=20split=20to=20che?= =?UTF-8?q?ck=20and=20do=20#161?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../userdetails/JustAuthUserAuthnAutoReg.java | 7 ++-- .../userdetails/JustAuthUserDetailsCombo.java | 3 +- .../auth/impl/DefaultUserAuthnAutoReg.java | 34 +++++++++++++------ .../auth/impl/DefaultUserDetailsCombo.java | 24 +++++++++---- 4 files changed, 48 insertions(+), 20 deletions(-) diff --git a/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/security/userdetails/JustAuthUserAuthnAutoReg.java b/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/security/userdetails/JustAuthUserAuthnAutoReg.java index 298a317fd..262fdbc88 100644 --- a/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/security/userdetails/JustAuthUserAuthnAutoReg.java +++ b/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/security/userdetails/JustAuthUserAuthnAutoReg.java @@ -23,7 +23,7 @@ public class JustAuthUserAuthnAutoReg extends DefaultUserAuthnAutoReg { @Override - protected void beforeSave(Basis basis, String username, WingsAuthDetails details) { + protected Long beforeSave(Basis basis, String username, WingsAuthDetails details) { AuthUser user = (AuthUser) details.getRealData(); AssertArgs.notNull(user, "need JustAuth User"); basis.setNickname(user.getNickname()); @@ -41,16 +41,19 @@ else if (aug == AuthUserGender.MALE) { basis.setRemark(user.getRemark()); basis.setStatus(UserStatus.ACTIVE); log.debug("nickName={}, Gender={}", user.getNickname(), aug); + + return null; } @Override - protected void beforeSave(Authn authn, String username, WingsAuthDetails details, long userId) { + protected Long beforeSave(Authn authn, String username, WingsAuthDetails details, long userId) { AuthUser user = (AuthUser) details.getRealData(); AssertArgs.notNull(user, "need JustAuth User"); authn.setUsername(user.getUuid()); authn.setExtraPara(JSON.toJSONString(user.getToken(), FastJsonHelper.DefaultWriter())); authn.setExtraUser(JSON.toJSONString(user.getRawUserInfo(), FastJsonHelper.DefaultWriter())); log.debug("uuid={}, userId={}", user.getUuid(), userId); + return null; } @Override diff --git a/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/security/userdetails/JustAuthUserDetailsCombo.java b/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/security/userdetails/JustAuthUserDetailsCombo.java index 2c0a70476..8491d635b 100644 --- a/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/security/userdetails/JustAuthUserDetailsCombo.java +++ b/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/security/userdetails/JustAuthUserDetailsCombo.java @@ -6,6 +6,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import pro.fessional.wings.slardar.security.WingsAuthDetails; +import pro.fessional.wings.slardar.security.impl.DefaultWingsUserDetails; import pro.fessional.wings.warlock.service.auth.WarlockAuthnService.Details; import pro.fessional.wings.warlock.service.auth.impl.DefaultUserDetailsCombo; @@ -19,7 +20,7 @@ public class JustAuthUserDetailsCombo extends DefaultUserDetailsCombo { @Override - public boolean authed(Enum authType) { + public boolean asAuthed(@NotNull DefaultWingsUserDetails details) { return true; } diff --git a/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/service/auth/impl/DefaultUserAuthnAutoReg.java b/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/service/auth/impl/DefaultUserAuthnAutoReg.java index fb852c12b..764029674 100644 --- a/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/service/auth/impl/DefaultUserAuthnAutoReg.java +++ b/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/service/auth/impl/DefaultUserAuthnAutoReg.java @@ -32,7 +32,7 @@ @Getter @Setter public class DefaultUserAuthnAutoReg implements ComboWarlockAuthnService.AutoReg { - public static final int ORDER = WingsOrdered. Lv3Service + 10; + public static final int ORDER = WingsOrdered.Lv3Service + 10; private int order = ORDER; @Setter(onMethod_ = {@Autowired}) @@ -62,10 +62,12 @@ public Details create(@NotNull Enum authType, String username, WingsAuthDetai user.setRemark("auto register"); user.setStatus(UserStatus.UNINIT); - beforeSave(user, username, details); - long uid = warlockUserBasisService.create(user); - log.debug("auto register user authType={}, username={}, userId={}", authType, username, uid); - afterSave(user, username, details, uid); + Long uid = beforeSave(user, username, details); + if (uid == null) { + uid = warlockUserBasisService.create(user); + log.debug("auto register user authType={}, username={}, userId={}", authType, username, uid); + afterSave(user, username, details, uid); + } // Authn authn = new Authn(); authn.setAuthType(authType); @@ -79,10 +81,12 @@ public Details create(@NotNull Enum authType, String username, WingsAuthDetai // Plain text, encrypt in WarlockUserAuthnService later. authn.setPassword(RandCode.human(16)); - beforeSave(authn, username, details, uid); - long aid = warlockUserAuthnService.create(uid, authn); - log.debug("auto register auth authType={}, username={}, authId={}", authType, username, aid); - afterSave(authn, username, details, uid, aid); + Long aid = beforeSave(authn, username, details, uid); + if (aid == null) { + aid = warlockUserAuthnService.create(uid, authn); + log.debug("auto register auth authType={}, username={}, authId={}", authType, username, aid); + afterSave(authn, username, details, uid, aid); + } final Details result = new Details(); result.setUserId(uid); @@ -101,13 +105,21 @@ public Details create(@NotNull Enum authType, String username, WingsAuthDetai }); } - protected void beforeSave(Basis basis, String username, WingsAuthDetails details) { + /** + * nonnull return value means existed user + */ + protected Long beforeSave(Basis basis, String username, WingsAuthDetails details) { + return null; } protected void afterSave(Basis basis, String username, WingsAuthDetails details, long userId) { } - protected void beforeSave(Authn authn, String username, WingsAuthDetails details, long userId) { + /** + * nonnull return value means existed authn + */ + protected Long beforeSave(Authn authn, String username, WingsAuthDetails details, long userId) { + return null; } protected void afterSave(Authn authn, String username, WingsAuthDetails details, long userId, long authId) { diff --git a/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/service/auth/impl/DefaultUserDetailsCombo.java b/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/service/auth/impl/DefaultUserDetailsCombo.java index 3cc7ad0c3..a2c1ccd15 100644 --- a/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/service/auth/impl/DefaultUserDetailsCombo.java +++ b/wings/warlock-shadow/src/main/java/pro/fessional/wings/warlock/service/auth/impl/DefaultUserDetailsCombo.java @@ -44,8 +44,8 @@ public final DefaultWingsUserDetails loadOrNull(String username, @NotNull Enum authType, @Nullable WingsAuthDetails authDetail) { + /** + * register user if it can be registered + * + * @see #canRegister(String, Enum, WingsAuthDetails) + */ + protected Details doRegister(String username, @NotNull Enum authType, @Nullable WingsAuthDetails authDetail) { return warlockAuthnService.register(authType, username, authDetail); } /** - * Whether pass the auth, default false + * can register if load null. + */ + protected boolean canRegister(String username, @NotNull Enum authType, @Nullable WingsAuthDetails authDetail) { + return autoRegisterType.contains(authType); + } + + /** + * Whether to pass the auth (preAuth), default false */ - public boolean authed(Enum authType) { + public boolean asAuthed(@NotNull DefaultWingsUserDetails details) { return false; }