From fa4eb76da7140fb52ef07b9ed54d22974b4f6846 Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Thu, 13 May 2021 10:27:30 +0800 Subject: [PATCH] :sparkles: `SocialStrategy` provides methods of `refreshToken`, `revokeToken`, and `getUserInfo` --- .../fujieid/jap/social/SocialStrategy.java | 113 +++++++++++++++--- 1 file changed, 97 insertions(+), 16 deletions(-) diff --git a/jap-social/src/main/java/com/fujieid/jap/social/SocialStrategy.java b/jap-social/src/main/java/com/fujieid/jap/social/SocialStrategy.java index 2337365..b0f5003 100644 --- a/jap-social/src/main/java/com/fujieid/jap/social/SocialStrategy.java +++ b/jap-social/src/main/java/com/fujieid/jap/social/SocialStrategy.java @@ -17,6 +17,7 @@ package com.fujieid.jap.social; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.ReflectUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; import com.fujieid.jap.core.JapUser; @@ -35,11 +36,13 @@ import me.zhyd.oauth.config.AuthConfig; import me.zhyd.oauth.config.AuthDefaultSource; import me.zhyd.oauth.model.AuthCallback; import me.zhyd.oauth.model.AuthResponse; +import me.zhyd.oauth.model.AuthToken; import me.zhyd.oauth.model.AuthUser; import me.zhyd.oauth.request.AuthRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import java.lang.reflect.Method; import java.util.Map; /** @@ -102,29 +105,15 @@ public class SocialStrategy extends AbstractJapStrategy { return JapResponse.success(sessionUser); } - // Convert AuthenticateConfig to SocialConfig + AuthRequest authRequest = null; try { - this.checkAuthenticateConfig(config, SocialConfig.class); + authRequest = this.getAuthRequest(config); } catch (JapException e) { return JapResponse.error(e.getErrorCode(), e.getErrorMessage()); } SocialConfig socialConfig = (SocialConfig) config; String source = socialConfig.getPlatform(); - // Get the AuthConfig of JustAuth - AuthConfig authConfig = socialConfig.getJustAuthConfig(); - if (ObjectUtil.isNull(authConfig)) { - return JapResponse.error(JapErrorCode.MISS_AUTH_CONFIG); - } - - // Instantiate the AuthRequest of JustAuth - AuthRequest authRequest = null; - try { - authRequest = JustAuthRequestContext.getRequest(source, socialConfig, authConfig, authStateCache); - } catch (JapSocialException e) { - return JapResponse.error(e.getErrorCode(), e.getErrorMessage()); - } - AuthCallback authCallback = this.parseRequest(request); // If it is not a callback request, it must be a request to jump to the authorization link @@ -140,6 +129,98 @@ public class SocialStrategy extends AbstractJapStrategy { } } + public JapResponse refreshToken(AuthenticateConfig config, AuthToken authToken) { + AuthRequest authRequest = null; + try { + authRequest = this.getAuthRequest(config); + } catch (JapException e) { + return JapResponse.error(e.getErrorCode(), e.getErrorMessage()); + } + SocialConfig socialConfig = (SocialConfig) config; + String source = socialConfig.getPlatform(); + + AuthResponse authUserAuthResponse = null; + try { + authUserAuthResponse = authRequest.refresh(authToken); + } catch (Exception e) { + throw new JapSocialException("Third party refresh access token of `" + source + "` failed. " + e.getMessage()); + } + if (!authUserAuthResponse.ok() || ObjectUtil.isNull(authUserAuthResponse.getData())) { + throw new JapUserException("Third party refresh access token of `" + source + "` failed. " + authUserAuthResponse.getMsg()); + } + + authToken = (AuthToken) authUserAuthResponse.getData(); + return JapResponse.success(authToken); + } + + public JapResponse revokeToken(AuthenticateConfig config, AuthToken authToken) { + AuthRequest authRequest = null; + try { + authRequest = this.getAuthRequest(config); + } catch (JapException e) { + return JapResponse.error(e.getErrorCode(), e.getErrorMessage()); + } + SocialConfig socialConfig = (SocialConfig) config; + String source = socialConfig.getPlatform(); + + AuthResponse authUserAuthResponse = null; + try { + authUserAuthResponse = authRequest.revoke(authToken); + } catch (Exception e) { + throw new JapSocialException("Third party refresh access token of `" + source + "` failed. " + e.getMessage()); + } + if (!authUserAuthResponse.ok() || ObjectUtil.isNull(authUserAuthResponse.getData())) { + throw new JapUserException("Third party refresh access token of `" + source + "` failed. " + authUserAuthResponse.getMsg()); + } + + return JapResponse.success(); + } + + public JapResponse getUserInfo(AuthenticateConfig config, AuthToken authToken) { + AuthRequest authRequest = null; + try { + authRequest = this.getAuthRequest(config); + } catch (JapException e) { + return JapResponse.error(e.getErrorCode(), e.getErrorMessage()); + } + SocialConfig socialConfig = (SocialConfig) config; + String source = socialConfig.getPlatform(); + + String funName = "getUserInfo"; + Method method = null; + AuthUser res = null; + JapUserException japUserException = new JapUserException("Failed to obtain user information on the third-party platform `" + source + "`"); + try { + if ((method = ReflectUtil.getMethod(authRequest.getClass(), funName, AuthToken.class)) != null) { + method.setAccessible(true); + res = ReflectUtil.invoke(authRequest, method, authToken); + if (null == res) { + throw japUserException; + } + } + } catch (Exception e) { + throw japUserException; + } + AuthUser authUser = res; + return JapResponse.success(authUser); + } + + private AuthRequest getAuthRequest(AuthenticateConfig config) { + // Convert AuthenticateConfig to SocialConfig + this.checkAuthenticateConfig(config, SocialConfig.class); + SocialConfig socialConfig = (SocialConfig) config; + String source = socialConfig.getPlatform(); + + // Get the AuthConfig of JustAuth + AuthConfig authConfig = socialConfig.getJustAuthConfig(); + if (ObjectUtil.isNull(authConfig)) { + throw new JapException(JapErrorCode.MISS_AUTH_CONFIG); + } + + // Instantiate the AuthRequest of JustAuth + return JustAuthRequestContext.getRequest(source, socialConfig, authConfig, authStateCache); + } + /** * Login with third party authorization * -- GitLab