Skip to content

Commit e7d8b6e

Browse files
committed
Refactoring
1 parent 8393770 commit e7d8b6e

File tree

1 file changed

+26
-67
lines changed

1 file changed

+26
-67
lines changed

app/domain/authentication/authn_jwt/v2/data_objects/authenticator_contract.rb

Lines changed: 26 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -44,43 +44,22 @@ class AuthenticatorContract < Dry::Validation::Contract
4444

4545
# Verify that `issuer` has a secret value set if the variable is present
4646
rule(:issuer, :account, :service_id) do
47-
if values[:issuer].empty?
48-
utils.failed_response(
49-
key: key,
50-
error: Errors::Conjur::RequiredSecretMissing.new(
51-
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/issuer"
52-
)
53-
)
54-
end
47+
variable_empty?(key: key, values: values, variable: 'issuer')
5548
end
5649

5750
# Verify that `claim_aliases` has a secret value set if variable is present
5851
rule(:claim_aliases, :account, :service_id) do
59-
if values[:claim_aliases].empty?
60-
utils.failed_response(
61-
key: key,
62-
error: Errors::Conjur::RequiredSecretMissing.new(
63-
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/claim-aliases"
64-
)
65-
)
66-
end
52+
variable_empty?(key: key, values: values, variable: 'claim-aliases')
6753
end
6854

6955
# Verify that `provider_uri` has a secret value set if variable is present
7056
rule(:provider_uri, :service_id, :account) do
71-
if values[:provider_uri].empty?
72-
utils.failed_response(
73-
key: key,
74-
error: Errors::Conjur::RequiredSecretMissing.new(
75-
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/provider-uri"
76-
)
77-
)
78-
end
57+
variable_empty?(key: key, values: values, variable: 'provider-uri')
7958
end
8059

8160
# Verify that `jwks-uri`, `public-keys`, or `provider-uri` has a secret value set if a variable exists
8261
rule(:jwks_uri, :public_keys, :provider_uri, :account, :service_id) do
83-
empty_variables = %i[jwks_uri provider_uri public_keys].select {|key, _| values[key].empty? && !values[key].nil? }
62+
empty_variables = %i[jwks_uri provider_uri public_keys].select {|key, _| values[key] == '' && !values[key].nil? }
8463
if empty_variables.count == 1
8564
# Performing this insanity to match current functionality :P
8665
error = if empty_variables.first == :provider_uri
@@ -122,14 +101,7 @@ class AuthenticatorContract < Dry::Validation::Contract
122101

123102
# Verify that `token_app_property` has a secret value set if the variable is present
124103
rule(:token_app_property, :account, :service_id) do
125-
if values[:token_app_property].empty?
126-
utils.failed_response(
127-
key: key,
128-
error: Errors::Conjur::RequiredSecretMissing.new(
129-
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/token-app-property"
130-
)
131-
)
132-
end
104+
variable_empty?(key: key, values: values, variable: 'token-app-property')
133105
end
134106

135107
# Verify that `token_app_property` includes only valid characters
@@ -158,38 +130,17 @@ class AuthenticatorContract < Dry::Validation::Contract
158130

159131
# Verify that `audience` has a secret value set if variable is present
160132
rule(:audience, :service_id, :account) do
161-
if values[:audience].empty?
162-
utils.failed_response(
163-
key: key,
164-
error: Errors::Conjur::RequiredSecretMissing.new(
165-
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/audience"
166-
)
167-
)
168-
end
133+
variable_empty?(key: key, values: values, variable: 'audience')
169134
end
170135

171136
# Verify that `identity_path` has a secret value set if variable is present
172137
rule(:identity_path, :service_id, :account) do
173-
if values[:identity_path].empty?
174-
utils.failed_response(
175-
key: key,
176-
error: Errors::Conjur::RequiredSecretMissing.new(
177-
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/identity-path"
178-
)
179-
)
180-
end
138+
variable_empty?(key: key, values: values, variable: 'identity-path')
181139
end
182140

183141
# Verify that `enforced_claims` has a secret value set if variable is present
184142
rule(:enforced_claims, :service_id, :account) do
185-
if values[:enforced_claims].empty?
186-
utils.failed_response(
187-
key: key,
188-
error: Errors::Conjur::RequiredSecretMissing.new(
189-
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/enforced-claims"
190-
)
191-
)
192-
end
143+
variable_empty?(key: key, values: values, variable: 'enforced-claims')
193144
end
194145

195146
# Verify that claim values contain only "allowed" characters (alpha-numeric, plus: "-", "_", "/", ".")
@@ -217,7 +168,7 @@ class AuthenticatorContract < Dry::Validation::Contract
217168

218169
# Verify that claim alias lookup has aliases defined only once
219170
rule(:claim_aliases) do
220-
claims = values[:claim_aliases].to_s.split(',').map{|s| s.split(':').map(&:strip)}.map(&:first)
171+
claims = claim_as_array(values[:claim_aliases])
221172
if (duplicate = claims.detect { |claim| claims.count(claim) > 1 })
222173
utils.failed_response(
223174
key: key,
@@ -249,7 +200,7 @@ class AuthenticatorContract < Dry::Validation::Contract
249200

250201
# Check for "/" in claim keys
251202
rule(:claim_aliases) do
252-
claims = values[:claim_aliases].to_s.split(',').map{|s| s.split(':').map(&:strip)}.map(&:first)
203+
claims = claim_as_array(values[:claim_aliases])
253204
claims.flatten.each do |claim|
254205
next unless claim.match(%r{/})
255206

@@ -262,7 +213,7 @@ class AuthenticatorContract < Dry::Validation::Contract
262213

263214
# Check for invalid characters in keys
264215
rule(:claim_aliases) do
265-
claims = values[:claim_aliases].to_s.split(',').map{|s| s.split(':').map(&:strip)}.map(&:first)
216+
claims = claim_as_array(values[:claim_aliases])
266217
if (bad_claim = claims.find { |claim| claim.count('a-zA-Z0-9\-_\.') != claim.length })
267218
utils.failed_response(
268219
key: key,
@@ -360,14 +311,22 @@ class AuthenticatorContract < Dry::Validation::Contract
360311

361312
# Verify that `ca_cert` has a secret value set if the variable is present
362313
rule(:ca_cert, :account, :service_id) do
363-
if values[:ca_cert].empty?
364-
utils.failed_response(
365-
key: key,
366-
error: Errors::Conjur::RequiredSecretMissing.new(
367-
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/ca-cert"
368-
)
314+
variable_empty?(key: key, values: values, variable: 'ca-cert')
315+
end
316+
317+
def claim_as_array(claim)
318+
claim.to_s.split(',').map{|s| s.split(':').map(&:strip)}.map(&:first)
319+
end
320+
321+
def variable_empty?(key:, values:, variable:)
322+
return unless values[variable.underscore.to_sym] == ''
323+
324+
utils.failed_response(
325+
key: key,
326+
error: Errors::Conjur::RequiredSecretMissing.new(
327+
"#{values[:account]}:variable:conjur/authn-jwt/#{values[:service_id]}/#{variable}"
369328
)
370-
end
329+
)
371330
end
372331
end
373332
end

0 commit comments

Comments
 (0)