Skip to content

Commit

Permalink
♻️ autoreg split to check and do #161
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Dec 12, 2023
1 parent 0bb193e commit c272b41
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -19,7 +20,7 @@
public class JustAuthUserDetailsCombo extends DefaultUserDetailsCombo {

@Override
public boolean authed(Enum<?> authType) {
public boolean asAuthed(@NotNull DefaultWingsUserDetails details) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public final DefaultWingsUserDetails loadOrNull(String username, @NotNull Enum<?

Details dt = doLoad(username, authType, authDetail);
boolean at = false;
if (dt == null && autoRegisterType.contains(authType)) {
dt = autoreg(username, authType, authDetail);
if (dt == null && canRegister(username, authType, authDetail)) {
dt = doRegister(username, authType, authDetail);
if (dt != null) {
at = true;
EventPublishHelper.SyncSpring.publishEvent(new WarlockAutoRegisterEvent(dt));
Expand All @@ -66,7 +66,7 @@ public final DefaultWingsUserDetails loadOrNull(String username, @NotNull Enum<?
warlockAuthzService.auth(wud);
if (!wud.isPreAuthed()) {
// If there is no pre-authentication, auto register consider as logged in.
wud.setPreAuthed(at || authed(authType));
wud.setPreAuthed(at || asAuthed(wud));
}

return wud;
Expand All @@ -76,14 +76,26 @@ protected DefaultWingsUserDetails newUserDetails(@NotNull Details dt) {
return new DefaultWingsUserDetails();
}

protected Details autoreg(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;
}

Expand Down

0 comments on commit c272b41

Please sign in to comment.