diff --git a/library/src/social/google.rs b/library/src/social/google.rs index cf75575..eae9ab7 100644 --- a/library/src/social/google.rs +++ b/library/src/social/google.rs @@ -24,7 +24,7 @@ pub struct GoogleSocial { // 假设GOOGLE_PUBLIC_CERT_URL是Google提供的公钥URL const GOOGLE_PUBLIC_CERT_URL: &str = "https://www.googleapis.com/oauth2/v3/certs"; -#[derive(Debug, Default)] +#[derive(Debug, Default, Deserialize)] pub struct GoogleJwtProfile { // iss (issuer):签发人 pub iss: String, @@ -48,6 +48,7 @@ pub struct GoogleJwtProfile { } impl GoogleJwtProfile { + #[allow(unused)] fn new() -> Self { GoogleJwtProfile { ..Default::default() @@ -57,49 +58,7 @@ impl GoogleJwtProfile { impl From for GoogleJwtProfile { fn from(value: Value) -> Self { - let mut google_jwt_profile = GoogleJwtProfile::new(); - if let Some(value) = value.get("iss") { - google_jwt_profile.iss = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("sub") { - google_jwt_profile.sub = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("azp") { - google_jwt_profile.azp = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("aud") { - google_jwt_profile.aud = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("iat") { - google_jwt_profile.iat = value.as_i64().unwrap_or_default(); - } - if let Some(value) = value.get("exp") { - google_jwt_profile.exp = value.as_i64().unwrap_or_default(); - } - if let Some(value) = value.get("email") { - google_jwt_profile.email = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("email_verified") { - google_jwt_profile.email_verified = value.as_bool().unwrap_or_default(); - } - if let Some(value) = value.get("at_hash") { - google_jwt_profile.at_hash = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("name") { - google_jwt_profile.name = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("picture") { - google_jwt_profile.picture = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("given_name") { - google_jwt_profile.given_name = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("family_name") { - google_jwt_profile.family_name = value.as_str().unwrap().to_string(); - } - if let Some(value) = value.get("locale") { - google_jwt_profile.locale = value.as_str().unwrap().to_string(); - } + let google_jwt_profile: GoogleJwtProfile = serde_json::from_value(value).expect("解析GoogleJwtProfile json失败"); google_jwt_profile } } @@ -167,9 +126,7 @@ impl GoogleSocial { let kid = kid.unwrap(); // 根据kid找到正确的公钥 - let key = public_keys - .get(&kid) - .ok_or_else(|| Box::new(ResErr::social("校验Token失败,未找到正确的公钥")))?; + let key = public_keys.get(&kid).ok_or_else(|| Box::new(ResErr::social("校验Token失败,未找到正确的公钥")))?; tracing::debug!("public key : {:?}", key); // TODO: diff --git a/library/src/social/wechat.rs b/library/src/social/wechat.rs index 5d606ef..9499ae3 100644 --- a/library/src/social/wechat.rs +++ b/library/src/social/wechat.rs @@ -162,7 +162,7 @@ impl WechatSocial { /// /// https://developers.weixin.qq.com/minigame/dev/api-backend/open-api/login/auth.checkSessionKey.html /// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/checkSessionKey.html - /// todo 使用的hmac签名是否正确,签名结果转换成字符串是否正确,目前未经验证 + /// TODO 使用的hmac签名是否正确,签名结果转换成字符串是否正确,目前未经验证 pub async fn check_session(&self, session_key: &str, open_id: &str) -> SocialResult<()> { let token = self.get_access_token().await?; let mut mac = diff --git a/server/src/controller/social_wx_controller.rs b/server/src/controller/social_wx_controller.rs index ff005b5..ba3c0f3 100644 --- a/server/src/controller/social_wx_controller.rs +++ b/server/src/controller/social_wx_controller.rs @@ -12,7 +12,7 @@ pub async fn get_wechat_access_token(context: Context) -> ResResult ResResult ResResult { let lang_tag = context.get_lang_tag(); let result = match WECHAT_SOCIAL.code_2_session(&code).await { @@ -42,7 +42,7 @@ pub async fn code_2_session(context: Context, code: String) -> ResResult ResResult<()> { let lang_tag = context.get_lang_tag(); let session_key = wx_login_info.session_key.unwrap();