forked from hellosign/hellosign-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventCallbackHelper.mustache
48 lines (39 loc) · 1.65 KB
/
EventCallbackHelper.mustache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{{>licenseInfo}}
package {{invokerPackage}};
import com.dropbox.sign.model.EventCallbackRequest;
import com.dropbox.sign.model.EventCallbackRequestEventMetadata;
import org.apache.commons.codec.digest.HmacAlgorithms;
import org.apache.commons.codec.digest.HmacUtils;
{{>generatedAnnotation}}
public class EventCallbackHelper {
public static final String EVENT_TYPE_ACCOUNT_CALLBACK = "account_callback";
public static final String EVENT_TYPE_APP_CALLBACK = "app_callback";
private EventCallbackHelper() {}
/**
* Verify that a callback came from HelloSign.com
*
* @param apiKey
* @param eventCallback
* @return a boolean value indicating whether the callback event is valid
*/
public static boolean isValid(String apiKey, EventCallbackRequest eventCallback) {
return new HmacUtils(HmacAlgorithms.HMAC_SHA_256, apiKey)
.hmacHex(eventCallback.getEvent().getEventTime() + eventCallback.getEvent().getEventType())
.equals(eventCallback.getEvent().getEventHash());
}
/**
* Identifies the callback type, one of "account_callback" or
* "app_callback".
*
* "app_callback" will always include a value for "reported_for_app_id"
*
* @param eventCallback
*/
public static String getCallbackType(EventCallbackRequest eventCallback) {
EventCallbackRequestEventMetadata metadata = eventCallback.getEvent().getEventMetadata();
if (metadata.getReportedForAppId() == null || metadata.getReportedForAppId().isEmpty()) {
return EventCallbackHelper.EVENT_TYPE_ACCOUNT_CALLBACK;
}
return EventCallbackHelper.EVENT_TYPE_APP_CALLBACK;
}
}