dina 0.7.0.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 +55 -42
- data/lib/dina/models/base_model.rb +2 -2
- data/lib/dina/version.rb +1 -1
- data/lib/dina.rb +14 -6
- metadata +1 -1
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
|
@@ -4,8 +4,6 @@ module Dina
|
|
4
4
|
class Authentication
|
5
5
|
|
6
6
|
class << self
|
7
|
-
attr_accessor :endpoint_url
|
8
|
-
|
9
7
|
def instance
|
10
8
|
Thread.current[:dina_authentication] ||= new
|
11
9
|
end
|
@@ -13,12 +11,12 @@ module Dina
|
|
13
11
|
|
14
12
|
def initialize
|
15
13
|
@token = nil
|
16
|
-
@
|
17
|
-
@
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
@
|
14
|
+
@config = nil
|
15
|
+
@opts = default_opts
|
16
|
+
end
|
17
|
+
|
18
|
+
def config
|
19
|
+
@config ||= OpenStruct.new(@opts)
|
22
20
|
end
|
23
21
|
|
24
22
|
# Sets Authentication configuration
|
@@ -35,31 +33,27 @@ module Dina
|
|
35
33
|
# }
|
36
34
|
#
|
37
35
|
# @param options [Hash] the configuration options
|
38
|
-
def config(
|
39
|
-
raise ConfigItemMissing, "Missing token_store_file from config." unless
|
40
|
-
raise ConfigItemMissing, "Missing user from config." unless
|
41
|
-
raise ConfigItemMissing, "Missing password from config." unless
|
42
|
-
raise ConfigItemMissing, "Missing server_name from config." unless
|
43
|
-
raise ConfigItemMissing, "Missing client_id from config." unless
|
44
|
-
raise ConfigItemMissing, "Missing endpoint_url from config." unless
|
45
|
-
raise ConfigItemMissing, "Missing authorization_url from config." unless
|
46
|
-
raise ConfigItemMissing, "Missing realm from config." unless
|
47
|
-
|
48
|
-
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])
|
49
47
|
raise TokenStoreFileNotFound
|
50
48
|
end
|
51
49
|
|
52
50
|
@token = nil
|
53
|
-
@
|
54
|
-
@
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
Keycloak.auth_server_url = options[:authorization_url]
|
60
|
-
Keycloak.realm = options[:realm]
|
61
|
-
|
62
|
-
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)
|
63
57
|
write_token(data: empty_token)
|
64
58
|
end
|
65
59
|
end
|
@@ -83,16 +77,35 @@ module Dina
|
|
83
77
|
"Bearer " + access_token
|
84
78
|
end
|
85
79
|
|
86
|
-
#
|
80
|
+
# Save default values in token store file
|
87
81
|
def flush
|
88
82
|
write_token(data: empty_token)
|
89
83
|
end
|
90
84
|
|
85
|
+
def flush_config
|
86
|
+
@opts = default_opts
|
87
|
+
@config = nil
|
88
|
+
@token = nil
|
89
|
+
end
|
90
|
+
|
91
91
|
private
|
92
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
|
105
|
+
|
93
106
|
def access_token
|
94
107
|
begin
|
95
|
-
token[
|
108
|
+
token[config.server_name.to_sym][:access_token]
|
96
109
|
rescue
|
97
110
|
raise TokenStoreContentInvalid
|
98
111
|
end
|
@@ -100,7 +113,7 @@ module Dina
|
|
100
113
|
|
101
114
|
def refresh_token
|
102
115
|
begin
|
103
|
-
token[
|
116
|
+
token[config.server_name.to_sym][:refresh_token]
|
104
117
|
rescue
|
105
118
|
raise TokenStoreContentInvalid
|
106
119
|
end
|
@@ -108,7 +121,7 @@ module Dina
|
|
108
121
|
|
109
122
|
def auth_expiry
|
110
123
|
begin
|
111
|
-
token[
|
124
|
+
token[config.server_name.to_sym][:auth_expiry]
|
112
125
|
rescue
|
113
126
|
raise TokenStoreContentInvalid
|
114
127
|
end
|
@@ -116,9 +129,9 @@ module Dina
|
|
116
129
|
|
117
130
|
def get_token
|
118
131
|
response = Keycloak::Client.get_token(
|
119
|
-
|
120
|
-
|
121
|
-
client_id=
|
132
|
+
config.user,
|
133
|
+
config.password,
|
134
|
+
client_id= config.client_id,
|
122
135
|
secret='')
|
123
136
|
JSON.parse(response, symbolize_names: true)
|
124
137
|
end
|
@@ -133,7 +146,7 @@ module Dina
|
|
133
146
|
begin
|
134
147
|
response = Keycloak::Client.get_token_by_refresh_token(
|
135
148
|
refresh_token,
|
136
|
-
client_id=
|
149
|
+
client_id= config.client_id,
|
137
150
|
secret='')
|
138
151
|
json = JSON.parse(response, symbolize_names: true)
|
139
152
|
auth_expiry = (Time.now + json[:expires_in].seconds).to_s
|
@@ -144,12 +157,12 @@ module Dina
|
|
144
157
|
end
|
145
158
|
|
146
159
|
def token
|
147
|
-
@token ||= JSON.parse(::File.read(
|
160
|
+
@token ||= JSON.parse(::File.read(config.token_store_file), symbolize_names: true)
|
148
161
|
end
|
149
162
|
|
150
163
|
def empty_token
|
151
164
|
data = {}
|
152
|
-
data[
|
165
|
+
data[config.server_name.to_sym] = {
|
153
166
|
access_token: nil,
|
154
167
|
refresh_token: nil,
|
155
168
|
auth_expiry: nil
|
@@ -158,8 +171,8 @@ module Dina
|
|
158
171
|
end
|
159
172
|
|
160
173
|
def save_token(access_token:, refresh_token:, auth_expiry:)
|
161
|
-
data = JSON.parse(::File.read(
|
162
|
-
data[
|
174
|
+
data = JSON.parse(::File.read(config.token_store_file), symbolize_names: true) rescue {}
|
175
|
+
data[config.server_name.to_sym] = {
|
163
176
|
access_token: access_token,
|
164
177
|
refresh_token: refresh_token,
|
165
178
|
auth_expiry: auth_expiry
|
@@ -168,7 +181,7 @@ module Dina
|
|
168
181
|
end
|
169
182
|
|
170
183
|
def write_token(data:)
|
171
|
-
::File.write(
|
184
|
+
::File.write(config.token_store_file, JSON.dump(data))
|
172
185
|
@token = data
|
173
186
|
end
|
174
187
|
|
@@ -15,8 +15,8 @@ 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.config has not yet been called." unless
|
19
|
-
|
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
|
data/lib/dina/version.rb
CHANGED
data/lib/dina.rb
CHANGED
@@ -9,10 +9,10 @@ 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
17
|
module_function
|
18
18
|
|
@@ -20,8 +20,12 @@ module Dina
|
|
20
20
|
BaseModel.subclasses
|
21
21
|
end
|
22
22
|
|
23
|
-
def config
|
24
|
-
Authentication.instance.config
|
23
|
+
def config
|
24
|
+
Authentication.instance.config
|
25
|
+
end
|
26
|
+
|
27
|
+
def config=(options = {})
|
28
|
+
Authentication.instance.config = options
|
25
29
|
end
|
26
30
|
|
27
31
|
def header
|
@@ -32,4 +36,8 @@ module Dina
|
|
32
36
|
Authentication.instance.flush
|
33
37
|
end
|
34
38
|
|
39
|
+
def flush_config
|
40
|
+
Authentication.instance.flush_config
|
41
|
+
end
|
42
|
+
|
35
43
|
end
|