yangys
2024-04-01 86cdd920b68274185233383f69ddb974052b6b6f
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
package com.qianwen.core.social.utils;
 
import java.util.Arrays;
import java.util.function.Function;
import me.zhyd.oauth.cache.AuthStateCache;
import me.zhyd.oauth.config.AuthConfig;
import me.zhyd.oauth.config.AuthDefaultSource;
import me.zhyd.oauth.config.AuthSource;
import me.zhyd.oauth.enums.AuthResponseStatus;
import me.zhyd.oauth.exception.AuthException;
import me.zhyd.oauth.request.AuthDefaultRequest;
import me.zhyd.oauth.request.AuthRequest;
import me.zhyd.oauth.utils.StringUtils;
 
/* loaded from: blade-starter-social-9.3.2.0-SNAPSHOT.jar:org/springblade/core/social/utils/AuthBuilder.class */
public class AuthBuilder {
    private String source;
    private AuthConfig authConfig;
    private AuthStateCache authStateCache;
    private AuthSource[] extendSource;
 
    private AuthBuilder() {
    }
 
    public static AuthBuilder builder() {
        return new AuthBuilder();
    }
 
    public AuthBuilder source(String source) {
        this.source = source;
        return this;
    }
 
    public AuthBuilder authConfig(AuthConfig authConfig) {
        this.authConfig = authConfig;
        return this;
    }
 
    public AuthBuilder authConfig(Function<String, AuthConfig> authConfig) {
        this.authConfig = authConfig.apply(this.source);
        return this;
    }
 
    public AuthBuilder authStateCache(AuthStateCache authStateCache) {
        this.authStateCache = authStateCache;
        return this;
    }
 
    public AuthBuilder extendSource(AuthSource... extendSource) {
        this.extendSource = extendSource;
        return this;
    }
 
    public AuthRequest build() {
        if (StringUtils.isEmpty(this.source) || null == this.authConfig) {
            throw new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
        }
        AuthSource[] sources = concat(AuthDefaultSource.values(), this.extendSource);
        AuthSource source = (AuthSource) Arrays.stream(sources).distinct().filter(authSource -> {
            return authSource.getName().equalsIgnoreCase(this.source);
        }).reduce((a, b) -> {
            return b;
        }).orElseThrow(() -> {
            return new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
        });
        Class<? extends AuthDefaultRequest> targetClass = source.getTargetClass();
        if (null == targetClass) {
            throw new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
        }
        try {
            return this.authStateCache == null ? (AuthRequest) targetClass.getDeclaredConstructor(AuthConfig.class).newInstance(this.authConfig) : (AuthRequest) targetClass.getDeclaredConstructor(AuthConfig.class, AuthStateCache.class).newInstance(this.authConfig, this.authStateCache);
        } catch (Exception e) {
            e.printStackTrace();
            throw new AuthException(AuthResponseStatus.NOT_IMPLEMENTED);
        }
    }
 
    private AuthSource[] concat(AuthSource[] first, AuthSource[] second) {
        if (null == second || second.length == 0) {
            return first;
        }
        AuthSource[] result = new AuthSource[first.length + second.length];
        System.arraycopy(first, 0, result, 0, first.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }
}