Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVEXP-492: Support webhook calls validation for server template #11

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,35 @@
import com.sinch.sdk.SinchClient;
import com.sinch.sdk.domains.numbers.api.v1.WebHooksService;
import com.sinch.sdk.domains.numbers.models.v1.webhooks.NumberEvent;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

@RestController("Numbers")
public class Controller {

private final SinchClient sinchClient;
private final ServerBusinessLogic webhooksBusinessLogic;

/**
* The secret value used for webhook calls validation have to equals to the one configured at
* number property (HmacSecret).
*
* @see <a
* href="https://developers.sinch.com/java-sdk/apidocs/com/sinch/sdk/domains/numbers/api/v1/CallbackConfigurationService.html#update(com.sinch.sdk.domains.numbers.models.v1.callbacks.request.CallbackConfigurationUpdateRequest)">update
* function Javadoc</a>
*/
@Value("${numbers.webhooks.secret}")
private String webhooksSecret;

@Autowired
public Controller(SinchClient sinchClient, ServerBusinessLogic webhooksBusinessLogic) {
this.sinchClient = sinchClient;
Expand All @@ -26,10 +42,25 @@ public Controller(SinchClient sinchClient, ServerBusinessLogic webhooksBusinessL
value = "/NumbersEvent",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Void> NumbersEvent(@RequestBody String body) {
public ResponseEntity<Void> NumbersEvent(
@RequestHeader Map<String, String> headers, @RequestBody String body) {

WebHooksService webhooks = sinchClient.numbers().v1().webhooks();

// ensure valid authentication to handle request
var validAuth =
webhooks.validateAuthenticationHeader(
webhooksSecret,
// request headers
headers,
// request payload body
body);

// token validation failed
if (!validAuth) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED);
}

// decode the request payload
NumberEvent event = webhooks.parseEvent(body);

Expand Down
3 changes: 3 additions & 0 deletions templates/server/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ credentials:
# - Voice
application-api-key:
application-api-secret:

# Secret value set for Numbers webhooks calls validation
numbers.webhooks.secret:
Loading