dina 0.6.1.0 → 0.7.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/dina/authentication/authentication.rb +95 -97
- 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 +17 -2
- 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: f683d4b9e156194f6c356e5ebfbd46fcb6f821548403cb5821357d0813d5ba6a
|
4
|
+
data.tar.gz: 867eef99e2ffb402420f425f495c4b44f4b5e98dbf955383d7fe39215f8d1104
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 815c80204d71ac6eb567ff3aee545123d582573a38d4602b85a18b20c2200837323a57e515663790951691ce642c8865ef0587532a6acefd1cc9dc378051d779
|
7
|
+
data.tar.gz: 4a8d252554cc5eff4a76ee00970a525ba15d805eee63ec026832ddd9ad5eeeeafac2e5eefb5667ab7ea9c22eb9ee5cca92f639492e5b6b7ea0c0a157a58a35e4
|
@@ -1,7 +1,25 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module Dina
|
4
|
-
|
4
|
+
class Authentication
|
5
|
+
|
6
|
+
class << self
|
7
|
+
attr_accessor :endpoint_url
|
8
|
+
|
9
|
+
def instance
|
10
|
+
Thread.current[:dina_authentication] ||= new
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@token = nil
|
16
|
+
@endpoint_url = nil
|
17
|
+
@token_store_file = nil
|
18
|
+
@user = nil
|
19
|
+
@password = nil
|
20
|
+
@server_name = nil
|
21
|
+
@client_id = nil
|
22
|
+
end
|
5
23
|
|
6
24
|
# Sets Authentication configuration
|
7
25
|
# Options hash as follows:
|
@@ -17,7 +35,7 @@ module Dina
|
|
17
35
|
# }
|
18
36
|
#
|
19
37
|
# @param options [Hash] the configuration options
|
20
|
-
def
|
38
|
+
def config(options = {})
|
21
39
|
raise ConfigItemMissing, "Missing token_store_file from config." unless options[:token_store_file]
|
22
40
|
raise ConfigItemMissing, "Missing user from config." unless options[:user]
|
23
41
|
raise ConfigItemMissing, "Missing password from config." unless options[:password]
|
@@ -31,6 +49,7 @@ module Dina
|
|
31
49
|
raise TokenStoreFileNotFound
|
32
50
|
end
|
33
51
|
|
52
|
+
@token = nil
|
34
53
|
@token_store_file = options[:token_store_file]
|
35
54
|
@user = options[:user]
|
36
55
|
@password = options[:password]
|
@@ -41,15 +60,18 @@ module Dina
|
|
41
60
|
Keycloak.realm = options[:realm]
|
42
61
|
|
43
62
|
if ::File.zero?(@token_store_file)
|
44
|
-
|
63
|
+
write_token(data: empty_token)
|
45
64
|
end
|
46
65
|
end
|
47
66
|
|
48
67
|
# Gets, sets, and renews a Bearer access token as required
|
49
68
|
# and produces a Header string
|
50
69
|
#
|
70
|
+
# WARNING: this is not likely to be threadsafe unless we do away with @token
|
71
|
+
# and load the token_store_file with every call to header
|
72
|
+
#
|
51
73
|
# @return [String] the Bearer token
|
52
|
-
def
|
74
|
+
def header
|
53
75
|
if access_token.nil? || refresh_token.nil?
|
54
76
|
set_token
|
55
77
|
end
|
@@ -61,119 +83,95 @@ module Dina
|
|
61
83
|
"Bearer " + access_token
|
62
84
|
end
|
63
85
|
|
64
|
-
# Flushes instance variables from memory
|
65
|
-
# but token store file content remains intact
|
66
|
-
def self.flush_variables
|
67
|
-
@token = nil
|
68
|
-
@token_store_file = nil
|
69
|
-
@user = nil
|
70
|
-
@password = nil
|
71
|
-
@server_name = nil
|
72
|
-
@client_id = nil
|
73
|
-
@endpoint_url = nil
|
74
|
-
Keycloak.auth_server_url = nil
|
75
|
-
Keycloak.realm = nil
|
76
|
-
end
|
77
|
-
|
78
|
-
# Saves default values in token store file
|
79
|
-
def self.flush_token
|
80
|
-
create_empty_token
|
81
|
-
end
|
82
|
-
|
83
86
|
# Flush instance variables and save default values in token store file
|
84
|
-
def
|
85
|
-
|
86
|
-
flush_token
|
87
|
+
def flush
|
88
|
+
write_token(data: empty_token)
|
87
89
|
end
|
88
90
|
|
89
|
-
|
90
|
-
attr_accessor :endpoint_url
|
91
|
-
|
92
|
-
private
|
91
|
+
private
|
93
92
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
end
|
93
|
+
def access_token
|
94
|
+
begin
|
95
|
+
token[@server_name.to_sym][:access_token]
|
96
|
+
rescue
|
97
|
+
raise TokenStoreContentInvalid
|
100
98
|
end
|
99
|
+
end
|
101
100
|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
end
|
101
|
+
def refresh_token
|
102
|
+
begin
|
103
|
+
token[@server_name.to_sym][:refresh_token]
|
104
|
+
rescue
|
105
|
+
raise TokenStoreContentInvalid
|
108
106
|
end
|
107
|
+
end
|
109
108
|
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
end
|
109
|
+
def auth_expiry
|
110
|
+
begin
|
111
|
+
token[@server_name.to_sym][:auth_expiry]
|
112
|
+
rescue
|
113
|
+
raise TokenStoreContentInvalid
|
116
114
|
end
|
115
|
+
end
|
117
116
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
117
|
+
def get_token
|
118
|
+
response = Keycloak::Client.get_token(
|
119
|
+
@user,
|
120
|
+
@password,
|
121
|
+
client_id= @client_id,
|
122
|
+
secret='')
|
123
|
+
JSON.parse(response, symbolize_names: true)
|
124
|
+
end
|
125
|
+
|
126
|
+
def set_token
|
127
|
+
json = get_token
|
128
|
+
auth_expiry = (Time.now + json[:expires_in].seconds).to_s
|
129
|
+
save_token(access_token: json[:access_token], refresh_token: json[:refresh_token], auth_expiry: auth_expiry)
|
130
|
+
end
|
131
|
+
|
132
|
+
def renew_token
|
133
|
+
begin
|
134
|
+
response = Keycloak::Client.get_token_by_refresh_token(
|
135
|
+
refresh_token,
|
122
136
|
client_id= @client_id,
|
123
137
|
secret='')
|
124
|
-
JSON.parse(response, symbolize_names: true)
|
125
|
-
end
|
126
|
-
|
127
|
-
def set_token
|
128
|
-
json = get_token
|
138
|
+
json = JSON.parse(response, symbolize_names: true)
|
129
139
|
auth_expiry = (Time.now + json[:expires_in].seconds).to_s
|
130
140
|
save_token(access_token: json[:access_token], refresh_token: json[:refresh_token], auth_expiry: auth_expiry)
|
141
|
+
rescue
|
142
|
+
set_token
|
131
143
|
end
|
144
|
+
end
|
132
145
|
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
refresh_token,
|
137
|
-
client_id= @client_id,
|
138
|
-
secret='')
|
139
|
-
json = JSON.parse(response, symbolize_names: true)
|
140
|
-
auth_expiry = (Time.now + json[:expires_in].seconds).to_s
|
141
|
-
save_token(access_token: json[:access_token], refresh_token: json[:refresh_token], auth_expiry: auth_expiry)
|
142
|
-
rescue
|
143
|
-
set_token
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def token
|
148
|
-
@token ||= JSON.parse(::File.read(@token_store_file), symbolize_names: true)
|
149
|
-
end
|
150
|
-
|
151
|
-
def create_empty_token
|
152
|
-
data = {}
|
153
|
-
data[@server_name.to_sym] = {
|
154
|
-
access_token: nil,
|
155
|
-
refresh_token: nil,
|
156
|
-
auth_expiry: nil
|
157
|
-
}
|
158
|
-
write_token(data: data)
|
159
|
-
end
|
146
|
+
def token
|
147
|
+
@token ||= JSON.parse(::File.read(@token_store_file), symbolize_names: true)
|
148
|
+
end
|
160
149
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
150
|
+
def empty_token
|
151
|
+
data = {}
|
152
|
+
data[@server_name.to_sym] = {
|
153
|
+
access_token: nil,
|
154
|
+
refresh_token: nil,
|
155
|
+
auth_expiry: nil
|
156
|
+
}
|
157
|
+
data
|
158
|
+
end
|
170
159
|
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
160
|
+
def save_token(access_token:, refresh_token:, auth_expiry:)
|
161
|
+
data = JSON.parse(::File.read(@token_store_file), symbolize_names: true) rescue {}
|
162
|
+
data[@server_name.to_sym] = {
|
163
|
+
access_token: access_token,
|
164
|
+
refresh_token: refresh_token,
|
165
|
+
auth_expiry: auth_expiry
|
166
|
+
}
|
167
|
+
write_token(data: data)
|
168
|
+
end
|
175
169
|
|
170
|
+
def write_token(data:)
|
171
|
+
::File.write(@token_store_file, JSON.dump(data))
|
172
|
+
@token = data
|
176
173
|
end
|
177
174
|
|
178
175
|
end
|
176
|
+
|
179
177
|
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
|
-
|
18
|
+
raise ConfigItemMissing, "Missing endpoint_url from config. Perhaps Dina.config has not yet been called." unless Authentication.endpoint_url
|
19
|
+
Authentication.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
@@ -14,7 +14,22 @@ module Dina
|
|
14
14
|
JsonApiClient::Schema.register multilingual_title: Dina::MultilingualTitleCaster
|
15
15
|
JsonApiClient::Schema.register multilingual_description: Dina::MultilingualDescriptionCaster
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
module_function
|
18
|
+
|
19
|
+
def classes
|
20
|
+
BaseModel.subclasses
|
21
|
+
end
|
22
|
+
|
23
|
+
def config(options = {})
|
24
|
+
Authentication.instance.config(options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def header
|
28
|
+
Authentication.instance.header
|
29
|
+
end
|
30
|
+
|
31
|
+
def flush
|
32
|
+
Authentication.instance.flush
|
19
33
|
end
|
34
|
+
|
20
35
|
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.0.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
|