事件通知
在VideoWorks中使用工作流处理媒资时,可以按照工作流中的通知设置,向待通知地址发送预定格式的通知消息,表示媒资处理已完成或失败。
在工作流中,除了开始节点,其他节点都可以设置通知。在此工作流运行中,设置了通知的节点运行结束后,会向通知指定地址发送一个通知消息,客户收到消息后,可以按自己的业务逻辑进行相应处理。
在工作流中设置通知前,需要先在控制台通知管理中添加可用的通知。通知地址支持http和https协议,https协议的通知地址需要客户预先准备好证书配置,在进行通知设置前,建议先测试下通知的endpoint是否能正常接受请求。通知请求的body内容如下:
1{
2 "id" : "ntf-xxxx",
3 "endpoint" : "<your notification endpoint>",
4 "content" : "<notification content>",
5 "expire" : 1565235600000
6}
body的每个参数说明如下:
参数 | 说明 |
---|---|
id | 通知id |
endpoint | 用户设置的通知地址 |
content | 通知内容 |
+ mediaId | 处理的媒资id |
+ workflowId | 处理的工作流id |
+ workflowName | 处理的工作流名称 |
+ instnaceId | 运行的实例Id |
+ instanceStatus | 运行的实例状态,SUCCESS/FAILED |
+ taskResult | 发送通知的节点运行信息 |
expire | 通知发送过期时间 |
如果设置了通知验证,通知的请求header中会附带验证信息,用户可以通过验证header的合法性来判断通知请求是否是第三方恶意请求。
例如,用户在VideoWorks的Console中创建了一个通知,通知设置是这样的:
1{
2 "notificationId": "not-xxxxxxx",
3 "name": "noti",
4 "endpoint": "http://qwe.com/vw/callback",
5 "status": "ENABLE",
6 "token": "qweASD123",
7 "authType": "SIGN",
8}
用户的userId为e95e33a028bd49dbb3e08f068dc975d5(可以在Console-用户中心获取)。
用户将此通知设置到一个工作流的发布节点中,在这个工作流处理完成后,VideoWorks将会发送通知给用户。在此实例中,通知的内容如下:
1POST /vw/callback HTTP/1.1
2notification-auth-expire: 1572923085545
3notification-auth-user: e95e33a028bd49dbb3e08f068dc975d5
4notification-auth-token: 67d987295025dccf2ea669b68e0eb5427009e0cb26b8663b19378d3ac77fec64
5content-type: application/json;charset=UTF-8
6content-length: 155
7host: qwe.com
8accept: */*
9user-agent: AHC/2.0
10
11{"mediaId":"mda-jijg31ym688jpuuc","workflowId":"wfs-jkec6badx3d8e6nn","workflowName":"aaaa","instanceId":"ins-jkedr4cu5mmeii2s","instanceStatus":"SUCCESS"}
可以注意到,设置了通知验证后,在通知请求的header中,有3个headernotification-auth-expire, notification-auth-user, notification-auth-token
,这3个header是这样设置的。
notification-auth-user
是用户的userId;
notification-auth-expire
是发送通知时取的时间戳(并非精准的过期时间,仅用于计算token);
notification-auth-token
的计算公式为
1content = POST;endpoint;body;notification-auth-expire;notification-auth-user
2notification-auth-token = SHA256-HEX(token, content)
endpoint是通知设置里的endpoint, body是通知请求的body, SHA256-HEX是一个Hash算法,详见。
为了便于用户开发,下面将给出Java实现的计算notification-auth-token的方法,其他语言版本可以自行实现。
1import javax.crypto.Mac;
2import javax.crypto.SecretKey;
3import javax.crypto.spec.SecretKeySpec;
4import java.nio.charset.Charset;
5import java.nio.charset.StandardCharsets;
6import java.security.InvalidKeyException;
7import java.security.NoSuchAlgorithmException;
8
9import org.apache.commons.codec.binary.Hex;
10
11public class Sha256Helper {
12
13 private static final String HMAC_SHA256 = "HmacSHA256";
14 private static final Charset CHARSET_UTF8 = StandardCharsets.UTF_8;
15
16 public static String hmacSha256(String secretKey, String input) {
17 return hmacSha256(secretKey.getBytes(CHARSET_UTF8), input.getBytes(CHARSET_UTF8));
18 }
19
20 public static String hmacSha256(byte[] secretKey, byte[] input) {
21 return hmacSha256(secretKey, input, 0, input.length);
22 }
23
24 private static String hmacSha256(byte[] secretKey, byte[] input, int offset, int length) {
25 return new String(Hex.encodeHex(mac(input, offset, length, new SecretKeySpec(secretKey, HMAC_SHA256))));
26 }
27
28 private static byte[] mac(byte[] input, int offset, int length, SecretKey secretKey) {
29 try {
30 Mac mac = Mac.getInstance(secretKey.getAlgorithm());
31 mac.init(secretKey);
32 mac.update(input, offset, length);
33 return mac.doFinal();
34 } catch (NoSuchAlgorithmException | InvalidKeyException ex) {
35 throw new RuntimeException(ex);
36 }
37 }
38
39 public static void main(String[] args) {
40 String endpoint = "http://qwe.com/vw/callback"; // notification endpoint
41 String token = "qweASD123";
42 String userId = "e95e33a028bd49dbb3e08f068dc975d5"; // notification-auth-user
43 String expireTime = "1572923085545"; // notification-auth-expire
44 String body = "{\"mediaId\":\"mda-jijg31ym688jpuuc\",\"workflowId\":\"wfs-jkec6badx3d8e6nn\","
45 + "\"workflowName\":\"aaaa\",\"instanceId\":\"ins-jkedr4cu5mmeii2s\",\"instanceStatus\":"
46 + "\"SUCCESS\"}"; // request body
47 String content = String.format("POST;%s;%s;%s;%s", endpoint, body, expireTime, userId);
48 String auth = Sha256Helper.hmacSha256(token, content);
49 System.out.println(auth);
50 }
51}