-
Notifications
You must be signed in to change notification settings - Fork 3
/
web.rb
282 lines (210 loc) · 7.42 KB
/
web.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require 'bcrypt'
require 'securerandom'
require_relative 'registration_service/helpers.rb'
require_relative 'registration_service/sparql_queries.rb'
configure do
set :salt, ENV['MU_APPLICATION_SALT']
set :auto_login_on_registration, ENV['MU_AUTO_LOGIN_ON_REGISTRATION'] == 'true'
end
###
# Vocabularies
###
MU_ACCOUNT = RDF::Vocabulary.new(MU.to_uri.to_s + 'account/')
MU_SESSION = RDF::Vocabulary.new(MU.to_uri.to_s + 'session/')
REGISTRATION_SERVICE_RESOURCE_BASE = SERVICE_RESOURCE_BASE + 'registration-service/'
###
# POST /accounts
#
# Body {"data":{"type":"accounts","attributes":{"name":"John Doe","nickname":"john_doe","password":"secret","password-confirmation":"secret"}}}
# Returns 201 on successful registration
# 400 if the session header is missing
# 400 if body is invalid
# 400 if the given nickname already exists
###
post '/accounts/?' do
content_type 'application/vnd.api+json'
data = @json_body['data']
attributes = data['attributes']
###
# Validate request
###
validate_json_api_content_type(request)
error('Id paramater is not allowed', 403) if not data['id'].nil?
session_uri = session_id_header(request)
error('Session header is missing') if session_uri.nil?
rewrite_url = rewrite_url_header(request)
error('X-Rewrite-URL header is missing') if rewrite_url.nil?
validate_resource_type('accounts', data)
error('Nickname might not be blank') if attributes['nickname'].nil? or attributes['nickname'].empty?
result = select_account_by_nickname(attributes['nickname'])
error('Nickname already exists') if not result.empty?
error('Password might not be blank') if attributes['password'].nil? or attributes['password'].empty?
error('Password and password confirmation do not match') if attributes['password'] != attributes['password-confirmation']
###
# Hash user password with custom salt
###
account_salt = SecureRandom.hex
hashed_password = BCrypt::Password.create attributes['password'] + settings.salt + account_salt
###
# Create user and account
###
user_id = generate_uuid()
account_id = generate_uuid()
create_user_and_account(user_id, attributes['name'], account_id, attributes['nickname'], hashed_password, account_salt)
if settings.auto_login_on_registration
###
# Remove old sessions
###
remove_old_sessions(session_uri)
###
# Insert new session for new account
###
session_id = generate_uuid()
account_uri = create_account_uri(account_id)
insert_new_session_for_account(account_uri, session_uri, session_id)
update_modified(session_uri)
end
status 201
{
links: {
self: rewrite_url.chomp('/') + '/' + account_id
},
data: {
type: 'accounts',
id: account_id,
attributes: {
name: attributes['name'],
nickname: attributes['nickname'].downcase
}
}
}.to_json
end
###
# DELETE /accounts/current
#
# Returns 204 on successful unregistration of the current account
# 400 if session header is missing or session header is invalid
###
delete '/accounts/current/?' do
content_type 'application/vnd.api+json'
###
# Validate session
###
session_uri = session_id_header(request)
error('Session header is missing') if session_uri.nil?
###
# Get account
###
result = select_account_id_by_session(session_uri)
error('Invalid session') if result.empty?
account_id = result.first[:id].to_s
delete_account(account_id)
end
###
# DELETE /accounts/:id
#
# This function will be typically used by a system administrator
#
# Returns 204 on successful unregistration
# 404 if account with given id doesn't exist
###
delete '/accounts/:id/?' do
content_type 'application/vnd.api+json'
delete_account(params['id'])
end
###
# PATCH /accounts/:id
#
# This function will be typically used by a system administrator
#
# Body {"data":{"type":"accounts","id":"1","attributes":{"nickname":"john_doe", "password":"anotherSecret"}}}
# Returns 204 on successful update
# 400 if account is inactive
# 400 if nickname is not unique
# 404 if account with given id doesn't exist
###
patch '/accounts/:id/?' do
content_type 'application/vnd.api+json'
data = @json_body['data']
attributes = data['attributes']
###
# Validate body
###
validate_json_api_content_type(request)
validate_resource_type('accounts', data)
error('Incorrect id. Id does not match the request URL.', 409) if data['id'] != params['id']
result = select_account_by_id(data['id'])
error("No active account found with id #{data['id']}", 404) if result.empty?
account = result.first
unless attributes['nickname'].nil?
result = select_account_by_nickname(attributes['nickname'])
error('Nickname already exists') if not result.empty? and result.first[:uri] != account[:uri]
end
error('User name cannot be updated') if not attributes['name'].nil?
###
# Hash and store new user password with custom salt
###
unless attributes['password'].nil?
account_salt = SecureRandom.hex
hashed_password = BCrypt::Password.create attributes['password'] + settings.salt + account_salt
update_account(account[:uri], hashed_password, account_salt, attributes['nickname'])
end
status 204
end
###
# PATCH /accounts/current/changePassword
#
# Body {"data":{"type":"accounts","id":"current","attributes":{"old-password":"secret", "new-password":"anotherSecret", "new-password-confirmation:"anotherSecret"}}}
# Returns 200 on successful update
# 400 if session header is missing or session header is invalid
# 400 if old password is incorrect
# 400 if new password and new password confirmation do not match
# 400 if account is inactive
###
patch '/accounts/current/changePassword/?' do
content_type 'application/vnd.api+json'
###
# Validate session
###
session_uri = session_id_header(request)
error('Session header is missing') if session_uri.nil?
###
# Validate body
###
data = @json_body['data']
attributes = data['attributes']
validate_json_api_content_type(request)
validate_resource_type('accounts', data)
error('Password might not be blank') if attributes['new-password'].nil? or attributes['new-password'].empty?
error('Password and password confirmation do not match') if attributes['new-password'] != attributes['new-password-confirmation']
###
# Get account
###
result = select_account_id_by_session(session_uri)
error('Invalid session') if result.empty?
account_id = result.first[:id].to_s
result = select_account_by_id(account_id)
error("No active account found with id #{account_id}", 404) if result.empty?
account = result.first
###
# Validate old password
###
result = select_salted_password_and_salt(account[:uri])
error("No password and salt found for account #{account[:uri]}.") if result.empty?
password_and_salt = result.first
db_password = BCrypt::Password.new password_and_salt[:password].to_s
password = attributes['old-password'] + settings.salt + password_and_salt[:salt].to_s
error('Incorrect old password given.') unless db_password == password
###
# Hash and store new user password with custom salt
###
account_salt = SecureRandom.hex
hashed_password = BCrypt::Password.create attributes['new-password'] + settings.salt + account_salt
update_password(account[:uri], hashed_password, account_salt)
status 204
end
###
# Helpers
###
helpers RegistrationService::Helpers
helpers RegistrationService::SparqlQueries