From c397fe741250fdecd1c8d79ca357e0db0430ed90 Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Tue, 2 Mar 2021 21:52:18 +0800 Subject: [PATCH] :bookmark: pre-releasing 1.0.1-alpha --- jap-social/pom.xml | 2 +- .../jap/social/JustAuthRequestContext.java | 2 +- .../com/fujieid/jap/social/SocialConfig.java | 2 +- .../fujieid/jap/social/SocialStrategy.java | 44 +++++++++++++------ 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/jap-social/pom.xml b/jap-social/pom.xml index 306c279..3515cb2 100644 --- a/jap-social/pom.xml +++ b/jap-social/pom.xml @@ -5,7 +5,7 @@ com.fujieid jap - 1.0.1 + 1.0.1-alpha 4.0.0 diff --git a/jap-social/src/main/java/com/fujieid/jap/social/JustAuthRequestContext.java b/jap-social/src/main/java/com/fujieid/jap/social/JustAuthRequestContext.java index 5d60b50..7625bb4 100644 --- a/jap-social/src/main/java/com/fujieid/jap/social/JustAuthRequestContext.java +++ b/jap-social/src/main/java/com/fujieid/jap/social/JustAuthRequestContext.java @@ -182,7 +182,7 @@ public class JustAuthRequestContext { * @param stateCache Custom state cache * @return AuthRequest */ - public static AuthRequest getRequest(String source, SocialConfig socialConfig, AuthConfig authConfig, AuthStateCache stateCache) { + public static AuthRequest getRequest(String source, SocialConfig socialConfig, AuthConfig authConfig, AuthStateCache stateCache) throws JapSocialException { if (StrUtil.isBlank(source)) { throw new JapSocialException("Social#Missing social source"); } diff --git a/jap-social/src/main/java/com/fujieid/jap/social/SocialConfig.java b/jap-social/src/main/java/com/fujieid/jap/social/SocialConfig.java index 4e249f0..323f392 100644 --- a/jap-social/src/main/java/com/fujieid/jap/social/SocialConfig.java +++ b/jap-social/src/main/java/com/fujieid/jap/social/SocialConfig.java @@ -15,7 +15,7 @@ */ package com.fujieid.jap.social; -import com.fujieid.jap.core.AuthenticateConfig; +import com.fujieid.jap.core.config.AuthenticateConfig; import me.zhyd.oauth.config.AuthConfig; /** 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 fe823d9..5aee2aa 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 @@ -19,10 +19,16 @@ import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; -import com.fujieid.jap.core.*; +import com.fujieid.jap.core.JapUser; +import com.fujieid.jap.core.JapUserService; import com.fujieid.jap.core.cache.JapCache; +import com.fujieid.jap.core.config.AuthenticateConfig; +import com.fujieid.jap.core.config.JapConfig; +import com.fujieid.jap.core.exception.JapException; import com.fujieid.jap.core.exception.JapSocialException; import com.fujieid.jap.core.exception.JapUserException; +import com.fujieid.jap.core.result.JapErrorCode; +import com.fujieid.jap.core.result.JapResponse; import com.fujieid.jap.core.strategy.AbstractJapStrategy; import me.zhyd.oauth.cache.AuthStateCache; import me.zhyd.oauth.config.AuthConfig; @@ -89,37 +95,49 @@ public class SocialStrategy extends AbstractJapStrategy { } @Override - public void authenticate(AuthenticateConfig config, HttpServletRequest request, HttpServletResponse response) { + public JapResponse authenticate(AuthenticateConfig config, HttpServletRequest request, HttpServletResponse response) { - if (this.checkSession(request, response)) { - return; + JapUser sessionUser = this.checkSession(request, response); + if (null != sessionUser) { + return JapResponse.success(sessionUser); } // Convert AuthenticateConfig to SocialConfig - this.checkAuthenticateConfig(config, SocialConfig.class); + try { + this.checkAuthenticateConfig(config, SocialConfig.class); + } 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)) { - throw new JapSocialException("AuthConfig in SocialStrategy is required"); + return JapResponse.error(JapErrorCode.MISS_AUTH_CONFIG); } // Instantiate the AuthRequest of JustAuth - AuthRequest authRequest = JustAuthRequestContext.getRequest(source, socialConfig, authConfig, authStateCache); + 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 if (!this.isCallback(source, authCallback)) { String url = authRequest.authorize(socialConfig.getState()); - String redirectErrorMsg = "JAP failed to redirect to " + source + " authorized endpoint through HttpServletResponse."; - JapUtil.redirect(url, redirectErrorMsg, response); - return; + return JapResponse.success(url); } - this.login(request, response, source, authRequest, authCallback); + try { + return this.login(request, response, source, authRequest, authCallback); + } catch (JapUserException e) { + return JapResponse.error(e.getErrorCode(), e.getErrorMessage()); + } } /** @@ -131,7 +149,7 @@ public class SocialStrategy extends AbstractJapStrategy { * @param authRequest AuthRequest of justauth * @param authCallback Parse the parameters obtained by the third party callback request */ - private void login(HttpServletRequest request, HttpServletResponse response, String source, AuthRequest authRequest, AuthCallback authCallback) { + private JapResponse login(HttpServletRequest request, HttpServletResponse response, String source, AuthRequest authRequest, AuthCallback authCallback) throws JapUserException { AuthResponse authUserAuthResponse = authRequest.login(authCallback); if (!authUserAuthResponse.ok() || ObjectUtil.isNull(authUserAuthResponse.getData())) { throw new JapUserException("Third party login of `" + source + "` cannot obtain user information. " @@ -147,7 +165,7 @@ public class SocialStrategy extends AbstractJapStrategy { } } - this.loginSuccess(japUser, request, response); + return this.loginSuccess(japUser, request, response); } /**