✨ 添加 AuthRequestBuilder 可以便捷的创建 AuthRequest
This commit is contained in:
105
src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
Normal file
105
src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
Normal file
@@ -0,0 +1,105 @@
|
||||
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.utils.AuthStateUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AuthRequestBuilderTest {
|
||||
|
||||
/**
|
||||
* 示例:一般场景下通过 AuthRequestBuilder 构建 AuthRequest
|
||||
*/
|
||||
@Test
|
||||
public void build2() {
|
||||
AuthRequest authRequest = AuthRequestBuilder.builder()
|
||||
.source("github")
|
||||
.authConfig(AuthConfig.builder()
|
||||
.clientId("a")
|
||||
.clientSecret("a")
|
||||
.redirectUri("https://www.justauth.cn")
|
||||
.build())
|
||||
.build();
|
||||
Assert.assertTrue(authRequest instanceof AuthGithubRequest);
|
||||
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例:AuthConfig 需要动态获取的场景下通过 AuthRequestBuilder 构建 AuthRequest
|
||||
*/
|
||||
@Test
|
||||
public void build() {
|
||||
AuthRequest authRequest = AuthRequestBuilder.builder()
|
||||
.source("gitee")
|
||||
.authConfig((source) -> {
|
||||
// 通过 source 动态获取 AuthConfig
|
||||
// 此处可以灵活的从 sql 中取配置也可以从配置文件中取配置
|
||||
return AuthConfig.builder()
|
||||
.clientId(source)
|
||||
.clientSecret(source)
|
||||
.redirectUri("https://www.justauth.cn/" + source)
|
||||
.build();
|
||||
})
|
||||
.build();
|
||||
Assert.assertTrue(authRequest instanceof AuthGiteeRequest);
|
||||
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 示例:自定义实现的 AuthRequest,通过 AuthRequestBuilder 构建 AuthRequest
|
||||
*/
|
||||
@Test
|
||||
public void build3() {
|
||||
AuthRequest authRequest = AuthRequestBuilder.builder()
|
||||
// 关键点:将自定义的 AuthSource 配置上
|
||||
.extendSource(AuthExtendSource.values())
|
||||
.source("other")
|
||||
.authConfig(AuthConfig.builder()
|
||||
.clientId("a")
|
||||
.clientSecret("a")
|
||||
.redirectUri("https://www.justauth.cn")
|
||||
.build())
|
||||
.build();
|
||||
Assert.assertTrue(authRequest instanceof AuthExtendRequest);
|
||||
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试不同平台
|
||||
*/
|
||||
@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()
|
||||
.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()));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
package me.zhyd.oauth.config;
|
||||
|
||||
import me.zhyd.oauth.cache.AuthStateCache;
|
||||
import me.zhyd.oauth.exception.AuthException;
|
||||
import me.zhyd.oauth.request.AuthDefaultRequest;
|
||||
import me.zhyd.oauth.request.AuthExtendRequest;
|
||||
import me.zhyd.oauth.request.AuthRequest;
|
||||
|
||||
/**
|
||||
* 测试自定义实现{@link AuthSource}接口后的枚举类
|
||||
@@ -15,7 +12,7 @@ import me.zhyd.oauth.request.AuthRequest;
|
||||
*/
|
||||
public enum AuthExtendSource implements AuthSource {
|
||||
|
||||
OTHER (AuthExtendRequest.class){
|
||||
OTHER {
|
||||
/**
|
||||
* 授权的api
|
||||
*
|
||||
@@ -65,27 +62,10 @@ public enum AuthExtendSource implements AuthSource {
|
||||
public String refresh() {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
private Class<? extends AuthDefaultRequest> targetClass;
|
||||
|
||||
AuthExtendSource(Class<? extends AuthDefaultRequest> targetClass) {
|
||||
this.targetClass = targetClass;
|
||||
}
|
||||
|
||||
public AuthRequest getAuthRequestInstance(AuthConfig authConfig) {
|
||||
return this.getAuthRequestInstance(authConfig,null);
|
||||
}
|
||||
|
||||
public AuthRequest getAuthRequestInstance(AuthConfig authConfig, AuthStateCache authStateCache) {
|
||||
try {
|
||||
if(authStateCache==null){
|
||||
return this.targetClass.getDeclaredConstructor(AuthConfig.class).newInstance(authConfig);
|
||||
}else{
|
||||
return this.targetClass.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class).newInstance(authConfig, authStateCache);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new AuthException("未获取到有效的Auth配置");
|
||||
@Override
|
||||
public Class<? extends AuthDefaultRequest> getTargetClass() {
|
||||
return AuthExtendRequest.class;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package me.zhyd.oauth.request;
|
||||
|
||||
import me.zhyd.oauth.config.AuthConfig;
|
||||
import me.zhyd.oauth.config.AuthDefaultSource;
|
||||
import me.zhyd.oauth.utils.AuthStateUtils;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AuthWeChatMpRequestTest {
|
||||
@@ -18,15 +16,4 @@ public class AuthWeChatMpRequestTest {
|
||||
.build());
|
||||
System.out.println(request.authorize(AuthStateUtils.createState()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authorize1() {
|
||||
AuthRequest request = AuthDefaultSource.getAuthSource("wechat_mp").getAuthRequestInstance(AuthConfig.builder()
|
||||
.clientId("a")
|
||||
.clientSecret("a")
|
||||
.redirectUri("https://www.justauth.cn")
|
||||
.build());
|
||||
Assert.assertTrue(request instanceof AuthWeChatMpRequest);
|
||||
System.out.println(request.authorize(AuthStateUtils.createState()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user