106 lines
3.7 KiB
Java
106 lines
3.7 KiB
Java
|
|
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()));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|