@@ -44,43 +44,22 @@ class AuthenticatorContract < Dry::Validation::Contract
44
44
45
45
# Verify that `issuer` has a secret value set if the variable is present
46
46
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' )
55
48
end
56
49
57
50
# Verify that `claim_aliases` has a secret value set if variable is present
58
51
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' )
67
53
end
68
54
69
55
# Verify that `provider_uri` has a secret value set if variable is present
70
56
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' )
79
58
end
80
59
81
60
# Verify that `jwks-uri`, `public-keys`, or `provider-uri` has a secret value set if a variable exists
82
61
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? }
84
63
if empty_variables . count == 1
85
64
# Performing this insanity to match current functionality :P
86
65
error = if empty_variables . first == :provider_uri
@@ -122,14 +101,7 @@ class AuthenticatorContract < Dry::Validation::Contract
122
101
123
102
# Verify that `token_app_property` has a secret value set if the variable is present
124
103
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' )
133
105
end
134
106
135
107
# Verify that `token_app_property` includes only valid characters
@@ -158,38 +130,17 @@ class AuthenticatorContract < Dry::Validation::Contract
158
130
159
131
# Verify that `audience` has a secret value set if variable is present
160
132
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' )
169
134
end
170
135
171
136
# Verify that `identity_path` has a secret value set if variable is present
172
137
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' )
181
139
end
182
140
183
141
# Verify that `enforced_claims` has a secret value set if variable is present
184
142
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' )
193
144
end
194
145
195
146
# Verify that claim values contain only "allowed" characters (alpha-numeric, plus: "-", "_", "/", ".")
@@ -217,7 +168,7 @@ class AuthenticatorContract < Dry::Validation::Contract
217
168
218
169
# Verify that claim alias lookup has aliases defined only once
219
170
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 ] )
221
172
if ( duplicate = claims . detect { |claim | claims . count ( claim ) > 1 } )
222
173
utils . failed_response (
223
174
key : key ,
@@ -249,7 +200,7 @@ class AuthenticatorContract < Dry::Validation::Contract
249
200
250
201
# Check for "/" in claim keys
251
202
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 ] )
253
204
claims . flatten . each do |claim |
254
205
next unless claim . match ( %r{/} )
255
206
@@ -262,7 +213,7 @@ class AuthenticatorContract < Dry::Validation::Contract
262
213
263
214
# Check for invalid characters in keys
264
215
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 ] )
266
217
if ( bad_claim = claims . find { |claim | claim . count ( 'a-zA-Z0-9\-_\.' ) != claim . length } )
267
218
utils . failed_response (
268
219
key : key ,
@@ -360,14 +311,22 @@ class AuthenticatorContract < Dry::Validation::Contract
360
311
361
312
# Verify that `ca_cert` has a secret value set if the variable is present
362
313
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 } "
369
328
)
370
- end
329
+ )
371
330
end
372
331
end
373
332
end
0 commit comments