dina 0.6.1.0 → 0.7.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fce6aa7545f2fe1bb7a340fd5ef64fb85ed56e50ad12b970a37cad5d0ba3bfc2
4
- data.tar.gz: 532c2e30e916fe27a13b3739efccd5adb770f12e4b6706f2fed7eec4276b80b8
3
+ metadata.gz: f683d4b9e156194f6c356e5ebfbd46fcb6f821548403cb5821357d0813d5ba6a
4
+ data.tar.gz: 867eef99e2ffb402420f425f495c4b44f4b5e98dbf955383d7fe39215f8d1104
5
5
  SHA512:
6
- metadata.gz: 2e7ff2cd1b927195c3c17d7986e31bc60fad738dff9f7198029af2e822673b05d8ea5748db0e7aeb330fd1cd59366a5e75870556fd7cf7b74d8160b8558fa79b
7
- data.tar.gz: 95f10aa0889f8eb03b782925458398ba1ed7c066df7cb5ff94c630710170a1fd567381334207242381d6e4736cb960c4fd4edad29b047ba7328ff86aa40ae5b1
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
- module Authentication
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 self.config(options = {})
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
- create_empty_token
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 self.header
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 self.flush
85
- flush_variables
86
- flush_token
87
+ def flush
88
+ write_token(data: empty_token)
87
89
  end
88
90
 
89
- class << self
90
- attr_accessor :endpoint_url
91
-
92
- private
91
+ private
93
92
 
94
- def access_token
95
- begin
96
- token[@server_name.to_sym][:access_token]
97
- rescue
98
- raise TokenStoreContentInvalid
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
- def refresh_token
103
- begin
104
- token[@server_name.to_sym][:refresh_token]
105
- rescue
106
- raise TokenStoreContentInvalid
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
- def auth_expiry
111
- begin
112
- token[@server_name.to_sym][:auth_expiry]
113
- rescue
114
- raise TokenStoreContentInvalid
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
- def get_token
119
- response = Keycloak::Client.get_token(
120
- @user,
121
- @password,
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
- def renew_token
134
- begin
135
- response = Keycloak::Client.get_token_by_refresh_token(
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
- def save_token(access_token:, refresh_token:, auth_expiry:)
162
- data = JSON.parse(::File.read(@token_store_file), symbolize_names: true) rescue {}
163
- data[@server_name.to_sym] = {
164
- access_token: access_token,
165
- refresh_token: refresh_token,
166
- auth_expiry: auth_expiry
167
- }
168
- write_token(data: data)
169
- end
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
- def write_token(data:)
172
- ::File.write(@token_store_file, JSON.dump(data))
173
- @token = data
174
- end
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::Authentication.config has not yet been called." unless Dina::Authentication.endpoint_url
19
- Dina::Authentication.endpoint_url + "/" + endpoint_path
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::Authentication.header }
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::Authentication.header },
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
- Dina::Authentication.endpoint_url
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::Authentication.header },
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
@@ -2,8 +2,8 @@ module Dina
2
2
  class Version
3
3
 
4
4
  MAJOR = 0
5
- MINOR = 6
6
- PATCH = 1
5
+ MINOR = 7
6
+ PATCH = 0
7
7
  BUILD = 0
8
8
 
9
9
  def self.version
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
- def self.classes
18
- Dina::BaseModel.subclasses
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.6.1.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: 2022-12-31 00:00:00.000000000 Z
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