dina 0.6.2.0 → 0.7.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 +4 -4
- data/lib/dina/authentication/authentication.rb +123 -96
- data/lib/dina/models/base_model.rb +3 -3
- data/lib/dina/models/object_store/file.rb +3 -3
- data/lib/dina/version.rb +2 -2
- data/lib/dina.rb +29 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f6c152942d702bdba2e7abbe4a11d5a9baddc01903e08c70c18c8952d1082479
|
4
|
+
data.tar.gz: d392fb4a47e0d34e1dd38ccbde15926b4bc45a817f2f1f7722ebd25ece9461bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d94a07c2f0bdbe1bbc73f3e39bf13ebb1740bc9939027383e588771ca58f46fee3c33829e33bfa224948c5ed19323f4ff770495656c5dad94a2cd5e3f962216
|
7
|
+
data.tar.gz: ec3005ea8d5dbd382d2db1e29722d598b74fdb27f5fafbdc34772c45040cdbe9ff80e45ac56b3c80dcbdae63fea036c2ff141ba55d548f6df1602c349540cc66
|
@@ -1,7 +1,23 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module Dina
|
4
|
-
|
4
|
+
class Authentication
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def instance
|
8
|
+
Thread.current[:dina_authentication] ||= new
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@token = nil
|
14
|
+
@config = nil
|
15
|
+
@opts = default_opts
|
16
|
+
end
|
17
|
+
|
18
|
+
def config
|
19
|
+
@config ||= OpenStruct.new(@opts)
|
20
|
+
end
|
5
21
|
|
6
22
|
# Sets Authentication configuration
|
7
23
|
# Options hash as follows:
|
@@ -17,31 +33,27 @@ module Dina
|
|
17
33
|
# }
|
18
34
|
#
|
19
35
|
# @param options [Hash] the configuration options
|
20
|
-
def
|
21
|
-
raise ConfigItemMissing, "Missing token_store_file from config." unless
|
22
|
-
raise ConfigItemMissing, "Missing user from config." unless
|
23
|
-
raise ConfigItemMissing, "Missing password from config." unless
|
24
|
-
raise ConfigItemMissing, "Missing server_name from config." unless
|
25
|
-
raise ConfigItemMissing, "Missing client_id from config." unless
|
26
|
-
raise ConfigItemMissing, "Missing endpoint_url from config." unless
|
27
|
-
raise ConfigItemMissing, "Missing authorization_url from config." unless
|
28
|
-
raise ConfigItemMissing, "Missing realm from config." unless
|
29
|
-
|
30
|
-
if !
|
36
|
+
def config=(opts = {})
|
37
|
+
raise ConfigItemMissing, "Missing token_store_file from config." unless opts[:token_store_file]
|
38
|
+
raise ConfigItemMissing, "Missing user from config." unless opts[:user]
|
39
|
+
raise ConfigItemMissing, "Missing password from config." unless opts[:password]
|
40
|
+
raise ConfigItemMissing, "Missing server_name from config." unless opts[:server_name]
|
41
|
+
raise ConfigItemMissing, "Missing client_id from config." unless opts[:client_id]
|
42
|
+
raise ConfigItemMissing, "Missing endpoint_url from config." unless opts[:endpoint_url]
|
43
|
+
raise ConfigItemMissing, "Missing authorization_url from config." unless opts[:authorization_url]
|
44
|
+
raise ConfigItemMissing, "Missing realm from config." unless opts[:realm]
|
45
|
+
|
46
|
+
if !opts[:token_store_file].instance_of?(String) || !::File.exist?(opts[:token_store_file])
|
31
47
|
raise TokenStoreFileNotFound
|
32
48
|
end
|
33
49
|
|
34
50
|
@token = nil
|
35
|
-
@
|
36
|
-
@
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
Keycloak.auth_server_url = options[:authorization_url]
|
42
|
-
Keycloak.realm = options[:realm]
|
43
|
-
|
44
|
-
if ::File.zero?(@token_store_file)
|
51
|
+
@config = nil
|
52
|
+
@opts.merge!(opts)
|
53
|
+
Keycloak.auth_server_url = config.authorization_url
|
54
|
+
Keycloak.realm = config.realm
|
55
|
+
|
56
|
+
if ::File.zero?(config.token_store_file)
|
45
57
|
write_token(data: empty_token)
|
46
58
|
end
|
47
59
|
end
|
@@ -53,7 +65,7 @@ module Dina
|
|
53
65
|
# and load the token_store_file with every call to header
|
54
66
|
#
|
55
67
|
# @return [String] the Bearer token
|
56
|
-
def
|
68
|
+
def header
|
57
69
|
if access_token.nil? || refresh_token.nil?
|
58
70
|
set_token
|
59
71
|
end
|
@@ -65,99 +77,114 @@ module Dina
|
|
65
77
|
"Bearer " + access_token
|
66
78
|
end
|
67
79
|
|
68
|
-
#
|
69
|
-
def
|
80
|
+
# Save default values in token store file
|
81
|
+
def flush
|
70
82
|
write_token(data: empty_token)
|
71
83
|
end
|
72
84
|
|
73
|
-
|
74
|
-
|
85
|
+
def flush_config
|
86
|
+
@opts = default_opts
|
87
|
+
@config = nil
|
88
|
+
@token = nil
|
89
|
+
end
|
75
90
|
|
76
|
-
|
91
|
+
private
|
92
|
+
|
93
|
+
def default_opts
|
94
|
+
{
|
95
|
+
token_store_file: nil,
|
96
|
+
user: nil,
|
97
|
+
password: nil,
|
98
|
+
server_name: nil,
|
99
|
+
client_id: nil,
|
100
|
+
endpoint_url: nil,
|
101
|
+
realm: nil,
|
102
|
+
authorization_url: nil
|
103
|
+
}
|
104
|
+
end
|
77
105
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
end
|
106
|
+
def access_token
|
107
|
+
begin
|
108
|
+
token[config.server_name.to_sym][:access_token]
|
109
|
+
rescue
|
110
|
+
raise TokenStoreContentInvalid
|
84
111
|
end
|
112
|
+
end
|
85
113
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
end
|
114
|
+
def refresh_token
|
115
|
+
begin
|
116
|
+
token[config.server_name.to_sym][:refresh_token]
|
117
|
+
rescue
|
118
|
+
raise TokenStoreContentInvalid
|
92
119
|
end
|
120
|
+
end
|
93
121
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
122
|
+
def auth_expiry
|
123
|
+
begin
|
124
|
+
token[config.server_name.to_sym][:auth_expiry]
|
125
|
+
rescue
|
126
|
+
raise TokenStoreContentInvalid
|
100
127
|
end
|
128
|
+
end
|
101
129
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
130
|
+
def get_token
|
131
|
+
response = Keycloak::Client.get_token(
|
132
|
+
config.user,
|
133
|
+
config.password,
|
134
|
+
client_id= config.client_id,
|
135
|
+
secret='')
|
136
|
+
JSON.parse(response, symbolize_names: true)
|
137
|
+
end
|
110
138
|
|
111
|
-
|
112
|
-
|
139
|
+
def set_token
|
140
|
+
json = get_token
|
141
|
+
auth_expiry = (Time.now + json[:expires_in].seconds).to_s
|
142
|
+
save_token(access_token: json[:access_token], refresh_token: json[:refresh_token], auth_expiry: auth_expiry)
|
143
|
+
end
|
144
|
+
|
145
|
+
def renew_token
|
146
|
+
begin
|
147
|
+
response = Keycloak::Client.get_token_by_refresh_token(
|
148
|
+
refresh_token,
|
149
|
+
client_id= config.client_id,
|
150
|
+
secret='')
|
151
|
+
json = JSON.parse(response, symbolize_names: true)
|
113
152
|
auth_expiry = (Time.now + json[:expires_in].seconds).to_s
|
114
153
|
save_token(access_token: json[:access_token], refresh_token: json[:refresh_token], auth_expiry: auth_expiry)
|
154
|
+
rescue
|
155
|
+
set_token
|
115
156
|
end
|
157
|
+
end
|
116
158
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
refresh_token,
|
121
|
-
client_id= @client_id,
|
122
|
-
secret='')
|
123
|
-
json = JSON.parse(response, symbolize_names: true)
|
124
|
-
auth_expiry = (Time.now + json[:expires_in].seconds).to_s
|
125
|
-
save_token(access_token: json[:access_token], refresh_token: json[:refresh_token], auth_expiry: auth_expiry)
|
126
|
-
rescue
|
127
|
-
set_token
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
def token
|
132
|
-
@token ||= JSON.parse(::File.read(@token_store_file), symbolize_names: true)
|
133
|
-
end
|
134
|
-
|
135
|
-
def empty_token
|
136
|
-
data = {}
|
137
|
-
data[@server_name.to_sym] = {
|
138
|
-
access_token: nil,
|
139
|
-
refresh_token: nil,
|
140
|
-
auth_expiry: nil
|
141
|
-
}
|
142
|
-
data
|
143
|
-
end
|
159
|
+
def token
|
160
|
+
@token ||= JSON.parse(::File.read(config.token_store_file), symbolize_names: true)
|
161
|
+
end
|
144
162
|
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
163
|
+
def empty_token
|
164
|
+
data = {}
|
165
|
+
data[config.server_name.to_sym] = {
|
166
|
+
access_token: nil,
|
167
|
+
refresh_token: nil,
|
168
|
+
auth_expiry: nil
|
169
|
+
}
|
170
|
+
data
|
171
|
+
end
|
154
172
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
173
|
+
def save_token(access_token:, refresh_token:, auth_expiry:)
|
174
|
+
data = JSON.parse(::File.read(config.token_store_file), symbolize_names: true) rescue {}
|
175
|
+
data[config.server_name.to_sym] = {
|
176
|
+
access_token: access_token,
|
177
|
+
refresh_token: refresh_token,
|
178
|
+
auth_expiry: auth_expiry
|
179
|
+
}
|
180
|
+
write_token(data: data)
|
181
|
+
end
|
159
182
|
|
183
|
+
def write_token(data:)
|
184
|
+
::File.write(config.token_store_file, JSON.dump(data))
|
185
|
+
@token = data
|
160
186
|
end
|
161
187
|
|
162
188
|
end
|
189
|
+
|
163
190
|
end
|
@@ -15,13 +15,13 @@ module Dina
|
|
15
15
|
|
16
16
|
# Required by json_api_client
|
17
17
|
def self.site
|
18
|
-
raise ConfigItemMissing, "Missing endpoint_url from config. Perhaps Dina
|
19
|
-
Dina
|
18
|
+
raise ConfigItemMissing, "Missing endpoint_url from config. Perhaps Dina.config has not yet been called." unless Dina.config.endpoint_url
|
19
|
+
Dina.config.endpoint_url + "/" + endpoint_path
|
20
20
|
end
|
21
21
|
|
22
22
|
# injects keybloak bearer token with all json_api_client calls
|
23
23
|
def self.custom_headers
|
24
|
-
{ content_type: "application/vnd.api+json", authorization: Dina
|
24
|
+
{ content_type: "application/vnd.api+json", authorization: Dina.header }
|
25
25
|
end
|
26
26
|
|
27
27
|
# helper method for all child classes to retrieve records by group
|
@@ -15,7 +15,7 @@ module Dina
|
|
15
15
|
obj.group = group
|
16
16
|
RestClient::Request.execute(
|
17
17
|
method: :get,
|
18
|
-
headers: { authorization: Dina
|
18
|
+
headers: { authorization: Dina.header },
|
19
19
|
url: obj.url + "/#{id}",
|
20
20
|
verify_ssl: verify_ssl
|
21
21
|
)
|
@@ -35,7 +35,7 @@ module Dina
|
|
35
35
|
end
|
36
36
|
|
37
37
|
def endpoint
|
38
|
-
|
38
|
+
Autentication.endpoint_url
|
39
39
|
end
|
40
40
|
|
41
41
|
def endpoint_path
|
@@ -58,7 +58,7 @@ module Dina
|
|
58
58
|
validate_params
|
59
59
|
response = RestClient::Request.execute(
|
60
60
|
method: :post,
|
61
|
-
headers: { authorization: Dina
|
61
|
+
headers: { authorization: Dina.header },
|
62
62
|
url: (!is_derivative) ? url : url + "/derivative",
|
63
63
|
payload: {
|
64
64
|
multipart: true,
|
data/lib/dina/version.rb
CHANGED
data/lib/dina.rb
CHANGED
@@ -9,12 +9,35 @@ module Dina
|
|
9
9
|
|
10
10
|
JsonApiClient::Paginating::NestedParamPaginator.page_param = "offset"
|
11
11
|
JsonApiClient::Paginating::NestedParamPaginator.per_page_param = "limit"
|
12
|
-
JsonApiClient::Schema.register array:
|
13
|
-
JsonApiClient::Schema.register object:
|
14
|
-
JsonApiClient::Schema.register multilingual_title:
|
15
|
-
JsonApiClient::Schema.register multilingual_description:
|
12
|
+
JsonApiClient::Schema.register array: ArrayCaster
|
13
|
+
JsonApiClient::Schema.register object: ObjectCaster
|
14
|
+
JsonApiClient::Schema.register multilingual_title: MultilingualTitleCaster
|
15
|
+
JsonApiClient::Schema.register multilingual_description: MultilingualDescriptionCaster
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
module_function
|
18
|
+
|
19
|
+
def classes
|
20
|
+
BaseModel.subclasses
|
21
|
+
end
|
22
|
+
|
23
|
+
def config
|
24
|
+
Authentication.instance.config
|
25
|
+
end
|
26
|
+
|
27
|
+
def config=(options = {})
|
28
|
+
Authentication.instance.config = options
|
29
|
+
end
|
30
|
+
|
31
|
+
def header
|
32
|
+
Authentication.instance.header
|
33
|
+
end
|
34
|
+
|
35
|
+
def flush
|
36
|
+
Authentication.instance.flush
|
19
37
|
end
|
38
|
+
|
39
|
+
def flush_config
|
40
|
+
Authentication.instance.flush_config
|
41
|
+
end
|
42
|
+
|
20
43
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dina
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David P. Shorthouse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json_api_client
|