-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsecret_server.rb
143 lines (110 loc) · 3.23 KB
/
secret_server.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'json'
require 'sinatra/base'
require "sinatra/contrib"
require 'rack-flash'
require 'cryptor'
require 'cryptor/symmetric_encryption/ciphers/xsalsa20poly1305'
require 'redcarpet'
require 'haml'
if ENV["RACK_ENV"] == "production"
require "rack-json-logs"
else
require 'byebug'
require "letter_opener"
end
require_relative "objects/secret"
require_relative "services/ses_mailer"
require_relative "services/secret_store"
require_relative "services/mail_notifier"
class SecretServer < Sinatra::Base
register Sinatra::Contrib
FLASH_TYPES = %i[danger warning info success]
set :session_secret, ENV["SESSION_SECRET"]
use Rack::Session::Cookie, key: "_rack_session",
path: "/",
expire_after: 2592000, # In seconds
secret: settings.session_secret
use Rack::Flash, accessorize: FLASH_TYPES
configure :development, :test do
set :redis_url, ENV.fetch('REDIS_URL', "redis://redis:6379")
set :mailer, [LetterOpener::DeliveryMethod, location: File.expand_path('../tmp/letter_opener', __FILE__)]
end
configure :production do
set :redis_url, ENV["REDIS_URL"]
set :mailer, [SESMailer, region: ENV.fetch('AWS_REGION', 'us-east-1')]
use Rack::JsonLogs
end
set :redis, Redis.new(url: settings.redis_url)
set :store, SecretStore.new(settings.redis)
Mail.defaults do
delivery_method *SecretServer.settings.mailer
end
def store
settings.store
end
def slack_request?
slack_token = ENV['SLACK_TOKEN']
!slack_token.nil? && params[:token] == slack_token
end
def timed?
params[:expire] == "time"
end
def notify_requested?
!params[:notify].nil? && params[:notify] != ""
end
def generate_share_url(fingerprint)
"#{request.scheme}://#{request.host}:#{request.port}/read/#{fingerprint}"
end
get "/" do
haml :write
end
get "/health" do
status = 200
body = ""
end
get "/about" do
markdown :info, layout_engine: :haml
end
post "/save", provides: [:html, :json] do
secret = Secret.new params[:text],
is_ttl: timed?,
ttl: params[:time],
notify: notify_requested?,
email: params[:notify_email]
key = store.save secret
# Generate url with key
url = generate_share_url(key)
if slack_request?
"<#{url}>"
else
respond_with :share, { url: url, time: secret.expire_in_words, key: key }
end
end
get "/read/not_found" do
haml :four_oh_four
end
get "/read/:fingerprint" do
fingerprint = params[:fingerprint]
redirect "/read/not_found" unless store.exists?(fingerprint)
haml :read, locals: { key: fingerprint }
end
# JSON fetch
get "/note/:fingerprint" do
fingerprint = params[:fingerprint]
secret = store.fetch(fingerprint)
MailNotifier.notify_read(secret.email, fingerprint, is_ttl: secret.auto_expire?) if secret.notify?
content_type :json
{
note: secret.message.force_encoding(Encoding::UTF_8),
ttl: secret.expire_in_words
}.to_json
end
get '/destroy/:key' do
store.destroy params[:key]
flash[:success] = "Secret has been destroyed!"
redirect "/"
end
not_found do
"\"You don't belong here.\" -Radiohead"
end
end