ImplicitDingTalkRequest.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package jnpf.implicit.request;
  2. import com.alibaba.fastjson.JSONObject;
  3. import jnpf.socials.enums.AuthDefaultSourceNew;
  4. import com.xkcoding.http.support.HttpHeader;
  5. import jnpf.constant.MsgCode;
  6. import jnpf.socials.utils.AuthSocialsUtil;
  7. import me.zhyd.oauth.config.AuthConfig;
  8. import me.zhyd.oauth.enums.AuthUserGender;
  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. import org.apache.commons.codec.binary.Base64;
  17. import javax.crypto.Mac;
  18. import javax.crypto.spec.SecretKeySpec;
  19. import java.net.URLEncoder;
  20. import java.util.Date;
  21. public class ImplicitDingTalkRequest extends AuthDefaultRequest{
  22. public ImplicitDingTalkRequest(AuthConfig config) {
  23. super(config, AuthDefaultSourceNew.DINGTALK_ACCOUNT);
  24. }
  25. @Override
  26. public String authorize(String state) {
  27. return UrlBuilder.fromBaseUrl(this.source.authorize())
  28. .queryParam("appid", this.config.getClientId())
  29. .queryParam("redirect_uri", this.config.getRedirectUri())
  30. .queryParam("response_type", "code")
  31. .queryParam("scope", "snsapi_auth")
  32. .queryParam("state", "STATE")
  33. .build();
  34. }
  35. @Override
  36. public AuthToken getAccessToken(AuthCallback authCallback) {
  37. return AuthToken.builder()
  38. .code(authCallback.getCode()).build();
  39. }
  40. @Override
  41. public AuthUser getUserInfo(AuthToken authToken) {
  42. String response = this.doGetUserByCode(authToken);
  43. JSONObject object = AuthSocialsUtil.checkResponse(response, this.source);
  44. if (object.get("user_info") != null) {
  45. JSONObject userInfo = (JSONObject) object.get("user_info");
  46. AuthToken token = AuthToken.builder().openId(userInfo.getString("openid")).unionId(userInfo.getString("unionid")).build();
  47. return AuthUser.builder().rawUserInfo(userInfo).uuid(userInfo.getString("unionid")).nickname(userInfo.getString("nick"))
  48. .username(object.getString("nick")).gender(AuthUserGender.UNKNOWN).source(this.source.toString()).token(token).build();
  49. } else {
  50. throw new AuthException(MsgCode.OA024.get());
  51. }
  52. }
  53. /**
  54. * 钉钉签名计算
  55. *
  56. * @param time
  57. * @return
  58. */
  59. private String getSignature(long time) {
  60. // 根据timestamp, appSecret计算签名值
  61. String appSecret = this.config.getClientSecret();
  62. // 根据timestamp, appSecret计算签名值
  63. String stringToSign = "" + time;
  64. Mac mac = null;
  65. try {
  66. mac = Mac.getInstance("HmacSHA256");
  67. mac.init(new SecretKeySpec(appSecret.getBytes("UTF-8"), "HmacSHA256"));
  68. byte[] signatureBytes = mac.doFinal(stringToSign.getBytes("UTF-8"));
  69. String signature = new String(Base64.encodeBase64(signatureBytes));
  70. if ("".equals(signature)) {
  71. return "";
  72. }
  73. String encoded = URLEncoder.encode(signature, "UTF-8");
  74. return encoded.replace("+", "%20").replace("*", "%2A").replace("~", "%7E").replace("/", "%2F");
  75. } catch (Exception e) {
  76. throw new RuntimeException(e);
  77. }
  78. }
  79. protected String doGetUserByCode(AuthToken authToken) {
  80. long time = new Date().getTime();
  81. String signature = this.getSignature(time);
  82. String url = UrlBuilder.fromBaseUrl("https://oapi.dingtalk.com/sns/getuserinfo_bycode")
  83. .queryParam("accessKey", this.config.getClientId())
  84. .queryParam("timestamp", time)
  85. .queryParam("signature", signature)
  86. .build();
  87. JSONObject map = new JSONObject();
  88. map.put("tmp_auth_code", authToken.getCode());
  89. return (new HttpUtils(this.config.getHttpConfig())).post(url, map.toJSONString(), new HttpHeader().add("Content-Type", "application/json")).getBody();
  90. }
  91. }