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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40bf041e6919be1d72c4a9c737d347f137655521390502d8ea295aa88af84a4d
4
- data.tar.gz: 9f1da8dbef7c18c16d0b639c31992ce077f9d5b93f35e3515b93990002afad2a
3
+ metadata.gz: f6c152942d702bdba2e7abbe4a11d5a9baddc01903e08c70c18c8952d1082479
4
+ data.tar.gz: d392fb4a47e0d34e1dd38ccbde15926b4bc45a817f2f1f7722ebd25ece9461bb
5
5
  SHA512:
6
- metadata.gz: 7e711ba392d49fb80afe98c808196302c85530fd83ab67849a555dbd7890e81c978727cef898c17be1494e161f3e453af9915d118732a93733c048b329d70f20
7
- data.tar.gz: ddf3131bf5c39099ee130907238668a1523b9e0883a5867d0fcfa4322f1e880a1b8f39fabb49de12e7811ef9929be74a6e19389b021eeb16803d4a1b39eaf7a1
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
- module Authentication
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 self.config(options = {})
21
- raise ConfigItemMissing, "Missing token_store_file from config." unless options[:token_store_file]
22
- raise ConfigItemMissing, "Missing user from config." unless options[:user]
23
- raise ConfigItemMissing, "Missing password from config." unless options[:password]
24
- raise ConfigItemMissing, "Missing server_name from config." unless options[:server_name]
25
- raise ConfigItemMissing, "Missing client_id from config." unless options[:client_id]
26
- raise ConfigItemMissing, "Missing endpoint_url from config." unless options[:endpoint_url]
27
- raise ConfigItemMissing, "Missing authorization_url from config." unless options[:authorization_url]
28
- raise ConfigItemMissing, "Missing realm from config." unless options[:realm]
29
-
30
- 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])
31
47
  raise TokenStoreFileNotFound
32
48
  end
33
49
 
34
50
  @token = nil
35
- @token_store_file = options[:token_store_file]
36
- @user = options[:user]
37
- @password = options[:password]
38
- @server_name = options[:server_name]
39
- @client_id = options[:client_id]
40
- @endpoint_url = options[:endpoint_url]
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 self.header
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
- # Flush instance variables and save default values in token store file
69
- def self.flush
80
+ # Save default values in token store file
81
+ def flush
70
82
  write_token(data: empty_token)
71
83
  end
72
84
 
73
- class << self
74
- attr_accessor :endpoint_url
85
+ def flush_config
86
+ @opts = default_opts
87
+ @config = nil
88
+ @token = nil
89
+ end
75
90
 
76
- private
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
- def access_token
79
- begin
80
- token[@server_name.to_sym][:access_token]
81
- rescue
82
- raise TokenStoreContentInvalid
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
- def refresh_token
87
- begin
88
- token[@server_name.to_sym][:refresh_token]
89
- rescue
90
- raise TokenStoreContentInvalid
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
- def auth_expiry
95
- begin
96
- token[@server_name.to_sym][:auth_expiry]
97
- rescue
98
- raise TokenStoreContentInvalid
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
- def get_token
103
- response = Keycloak::Client.get_token(
104
- @user,
105
- @password,
106
- client_id= @client_id,
107
- secret='')
108
- JSON.parse(response, symbolize_names: true)
109
- end
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
- def set_token
112
- json = get_token
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
- def renew_token
118
- begin
119
- response = Keycloak::Client.get_token_by_refresh_token(
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
- def save_token(access_token:, refresh_token:, auth_expiry:)
146
- data = JSON.parse(::File.read(@token_store_file), symbolize_names: true) rescue {}
147
- data[@server_name.to_sym] = {
148
- access_token: access_token,
149
- refresh_token: refresh_token,
150
- auth_expiry: auth_expiry
151
- }
152
- write_token(data: data)
153
- end
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
- def write_token(data:)
156
- ::File.write(@token_store_file, JSON.dump(data))
157
- @token = data
158
- end
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::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 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::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 = 2
5
+ MINOR = 7
6
+ PATCH = 1
7
7
  BUILD = 0
8
8
 
9
9
  def self.version
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: 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
- def self.classes
18
- Dina::BaseModel.subclasses
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.6.2.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: 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