mirror of
https://gitee.com/yadong.zhang/JustAuth.git
synced 2025-12-06 08:48:27 +08:00
把第三方服务独有的配置从AuthConfig里提取出来
This commit is contained in:
parent
2de0ad5013
commit
4c205a9957
@ -38,7 +38,10 @@ public class AuthConfig {
|
||||
/**
|
||||
* 支付宝公钥:当选择支付宝登录时,该值可用
|
||||
* 对应“RSA2(SHA256)密钥”中的“支付宝公钥”
|
||||
*
|
||||
* @deprecated 请使用AuthAlipayRequest的构造方法设置"alipayPublicKey"
|
||||
*/
|
||||
@Deprecated
|
||||
private String alipayPublicKey;
|
||||
|
||||
/**
|
||||
|
||||
@ -18,6 +18,8 @@ import me.zhyd.oauth.model.AuthCallback;
|
||||
import me.zhyd.oauth.model.AuthResponse;
|
||||
import me.zhyd.oauth.model.AuthToken;
|
||||
import me.zhyd.oauth.model.AuthUser;
|
||||
import me.zhyd.oauth.utils.AuthChecker;
|
||||
import me.zhyd.oauth.utils.GlobalAuthUtils;
|
||||
import me.zhyd.oauth.utils.StringUtils;
|
||||
import me.zhyd.oauth.utils.UrlBuilder;
|
||||
|
||||
@ -31,31 +33,115 @@ import java.net.InetSocketAddress;
|
||||
*/
|
||||
public class AuthAlipayRequest extends AuthDefaultRequest {
|
||||
|
||||
private AlipayClient alipayClient;
|
||||
/**
|
||||
* 支付宝公钥:当选择支付宝登录时,该值可用
|
||||
* 对应“RSA2(SHA256)密钥”中的“支付宝公钥”
|
||||
*/
|
||||
private final String alipayPublicKey;
|
||||
|
||||
private final AlipayClient alipayClient;
|
||||
|
||||
/**
|
||||
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, java.lang.String)
|
||||
* @deprecated 请使用带有"alipayPublicKey"参数的构造方法
|
||||
*/
|
||||
@Deprecated
|
||||
public AuthAlipayRequest(AuthConfig config) {
|
||||
super(config, AuthDefaultSource.ALIPAY);
|
||||
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config
|
||||
.getAlipayPublicKey(), "RSA2");
|
||||
this(config, (String) null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, java.lang.String, me.zhyd.oauth.cache.AuthStateCache)
|
||||
* @deprecated 请使用带有"alipayPublicKey"参数的构造方法
|
||||
*/
|
||||
@Deprecated
|
||||
public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache) {
|
||||
this(config, null, authStateCache);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, java.lang.String, me.zhyd.oauth.cache.AuthStateCache, java.lang.String, java.lang.Integer)
|
||||
* @deprecated 请使用带有"alipayPublicKey"参数的构造方法
|
||||
*/
|
||||
@Deprecated
|
||||
public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache, String proxyHost, Integer proxyPort) {
|
||||
this(config, null, authStateCache, proxyHost, proxyPort);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造方法,需要设置"alipayPublicKey"
|
||||
*
|
||||
* @param config 公共的OAuth配置
|
||||
* @param alipayPublicKey 支付宝公钥
|
||||
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig)
|
||||
*/
|
||||
public AuthAlipayRequest(AuthConfig config, String alipayPublicKey) {
|
||||
super(config, AuthDefaultSource.ALIPAY);
|
||||
this.alipayPublicKey = determineAlipayPublicKey(alipayPublicKey, config);
|
||||
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", alipayPublicKey, "RSA2");
|
||||
check(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造方法,需要设置"alipayPublicKey"
|
||||
*
|
||||
* @param config 公共的OAuth配置
|
||||
* @param alipayPublicKey 支付宝公钥
|
||||
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, me.zhyd.oauth.cache.AuthStateCache)
|
||||
*/
|
||||
public AuthAlipayRequest(AuthConfig config, String alipayPublicKey, AuthStateCache authStateCache) {
|
||||
super(config, AuthDefaultSource.ALIPAY, authStateCache);
|
||||
this.alipayPublicKey = determineAlipayPublicKey(alipayPublicKey, config);
|
||||
if (config.getHttpConfig() != null && config.getHttpConfig().getProxy() != null
|
||||
&& config.getHttpConfig().getProxy().address() instanceof InetSocketAddress) {
|
||||
InetSocketAddress address = (InetSocketAddress) config.getHttpConfig().getProxy().address();
|
||||
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(),
|
||||
"json", "UTF-8", config.getAlipayPublicKey(), "RSA2", address.getHostName(), address.getPort());
|
||||
"json", "UTF-8", alipayPublicKey, "RSA2", address.getHostName(), address.getPort());
|
||||
} else {
|
||||
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(),
|
||||
"json", "UTF-8", config.getAlipayPublicKey(), "RSA2");
|
||||
"json", "UTF-8", alipayPublicKey, "RSA2");
|
||||
}
|
||||
check(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造方法,需要设置"alipayPublicKey"
|
||||
*
|
||||
* @param config 公共的OAuth配置
|
||||
* @param alipayPublicKey 支付宝公钥
|
||||
* @see AuthAlipayRequest#AuthAlipayRequest(me.zhyd.oauth.config.AuthConfig, me.zhyd.oauth.cache.AuthStateCache, java.lang.String, java.lang.Integer)
|
||||
*/
|
||||
public AuthAlipayRequest(AuthConfig config, String alipayPublicKey, AuthStateCache authStateCache, String proxyHost, Integer proxyPort) {
|
||||
super(config, AuthDefaultSource.ALIPAY, authStateCache);
|
||||
this.alipayPublicKey = determineAlipayPublicKey(alipayPublicKey, config);
|
||||
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(),
|
||||
"json", "UTF-8", alipayPublicKey, "RSA2", proxyHost, proxyPort);
|
||||
check(config);
|
||||
}
|
||||
|
||||
private String determineAlipayPublicKey(String alipayPublicKey, AuthConfig config) {
|
||||
return alipayPublicKey != null ? alipayPublicKey : config.getAlipayPublicKey();
|
||||
}
|
||||
|
||||
protected void check(AuthConfig config) {
|
||||
AuthChecker.checkConfig(config, AuthDefaultSource.ALIPAY);
|
||||
|
||||
if (!StringUtils.isNotEmpty(alipayPublicKey)) {
|
||||
throw new AuthException(AuthResponseStatus.PARAMETER_INCOMPLETE, AuthDefaultSource.ALIPAY);
|
||||
}
|
||||
|
||||
// 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1
|
||||
if (GlobalAuthUtils.isLocalHost(config.getRedirectUri())) {
|
||||
// The redirect uri of alipay is forbidden to use localhost or 127.0.0.1
|
||||
throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, AuthDefaultSource.ALIPAY);
|
||||
}
|
||||
}
|
||||
|
||||
public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache, String proxyHost, Integer proxyPort) {
|
||||
super(config, AuthDefaultSource.ALIPAY, authStateCache);
|
||||
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(),
|
||||
"json", "UTF-8", config.getAlipayPublicKey(), "RSA2", proxyHost, proxyPort);
|
||||
@Override
|
||||
protected void checkCode(AuthCallback authCallback) {
|
||||
if (StringUtils.isEmpty(authCallback.getAuth_code())) {
|
||||
throw new AuthException(AuthResponseStatus.ILLEGAL_CODE, source);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -72,7 +72,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
|
||||
@Override
|
||||
public AuthResponse login(AuthCallback authCallback) {
|
||||
try {
|
||||
AuthChecker.checkCode(source, authCallback);
|
||||
checkCode(authCallback);
|
||||
if (!config.isIgnoreCheckState()) {
|
||||
AuthChecker.checkState(authCallback.getState(), source, authStateCache);
|
||||
}
|
||||
@ -86,6 +86,10 @@ public abstract class AuthDefaultRequest implements AuthRequest {
|
||||
}
|
||||
}
|
||||
|
||||
protected void checkCode(AuthCallback authCallback) {
|
||||
AuthChecker.checkCode(source, authCallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理{@link AuthDefaultRequest#login(AuthCallback)} 发生异常的情况,统一响应参数
|
||||
*
|
||||
|
||||
@ -27,9 +27,6 @@ public class AuthChecker {
|
||||
public static boolean isSupportedAuth(AuthConfig config, AuthSource source) {
|
||||
boolean isSupported = StringUtils.isNotEmpty(config.getClientId())
|
||||
&& StringUtils.isNotEmpty(config.getClientSecret());
|
||||
if (isSupported && AuthDefaultSource.ALIPAY == source) {
|
||||
isSupported = StringUtils.isNotEmpty(config.getAlipayPublicKey());
|
||||
}
|
||||
if (isSupported && AuthDefaultSource.STACK_OVERFLOW == source) {
|
||||
isSupported = StringUtils.isNotEmpty(config.getStackOverflowKey());
|
||||
}
|
||||
@ -71,11 +68,6 @@ public class AuthChecker {
|
||||
// Facebook's redirect uri must use the HTTPS protocol
|
||||
throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
|
||||
}
|
||||
// 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1
|
||||
if (AuthDefaultSource.ALIPAY == source && GlobalAuthUtils.isLocalHost(redirectUri)) {
|
||||
// The redirect uri of alipay is forbidden to use localhost or 127.0.0.1
|
||||
throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
|
||||
}
|
||||
// 微软的回调地址必须为https的链接或者localhost,不允许使用http
|
||||
if (AuthDefaultSource.MICROSOFT == source && !GlobalAuthUtils.isHttpsProtocolOrLocalHost(redirectUri)) {
|
||||
// Microsoft's redirect uri must use the HTTPS or localhost
|
||||
@ -103,9 +95,7 @@ public class AuthChecker {
|
||||
return;
|
||||
}
|
||||
String code = callback.getCode();
|
||||
if (source == AuthDefaultSource.ALIPAY) {
|
||||
code = callback.getAuth_code();
|
||||
} else if (source == AuthDefaultSource.HUAWEI) {
|
||||
if (source == AuthDefaultSource.HUAWEI) {
|
||||
code = callback.getAuthorization_code();
|
||||
}
|
||||
if (StringUtils.isEmpty(code)) {
|
||||
|
||||
@ -3,10 +3,7 @@ package me.zhyd.oauth;
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.config.AuthDefaultSource;
|
||||
import me.zhyd.oauth.config.AuthExtendSource;
|
||||
import me.zhyd.oauth.request.AuthExtendRequest;
|
||||
import me.zhyd.oauth.request.AuthGiteeRequest;
|
||||
import me.zhyd.oauth.request.AuthGithubRequest;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
import me.zhyd.oauth.request.*;
|
||||
import me.zhyd.oauth.utils.AuthStateUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
@ -75,31 +72,39 @@ public class AuthRequestBuilderTest {
|
||||
*/
|
||||
@Test
|
||||
public void build4() {
|
||||
for (AuthDefaultSource value : AuthDefaultSource.values()) {
|
||||
if (value == AuthDefaultSource.TWITTER) {
|
||||
System.out.println(value.getTargetClass());
|
||||
System.out.println("忽略 twitter");
|
||||
continue;
|
||||
}
|
||||
AuthRequest authRequest = AuthRequestBuilder.builder()
|
||||
.source(value.getName())
|
||||
.authConfig(AuthConfig.builder()
|
||||
AuthConfig config = AuthConfig.builder()
|
||||
.clientId("a")
|
||||
.clientSecret("a")
|
||||
.redirectUri("https://www.justauth.cn")
|
||||
.alipayPublicKey("asd")
|
||||
.authServerId("asd")
|
||||
.agentId("asd")
|
||||
.domainPrefix("asd")
|
||||
.stackOverflowKey("asd")
|
||||
|
||||
.deviceId("asd")
|
||||
.clientOsType(3)
|
||||
.build())
|
||||
.build();
|
||||
|
||||
for (AuthDefaultSource value : AuthDefaultSource.values()) {
|
||||
switch (value) {
|
||||
case TWITTER:
|
||||
System.out.println(value.getTargetClass());
|
||||
System.out.println("忽略 twitter");
|
||||
continue;
|
||||
case ALIPAY: {
|
||||
// 单独给Alipay执行测试
|
||||
AuthRequest authRequest = new AuthAlipayRequest(config, "asd");
|
||||
System.out.println(value.getTargetClass());
|
||||
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
|
||||
continue;
|
||||
}
|
||||
default:
|
||||
AuthRequest authRequest = AuthRequestBuilder.builder()
|
||||
.source(value.getName())
|
||||
.authConfig(config)
|
||||
.build();
|
||||
System.out.println(value.getTargetClass());
|
||||
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user