dina 0.6.2.0 → 0.7.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40bf041e6919be1d72c4a9c737d347f137655521390502d8ea295aa88af84a4d
4
- data.tar.gz: 9f1da8dbef7c18c16d0b639c31992ce077f9d5b93f35e3515b93990002afad2a
3
+ metadata.gz: f683d4b9e156194f6c356e5ebfbd46fcb6f821548403cb5821357d0813d5ba6a
4
+ data.tar.gz: 867eef99e2ffb402420f425f495c4b44f4b5e98dbf955383d7fe39215f8d1104
5
5
  SHA512:
6
- metadata.gz: 7e711ba392d49fb80afe98c808196302c85530fd83ab67849a555dbd7890e81c978727cef898c17be1494e161f3e453af9915d118732a93733c048b329d70f20
7
- data.tar.gz: ddf3131bf5c39099ee130907238668a1523b9e0883a5867d0fcfa4322f1e880a1b8f39fabb49de12e7811ef9929be74a6e19389b021eeb16803d4a1b39eaf7a1
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]
@@ -53,7 +71,7 @@ module Dina
53
71
  # and load the token_store_file with every call to header
54
72
  #
55
73
  # @return [String] the Bearer token
56
- def self.header
74
+ def header
57
75
  if access_token.nil? || refresh_token.nil?
58
76
  set_token
59
77
  end
@@ -66,98 +84,94 @@ module Dina
66
84
  end
67
85
 
68
86
  # Flush instance variables and save default values in token store file
69
- def self.flush
87
+ def flush
70
88
  write_token(data: empty_token)
71
89
  end
72
90
 
73
- class << self
74
- attr_accessor :endpoint_url
91
+ private
75
92
 
76
- private
77
-
78
- def access_token
79
- begin
80
- token[@server_name.to_sym][:access_token]
81
- rescue
82
- raise TokenStoreContentInvalid
83
- end
93
+ def access_token
94
+ begin
95
+ token[@server_name.to_sym][:access_token]
96
+ rescue
97
+ raise TokenStoreContentInvalid
84
98
  end
99
+ end
85
100
 
86
- def refresh_token
87
- begin
88
- token[@server_name.to_sym][:refresh_token]
89
- rescue
90
- raise TokenStoreContentInvalid
91
- end
101
+ def refresh_token
102
+ begin
103
+ token[@server_name.to_sym][:refresh_token]
104
+ rescue
105
+ raise TokenStoreContentInvalid
92
106
  end
107
+ end
93
108
 
94
- def auth_expiry
95
- begin
96
- token[@server_name.to_sym][:auth_expiry]
97
- rescue
98
- raise TokenStoreContentInvalid
99
- end
109
+ def auth_expiry
110
+ begin
111
+ token[@server_name.to_sym][:auth_expiry]
112
+ rescue
113
+ raise TokenStoreContentInvalid
100
114
  end
115
+ end
116
+
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
101
125
 
102
- def get_token
103
- response = Keycloak::Client.get_token(
104
- @user,
105
- @password,
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,
106
136
  client_id= @client_id,
107
137
  secret='')
108
- JSON.parse(response, symbolize_names: true)
109
- end
110
-
111
- def set_token
112
- json = get_token
138
+ json = JSON.parse(response, symbolize_names: true)
113
139
  auth_expiry = (Time.now + json[:expires_in].seconds).to_s
114
140
  save_token(access_token: json[:access_token], refresh_token: json[:refresh_token], auth_expiry: auth_expiry)
141
+ rescue
142
+ set_token
115
143
  end
144
+ end
116
145
 
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
146
+ def token
147
+ @token ||= JSON.parse(::File.read(@token_store_file), symbolize_names: true)
148
+ end
144
149
 
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
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
154
159
 
155
- def write_token(data:)
156
- ::File.write(@token_store_file, JSON.dump(data))
157
- @token = data
158
- 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
159
169
 
170
+ def write_token(data:)
171
+ ::File.write(@token_store_file, JSON.dump(data))
172
+ @token = data
160
173
  end
161
174
 
162
175
  end
176
+
163
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 = 2
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.2.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