@Timed @GET @Path("/{transport}/code/{number}") public Response createAccount( @PathParam("transport") String transport, @PathParam("number") String number) throws IOException, RateLimitExceededException { if (!Util.isValidNumber(number)) { logger.debug("Invalid number: " + number); throw new WebApplicationException(Response.status(400).build()); } switch (transport) { case "sms": rateLimiters.getSmsDestinationLimiter().validate(number); break; case "voice": rateLimiters.getVoiceDestinationLimiter().validate(number); break; default: throw new WebApplicationException(Response.status(422).build()); } VerificationCode verificationCode = generateVerificationCode(); pendingAccounts.store(number, verificationCode.getVerificationCode()); if (transport.equals("sms")) { smsSender.deliverSmsVerification(number, verificationCode.getVerificationCodeDisplay()); } else if (transport.equals("voice")) { smsSender.deliverVoxVerification(number, verificationCode.getVerificationCodeSpeech()); } return Response.ok().build(); }
@Timed @PUT @Consumes(MediaType.APPLICATION_JSON) @Path("/code/{verification_code}") public void verifyAccount( @PathParam("verification_code") String verificationCode, @HeaderParam("Authorization") String authorizationHeader, @Valid AccountAttributes accountAttributes) throws RateLimitExceededException { try { AuthorizationHeader header = AuthorizationHeader.fromFullHeader(authorizationHeader); String number = header.getNumber(); String password = header.getPassword(); rateLimiters.getVerifyLimiter().validate(number); Optional<String> storedVerificationCode = pendingAccounts.getCodeForNumber(number); if (!storedVerificationCode.isPresent() || !verificationCode.equals(storedVerificationCode.get())) { throw new WebApplicationException(Response.status(403).build()); } Device device = new Device(); device.setId(Device.MASTER_ID); device.setAuthenticationCredentials(new AuthenticationCredentials(password)); device.setSignalingKey(accountAttributes.getSignalingKey()); device.setFetchesMessages(accountAttributes.getFetchesMessages()); device.setRegistrationId(accountAttributes.getRegistrationId()); Account account = new Account(); account.setNumber(number); account.setSupportsSms(accountAttributes.getSupportsSms()); account.addDevice(device); accounts.create(account); pendingAccounts.remove(number); logger.debug("Stored device..."); } catch (InvalidAuthorizationHeaderException e) { logger.info("Bad Authorization Header", e); throw new WebApplicationException(Response.status(401).build()); } }