mirror of
https://gitee.com/fujieid/jap.git
synced 2024-11-30 02:27:34 +08:00
⬆️ The simple-json dependency is upgraded to version 0.0.2
This commit is contained in:
parent
a764efb13c
commit
fa2e901869
@ -29,6 +29,7 @@ import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.xkcoding.http.HttpUtil;
|
||||
import com.xkcoding.json.JsonUtil;
|
||||
import com.xkcoding.json.util.Kv;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
@ -110,7 +111,7 @@ public class Oauth2Strategy extends AbstractJapStrategy {
|
||||
private JapUser getUserInfo(OAuthConfig oAuthConfig, AccessToken accessToken) {
|
||||
String userinfoResponse = HttpUtil.post(oAuthConfig.getUserinfoUrl(),
|
||||
ImmutableMap.of("access_token", accessToken.getAccessToken()), false);
|
||||
Map<String, Object> userinfo = JsonUtil.toBean(userinfoResponse, Map.class);
|
||||
Kv userinfo = JsonUtil.parseKv(userinfoResponse);
|
||||
|
||||
Oauth2Util.checkOauthResponse(userinfoResponse, userinfo, "Oauth2Strategy failed to get userinfo with accessToken.");
|
||||
|
||||
|
@ -22,11 +22,11 @@ import cn.hutool.crypto.SecureUtil;
|
||||
import com.fujieid.jap.core.cache.JapCacheContextHolder;
|
||||
import com.fujieid.jap.core.exception.JapOauth2Exception;
|
||||
import com.fujieid.jap.oauth2.pkce.PkceCodeChallengeMethod;
|
||||
import com.xkcoding.json.util.Kv;
|
||||
import org.jose4j.base64url.Base64Url;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
@ -70,10 +70,10 @@ public class Oauth2Util {
|
||||
}
|
||||
}
|
||||
|
||||
public static void checkOauthResponse(String responseStr, Map<String, ?> responseMap, String errorMsg) {
|
||||
if (responseMap.containsKey("error") && ObjectUtil.isNotEmpty(responseMap.get("error"))) {
|
||||
public static void checkOauthResponse(String responseStr, Kv responseKv, String errorMsg) {
|
||||
if (responseKv.containsKey("error") && ObjectUtil.isNotEmpty(responseKv.get("error"))) {
|
||||
throw new JapOauth2Exception(Optional.ofNullable(errorMsg).orElse("") +
|
||||
responseMap.get("error_description") + " " + responseStr);
|
||||
responseKv.get("error_description") + " " + responseStr);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,7 @@ import com.fujieid.jap.oauth2.pkce.PkceParams;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.xkcoding.http.HttpUtil;
|
||||
import com.xkcoding.json.JsonUtil;
|
||||
import com.xkcoding.json.util.Kv;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
@ -91,14 +92,14 @@ public class AccessTokenHelper {
|
||||
}
|
||||
|
||||
String tokenResponse = HttpUtil.post(oAuthConfig.getTokenUrl(), params, false);
|
||||
Map<String, Object> tokenMap = JsonUtil.toBean(tokenResponse, Map.class);
|
||||
Oauth2Util.checkOauthResponse(tokenResponse, tokenMap, "Oauth2Strategy failed to get AccessToken.");
|
||||
Kv tokenInfo = JsonUtil.parseKv(tokenResponse);
|
||||
Oauth2Util.checkOauthResponse(tokenResponse, tokenInfo, "Oauth2Strategy failed to get AccessToken.");
|
||||
|
||||
if (!tokenMap.containsKey("access_token")) {
|
||||
if (!tokenInfo.containsKey("access_token")) {
|
||||
throw new JapOauth2Exception("Oauth2Strategy failed to get AccessToken." + tokenResponse);
|
||||
}
|
||||
|
||||
return mapToAccessToken(tokenMap);
|
||||
return mapToAccessToken(tokenInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -143,13 +144,13 @@ public class AccessTokenHelper {
|
||||
}
|
||||
String url = oAuthConfig.getTokenUrl();
|
||||
String tokenResponse = HttpUtil.post(url, params, false);
|
||||
Map<String, Object> tokenMap = JsonUtil.toBean(tokenResponse, Map.class);
|
||||
Oauth2Util.checkOauthResponse(tokenResponse, tokenMap, "Oauth2Strategy failed to get AccessToken.");
|
||||
Kv tokenInfo = JsonUtil.parseKv(tokenResponse);
|
||||
Oauth2Util.checkOauthResponse(tokenResponse, tokenInfo, "Oauth2Strategy failed to get AccessToken.");
|
||||
|
||||
if (!tokenMap.containsKey("access_token")) {
|
||||
if (!tokenInfo.containsKey("access_token")) {
|
||||
throw new JapOauth2Exception("Oauth2Strategy failed to get AccessToken." + tokenResponse);
|
||||
}
|
||||
return mapToAccessToken(tokenMap);
|
||||
return mapToAccessToken(tokenInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -168,29 +169,23 @@ public class AccessTokenHelper {
|
||||
String url = oAuthConfig.getTokenUrl();
|
||||
|
||||
String tokenResponse = HttpUtil.post(url, params, false);
|
||||
Map<String, Object> tokenMap = JsonUtil.toBean(tokenResponse, Map.class);
|
||||
Oauth2Util.checkOauthResponse(tokenResponse, tokenMap, "Oauth2Strategy failed to get AccessToken.");
|
||||
Kv tokenInfo = JsonUtil.parseKv(tokenResponse);
|
||||
Oauth2Util.checkOauthResponse(tokenResponse, tokenInfo, "Oauth2Strategy failed to get AccessToken.");
|
||||
|
||||
if (ObjectUtil.isEmpty(request.getParameter("access_token"))) {
|
||||
throw new JapOauth2Exception("Oauth2Strategy failed to get AccessToken.");
|
||||
}
|
||||
|
||||
return mapToAccessToken(tokenMap);
|
||||
return mapToAccessToken(tokenInfo);
|
||||
}
|
||||
|
||||
private static AccessToken mapToAccessToken(Map<String, Object> tokenMap) {
|
||||
Object accessToken = tokenMap.get("access_token");
|
||||
Object refreshToken = tokenMap.get("refresh_token");
|
||||
Object idToken = tokenMap.get("id_token");
|
||||
Object tokenType = tokenMap.get("token_type");
|
||||
Object expiresIn = tokenMap.get("expires_in");
|
||||
Object scope = tokenMap.get("scope");
|
||||
private static AccessToken mapToAccessToken(Kv tokenMap) {
|
||||
return new AccessToken()
|
||||
.setAccessToken(JapUtil.convertToStr(accessToken))
|
||||
.setRefreshToken(JapUtil.convertToStr(refreshToken))
|
||||
.setIdToken(JapUtil.convertToStr(idToken))
|
||||
.setTokenType(JapUtil.convertToStr(tokenType))
|
||||
.setScope(JapUtil.convertToStr(scope))
|
||||
.setExpiresIn(JapUtil.convertToInt(expiresIn));
|
||||
.setAccessToken(tokenMap.getString("access_token"))
|
||||
.setRefreshToken(tokenMap.getString("refresh_token"))
|
||||
.setIdToken(tokenMap.getString("id_token"))
|
||||
.setTokenType(tokenMap.getString("token_type"))
|
||||
.setScope(tokenMap.getString("scope"))
|
||||
.setExpiresIn(tokenMap.getInteger("expires_in"));
|
||||
}
|
||||
}
|
||||
|
2
pom.xml
2
pom.xml
@ -72,7 +72,7 @@
|
||||
<slf4j-api.version>1.7.30</slf4j-api.version>
|
||||
<jedis.version>3.2.0</jedis.version>
|
||||
<kisso.version>3.7.6</kisso.version>
|
||||
<simple-json.version>0.0.1</simple-json.version>
|
||||
<simple-json.version>0.0.2</simple-json.version>
|
||||
<simple-http.version>1.0.3</simple-http.version>
|
||||
</properties>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user