Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
wslongchen committed Oct 10, 2022
2 parents 3934de5 + 4f703fd commit b6f3128
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 19 deletions.
9 changes: 4 additions & 5 deletions src/wechat/cp/api/oauth2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub struct WechatCpOauth2<'a, T: SessionStore> {

#[allow(unused)]
impl<'a, T: SessionStore> WechatCpOauth2<'a, T> {

#[inline]
pub fn new(client: &WechatCpClient<T>) -> WechatCpOauth2<T> {
WechatCpOauth2 {
Expand Down Expand Up @@ -116,23 +115,23 @@ impl<'a, T: SessionStore> WechatCpOauth2<'a, T> {
let v = self.client.post(WechatCpMethod::Oauth2(CpOauth2Method::GetAuthUserDetail), vec![], json!({USER_TICKET: user_ticket}), RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse::<WechatCpUserDetail>(v)
}

}

//----------------------------------------------------------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WechatCpOauth2UserInfo {
#[serde(alias="OpenId", alias="openid")]
#[serde(alias = "OpenId", alias = "openid")]
pub openid: Option<String>,
pub external_userid: Option<String>,
#[serde(alias="UserId", alias="userid")]
#[serde(alias = "UserId", alias = "userid")]
pub user_id: Option<String>,
pub user_ticket: Option<String>,
pub expires_in: Option<i64>,
#[serde(rename="DeviceId")]
#[serde(rename = "DeviceId")]
pub device_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WechatCpUserDetail {
/// 成员UserID
Expand Down
2 changes: 2 additions & 0 deletions src/wechat/cp/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub enum WechatCpMethod {
GetOrder,
GetOrderList,
GetCustomizedAuthUrl,
GetAuthUserInfo3rd,
Media(CpMediaMethod),
Tag(CpTagMethod),
Agent(CpAgentMethod),
Expand Down Expand Up @@ -58,6 +59,7 @@ impl RequestMethod for WechatCpMethod {
WechatCpMethod::GetCallbackIp => String::from("/cgi-bin/getcallbackip"),
WechatCpMethod::GetAgentConfigTicket => String::from("/cgi-bin/ticket/get?&type=agent_config"),
WechatCpMethod::GetCustomizedAuthUrl => String::from("/cgi-bin/service/get_customized_auth_url"),
WechatCpMethod::GetAuthUserInfo3rd => String::from("/cgi-bin/service/auth/getuserinfo3rd"),
WechatCpMethod::Media(v) => v.get_method(),
WechatCpMethod::ExternalContact(v) => v.get_method(),
WechatCpMethod::Oauth2(v) => v.get_method(),
Expand Down
42 changes: 42 additions & 0 deletions src/wechat/cp/tp/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};

use crate::{LabradorResult, request::RequestType, session::SessionStore, WechatCommonResponse, WechatCpTpClient};
use crate::wechat::cp::constants::{CODE, PROVIDER_ACCESS_TOKEN};
use crate::wechat::cp::method::WechatCpMethod;

#[derive(Debug, Clone)]
pub struct WechatCpTpAuth<'a, T: SessionStore> {
client: &'a WechatCpTpClient<T>,
}

#[allow(unused)]
impl<'a, T: SessionStore> WechatCpTpAuth<'a, T> {
#[inline]
pub fn new(client: &WechatCpTpClient<T>) -> WechatCpTpAuth<T> {
WechatCpTpAuth {
client,
}
}

/// <pre>
/// 获取访问用户身份
/// <a href="https://developer.work.weixin.qq.com/document/path/91121">获取访问用户身份</a>
/// 该接口用于根据code获取成员信息,适用于自建应用与代开发应用
pub async fn get_user_info_auth_3rd(&self, code: &str) -> LabradorResult<WechatCpOauth2UserInfo3rd> {
let v = self.client.get(WechatCpMethod::GetAuthUserInfo3rd, vec![(CODE.to_string(), code.to_string())], RequestType::Json).await?.json::<Value>()?;
WechatCommonResponse::parse::<WechatCpOauth2UserInfo3rd>(v)
}
}

//----------------------------------------------------------------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WechatCpOauth2UserInfo3rd {
pub corpid: Option<String>,
#[serde(alias = "UserId", alias = "userid")]
pub user_id: Option<String>,
pub user_ticket: Option<String>,
pub expires_in: Option<i64>,
pub open_userid: Option<String>,
}
31 changes: 17 additions & 14 deletions src/wechat/cp/tp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod department;
mod user;
mod order;
mod agent;
mod auth;

pub use tag::*;
pub use license::*;
Expand All @@ -22,6 +23,7 @@ pub use department::*;
pub use user::*;
pub use order::*;
pub use agent::*;
use crate::wechat::cp::tp::auth::WechatCpTpAuth;


/// 企业微信第三方应用API
Expand All @@ -44,7 +46,6 @@ pub struct WechatCpTpClient<T: SessionStore> {

#[allow(unused)]
impl<T: SessionStore> WechatCpTpClient<T> {

fn from_client(client: APIClient<T>) -> WechatCpTpClient<T> {
WechatCpTpClient {
corp_id: client.app_key.to_owned(),
Expand All @@ -54,7 +55,7 @@ impl<T: SessionStore> WechatCpTpClient<T> {
suite_id: None,
suite_secret: None,
client,
provider_secret: None
provider_secret: None,
}
}

Expand Down Expand Up @@ -111,7 +112,7 @@ impl<T: SessionStore> WechatCpTpClient<T> {
/// 授权企业的access token相关
fn get_access_token(&self, auth_corp_id: &str) -> String {
let session = self.client.session();
session.get::<_,String>(self.key_with_prefix(auth_corp_id) + ACCESS_TOKEN_KEY, None).unwrap_or(None).unwrap_or_default()
session.get::<_, String>(self.key_with_prefix(auth_corp_id) + ACCESS_TOKEN_KEY, None).unwrap_or(None).unwrap_or_default()
}

/// <pre>
Expand Down Expand Up @@ -140,7 +141,7 @@ impl<T: SessionStore> WechatCpTpClient<T> {
let timestamp = current_timestamp();
let expires_at: i64 = session.get(&expires_key, Some(timestamp))?.unwrap_or_default();
if expires_at <= timestamp {
return Err(LabraError::ApiError("invaild suite ticket".to_string()))
return Err(LabraError::ApiError("invaild suite ticket".to_string()));
}
Ok(token)
}
Expand Down Expand Up @@ -199,7 +200,8 @@ impl<T: SessionStore> WechatCpTpClient<T> {
"suite_secret": self.suite_secret,
"suite_ticket": suite_ticket
});
let result = self.client.post(WechatCpMethod::GetSuiteToken, vec![], req, RequestType::Json).await?.json::<WechatCpSuiteAccessTokenResponse>()?;
let result = self.client.post(WechatCpMethod::GetSuiteToken, vec![], req, RequestType::Json).await?.json::<Value>()?;
let result = WechatCommonResponse::parse::<WechatCpSuiteAccessTokenResponse>(result)?;
let token = result.suite_access_token;
let expires_in = result.expires_in;
// 预留200秒的时间
Expand Down Expand Up @@ -280,12 +282,11 @@ impl<T: SessionStore> WechatCpTpClient<T> {
}



/// <pre>
/// 获取企业凭证
/// </pre>
pub async fn get_corp_token(&self, auth_corpid: &str, permanent_code: &str) -> LabradorResult<AccessTokenResponse> {
self.get_corp_token_force(auth_corpid, permanent_code,false).await
self.get_corp_token_force(auth_corpid, permanent_code, false).await
}

/// <pre>
Expand Down Expand Up @@ -314,7 +315,7 @@ impl<T: SessionStore> WechatCpTpClient<T> {
session.set(&expires_key, expires_at, Some(expires_in as usize));
Ok(result)
} else {
Ok(AccessTokenResponse{ access_token: token.to_string(), expires_in: expires_at })
Ok(AccessTokenResponse { access_token: token.to_string(), expires_in: expires_at })
}
}

Expand Down Expand Up @@ -485,8 +486,8 @@ impl<T: SessionStore> WechatCpTpClient<T> {
let noncestr = get_nonce_str();
let signature = WechatCrypto::get_sha1_sign(&vec!["jsapi_ticket=".to_string() + &jsapi_ticket,
"noncestr=".to_string() + &noncestr,
"timestamp=".to_string() + &timestamp.to_string(),"url=".to_string() + &url].join("&"));
JsapiSignature{
"timestamp=".to_string() + &timestamp.to_string(), "url=".to_string() + &url].join("&"));
JsapiSignature {
app_id: auth_corp_id.to_string(),
nonce_str: noncestr,
url: url.to_string(),
Expand Down Expand Up @@ -567,6 +568,11 @@ impl<T: SessionStore> WechatCpTpClient<T> {
pub fn agent(&self) -> WechatCpTpAgent<T> {
WechatCpTpAgent::new(self)
}

/// 身份
pub fn auth(&self) -> WechatCpTpAuth<T> {
WechatCpTpAuth::new(self)
}
}

//----------------------------------------------------------------------------------------------------------------------------
Expand All @@ -592,7 +598,7 @@ pub struct WechatCpThirdPermanentCodeInfo {
pub edition_info: Option<EditionInfo>,
pub expires_in: Option<i64>,
/// 安装应用时,扫码或者授权链接中带的state值。详见state说明
pub state: Option<String>
pub state: Option<String>,
}


Expand Down Expand Up @@ -731,7 +737,6 @@ pub struct WechatCpThirdPreauthCode {
}



/// 服务商模式获取授权信息
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct WechatCpThirdAuthInfo {
Expand All @@ -746,8 +751,6 @@ pub struct WechatCpThirdAuthInfo {
}




/// 服务商模式获取授权信息
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DealerCorpInfo {
Expand Down

0 comments on commit b6f3128

Please sign in to comment.