Browse Source

:new: 微信开发平台模块增加OAuth2相关接口(网页授权、网页登录等)的实现

Binary Wang 4 years ago
parent
commit
07bc5a5eac

+ 79 - 0
weixin-java-open/src/main/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImpl.java

@@ -0,0 +1,79 @@
+package me.chanjar.weixin.open.api.impl;
+
+import lombok.AllArgsConstructor;
+import me.chanjar.weixin.common.bean.WxOAuth2UserInfo;
+import me.chanjar.weixin.common.bean.oauth2.WxOAuth2AccessToken;
+import me.chanjar.weixin.common.enums.WxType;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.error.WxRuntimeException;
+import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
+import me.chanjar.weixin.common.util.http.URIUtil;
+import me.chanjar.weixin.mp.api.WxOAuth2Service;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.IOException;
+
+import static me.chanjar.weixin.mp.enums.WxMpApiUrl.OAuth2.*;
+
+/**
+ * oauth2接口实现.
+ *
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ * @date 2020-10-19
+ */
+@AllArgsConstructor
+public class WxOpenOAuth2ServiceImpl extends WxOpenServiceImpl implements WxOAuth2Service {
+  private final String appId;
+  private final String appSecret;
+
+  @Override
+  public String buildAuthorizationUrl(String redirectUri, String scope, String state) {
+    return String.format(CONNECT_OAUTH2_AUTHORIZE_URL.getUrl(null),
+      this.appId, URIUtil.encodeURIComponent(redirectUri), scope, StringUtils.trimToEmpty(state));
+  }
+
+  private WxOAuth2AccessToken getOAuth2AccessToken(String url) throws WxErrorException {
+    return WxOAuth2AccessToken.fromJson(this.get(url, null));
+  }
+
+  @Override
+  public WxOAuth2AccessToken getAccessToken(String code) throws WxErrorException {
+    return this.getAccessToken(this.appId, this.appSecret, code);
+  }
+
+  @Override
+  public WxOAuth2AccessToken getAccessToken(String appId, String appSecret, String code) throws WxErrorException {
+    return this.getOAuth2AccessToken(String.format(OAUTH2_ACCESS_TOKEN_URL.getUrl(null), appId, appSecret, code));
+  }
+
+  @Override
+  public WxOAuth2AccessToken refreshAccessToken(String refreshToken) throws WxErrorException {
+    String url = String.format(OAUTH2_REFRESH_TOKEN_URL.getUrl(null), this.appId, refreshToken);
+    return this.getOAuth2AccessToken(url);
+  }
+
+  @Override
+  public WxOAuth2UserInfo getUserInfo(WxOAuth2AccessToken token, String lang) throws WxErrorException {
+    if (lang == null) {
+      lang = "zh_CN";
+    }
+
+    String url = String.format(OAUTH2_USERINFO_URL.getUrl(null), token.getAccessToken(), token.getOpenId(), lang);
+
+    return WxOAuth2UserInfo.fromJson(this.get(url, null));
+  }
+
+  @Override
+  public boolean validateAccessToken(WxOAuth2AccessToken token) {
+    String url = String.format(OAUTH2_VALIDATE_TOKEN_URL.getUrl(null), token.getAccessToken(), token.getOpenId());
+
+    try {
+      SimpleGetRequestExecutor.create(this).execute(url, null, WxType.MP);
+    } catch (IOException e) {
+      throw new WxRuntimeException(e);
+    } catch (WxErrorException e) {
+      return false;
+    }
+    return true;
+  }
+}

+ 38 - 0
weixin-java-open/src/test/java/me/chanjar/weixin/open/api/impl/WxOpenOAuth2ServiceImplTest.java

@@ -0,0 +1,38 @@
+package me.chanjar.weixin.open.api.impl;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+
+/**
+ * 单元测试.
+ *
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ * @date 2020-10-19
+ */
+public class WxOpenOAuth2ServiceImplTest {
+
+  @Test
+  public void testBuildAuthorizationUrl() {
+  }
+
+  @Test
+  public void testGetAccessToken() {
+  }
+
+  @Test
+  public void testTestGetAccessToken() {
+  }
+
+  @Test
+  public void testRefreshAccessToken() {
+  }
+
+  @Test
+  public void testGetUserInfo() {
+  }
+
+  @Test
+  public void testValidateAccessToken() {
+  }
+}