AuthRequestBuilderTest.java 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 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()));
        }

    }
}