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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f683d4b9e156194f6c356e5ebfbd46fcb6f821548403cb5821357d0813d5ba6a
4
- data.tar.gz: 867eef99e2ffb402420f425f495c4b44f4b5e98dbf955383d7fe39215f8d1104
3
+ metadata.gz: f6c152942d702bdba2e7abbe4a11d5a9baddc01903e08c70c18c8952d1082479
4
+ data.tar.gz: d392fb4a47e0d34e1dd38ccbde15926b4bc45a817f2f1f7722ebd25ece9461bb
5
5
  SHA512:
6
- metadata.gz: 815c80204d71ac6eb567ff3aee545123d582573a38d4602b85a18b20c2200837323a57e515663790951691ce642c8865ef0587532a6acefd1cc9dc378051d779
7
- data.tar.gz: 4a8d252554cc5eff4a76ee00970a525ba15d805eee63ec026832ddd9ad5eeeeafac2e5eefb5667ab7ea9c22eb9ee5cca92f639492e5b6b7ea0c0a157a58a35e4
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
- @endpoint_url = nil
17
- @token_store_file = nil
18
- @user = nil
19
- @password = nil
20
- @server_name = nil
21
- @client_id = nil
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(options = {})
39
- raise ConfigItemMissing, "Missing token_store_file from config." unless options[:token_store_file]
40
- raise ConfigItemMissing, "Missing user from config." unless options[:user]
41
- raise ConfigItemMissing, "Missing password from config." unless options[:password]
42
- raise ConfigItemMissing, "Missing server_name from config." unless options[:server_name]
43
- raise ConfigItemMissing, "Missing client_id from config." unless options[:client_id]
44
- raise ConfigItemMissing, "Missing endpoint_url from config." unless options[:endpoint_url]
45
- raise ConfigItemMissing, "Missing authorization_url from config." unless options[:authorization_url]
46
- raise ConfigItemMissing, "Missing realm from config." unless options[:realm]
47
-
48
- if !options[:token_store_file].instance_of?(String) || !::File.exist?(options[:token_store_file])
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
- @token_store_file = options[:token_store_file]
54
- @user = options[:user]
55
- @password = options[:password]
56
- @server_name = options[:server_name]
57
- @client_id = options[:client_id]
58
- @endpoint_url = options[:endpoint_url]
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
- # Flush instance variables and save default values in token store file
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[@server_name.to_sym][:access_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[@server_name.to_sym][:refresh_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[@server_name.to_sym][:auth_expiry]
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
- @user,
120
- @password,
121
- client_id= @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= @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(@token_store_file), symbolize_names: true)
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[@server_name.to_sym] = {
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(@token_store_file), symbolize_names: true) rescue {}
162
- data[@server_name.to_sym] = {
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(@token_store_file, JSON.dump(data))
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 Authentication.endpoint_url
19
- Authentication.endpoint_url + "/" + endpoint_path
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
@@ -3,7 +3,7 @@ module Dina
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 7
6
- PATCH = 0
6
+ PATCH = 1
7
7
  BUILD = 0
8
8
 
9
9
  def self.version
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: Dina::ArrayCaster
13
- JsonApiClient::Schema.register object: Dina::ObjectCaster
14
- JsonApiClient::Schema.register multilingual_title: Dina::MultilingualTitleCaster
15
- JsonApiClient::Schema.register multilingual_description: Dina::MultilingualDescriptionCaster
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(options = {})
24
- Authentication.instance.config(options)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dina
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.0
4
+ version: 0.7.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David P. Shorthouse