rodauth-json 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +27 -0
- data/LICENSE.txt +21 -0
- data/README.md +44 -0
- data/Rakefile +5 -0
- data/lib/rodauth/features/json.rb +221 -0
- data/lib/rodauth/features/json_cors.rb +53 -0
- data/rodauth-json.gemspec +23 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 785c1bf53551151eb72d8477c4402ca761b47f5ddc18ff40d233257d6e6e7fc0
|
4
|
+
data.tar.gz: e5a4175561eba6ba14cbad6e7ceca40c32b564e7f1854773e4c45b3cefeabc1d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e61fe9df18448b6891f001214d7ea635046eb7fbfdb1e0df6ce23c986079299ff72bc46c191fee8dae7179a0ca4ea42c4f12c1fabaf5f7bf4928403080165347
|
7
|
+
data.tar.gz: 004c200c4eb01bb54467900f400d9fbb5e50c2f7d17eed195c2812b749700bccd68f963670fbd8fa1d2592154567ccd88f474d8f8dcc3d68a14e946b12c5cb9e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rodauth-json (0.1.0)
|
5
|
+
rodauth (~> 2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
rack (2.2.3)
|
11
|
+
rake (12.3.3)
|
12
|
+
roda (3.38.0)
|
13
|
+
rack
|
14
|
+
rodauth (2.6.0)
|
15
|
+
roda (>= 2.6.0)
|
16
|
+
sequel (>= 4)
|
17
|
+
sequel (5.39.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
rake (~> 12.0)
|
24
|
+
rodauth-json!
|
25
|
+
|
26
|
+
BUNDLED WITH
|
27
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Koell
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Rodauth::Json
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rodauth/json`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'rodauth-json'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install rodauth-json
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rodauth-json. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rodauth-json/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
41
|
+
|
42
|
+
## Code of Conduct
|
43
|
+
|
44
|
+
Everyone interacting in the Rodauth::Json project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rodauth-json/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,221 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
module Rodauth
|
4
|
+
Feature.define(:json, :Json) do
|
5
|
+
translatable_method :json_non_post_error_message, 'non-POST method used in JSON API'
|
6
|
+
translatable_method :json_not_accepted_error_message, 'Unsupported Accept header. Must accept "application/json" or compatible content type'
|
7
|
+
translatable_method :non_json_request_error_message, 'Only JSON format requests are allowed'
|
8
|
+
auth_value_method :json_request_content_type_regexp, /\bapplication\/(?:vnd\.api\+)?json\b/i
|
9
|
+
auth_value_method :json_accept_regexp, /(?:(?:\*|\bapplication)\/\*|\bapplication\/(?:vnd\.api\+)?json\b)/i
|
10
|
+
auth_value_method :json_response_content_type, 'application/json'
|
11
|
+
auth_value_method :json_response_error_status, 400
|
12
|
+
auth_value_method :json_response_custom_error_status?, true
|
13
|
+
auth_value_method :json_response_status_key, 'status'
|
14
|
+
auth_value_method :json_response_success_status, 'success'
|
15
|
+
auth_value_method :json_response_error_status, 'fail'
|
16
|
+
auth_value_method :json_response_error_code_key, 'error'
|
17
|
+
auth_value_method :json_message_key, 'message'
|
18
|
+
auth_value_method :json_response_field_error_key, 'field-error'
|
19
|
+
auth_value_method :json_include_message?, true
|
20
|
+
auth_value_method :json_check_accept?, true
|
21
|
+
|
22
|
+
|
23
|
+
auth_value_methods(
|
24
|
+
:only_json?
|
25
|
+
)
|
26
|
+
|
27
|
+
auth_methods(
|
28
|
+
:json_request?
|
29
|
+
)
|
30
|
+
|
31
|
+
auth_private_methods :json_response_body
|
32
|
+
|
33
|
+
def set_field_error(field, message)
|
34
|
+
return super unless json_request?
|
35
|
+
|
36
|
+
json_response[json_response_error_code_key] = json_response_field_error_key
|
37
|
+
json_response[json_response_status_key] = json_response_field_error_key
|
38
|
+
json_response[json_response_field_error_key] = [field, message]
|
39
|
+
end
|
40
|
+
|
41
|
+
def set_error_flash(message)
|
42
|
+
return super unless json_request?
|
43
|
+
|
44
|
+
json_response[json_response_status_key] = json_response_error_status
|
45
|
+
json_response[json_message_key] = message
|
46
|
+
end
|
47
|
+
|
48
|
+
def set_redirect_error_flash(message)
|
49
|
+
return super unless json_request?
|
50
|
+
|
51
|
+
json_response[json_response_status_key] = json_response_error_status
|
52
|
+
json_response[json_message_key] = message
|
53
|
+
end
|
54
|
+
|
55
|
+
def set_notice_flash(message)
|
56
|
+
return super unless json_request?
|
57
|
+
|
58
|
+
json_response[json_response_status_key] = json_response_success_status
|
59
|
+
json_response[json_message_key] = message if json_include_message?
|
60
|
+
end
|
61
|
+
|
62
|
+
def set_notice_now_flash(message)
|
63
|
+
return super unless json_request?
|
64
|
+
|
65
|
+
json_response[json_response_status_key] = json_response_success_status
|
66
|
+
json_response[json_message_key] = message if json_include_message?
|
67
|
+
end
|
68
|
+
|
69
|
+
def json_request?
|
70
|
+
return @json_request if defined?(@json_request)
|
71
|
+
|
72
|
+
@json_request = request.content_type =~ json_request_content_type_regexp
|
73
|
+
end
|
74
|
+
|
75
|
+
def view(page, title)
|
76
|
+
return super unless json_request?
|
77
|
+
return_json_response
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def check_csrf?
|
84
|
+
return false if json_request?
|
85
|
+
super
|
86
|
+
end
|
87
|
+
|
88
|
+
def before_rodauth
|
89
|
+
if json_request?
|
90
|
+
if json_check_accept? && (accept = request.env['HTTP_ACCEPT']) && accept !~ json_accept_regexp
|
91
|
+
response.status = 406
|
92
|
+
json_response[json_response_status_key] = json_response_error_status
|
93
|
+
json_response[json_message_key] = json_not_accepted_error_message
|
94
|
+
response['Content-Type'] ||= json_response_content_type
|
95
|
+
response.write(_json_response_body(json_response))
|
96
|
+
request.halt
|
97
|
+
end
|
98
|
+
|
99
|
+
unless request.post?
|
100
|
+
response.status = 405
|
101
|
+
response.headers['Allow'] = 'POST'
|
102
|
+
json_response[json_response_status_key] = json_response_error_status
|
103
|
+
json_response[json_message_key] = json_non_post_error_message
|
104
|
+
return_json_response
|
105
|
+
end
|
106
|
+
elsif only_json?
|
107
|
+
response.status = json_response_error_status
|
108
|
+
response.write non_json_request_error_message
|
109
|
+
request.halt
|
110
|
+
end
|
111
|
+
|
112
|
+
super
|
113
|
+
end
|
114
|
+
|
115
|
+
def before_login_attempt
|
116
|
+
if json_request?
|
117
|
+
unless open_account?
|
118
|
+
json_response[json_response_error_code_key] = "unverified_account"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
super
|
122
|
+
end
|
123
|
+
|
124
|
+
def new_account(login)
|
125
|
+
if account_from_login(login) && allow_resending_verify_account_email?
|
126
|
+
json_response[json_response_error_code_key] = "unverified_account" if json_request?
|
127
|
+
end
|
128
|
+
super
|
129
|
+
end
|
130
|
+
|
131
|
+
|
132
|
+
def before_view_recovery_codes
|
133
|
+
super if defined?(super)
|
134
|
+
if json_request?
|
135
|
+
json_response[:codes] = recovery_codes
|
136
|
+
json_response[json_message_key] ||= "" if json_include_message?
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def before_webauthn_setup_route
|
141
|
+
super if defined?(super)
|
142
|
+
if json_request? && !param_or_nil(webauthn_setup_param)
|
143
|
+
cred = new_webauthn_credential
|
144
|
+
json_response[webauthn_setup_param] = cred.as_json
|
145
|
+
json_response[webauthn_setup_challenge_param] = cred.challenge
|
146
|
+
json_response[webauthn_setup_challenge_hmac_param] = compute_hmac(cred.challenge)
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
def before_webauthn_auth_route
|
151
|
+
super if defined?(super)
|
152
|
+
if json_request? && !param_or_nil(webauthn_auth_param)
|
153
|
+
cred = webauth_credential_options_for_get
|
154
|
+
json_response[webauthn_auth_param] = cred.as_json
|
155
|
+
json_response[webauthn_auth_challenge_param] = cred.challenge
|
156
|
+
json_response[webauthn_auth_challenge_hmac_param] = compute_hmac(cred.challenge)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def before_webauthn_login_route
|
161
|
+
super if defined?(super)
|
162
|
+
if json_request? && !param_or_nil(webauthn_auth_param) && account_from_login(param(login_param))
|
163
|
+
cred = webauth_credential_options_for_get
|
164
|
+
json_response[webauthn_auth_param] = cred.as_json
|
165
|
+
json_response[webauthn_auth_challenge_param] = cred.challenge
|
166
|
+
json_response[webauthn_auth_challenge_hmac_param] = compute_hmac(cred.challenge)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
def before_webauthn_remove_route
|
171
|
+
super if defined?(super)
|
172
|
+
if json_request? && !param_or_nil(webauthn_remove_param)
|
173
|
+
json_response[webauthn_remove_param] = account_webauthn_usage
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def before_otp_setup_route
|
178
|
+
super if defined?(super)
|
179
|
+
if json_request? && otp_keys_use_hmac? && !param_or_nil(otp_setup_raw_param)
|
180
|
+
_otp_tmp_key(otp_new_secret)
|
181
|
+
json_response[otp_setup_param] = otp_user_key
|
182
|
+
json_response[otp_setup_raw_param] = otp_key
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
def redirect(path)
|
187
|
+
return super unless json_request?
|
188
|
+
# json_response["redirect"] = path
|
189
|
+
return_json_response
|
190
|
+
end
|
191
|
+
|
192
|
+
def json_response
|
193
|
+
@json_response ||= {json_response_status_key => json_response_success_status}
|
194
|
+
end
|
195
|
+
|
196
|
+
def _json_response_body(hash)
|
197
|
+
request.send(:convert_to_json, hash)
|
198
|
+
end
|
199
|
+
|
200
|
+
def return_json_response
|
201
|
+
response.status ||= json_response_error_status if json_response[json_response_status_key] == json_response_error_status
|
202
|
+
response['Content-Type'] ||= json_response_content_type
|
203
|
+
response.write(_json_response_body(json_response))
|
204
|
+
request.halt
|
205
|
+
end
|
206
|
+
|
207
|
+
def set_redirect_error_status(status)
|
208
|
+
if json_request? && json_response_custom_error_status?
|
209
|
+
response.status = status
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
def set_response_error_status(status)
|
214
|
+
if json_request? && !json_response_custom_error_status?
|
215
|
+
status = json_response_error_status
|
216
|
+
end
|
217
|
+
|
218
|
+
super
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen-string-literal: true
|
2
|
+
|
3
|
+
module Rodauth
|
4
|
+
Feature.define(:json_cors, :JsonCors) do
|
5
|
+
depends :json
|
6
|
+
|
7
|
+
auth_value_method :json_cors_allow_origin, false
|
8
|
+
auth_value_method :json_cors_allow_methods, 'POST'
|
9
|
+
auth_value_method :json_cors_allow_headers, 'Content-Type, Authorization, Accept'
|
10
|
+
auth_value_method :json_cors_expose_headers, 'Authorization'
|
11
|
+
auth_value_method :json_cors_max_age, 86400
|
12
|
+
|
13
|
+
auth_methods(:json_cors_allow?)
|
14
|
+
|
15
|
+
def json_cors_allow?
|
16
|
+
return false unless origin = request.env['HTTP_ORIGIN']
|
17
|
+
|
18
|
+
case allowed = json_cors_allow_origin
|
19
|
+
when String
|
20
|
+
timing_safe_eql?(origin, allowed)
|
21
|
+
when Array
|
22
|
+
allowed.any?{|s| timing_safe_eql?(origin, s)}
|
23
|
+
when Regexp
|
24
|
+
allowed =~ origin
|
25
|
+
when true
|
26
|
+
true
|
27
|
+
else
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def before_rodauth
|
35
|
+
if json_cors_allow?
|
36
|
+
response['Access-Control-Allow-Origin'] = request.env['HTTP_ORIGIN']
|
37
|
+
|
38
|
+
# Handle CORS preflight request
|
39
|
+
if request.request_method == 'OPTIONS'
|
40
|
+
response['Access-Control-Allow-Methods'] = json_cors_allow_methods
|
41
|
+
response['Access-Control-Allow-Headers'] = json_cors_allow_headers
|
42
|
+
response['Access-Control-Max-Age'] = json_cors_max_age.to_s
|
43
|
+
response.status = 204
|
44
|
+
request.halt(response.finish)
|
45
|
+
end
|
46
|
+
|
47
|
+
response['Access-Control-Expose-Headers'] = json_cors_expose_headers
|
48
|
+
end
|
49
|
+
|
50
|
+
super
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "rodauth-json"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["Koell"]
|
5
|
+
spec.email = ["i@wug.moe"]
|
6
|
+
|
7
|
+
spec.summary = "Rodauth JSON extension"
|
8
|
+
spec.description = "Rodauth JSON extension"
|
9
|
+
spec.homepage = "https://git.localhost/rodauth-json"
|
10
|
+
spec.license = "MIT"
|
11
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
15
|
+
|
16
|
+
spec.add_dependency "rodauth", "~> 2.0"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rodauth-json
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Koell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rodauth
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
description: Rodauth JSON extension
|
28
|
+
email:
|
29
|
+
- i@wug.moe
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- Gemfile
|
36
|
+
- Gemfile.lock
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- lib/rodauth/features/json.rb
|
41
|
+
- lib/rodauth/features/json_cors.rb
|
42
|
+
- rodauth-json.gemspec
|
43
|
+
homepage: https://git.localhost/rodauth-json
|
44
|
+
licenses:
|
45
|
+
- MIT
|
46
|
+
metadata:
|
47
|
+
homepage_uri: https://git.localhost/rodauth-json
|
48
|
+
source_code_uri: https://git.localhost/rodauth-json
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 2.3.0
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubygems_version: 3.1.4
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Rodauth JSON extension
|
68
|
+
test_files: []
|