forked from mu-semtech/#-service
-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.rb
237 lines (192 loc) · 4.93 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
require_relative 'login_service/sparql_queries.rb'
## Monkeypatch sparql-client with mu-auth-sudo header
require_relative 'auth_extensions/sudo'
include AuthExtensions::Sudo
###
# Vocabularies
###
MU_ACCOUNT = RDF::Vocabulary.new(MU.to_uri.to_s + 'account/')
MU_SESSION = RDF::Vocabulary.new(MU.to_uri.to_s + 'session/')
BESLUIT = RDF::Vocabulary.new('http://data.vlaanderen.be/ns/besluit#')
###
# ENV Variables
###
GROUP_TYPE = ENV['GROUP_TYPE'] || BESLUIT.Bestuurseenheid
###
# POST /sessions
#
# Body
# data: {
# relationships: {
# account:{
# data: {
# id: "account_id",
# type: "accounts"
# }
# }
# },
# type: "sessions"
# }
# Returns 201 on successful login
# 400 if session header is missing
# 400 on login failure (incorrect user/password or inactive account)
###
post '/sessions/' do
content_type 'application/vnd.api+json'
###
# Validate headers
###
validate_json_api_content_type(request)
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 request
###
data = @json_body['data']
validate_resource_type('sessions', data)
error('Id paramater is not allowed', 400) if not data['id'].nil?
error('exactly one account should be linked') unless data.dig("relationships","account", "data", "id")
error('exactly one group should be linked') unless data.dig("relationships","group", "data", "id")
###
# Validate login
###
result = select_account(data["relationships"]["account"]["data"]["id"])
error('account not found.', 400) if result.empty?
account = result.first
group_id = data["relationships"]["group"]["data"]["id"]
result = select_group(group_id)
error('group not found', 400) if result.empty?
group = result.first
result = select_roles(data["relationships"]["account"]["data"]["id"])
error('roles not found', 400) if result.empty?
roles = result.map { |r| r[:role].to_s }
###
# Remove old sessions
###
remove_old_sessions(session_uri)
###
# Insert new session
###
session_id = generate_uuid()
insert_new_session_for_account(account[:uri].to_s, session_uri, session_id, group[:group].to_s, group_id, roles)
status 201
headers['mu-auth-allowed-groups'] = 'CLEAR'
{
links: {
self: rewrite_url.chomp('/') + '/current'
},
data: {
type: 'sessions',
id: session_id,
attributes: {
roles: roles
}
},
relationships: {
account: {
links: {
related: "/accounts/#{data['relationships']['account']['data']['id']}"
},
data: {
type: "accounts",
id: data['relationships']['account']['data']['id']
}
},
group: {
links: {
related: "/groups/#{data['relationships']['group']['data']['id']}"
},
data: {
type: "groups",
id: data['relationships']['group']['data']['id']
}
}
}
}.to_json
end
###
# DELETE /sessions/current
#
# Returns 204 on successful logout
# 400 if session header is missing or session header is invalid
###
delete '/sessions/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_by_session(session_uri)
error('Invalid session') if result.empty?
account = result.first[:account].to_s
###
# Remove session
###
delete_current_session(account)
status 204
headers['mu-auth-allowed-groups'] = 'CLEAR'
end
###
# GET /sessions/current
#
# Returns 200 if current session exists
# 400 if session header is missing or session header is invalid
###
get '/sessions/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_by_session(session_uri)
error('Invalid session') if result.empty?
session = result.first
rewrite_url = rewrite_url_header(request)
status 200
{
links: {
self: rewrite_url.chomp('/') + '/current'
},
data: {
type: 'sessions',
id: session[:session_uuid],
attributes: {
roles: session[:roles].to_s.split(',')
}
},
relationships: {
account: {
links: {
related: "/accounts/#{session[:account_uuid]}"
},
data: {
type: "accounts",
id: session[:account_uuid]
}
},
group: {
links: {
related: "/groups/#{session[:group_uuid]}"
},
data: {
type: "groups",
id: session[:group_uuid]
}
}
}
}.to_json
end
###
# Helpers
###
helpers LoginService::SparqlQueries