|
@@ -6,13 +6,18 @@ import com.xingyuv.http.support.HttpHeader;
|
|
|
import com.xingyuv.http.util.MapUtil;
|
|
|
import com.xingyuv.jushauth.cache.AuthStateCache;
|
|
|
import com.xingyuv.jushauth.config.AuthConfig;
|
|
|
+import com.xingyuv.jushauth.enums.AuthResponseStatus;
|
|
|
import com.xingyuv.jushauth.enums.AuthUserGender;
|
|
|
import com.xingyuv.jushauth.exception.AuthException;
|
|
|
+import com.xingyuv.jushauth.log.Log;
|
|
|
import com.xingyuv.jushauth.model.AuthCallback;
|
|
|
+import com.xingyuv.jushauth.model.AuthResponse;
|
|
|
import com.xingyuv.jushauth.model.AuthToken;
|
|
|
import com.xingyuv.jushauth.model.AuthUser;
|
|
|
import com.xingyuv.jushauth.request.AuthDefaultRequest;
|
|
|
+import com.xingyuv.jushauth.utils.AuthChecker;
|
|
|
import com.xingyuv.jushauth.utils.HttpUtils;
|
|
|
+import com.xingyuv.jushauth.utils.StringUtils;
|
|
|
import com.xingyuv.jushauth.utils.UrlBuilder;
|
|
|
import com.zjugis.framework.social.core.enums.AuthExtendSource;
|
|
|
|
|
@@ -71,6 +76,60 @@ public class AuthDingtalkRequest extends AuthDefaultRequest {
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
+ protected AuthToken getAccessTokenApp() {
|
|
|
+ HttpHeader httpHeader = new HttpHeader();
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
+ paramsMap.put("appSecret", config.getClientSecret());
|
|
|
+ paramsMap.put("appKey", config.getClientId());
|
|
|
+ String response = new HttpUtils(config.getHttpConfig()).post("https://api.dingtalk.com/v1.0/oauth2/accessToken", JSONObject.toJSONString(paramsMap), httpHeader).getBody();
|
|
|
+ JSONObject object = JSON.parseObject(response);
|
|
|
+ return AuthToken.builder()
|
|
|
+ .accessToken(object.getString("accessToken"))
|
|
|
+ .expireIn(object.getIntValue("expireIn"))
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public AuthUser getUserInfoApp(String authCode) {
|
|
|
+ AuthToken authToken = this.getAccessTokenApp();
|
|
|
+ String accessToken = authToken.getAccessToken();
|
|
|
+ HttpHeader httpHeader = new HttpHeader();
|
|
|
+ httpHeader.add("x-acs-dingtalk-access-token", accessToken);
|
|
|
+ Map<String, Object> paramsMap = new HashMap<>();
|
|
|
+ paramsMap.put("code", authCode);
|
|
|
+ String response = new HttpUtils(config.getHttpConfig()).post("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?access_token="+accessToken, JSONObject.toJSONString(paramsMap), httpHeader).getBody();
|
|
|
+ JSONObject object = JSON.parseObject(response).getJSONObject("result");
|
|
|
+ return AuthUser.builder()
|
|
|
+ .rawUserInfo(object)
|
|
|
+ .uuid(object.getString("unionid"))
|
|
|
+ .nickname(object.getString("name"))
|
|
|
+ .username(object.getString("name"))
|
|
|
+ .gender(AuthUserGender.UNKNOWN)
|
|
|
+ .source(source.toString())
|
|
|
+ .token(authToken)
|
|
|
+ .build();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理{@link AuthDefaultRequest#login(AuthCallback)} 发生异常的情况,统一响应参数
|
|
|
+ *
|
|
|
+ * @param e 具体的异常
|
|
|
+ * @return AuthResponse
|
|
|
+ */
|
|
|
+ AuthResponse responseError(Exception e) {
|
|
|
+ int errorCode = AuthResponseStatus.FAILURE.getCode();
|
|
|
+ String errorMsg = e.getMessage();
|
|
|
+ if (e instanceof AuthException) {
|
|
|
+ AuthException authException = ((AuthException) e);
|
|
|
+ errorCode = authException.getErrorCode();
|
|
|
+ if (StringUtils.isNotEmpty(authException.getErrorMsg())) {
|
|
|
+ errorMsg = authException.getErrorMsg();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return AuthResponse.builder().code(errorCode).msg(errorMsg).build();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 返回带{@code state}参数的授权url,授权回调时会带上这个{@code state}
|
|
|
*
|