forked from ubicloud/ubicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclover_api.rb
90 lines (74 loc) · 2.09 KB
/
clover_api.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
# frozen_string_literal: true
class CloverApi < Roda
include CloverBase
plugin :default_headers,
"Content-Type" => "application/json"
plugin :hash_branches
plugin :json
plugin :json_parser
autoload_routes("api")
plugin :not_found do
response["Content-Type"] = "application/json"
{
error: {
code: 404,
type: "ResourceNotFound",
message: "Sorry, we couldn’t find the resource you’re looking for."
}
}.to_json
end
plugin :error_handler do |e|
response["Content-Type"] = "application/json"
error = parse_error(e)
{error: error}.to_json
end
plugin :rodauth do
enable :argon2, :json, :jwt, :active_sessions, :login
only_json? true
use_jwt? true
# Converting rodauth error response to the common error format of the API
json_response_body do |hash|
# In case of an error, rodauth returns the error in the following format
# {
# (required) "error": "There was an error logging in"
# (optional) "field-error": [
# "password",
# "invalid password"
# ]
# }
if json_response_error?
error_message = hash["error"]
type, code = case error_message
when "There was an error logging in"
["InvalidCredentials", 401]
when "invalid JWT format or claim in Authorization header"
["InvalidRequest", 400]
when "Please login to continue"
["LoginRequired", 401]
else
# :nocov:
["AuthenticationError", 401]
# :nocov:
end
hash.clear
hash["error"] = {
"code" => code,
"type" => type,
"message" => error_message
}
end
hash.to_json
end
hmac_secret Config.clover_session_secret
jwt_secret Config.clover_session_secret
argon2_secret { Config.clover_session_secret }
require_bcrypt? false
end
route do |r|
r.rodauth
rodauth.check_active_session
rodauth.require_authentication
@current_user = Account[rodauth.session_value]
r.hash_branches("")
end
end