AuthSocialsUtil.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package jnpf.socials.utils;
  2. import cn.hutool.core.text.StrPool;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.xkcoding.http.config.HttpConfig;
  5. import jnpf.socials.config.CustomAuthConfig;
  6. import jnpf.socials.config.SocialsConfig;
  7. import jnpf.socials.request.AuthDingTalkNewRequest;
  8. import jnpf.socials.request.AuthWeChatEnterpriseWWQrcodeRequest;
  9. import jnpf.socials.request.AuthWechatAppletsRequest;
  10. import jnpf.config.ConfigValueUtil;
  11. import jnpf.constant.MsgCode;
  12. import jnpf.consts.RedisConst;
  13. import jnpf.util.RedisUtil;
  14. import jnpf.util.StringUtil;
  15. import me.zhyd.oauth.config.AuthSource;
  16. import me.zhyd.oauth.enums.AuthResponseStatus;
  17. import me.zhyd.oauth.enums.scope.*;
  18. import me.zhyd.oauth.exception.AuthException;
  19. import me.zhyd.oauth.model.AuthResponse;
  20. import me.zhyd.oauth.request.*;
  21. import me.zhyd.oauth.utils.AuthScopeUtils;
  22. import me.zhyd.oauth.utils.StringUtils;
  23. import org.springframework.beans.BeanUtils;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.stereotype.Component;
  26. import java.net.URLEncoder;
  27. import java.util.Arrays;
  28. /**
  29. * 流程设计
  30. *
  31. * @author JNPF开发平台组
  32. * @version V3.4.2
  33. * @copyright 引迈信息技术有限公司
  34. * @date 2022/7/21 12:00:56
  35. */
  36. @Component
  37. public class AuthSocialsUtil {
  38. @Autowired
  39. private SocialsConfig socialsConfig;
  40. @Autowired
  41. private ConfigValueUtil configValueUtil;
  42. private static RedisUtil redisUtil;
  43. // 针对国外平台配置代理
  44. private HttpConfig httpConfig = null;
  45. public AuthSocialsUtil() {
  46. // httpConfig = HttpConfig.builder()
  47. // .timeout(15000)
  48. // .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 10080)))
  49. // .build();
  50. }
  51. /**
  52. * 根据配置信息获取请求对象
  53. *
  54. * @param
  55. * @return
  56. * @copyright 引迈信息技术有限公司
  57. * @date 2022/7/21
  58. */
  59. public AuthRequest getAuthRequest(String source, String userId, boolean isLogin, String ticket, String tenantId) {
  60. AuthRequest authRequest = null;
  61. String addUrlStr = "";
  62. String urlStr = configValueUtil.getApiDomain() + "/api/oauth/Login/socials?source=" + source;
  63. if (!isLogin) {
  64. urlStr = configValueUtil.getApiDomain() + "/api/oauth/Login/socials?source=" + source;
  65. }
  66. if (StringUtil.isNotEmpty(userId)) {
  67. addUrlStr = "&userId=" + userId;
  68. }
  69. if (StringUtil.isNotEmpty(ticket)) {
  70. addUrlStr = "&jnpf_ticket=" + ticket;
  71. }
  72. if (StringUtil.isNotEmpty(tenantId)) {
  73. addUrlStr += "&tenantId=" + tenantId;
  74. }
  75. String url = urlStr + addUrlStr;
  76. String platform = source.toLowerCase();
  77. CustomAuthConfig socialConfig = socialsConfig.getSocialMap().get(platform);
  78. CustomAuthConfig newSocialConfig = new CustomAuthConfig();
  79. BeanUtils.copyProperties(socialConfig, newSocialConfig);
  80. newSocialConfig.setRedirectUri(url);
  81. // 针对国外平台配置代理
  82. switch (platform) {
  83. //todo 官方登录api调整目前数据问题
  84. case "dingtalk":
  85. authRequest = new AuthDingTalkNewRequest(newSocialConfig);
  86. break;
  87. //todo 未申请企业
  88. case "qq":
  89. authRequest = new AuthQqRequest(newSocialConfig);
  90. break;
  91. case "wechat_open":
  92. newSocialConfig.setRedirectUri(URLEncoder.encode(newSocialConfig.getRedirectUri()));
  93. authRequest = new AuthWeChatOpenRequest(newSocialConfig);
  94. break;
  95. case "github":
  96. newSocialConfig.setRedirectUri(URLEncoder.encode(newSocialConfig.getRedirectUri()));
  97. newSocialConfig.setScopes(AuthScopeUtils.getScopes(AuthGithubScope.values()));
  98. if(httpConfig != null){
  99. newSocialConfig.setHttpConfig(httpConfig);
  100. }
  101. authRequest = new AuthGithubRequest(newSocialConfig);
  102. break;
  103. case "wechat_enterprise":
  104. newSocialConfig.setRedirectUri(URLEncoder.encode(newSocialConfig.getRedirectUri()));
  105. authRequest = new AuthWeChatEnterpriseQrcodeRequest(newSocialConfig);
  106. break;
  107. case "wechat_enterprise_ww":
  108. newSocialConfig.setRedirectUri(URLEncoder.encode(newSocialConfig.getRedirectUri()));
  109. authRequest = new AuthWeChatEnterpriseWWQrcodeRequest(newSocialConfig);
  110. break;
  111. case "feishu":
  112. authRequest = new AuthFeishuRequest(newSocialConfig);
  113. break;
  114. case "baidu":
  115. newSocialConfig.setScopes(Arrays.asList(
  116. AuthBaiduScope.BASIC.getScope(),
  117. AuthBaiduScope.SUPER_MSG.getScope(),
  118. AuthBaiduScope.NETDISK.getScope()
  119. ));
  120. authRequest = new AuthBaiduRequest(newSocialConfig);
  121. break;
  122. case "gitee":
  123. newSocialConfig.setScopes(AuthScopeUtils.getScopes(AuthGiteeScope.values()));
  124. authRequest = new AuthGiteeRequest(newSocialConfig);
  125. break;
  126. case "weibo":
  127. newSocialConfig.setScopes(Arrays.asList(
  128. AuthWeiboScope.EMAIL.getScope(),
  129. AuthWeiboScope.FRIENDSHIPS_GROUPS_READ.getScope(),
  130. AuthWeiboScope.STATUSES_TO_ME_READ.getScope()
  131. ));
  132. authRequest = new AuthWeiboRequest(newSocialConfig);
  133. break;
  134. case "coding":
  135. newSocialConfig.setDomainPrefix("");
  136. newSocialConfig.setScopes(Arrays.asList(
  137. AuthCodingScope.USER.getScope(),
  138. AuthCodingScope.USER_EMAIL.getScope(),
  139. AuthCodingScope.USER_PHONE.getScope()
  140. ));
  141. authRequest = new AuthCodingRequest(newSocialConfig);
  142. break;
  143. case "oschina":
  144. authRequest = new AuthOschinaRequest(newSocialConfig);
  145. break;
  146. case "alipay":
  147. // 支付宝在创建回调地址时,不允许使用localhost或者127.0.0.1,所以这儿的回调地址使用的局域网内的ip
  148. authRequest = new AuthAlipayRequest(newSocialConfig, newSocialConfig.getAlipayPublicKey());
  149. break;
  150. case "csdn":
  151. authRequest = new AuthCsdnRequest(newSocialConfig);
  152. break;
  153. case "taobao":
  154. authRequest = new AuthTaobaoRequest(newSocialConfig);
  155. break;
  156. case "google":
  157. newSocialConfig.setScopes(AuthScopeUtils.getScopes(AuthGoogleScope.USER_EMAIL, AuthGoogleScope.USER_PROFILE, AuthGoogleScope.USER_OPENID));
  158. if(httpConfig != null){
  159. newSocialConfig.setHttpConfig(httpConfig);
  160. }
  161. authRequest = new AuthGoogleRequest(socialConfig);
  162. break;
  163. case "facebook":
  164. newSocialConfig.setScopes(AuthScopeUtils.getScopes(AuthFacebookScope.values()));
  165. if(httpConfig != null){
  166. newSocialConfig.setHttpConfig(httpConfig);
  167. }
  168. authRequest = new AuthFacebookRequest(newSocialConfig);
  169. break;
  170. case "douyin":
  171. authRequest = new AuthDouyinRequest(newSocialConfig);
  172. break;
  173. case "linkedin":
  174. authRequest = new AuthLinkedinRequest(newSocialConfig);
  175. break;
  176. case "microsoft":
  177. newSocialConfig.setScopes(Arrays.asList(
  178. AuthMicrosoftScope.USER_READ.getScope(),
  179. AuthMicrosoftScope.USER_READWRITE.getScope(),
  180. AuthMicrosoftScope.USER_READBASIC_ALL.getScope(),
  181. AuthMicrosoftScope.USER_READ_ALL.getScope(),
  182. AuthMicrosoftScope.USER_READWRITE_ALL.getScope(),
  183. AuthMicrosoftScope.USER_INVITE_ALL.getScope(),
  184. AuthMicrosoftScope.USER_EXPORT_ALL.getScope(),
  185. AuthMicrosoftScope.USER_MANAGEIDENTITIES_ALL.getScope(),
  186. AuthMicrosoftScope.FILES_READ.getScope()
  187. ));
  188. authRequest = new AuthMicrosoftRequest(newSocialConfig);
  189. break;
  190. case "mi":
  191. authRequest = new AuthMiRequest(newSocialConfig);
  192. break;
  193. case "toutiao":
  194. authRequest = new AuthToutiaoRequest(newSocialConfig);
  195. break;
  196. case "teambition":
  197. authRequest = new AuthTeambitionRequest(newSocialConfig);
  198. break;
  199. case "pinterest":
  200. if(httpConfig != null){
  201. newSocialConfig.setHttpConfig(httpConfig);
  202. }
  203. authRequest = new AuthPinterestRequest(newSocialConfig);
  204. break;
  205. case "renren":
  206. authRequest = new AuthRenrenRequest(newSocialConfig);
  207. break;
  208. case "stack_overflow":
  209. authRequest = new AuthStackOverflowRequest(newSocialConfig);
  210. break;
  211. case "huawei":
  212. newSocialConfig.setScopes(Arrays.asList(
  213. AuthHuaweiScope.BASE_PROFILE.getScope(),
  214. AuthHuaweiScope.MOBILE_NUMBER.getScope(),
  215. AuthHuaweiScope.ACCOUNTLIST.getScope(),
  216. AuthHuaweiScope.SCOPE_DRIVE_FILE.getScope(),
  217. AuthHuaweiScope.SCOPE_DRIVE_APPDATA.getScope()
  218. ));
  219. authRequest = new AuthHuaweiRequest(newSocialConfig);
  220. break;
  221. case "kujiale":
  222. authRequest = new AuthKujialeRequest(newSocialConfig);
  223. break;
  224. case "gitlab":
  225. newSocialConfig.setScopes(AuthScopeUtils.getScopes(AuthGitlabScope.values()));
  226. authRequest = new AuthGitlabRequest(newSocialConfig);
  227. break;
  228. case "meituan":
  229. authRequest = new AuthMeituanRequest(newSocialConfig);
  230. break;
  231. case "eleme":
  232. authRequest = new AuthElemeRequest(newSocialConfig);
  233. break;
  234. // case "mygitlab":
  235. // authRequest = new AuthMyGitlabRequest(AuthConfig.builder()
  236. // .clientId("")
  237. // .clientSecret("")
  238. // .redirectUri("http://127.0.0.1:8443/oauth/callback/mygitlab")
  239. // .build());
  240. // break;
  241. case "twitter":
  242. if(httpConfig != null){
  243. newSocialConfig.setHttpConfig(httpConfig);
  244. }
  245. authRequest = new AuthTwitterRequest(newSocialConfig);
  246. break;
  247. case "wechat_mp":
  248. authRequest = new AuthWeChatMpRequest(newSocialConfig);
  249. break;
  250. case "aliyun":
  251. authRequest = new AuthAliyunRequest(newSocialConfig);
  252. break;
  253. case "xmly":
  254. authRequest = new AuthXmlyRequest(newSocialConfig);
  255. break;
  256. case "wechat_enterprise_web":
  257. authRequest = new AuthWeChatEnterpriseWebRequest(newSocialConfig);
  258. break;
  259. case "wechat_applets":
  260. authRequest = new AuthWechatAppletsRequest(newSocialConfig);
  261. break;
  262. default:
  263. break;
  264. }
  265. if (null == authRequest) {
  266. throw new AuthException(MsgCode.OA024.get());
  267. }
  268. return authRequest;
  269. }
  270. @Autowired
  271. public void setRedisUtil(RedisUtil redisUtil) {
  272. AuthSocialsUtil.redisUtil = redisUtil;
  273. }
  274. public static void setSuitTicket(String suitId, String ticket) {
  275. redisUtil.insert(RedisConst.REDIS_LOCK4J_PREFIX + "suitticket" + StrPool.COLON + suitId, ticket, 30 * 60000);
  276. redisUtil.remove(RedisConst.REDIS_LOCK4J_PREFIX + "suitaccesstoken" + StrPool.COLON + suitId);
  277. }
  278. public static String getSuitTicket(String suitId) {
  279. String key = RedisConst.REDIS_LOCK4J_PREFIX + "suitticket" + StrPool.COLON + suitId;
  280. return (String) redisUtil.getString(key);
  281. }
  282. public static void setSuitAccessToken(String suitId, String token) {
  283. redisUtil.insert(RedisConst.REDIS_LOCK4J_PREFIX + "suitaccesstoken" + StrPool.COLON + suitId, token, 120 * 60000);
  284. }
  285. public static String getSuitAccessToken(String suitId) {
  286. String key = RedisConst.REDIS_LOCK4J_PREFIX + "suitaccesstoken" + StrPool.COLON + suitId;
  287. return (String) redisUtil.getString(key);
  288. }
  289. public static JSONObject checkResponse(String response, AuthSource source) {
  290. JSONObject object = JSONObject.parseObject(response);
  291. if (object.containsKey("errcode") && object.getIntValue("errcode") != 0) {
  292. throw new AuthException(object.getString("errmsg"), source);
  293. } else {
  294. return object;
  295. }
  296. }
  297. public static AuthResponse responseError(Exception e) {
  298. int errorCode = AuthResponseStatus.FAILURE.getCode();
  299. String errorMsg = e.getMessage();
  300. if (e instanceof AuthException) {
  301. AuthException authException = ((AuthException) e);
  302. errorCode = authException.getErrorCode();
  303. if (StringUtils.isNotEmpty(authException.getErrorMsg())) {
  304. errorMsg = authException.getErrorMsg();
  305. }
  306. }
  307. return AuthResponse.builder().code(errorCode).msg(errorMsg).build();
  308. }
  309. }