Skip to content

Commit

Permalink
Add registration flow
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Aug 20, 2024
1 parent 1c1333d commit 98d254c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class ApplicationConfiguration {

//final HttpClient javaHttpClient = HttpClient.newHttpClient();
final VonageClient vonageClient;
final URI hostUrl;
final String brand = "Vonage Hackathon Demo";
int port;

@Bean
public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
Expand Down Expand Up @@ -65,6 +68,9 @@ private static String getEnvWithAlt(String primary, String fallbackEnv) {
var apiSecret = getEnvWithAlt("VONAGE_API_SECRET", "VCR_API_ACCOUNT_SECRET");
var applicationId = getEnvWithAlt("VONAGE_APPLICATION_ID", "VCR_API_APPLICATION_ID");
var privateKey = getEnvWithAlt("VONAGE_PRIVATE_KEY_PATH", "VCR_PRIVATE_KEY");
hostUrl = URI.create(getEnv("VONAGE_HACKATHON_SERVER_URL").map(
self -> port > 80 ? self + ":" + port : self
).orElseThrow());

if (credentials != null) {
if (credentials.apiKey != null && !credentials.apiKey.isEmpty()) {
Expand Down
48 changes: 41 additions & 7 deletions src/main/java/com/vonage/hackathon/rce/ApplicationController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@
import com.vonage.client.messages.InboundMessage;
import com.vonage.client.messages.MessageStatus;
import com.vonage.client.messages.sms.SmsTextRequest;
import com.vonage.client.verify2.SilentAuthWorkflow;
import com.vonage.client.verify2.VerificationCallback;
import com.vonage.client.verify2.VerificationRequest;
import com.vonage.client.verify2.VerificationStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;

@Controller
public final class ApplicationController {
private final Logger logger = Logger.getLogger("controller");

String verifiedNumber = System.getenv("TO_NUMBER");
private final Map<UUID, String> pendingRegistrations = new LinkedHashMap<>(2);
String verifiedNumber;

@Autowired
private ApplicationConfiguration configuration;
Expand All @@ -25,7 +34,7 @@ private String standardWebhookResponse() {
}

private void sendMessage(String from, String text) {
int threshold = 950, length = text.length();
int threshold = 1000, length = text.length();
if (length > threshold) {
logger.info("Long message ("+length+" characters). Sending in parts...");
}
Expand All @@ -34,12 +43,10 @@ private void sendMessage(String from, String text) {
parts[i] = text.substring(i * threshold, Math.min(length, (i + 1) * threshold));
}

var client = configuration.vonageClient.getMessagesClient();
var builder = SmsTextRequest.builder().from(from).to(verifiedNumber);

for (var part : parts) {
logger.info("Message sent: " + configuration.vonageClient.getMessagesClient()
.sendMessage(builder.text(part).build()).getMessageUuid()
);
logger.info("Message sent: " + client.sendMessage(builder.text(part).build()).getMessageUuid());
}
}

Expand All @@ -56,6 +63,34 @@ public String messageStatus(@RequestBody MessageStatus status) {
return standardWebhookResponse();
}

@ResponseBody
@GetMapping("/register")
public URI beginRegistration(@RequestParam String number) {
if (configuration.vonageClient.getSimSwapClient().checkSimSwap(number)) {
logger.warning("SIM Swap detected for number: "+number);
}
var redirectUrl = configuration.hostUrl.resolve("/register/complete").toString();
var request = configuration.vonageClient.getVerify2Client().sendVerification(
VerificationRequest.builder()
.addWorkflow(new SilentAuthWorkflow(number, true, redirectUrl))
.brand(configuration.brand).build()
);
logger.info("Verification sent: "+request.getRequestId());
pendingRegistrations.put(request.getRequestId(), number);
return request.getCheckUrl();
}

@ResponseBody
@PostMapping("/webhooks/verify/complete")
public String completeRegistration(@RequestBody VerificationCallback callback) {
logger.info("Received verification status update: "+callback.toJson());
if (callback.getStatus() == VerificationStatus.COMPLETED) {
logger.info("Registered number: " + (verifiedNumber = pendingRegistrations.get(callback.getRequestId())));
sendMessage(configuration.brand, "Registration successful!");
}
return standardWebhookResponse();
}

@SuppressWarnings("StatementWithEmptyBody")
@ResponseBody
@PostMapping("/webhooks/messages/inbound")
Expand All @@ -68,7 +103,6 @@ public String inboundMessage(@RequestBody InboundMessage inbound) throws IOExcep
.command("sh", "-cr", command).start();

String parsedOutput;

try (var stream = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
var output = new StringBuilder();
for (String line; (line = stream.readLine()) != null; output.append(line).append("\n"));
Expand Down

0 comments on commit 98d254c

Please sign in to comment.