Search in sources :

Example 1 with OAuth2DefinitionHandler

use of com.dtflys.forest.handler.OAuth2DefinitionHandler in project forest by dromara.

the class OAuth2LifeCycle method executeRequestToken.

/**
 * 执行实际的网络请求
 *
 * @param request  当前请求
 * @param clientId 客户端ID
 * @param body     请求内容
 * @return 返回新的 Token 信息
 */
private TokenCache executeRequestToken(ForestRequest request, String clientId, Map<String, Object> body) {
    // 加入扩展参数
    String[] bodyItems = (String[]) getAttribute(request, "body");
    body.putAll(kv2map(bodyItems));
    Map<String, Object> queryItems = kv2map((String[]) getAttribute(request, "query"));
    Class<? extends OAuth2DefinitionHandler> handlerClass = request.getMethod().getMethod().getAnnotation(OAuth2.class).OAuth2TokenHandler();
    ForestResponse<String> response = oAuth2Client.token(getAttributeAsString(request, "tokenUri"), queryItems, body);
    OAuth2Token token;
    try {
        OAuth2DefinitionHandler handler = handlerClass.newInstance();
        ForestConfiguration configuration = request.getConfiguration();
        Map map = (Map) configuration.getConverter(ForestDataType.AUTO).convertToJavaObject(response.getContent(), Map.class);
        token = handler.getOAuth2Token(response, map);
    } catch (InstantiationException | IllegalAccessException e) {
        throw new ForestRuntimeException("OAuth2 request OAuth2DefinitionHandler error" + e.getMessage());
    }
    if (token == null) {
        throw new ForestRuntimeException("OAuth2 request OAuth2Token is empty");
    }
    return new TokenCache(clientId, token);
}
Also used : OAuth2(com.dtflys.forest.extensions.OAuth2) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) OAuth2DefinitionHandler(com.dtflys.forest.handler.OAuth2DefinitionHandler) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 OAuth2 (com.dtflys.forest.extensions.OAuth2)1 OAuth2DefinitionHandler (com.dtflys.forest.handler.OAuth2DefinitionHandler)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1