未验证 提交 497498db 编写于 作者: 智布道's avatar 智布道 👁 提交者: GitHub

Merge pull request #134 from verils/remove-provider-specific-properties

把Alipay独有的配置从AuthConfig里提取出来
...@@ -38,7 +38,10 @@ public class AuthConfig { ...@@ -38,7 +38,10 @@ public class AuthConfig {
/** /**
* 支付宝公钥:当选择支付宝登录时,该值可用 * 支付宝公钥:当选择支付宝登录时,该值可用
* 对应“RSA2(SHA256)密钥”中的“支付宝公钥” * 对应“RSA2(SHA256)密钥”中的“支付宝公钥”
*
* @deprecated 请使用AuthAlipayRequest的构造方法设置"alipayPublicKey"
*/ */
@Deprecated
private String alipayPublicKey; private String alipayPublicKey;
/** /**
......
...@@ -18,6 +18,8 @@ import me.zhyd.oauth.model.AuthCallback; ...@@ -18,6 +18,8 @@ import me.zhyd.oauth.model.AuthCallback;
import me.zhyd.oauth.model.AuthResponse; import me.zhyd.oauth.model.AuthResponse;
import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthToken;
import me.zhyd.oauth.model.AuthUser; 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.StringUtils;
import me.zhyd.oauth.utils.UrlBuilder; import me.zhyd.oauth.utils.UrlBuilder;
...@@ -31,31 +33,115 @@ import java.net.InetSocketAddress; ...@@ -31,31 +33,115 @@ import java.net.InetSocketAddress;
*/ */
public class AuthAlipayRequest extends AuthDefaultRequest { 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) { public AuthAlipayRequest(AuthConfig config) {
super(config, AuthDefaultSource.ALIPAY); this(config, (String) null);
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config
.getAlipayPublicKey(), "RSA2");
} }
/**
* @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) { 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); super(config, AuthDefaultSource.ALIPAY, authStateCache);
this.alipayPublicKey = determineAlipayPublicKey(alipayPublicKey, config);
if (config.getHttpConfig() != null && config.getHttpConfig().getProxy() != null if (config.getHttpConfig() != null && config.getHttpConfig().getProxy() != null
&& config.getHttpConfig().getProxy().address() instanceof InetSocketAddress) { && config.getHttpConfig().getProxy().address() instanceof InetSocketAddress) {
InetSocketAddress address = (InetSocketAddress) config.getHttpConfig().getProxy().address(); InetSocketAddress address = (InetSocketAddress) config.getHttpConfig().getProxy().address();
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), 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 { } else {
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(),
"json", "UTF-8", config.getAlipayPublicKey(), "RSA2"); "json", "UTF-8", alipayPublicKey, "RSA2");
} }
check(config);
} }
public AuthAlipayRequest(AuthConfig config, AuthStateCache authStateCache, String proxyHost, Integer proxyPort) { /**
* 构造方法,需要设置"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); super(config, AuthDefaultSource.ALIPAY, authStateCache);
this.alipayPublicKey = determineAlipayPublicKey(alipayPublicKey, config);
this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), this.alipayClient = new DefaultAlipayClient(AuthDefaultSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(),
"json", "UTF-8", config.getAlipayPublicKey(), "RSA2", proxyHost, proxyPort); "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);
}
}
@Override
protected void checkCode(AuthCallback authCallback) {
if (StringUtils.isEmpty(authCallback.getAuth_code())) {
throw new AuthException(AuthResponseStatus.ILLEGAL_CODE, source);
}
} }
@Override @Override
......
...@@ -72,7 +72,7 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -72,7 +72,7 @@ public abstract class AuthDefaultRequest implements AuthRequest {
@Override @Override
public AuthResponse login(AuthCallback authCallback) { public AuthResponse login(AuthCallback authCallback) {
try { try {
AuthChecker.checkCode(source, authCallback); checkCode(authCallback);
if (!config.isIgnoreCheckState()) { if (!config.isIgnoreCheckState()) {
AuthChecker.checkState(authCallback.getState(), source, authStateCache); AuthChecker.checkState(authCallback.getState(), source, authStateCache);
} }
...@@ -86,6 +86,10 @@ public abstract class AuthDefaultRequest implements AuthRequest { ...@@ -86,6 +86,10 @@ public abstract class AuthDefaultRequest implements AuthRequest {
} }
} }
protected void checkCode(AuthCallback authCallback) {
AuthChecker.checkCode(source, authCallback);
}
/** /**
* 处理{@link AuthDefaultRequest#login(AuthCallback)} 发生异常的情况,统一响应参数 * 处理{@link AuthDefaultRequest#login(AuthCallback)} 发生异常的情况,统一响应参数
* *
......
...@@ -27,9 +27,6 @@ public class AuthChecker { ...@@ -27,9 +27,6 @@ public class AuthChecker {
public static boolean isSupportedAuth(AuthConfig config, AuthSource source) { public static boolean isSupportedAuth(AuthConfig config, AuthSource source) {
boolean isSupported = StringUtils.isNotEmpty(config.getClientId()) boolean isSupported = StringUtils.isNotEmpty(config.getClientId())
&& StringUtils.isNotEmpty(config.getClientSecret()); && StringUtils.isNotEmpty(config.getClientSecret());
if (isSupported && AuthDefaultSource.ALIPAY == source) {
isSupported = StringUtils.isNotEmpty(config.getAlipayPublicKey());
}
if (isSupported && AuthDefaultSource.STACK_OVERFLOW == source) { if (isSupported && AuthDefaultSource.STACK_OVERFLOW == source) {
isSupported = StringUtils.isNotEmpty(config.getStackOverflowKey()); isSupported = StringUtils.isNotEmpty(config.getStackOverflowKey());
} }
...@@ -71,18 +68,13 @@ public class AuthChecker { ...@@ -71,18 +68,13 @@ public class AuthChecker {
// Facebook's redirect uri must use the HTTPS protocol // Facebook's redirect uri must use the HTTPS protocol
throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source); 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 // 微软的回调地址必须为https的链接或者localhost,不允许使用http
if(AuthDefaultSource.MICROSOFT== source && !GlobalAuthUtils.isHttpsProtocolOrLocalHost(redirectUri) ){ if (AuthDefaultSource.MICROSOFT == source && !GlobalAuthUtils.isHttpsProtocolOrLocalHost(redirectUri)) {
// Microsoft's redirect uri must use the HTTPS or localhost // Microsoft's redirect uri must use the HTTPS or localhost
throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source); throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
} }
// 微软中国的回调地址必须为https的链接或者localhost,不允许使用http // 微软中国的回调地址必须为https的链接或者localhost,不允许使用http
if(AuthDefaultSource.MICROSOFT_CN== source && !GlobalAuthUtils.isHttpsProtocolOrLocalHost(redirectUri) ){ if (AuthDefaultSource.MICROSOFT_CN == source && !GlobalAuthUtils.isHttpsProtocolOrLocalHost(redirectUri)) {
// Microsoft's redirect uri must use the HTTPS or localhost // Microsoft's redirect uri must use the HTTPS or localhost
throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source); throw new AuthException(AuthResponseStatus.ILLEGAL_REDIRECT_URI, source);
} }
...@@ -103,9 +95,7 @@ public class AuthChecker { ...@@ -103,9 +95,7 @@ public class AuthChecker {
return; return;
} }
String code = callback.getCode(); String code = callback.getCode();
if (source == AuthDefaultSource.ALIPAY) { if (source == AuthDefaultSource.HUAWEI) {
code = callback.getAuth_code();
} else if (source == AuthDefaultSource.HUAWEI) {
code = callback.getAuthorization_code(); code = callback.getAuthorization_code();
} }
if (StringUtils.isEmpty(code)) { if (StringUtils.isEmpty(code)) {
......
...@@ -3,10 +3,7 @@ package me.zhyd.oauth; ...@@ -3,10 +3,7 @@ package me.zhyd.oauth;
import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.config.AuthExtendSource; import me.zhyd.oauth.config.AuthExtendSource;
import me.zhyd.oauth.request.AuthExtendRequest; import me.zhyd.oauth.request.*;
import me.zhyd.oauth.request.AuthGiteeRequest;
import me.zhyd.oauth.request.AuthGithubRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.AuthStateUtils; import me.zhyd.oauth.utils.AuthStateUtils;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
...@@ -75,31 +72,39 @@ public class AuthRequestBuilderTest { ...@@ -75,31 +72,39 @@ public class AuthRequestBuilderTest {
*/ */
@Test @Test
public void build4() { public void build4() {
AuthConfig config = AuthConfig.builder()
.clientId("a")
.clientSecret("a")
.redirectUri("https://www.justauth.cn")
.authServerId("asd")
.agentId("asd")
.domainPrefix("asd")
.stackOverflowKey("asd")
.deviceId("asd")
.clientOsType(3)
.build();
for (AuthDefaultSource value : AuthDefaultSource.values()) { for (AuthDefaultSource value : AuthDefaultSource.values()) {
if (value == AuthDefaultSource.TWITTER) { switch (value) {
System.out.println(value.getTargetClass()); case TWITTER:
System.out.println("忽略 twitter"); System.out.println(value.getTargetClass());
continue; 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()));
} }
AuthRequest authRequest = AuthRequestBuilder.builder()
.source(value.getName())
.authConfig(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();
System.out.println(value.getTargetClass());
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
} }
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册