/
angdem.rb
68 lines (55 loc) · 1.57 KB
/
angdem.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
class Angdem < Sinatra::Base
helpers do
def protected!
unless env['warden'].user
throw(:halt, [401, "Not authorized\n"])
end
end
end
set :session_secret, "some big secret"
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
get '/logout/?' do
puts 'logging out'
env['warden'].logout
response.set_cookie('username', {:value => nil, :expires => Time.now - 24 * 60 * 60})
status 204
end
post '/login' do
env['warden'].authenticate!
response.set_cookie('username', env['warden'].user.id)
status 204
end
post '/unauthenticated/?' do
status 401
end
get '/signup/exists/:identifier' do
name = params[:identifier]
unused = User.filter({:id => name} | {:email => name}).empty?
(!unused).to_s
end
get '/activate/:code' do
begin
Activation[params[:code]].delete
status 204
rescue
status 404
end
end
post '/signup/:username/:email/:password' do
timestamp = DateTime.now
user = User.create(id: params[:username],
password: BCrypt::Password.create(params[:password]),
email: params[:email],
member_since: timestamp)
activation = Activation.create(code: SecureRandom.hex(32),
user_id: user.id,
created_at: timestamp)
status 201
end
get '/test' do
protected!
"Welcome, authenticated client"
end
end