AbstractAuthWeChatEnterpriseWWRequest.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package jnpf.socials.request;
  2. import com.alibaba.fastjson.JSONObject;
  3. import jnpf.socials.utils.AuthSocialsUtil;
  4. import jnpf.util.StringUtil;
  5. import me.zhyd.oauth.cache.AuthStateCache;
  6. import me.zhyd.oauth.config.AuthConfig;
  7. import me.zhyd.oauth.config.AuthSource;
  8. import me.zhyd.oauth.enums.AuthResponseStatus;
  9. import me.zhyd.oauth.exception.AuthException;
  10. import me.zhyd.oauth.model.AuthCallback;
  11. import me.zhyd.oauth.model.AuthToken;
  12. import me.zhyd.oauth.model.AuthUser;
  13. import me.zhyd.oauth.request.AuthDefaultRequest;
  14. import me.zhyd.oauth.utils.HttpUtils;
  15. import me.zhyd.oauth.utils.UrlBuilder;
  16. /**
  17. * <p>
  18. * 企业微信服务商适配器父类
  19. * </p>
  20. *
  21. * @author JNPF开发平台组
  22. * @version V5.2.0
  23. * @copyright 引迈信息技术有限公司
  24. * @date 2025/6/5 15:40:14
  25. */
  26. public abstract class AbstractAuthWeChatEnterpriseWWRequest extends AuthDefaultRequest {
  27. public AbstractAuthWeChatEnterpriseWWRequest(AuthConfig config, AuthSource source) {
  28. super(config, source);
  29. }
  30. public AbstractAuthWeChatEnterpriseWWRequest(AuthConfig config, AuthSource source, AuthStateCache authStateCache) {
  31. super(config, source, authStateCache);
  32. }
  33. /**
  34. * 获取第三方应用access_token
  35. * @param code code码
  36. * @return
  37. */
  38. @Override
  39. protected String doGetAuthorizationCode(String code) {
  40. JSONObject param = new JSONObject();
  41. param.put("suite_id", this.config.getClientId());
  42. param.put("suite_secret", this.config.getClientSecret());
  43. param.put("suite_ticket", AuthSocialsUtil.getSuitTicket(this.config.getClientId()));
  44. return new HttpUtils(config.getHttpConfig()).post(source.accessToken(), param.toJSONString()).getBody();
  45. }
  46. @Override
  47. public AuthToken getAccessToken(AuthCallback authCallback) {
  48. if (StringUtil.isEmpty(AuthSocialsUtil.getSuitTicket(this.config.getClientId()))) {
  49. throw new AuthException("企业微信后台未推送应用SuitTicket");
  50. }
  51. String token = AuthSocialsUtil.getSuitAccessToken(this.config.getClientId());
  52. if (StringUtil.isEmpty(token)) {
  53. String response = doGetAuthorizationCode(null);
  54. JSONObject object = AuthSocialsUtil.checkResponse(response, this.source);
  55. token = object.getString("suite_access_token");
  56. AuthSocialsUtil.setSuitAccessToken(this.config.getClientId(), token);
  57. }
  58. return AuthToken.builder()
  59. .accessToken(token)
  60. .expireIn(0)
  61. .code(authCallback.getCode())
  62. .build();
  63. }
  64. /**
  65. * 获取第三方应用授权用户
  66. * @param authToken token
  67. * @return
  68. */
  69. @Override
  70. protected String userInfoUrl(AuthToken authToken) {
  71. return UrlBuilder.fromBaseUrl(source.userInfo())
  72. .queryParam("suite_access_token", authToken.getAccessToken())
  73. .queryParam("code", authToken.getCode())
  74. .build();
  75. }
  76. @Override
  77. public AuthUser getUserInfo(AuthToken authToken) {
  78. String response = doGetUserInfo(authToken);
  79. JSONObject object = AuthSocialsUtil.checkResponse(response, this.source);
  80. // 返回 OpenId 或其他,均代表非当前企业用户,不支持
  81. if (!object.containsKey("userid")) {
  82. throw new AuthException(AuthResponseStatus.UNIDENTIFIED_PLATFORM, source);
  83. }
  84. String userId = object.getString("userid");
  85. return AuthUser.builder()
  86. .rawUserInfo(object)
  87. .uuid(userId)
  88. .token(authToken)
  89. .source(source.toString())
  90. .build();
  91. }
  92. }