Skip to content

Commit

Permalink
🆕 #2962 【企业微信】 增加构造第三方应用oauth2链接的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
feidian108 committed Mar 26, 2023
1 parent bae84e1 commit 36028b8
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import me.chanjar.weixin.common.util.xml.IntegerArrayConverter;
import me.chanjar.weixin.common.util.xml.StringArrayConverter;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
import me.chanjar.weixin.cp.util.crypto.WxCpTpCryptUtil;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;

import java.io.Serializable;
Expand Down Expand Up @@ -774,4 +776,20 @@ public static WxCpTpXmlMessage fromXml(String xml) {
return xmlPackage;
}

/**
*
* @param encryptedXml the encrypted xml
* @param wxCpTpConfigStorage the wx cp config storage
* @param timestamp the timestamp
* @param nonce the nonce
* @param msgSignature the msg signature
* @return the wx cp tp xml message
*/
public static WxCpTpXmlMessage fromEncryptedXml(String encryptedXml, WxCpTpConfigStorage wxCpTpConfigStorage,
String timestamp, String nonce, String msgSignature) {
WxCpTpCryptUtil cryptUtil = new WxCpTpCryptUtil(wxCpTpConfigStorage);
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
log.debug("解密后的原始xml消息内容:{}", plainText);
return fromXml(plainText);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.chanjar.weixin.cp.tp.service;

/**
* <pre>
* 构造第三方应用oauth2链接
* Created by feidian108 on 2023/3/24.
* </pre>
* <p>
* <a href="https://developer.work.weixin.qq.com/document/path/91120">企业微信服务商文档</a>
*/
public interface WxCpTpOAuth2Service {

/**
* <pre>
* 构造第三方应用oauth2链接(静默授权)
* </pre>
* @param redirectUri 授权后重定向的回调链接地址
* @param state 重定向后state参数
* @return url string
*/
String buildAuthorizeUrl(String redirectUri, String state);


/**
* <pre>
* 构造第三方应用oauth2链接
* </pre>
* @param redirectUri 授权后重定向的回调链接地址
* @param state 重定向后state参数
* @param scope 应用授权作用域,snsapi_base:静默授权,snsapi_privateinfo:手动授权
* @return url string
*/
String buildAuthorizeUrl(String redirectUri, String state, String scope);
}
Original file line number Diff line number Diff line change
Expand Up @@ -631,4 +631,11 @@ public interface WxCpTpService {

void setWxCpTpIdConverService(WxCpTpIdConvertService wxCpTpIdConvertService);

/**
* 构造第三方应用oauth2链接
*/
WxCpTpOAuth2Service getWxCpTpOAuth2Service();

void setWxCpTpOAuth2Service(WxCpTpOAuth2Service wxCpTpOAuth2Service);

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public abstract class BaseWxCpTpServiceImpl<H, P> implements WxCpTpService, Requ
private WxCpTpEditionService wxCpTpEditionService = new WxCpTpEditionServiceImpl(this);
private WxCpTpLicenseService wxCpTpLicenseService = new WxCpTpLicenseServiceImpl(this);
private WxCpTpIdConvertService wxCpTpIdConvertService = new WxCpTpIdConvertServiceImpl(this);

private WxCpTpOAuth2Service wxCpTpOAuth2Service = new WxCpTpOAuth2ServiceImpl(this);
/**
* 全局的是否正在刷新access token的锁.
*/
Expand Down Expand Up @@ -753,5 +753,13 @@ public void setWxCpTpIdConverService(WxCpTpIdConvertService wxCpTpIdConvertServi
}


@Override
public WxCpTpOAuth2Service getWxCpTpOAuth2Service() {
return wxCpTpOAuth2Service;
}

@Override
public void setWxCpTpOAuth2Service(WxCpTpOAuth2Service wxCpTpOAuth2Service) {
this.wxCpTpOAuth2Service = wxCpTpOAuth2Service;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package me.chanjar.weixin.cp.tp.service.impl;

import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.util.http.URIUtil;
import me.chanjar.weixin.cp.tp.service.WxCpTpOAuth2Service;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;

import static me.chanjar.weixin.common.api.WxConsts.OAuth2Scope.SNSAPI_BASE;
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.OAuth2.URL_OAUTH2_AUTHORIZE;

@RequiredArgsConstructor
public class WxCpTpOAuth2ServiceImpl implements WxCpTpOAuth2Service {

private final WxCpTpService mainService;


@Override
public String buildAuthorizeUrl(String redirectUri, String state) {
return this.buildAuthorizeUrl(redirectUri, state, SNSAPI_BASE);
}

@Override
public String buildAuthorizeUrl(String redirectUri, String state, String scope) {
StringBuilder url = new StringBuilder(URL_OAUTH2_AUTHORIZE);
url.append("?appid=").append(this.mainService.getWxCpTpConfigStorage().getSuiteId());
url.append("&redirect_uri=").append(URIUtil.encodeURIComponent(redirectUri));
url.append("&response_type=code");
url.append("&scope=").append(scope);
if (state != null) {
url.append("&state=").append(state);
}
url.append("#wechat_redirect");
return url.toString();
}
}

0 comments on commit 36028b8

Please sign in to comment.