|
| 1 | +/* |
| 2 | + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. |
| 3 | + * <p> |
| 4 | + * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * http://www.gnu.org/licenses/lgpl.html |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package top.continew.starter.auth.justauth; |
| 18 | + |
| 19 | +import cn.hutool.core.collection.CollUtil; |
| 20 | +import cn.hutool.core.util.EnumUtil; |
| 21 | +import cn.hutool.core.util.ReflectUtil; |
| 22 | +import cn.hutool.core.util.StrUtil; |
| 23 | +import com.xkcoding.http.config.HttpConfig; |
| 24 | +import me.zhyd.oauth.AuthRequestBuilder; |
| 25 | +import me.zhyd.oauth.cache.AuthStateCache; |
| 26 | +import me.zhyd.oauth.config.AuthConfig; |
| 27 | +import me.zhyd.oauth.config.AuthDefaultSource; |
| 28 | +import me.zhyd.oauth.config.AuthSource; |
| 29 | +import me.zhyd.oauth.enums.AuthResponseStatus; |
| 30 | +import me.zhyd.oauth.exception.AuthException; |
| 31 | +import me.zhyd.oauth.request.*; |
| 32 | +import top.continew.starter.auth.justauth.autoconfigure.JustAuthExtendProperties; |
| 33 | +import top.continew.starter.auth.justauth.autoconfigure.JustAuthHttpProperties; |
| 34 | +import top.continew.starter.auth.justauth.autoconfigure.JustAuthProperties; |
| 35 | + |
| 36 | +import java.net.InetSocketAddress; |
| 37 | +import java.net.Proxy; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | +/** |
| 42 | + * AuthRequest 工厂类 |
| 43 | + * |
| 44 | + * @author <a href="https://gitee.com/justauth/justauth-spring-boot-starter">yangkai.shen</a> |
| 45 | + * @author Charles7c |
| 46 | + * @since 2.15.0 |
| 47 | + */ |
| 48 | +public class AuthRequestFactory { |
| 49 | + |
| 50 | + private final JustAuthProperties properties; |
| 51 | + private final AuthStateCache stateCache; |
| 52 | + |
| 53 | + public AuthRequestFactory(JustAuthProperties properties, AuthStateCache stateCache) { |
| 54 | + this.properties = properties; |
| 55 | + this.stateCache = stateCache; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * 获取 AuthRequest |
| 60 | + * |
| 61 | + * @param source {@link AuthSource} |
| 62 | + * @return {@link AuthRequest} |
| 63 | + */ |
| 64 | + public AuthRequest getAuthRequest(String source) { |
| 65 | + if (StrUtil.isBlank(source)) { |
| 66 | + throw new AuthException(AuthResponseStatus.NO_AUTH_SOURCE); |
| 67 | + } |
| 68 | + |
| 69 | + // 获取内置 AuthRequest |
| 70 | + AuthRequest authRequest = this.getDefaultAuthRequest(source); |
| 71 | + |
| 72 | + // 获取自定义 AuthRequest |
| 73 | + if (authRequest == null) { |
| 74 | + authRequest = this.getExtendAuthRequest(properties.getExtend().getEnumClass(), source); |
| 75 | + } |
| 76 | + |
| 77 | + if (authRequest == null) { |
| 78 | + throw new AuthException(AuthResponseStatus.UNSUPPORTED); |
| 79 | + } |
| 80 | + return authRequest; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * 获取自定义 AuthRequest |
| 85 | + * |
| 86 | + * @param clazz 枚举类 {@link AuthSource} |
| 87 | + * @param source {@link AuthSource} |
| 88 | + * @return {@link AuthRequest} |
| 89 | + */ |
| 90 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 91 | + private AuthRequest getExtendAuthRequest(Class clazz, String source) { |
| 92 | + String upperSource = source.toUpperCase(); |
| 93 | + try { |
| 94 | + EnumUtil.fromString(clazz, upperSource); |
| 95 | + } catch (IllegalArgumentException e) { |
| 96 | + // 无自定义匹配 |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + Map<String, JustAuthExtendProperties.ExtendRequestConfig> extendConfig = properties.getExtend().getConfig(); |
| 101 | + Map<String, JustAuthExtendProperties.ExtendRequestConfig> upperConfig = new HashMap<>(6); |
| 102 | + extendConfig.forEach((k, v) -> upperConfig.put(k.toUpperCase(), v)); |
| 103 | + JustAuthExtendProperties.ExtendRequestConfig extendRequestConfig = upperConfig.get(upperSource); |
| 104 | + if (extendRequestConfig != null) { |
| 105 | + // 配置 HTTP |
| 106 | + this.configureHttpConfig(upperSource, extendRequestConfig, properties.getHttp()); |
| 107 | + |
| 108 | + Class<? extends AuthRequest> requestClass = extendRequestConfig.getRequestClass(); |
| 109 | + |
| 110 | + if (requestClass != null) { |
| 111 | + // 反射获取 Request 对象,所以必须实现 2 个参数的构造方法 |
| 112 | + return ReflectUtil.newInstance(requestClass, extendRequestConfig, stateCache); |
| 113 | + } |
| 114 | + } |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * 获取内置 AuthRequest |
| 120 | + * |
| 121 | + * @param source {@link AuthSource} |
| 122 | + * @return {@link AuthRequest} |
| 123 | + */ |
| 124 | + private AuthRequest getDefaultAuthRequest(String source) { |
| 125 | + AuthDefaultSource authSource; |
| 126 | + try { |
| 127 | + authSource = EnumUtil.fromString(AuthDefaultSource.class, source.toUpperCase()); |
| 128 | + } catch (IllegalArgumentException e) { |
| 129 | + // 无自定义匹配 |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + AuthConfig config = properties.getType().get(authSource.name()); |
| 134 | + // 找不到对应关系,直接返回空 |
| 135 | + if (config == null) { |
| 136 | + return null; |
| 137 | + } |
| 138 | + |
| 139 | + // 配置 HTTP |
| 140 | + this.configureHttpConfig(authSource.name(), config, properties.getHttp()); |
| 141 | + return AuthRequestBuilder.builder().source(source).authConfig(config).build(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * 配置 HTTP 相关的配置 |
| 146 | + * |
| 147 | + * @param authSource {@link AuthSource} |
| 148 | + * @param authConfig {@link AuthConfig} |
| 149 | + * @param httpConfig {@link JustAuthHttpProperties} |
| 150 | + */ |
| 151 | + private void configureHttpConfig(String authSource, AuthConfig authConfig, JustAuthHttpProperties httpConfig) { |
| 152 | + if (null == httpConfig) { |
| 153 | + return; |
| 154 | + } |
| 155 | + |
| 156 | + Map<String, JustAuthHttpProperties.JustAuthProxyConfig> proxyConfigMap = httpConfig.getProxy(); |
| 157 | + if (CollUtil.isEmpty(proxyConfigMap)) { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + JustAuthHttpProperties.JustAuthProxyConfig proxyConfig = proxyConfigMap.get(authSource); |
| 162 | + if (null == proxyConfig) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + authConfig.setHttpConfig(HttpConfig.builder() |
| 167 | + .timeout(httpConfig.getTimeout()) |
| 168 | + .proxy(new Proxy(Proxy.Type.valueOf(proxyConfig.getType()), new InetSocketAddress(proxyConfig |
| 169 | + .getHostname(), proxyConfig.getPort()))) |
| 170 | + .build()); |
| 171 | + } |
| 172 | +} |
0 commit comments