v1.1.2
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "dingtalk"
|
name = "dingtalk"
|
||||||
version = "1.1.1"
|
version = "1.1.2"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "DingTalk Robot Util, Send text/markdown/link messages using DingTalk robot, 钉钉机器人"
|
description = "DingTalk Robot Util, Send text/markdown/link messages using DingTalk robot, 钉钉机器人"
|
||||||
@@ -11,11 +11,12 @@ license = "MIT"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.11.0"
|
base64 = "0.11.0"
|
||||||
rust-crypto = "0.2.36"
|
|
||||||
reqwest = "0.10.0"
|
reqwest = "0.10.0"
|
||||||
urlencoding = "1.0.0"
|
urlencoding = "1.0.0"
|
||||||
json = "0.12.0"
|
json = "0.12.0"
|
||||||
futures = "0.3.1"
|
futures = "0.3.1"
|
||||||
|
hmac = "0.7.1"
|
||||||
|
sha2 = "0.8.1"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tokio-test = "0.2.0"
|
tokio-test = "0.2.0"
|
||||||
|
|||||||
@@ -89,6 +89,8 @@ dt.send_message(&DingTalkMessage::new_action_card("action card 002", "action car
|
|||||||
|
|
||||||
#### Changelog
|
#### Changelog
|
||||||
|
|
||||||
|
* v1.1.2
|
||||||
|
* Use `hmac` and `sha2` crates, replace `rust-crypto` crate
|
||||||
* v1.1.1
|
* v1.1.1
|
||||||
* `DingTalk::from_json` add `direct_url`
|
* `DingTalk::from_json` add `direct_url`
|
||||||
* Fix problems by clippy
|
* Fix problems by clippy
|
||||||
|
|||||||
30
src/lib.rs
30
src/lib.rs
@@ -11,13 +11,10 @@ use std::{
|
|||||||
ErrorKind,
|
ErrorKind,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
use crypto::{
|
use sha2::Sha256;
|
||||||
mac::{
|
use hmac::{
|
||||||
|
Hmac,
|
||||||
Mac,
|
Mac,
|
||||||
MacResult,
|
|
||||||
},
|
|
||||||
hmac::Hmac,
|
|
||||||
sha2::Sha256,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;
|
pub type XResult<T> = Result<T, Box<dyn std::error::Error>>;
|
||||||
@@ -448,7 +445,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
/// Direct send JSON message
|
/// Direct send JSON message
|
||||||
pub async fn send(&self, json_message: &str) -> XResult<()> {
|
pub async fn send(&self, json_message: &str) -> XResult<()> {
|
||||||
let client = reqwest::Client::new();
|
let client = reqwest::Client::new();
|
||||||
let response = match client.post(&self.generate_signed_url())
|
let response = match client.post(&self.generate_signed_url()?)
|
||||||
.header(CONTENT_TYPE, APPLICATION_JSON_UTF8)
|
.header(CONTENT_TYPE, APPLICATION_JSON_UTF8)
|
||||||
.body(json_message.as_bytes().to_vec())
|
.body(json_message.as_bytes().to_vec())
|
||||||
.send().await {
|
.send().await {
|
||||||
@@ -463,9 +460,9 @@ impl <'a> DingTalk<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Generate signed dingtalk webhook URL
|
/// Generate signed dingtalk webhook URL
|
||||||
pub fn generate_signed_url(&self) -> String {
|
pub fn generate_signed_url(&self) -> XResult<String> {
|
||||||
if !self.direct_url.is_empty() {
|
if !self.direct_url.is_empty() {
|
||||||
return self.direct_url.into();
|
return Ok(self.direct_url.into());
|
||||||
}
|
}
|
||||||
let mut signed_url = String::with_capacity(1024);
|
let mut signed_url = String::with_capacity(1024);
|
||||||
signed_url.push_str(self.default_webhook_url);
|
signed_url.push_str(self.default_webhook_url);
|
||||||
@@ -486,7 +483,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
if self.sec_token != "" {
|
if self.sec_token != "" {
|
||||||
let timestamp = &format!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis());
|
let timestamp = &format!("{}", SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis());
|
||||||
let timestamp_and_secret = &format!("{}\n{}", timestamp, self.sec_token);
|
let timestamp_and_secret = &format!("{}\n{}", timestamp, self.sec_token);
|
||||||
let hmac_sha256 = base64::encode(calc_hmac_sha256(self.sec_token.as_bytes(), timestamp_and_secret.as_bytes()).code());
|
let hmac_sha256 = base64::encode(&calc_hmac_sha256(self.sec_token.as_bytes(), timestamp_and_secret.as_bytes())?[..]);
|
||||||
|
|
||||||
signed_url.push_str("×tamp=");
|
signed_url.push_str("×tamp=");
|
||||||
signed_url.push_str(timestamp);
|
signed_url.push_str(timestamp);
|
||||||
@@ -494,7 +491,7 @@ impl <'a> DingTalk<'a> {
|
|||||||
signed_url.push_str(&urlencoding::encode(&hmac_sha256));
|
signed_url.push_str(&urlencoding::encode(&hmac_sha256));
|
||||||
}
|
}
|
||||||
|
|
||||||
signed_url
|
Ok(signed_url)
|
||||||
}
|
}
|
||||||
|
|
||||||
// SAFE? may these codes cause memory leak?
|
// SAFE? may these codes cause memory leak?
|
||||||
@@ -504,10 +501,13 @@ impl <'a> DingTalk<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// calc hma_sha256 digest
|
/// calc hma_sha256 digest
|
||||||
fn calc_hmac_sha256(key: &[u8], message: &[u8]) -> MacResult {
|
fn calc_hmac_sha256(key: &[u8], message: &[u8]) -> XResult<Vec<u8>> {
|
||||||
let mut hmac = Hmac::new(Sha256::new(), key);
|
let mut mac = match Hmac::<Sha256>::new_varkey(key) {
|
||||||
hmac.input(message);
|
Ok(m) => m,
|
||||||
hmac.result()
|
Err(e) => return Err(Box::new(Error::new(ErrorKind::Other, format!("Hmac error: {}", e))))
|
||||||
|
};
|
||||||
|
mac.input(message);
|
||||||
|
Ok(mac.result().code().to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user