1
0
mirror of synced 2026-05-22 21:53:18 +00:00
Files
JustAuth/src/test/java/me/zhyd/oauth/AuthRequestBuilderTest.java
T

127 lines
4.6 KiB
Java
Raw Normal View History

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.*;
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() {
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)
2024-08-04 15:51:38 +08:00
.kid("kid")
.teamId("teamid")
2024-08-17 20:16:14 +08:00
.ignoreCheckState(true)
.ignoreCheckRedirectUri(true)
.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;
}
2024-08-17 20:16:14 +08:00
case WECHAT_MINI_PROGRAM: {
// 小程序不支持获取调用 authorize
AuthRequest authRequest = new AuthWechatMiniProgramRequest(config);
System.out.println(value.getTargetClass());
continue;
}
2024-10-17 09:52:33 +08:00
case QQ_MINI_PROGRAM: {
// 小程序不支持获取调用 authorize
AuthRequest authRequest = new AuthQQMiniProgramRequest(config);
System.out.println(value.getTargetClass());
continue;
}
default:
AuthRequest authRequest = AuthRequestBuilder.builder()
.source(value.getName())
.authConfig(config)
.build();
System.out.println(value.getTargetClass());
System.out.println(authRequest.authorize(AuthStateUtils.createState()));
}
}
}
}