-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.rb
116 lines (94 loc) · 3.47 KB
/
app.rb
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
require "sensible_logging"
require "sinatra/base"
require "net/http"
require "logger"
require "mail"
require "notifications/client"
require "./lib/loader"
class App < Sinatra::Base
if ENV.key?("SENTRY_DSN")
use Sentry::Rack::CaptureExceptions
end
register Sinatra::SensibleLogging
sensible_logging(
logger: Logger.new($stdout),
)
configure do
set :log_level, Logger::DEBUG
set :govnotify_token, ENV["GOVNOTIFY_BEARER_TOKEN"]
end
configure :production, :staging do
set :dump_errors, false
end
configure :production do
set :log_level, Logger::INFO
end
get "/healthcheck" do
Notifications::NotifyTemplates.verify_templates
"Healthy"
rescue StandardError => e
logger.error(e.message)
halt 500, e.message
end
post "/user-signup/email-notification" do
request.body.rewind
raise UserSignupError, "Unexpected request: \n #{request.body.read}" if request_invalid?(request)
sns_message = WifiUser::SnsMessage.new(body: request.body.read)
halt 200, "" if sns_message.type != "Notification" || sns_message.message_id == "AMAZON_SES_SETUP_NOTIFICATION"
logger.info(sns_message.to_s) if sns_message.type == "SubscriptionConfirmation"
if sns_message.sponsor_request?
WifiUser::UseCase::SponsorJourneyHandler.new(sns_message:).execute
else
WifiUser::UseCase::EmailJourneyHandler.new(from_address: sns_message.from_address).execute
end
rescue UserSignupError => e
logger.warn(e.message)
halt 200, ""
rescue StandardError => e
logger.error(e.message)
raise
end
post "/user-signup/sms-notification/notify" do
request.body.rewind
halt(401, "") unless is_govnotify_token_valid?
payload = JSON.parse(request.body.read)
source = payload["source_number"]
destination = payload["destination_number"]
message = payload["message"]
logger.info("Processing SMS on /user-signup/sms-notification/notify from #{source} to #{destination} with message #{message}")
raise UserSignupError, "Source number is unavailable" if source.nil?
raise UserSignupError, "Destination number is unavailable" if destination.nil?
raise UserSignupError, "SMS loop detected: #{destination}" if numbers_are_equal?(source, destination)
raise UserSignupError, "Too many messages received from #{source} - (possible bot loop)" if sender_is_repetitive?(source, message)
WifiUser::UseCase::SmsResponse.new(logger:).execute(
contact: source,
sms_content: message,
)
""
rescue UserSignupError => e
logger.error(e.message)
""
end
def numbers_are_equal?(number1, number2)
WifiUser::PhoneNumber.internationalise(number1) == WifiUser::PhoneNumber.internationalise(number2)
end
def sender_is_repetitive?(source, message)
repetitive_sms_checker = WifiUser::UseCase::RepetitiveSmsChecker.new(
smslog_model: WifiUser::Repository::Smslog.new,
)
repetitive_sms_checker.execute(WifiUser::PhoneNumber.internationalise(source), message)
end
def is_govnotify_token_valid?
env.fetch("HTTP_AUTHORIZATION") == "Bearer #{settings.govnotify_token}"
end
def request_invalid?(request)
!request_valid?(request)
end
def request_valid?(request)
# For now, we only care that the correct header is set to see if we're
# actually dealing with a notification.
# There is much more that should be in here.
request.has_header?("HTTP_X_AMZ_SNS_MESSAGE_TYPE") \
&& request.get_header("HTTP_X_AMZ_SNS_MESSAGE_TYPE") == "Notification"
end
end