package com.qianwen.core.social.utils;
|
|
import java.util.Arrays;
|
import java.util.function.Function;
|
import me.zhyd.oauth.cache.AuthStateCache;
|
import me.zhyd.oauth.config.AuthConfig;
|
import me.zhyd.oauth.config.AuthDefaultSource;
|
import me.zhyd.oauth.config.AuthSource;
|
import me.zhyd.oauth.enums.AuthResponseStatus;
|
import me.zhyd.oauth.exception.AuthException;
|
import me.zhyd.oauth.request.AuthDefaultRequest;
|
import me.zhyd.oauth.request.AuthRequest;
|
import me.zhyd.oauth.utils.StringUtils;
|
|
/* loaded from: blade-starter-social-9.3.2.0-SNAPSHOT.jar:org/springblade/core/social/utils/AuthBuilder.class */
|
public class AuthBuilder {
|
private String source;
|
private AuthConfig authConfig;
|
private AuthStateCache authStateCache;
|
private AuthSource[] extendSource;
|
|
private AuthBuilder() {
|
}
|
|
public static AuthBuilder builder() {
|
return new AuthBuilder();
|
}
|
|
public AuthBuilder source(String source) {
|
this.source = source;
|
return this;
|
}
|
|
public AuthBuilder authConfig(AuthConfig authConfig) {
|
this.authConfig = authConfig;
|
return this;
|
}
|
|
public AuthBuilder authConfig(Function<String, AuthConfig> authConfig) {
|
this.authConfig = authConfig.apply(this.source);
|
return this;
|
}
|
|
public AuthBuilder authStateCache(AuthStateCache authStateCache) {
|
this.authStateCache = authStateCache;
|
return this;
|
}
|
|
public AuthBuilder extendSource(AuthSource... extendSource) {
|
this.extendSource = extendSource;
|
return this;
|
}
|
|
public AuthRequest build() {
|
if (StringUtils.isEmpty(this.source) || null == this.authConfig) {
|
throw new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
|
}
|
AuthSource[] sources = concat(AuthDefaultSource.values(), this.extendSource);
|
AuthSource source = (AuthSource) Arrays.stream(sources).distinct().filter(authSource -> {
|
return authSource.getName().equalsIgnoreCase(this.source);
|
}).reduce((a, b) -> {
|
return b;
|
}).orElseThrow(() -> {
|
return new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
|
});
|
Class<? extends AuthDefaultRequest> targetClass = source.getTargetClass();
|
if (null == targetClass) {
|
throw new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
|
}
|
try {
|
return this.authStateCache == null ? (AuthRequest) targetClass.getDeclaredConstructor(AuthConfig.class).newInstance(this.authConfig) : (AuthRequest) targetClass.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class).newInstance(this.authConfig, this.authStateCache);
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
|
}
|
}
|
|
private AuthSource[] concat(AuthSource[] first, AuthSource[] second) {
|
if (null == second || second.length == 0) {
|
return first;
|
}
|
AuthSource[] result = new AuthSource[first.length + second.length];
|
System.arraycopy(first, 0, result, 0, first.length);
|
System.arraycopy(second, 0, result, first.length, second.length);
|
return result;
|
}
|
}
|