1
+ // Copyright © 2024-Present The Serverless Workflow Specification Authors
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License"),
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ // http://www.apache.org/licenses/LICENSE-2.0
7
+ //
8
+ // Unless required by applicable law or agreed to in writing, software
9
+ // distributed under the License is distributed on an "AS IS" BASIS,
10
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ // See the License for the specific language governing permissions and
12
+ // limitations under the License.
13
+
14
+ using FluentValidation ;
15
+ using ServerlessWorkflow . Sdk . Models ;
16
+
17
+ namespace ServerlessWorkflow . Sdk . Validation ;
18
+
19
+ /// <summary>
20
+ /// Represents the <see cref="IValidator"/> used to validate <see cref="AuthenticationPolicyDefinition"/>s
21
+ /// </summary>
22
+ public class AuthenticationPolicyDefinitionValidator
23
+ : AbstractValidator < AuthenticationPolicyDefinition >
24
+ {
25
+
26
+ /// <inheritdoc/>
27
+ public AuthenticationPolicyDefinitionValidator ( IServiceProvider serviceProvider , ComponentDefinitionCollection ? components )
28
+ {
29
+ this . ServiceProvider = serviceProvider ;
30
+ this . Components = components ;
31
+ this . RuleFor ( auth => auth . Use ! )
32
+ . Must ( ReferenceAnExistingAuthentication )
33
+ . When ( auth => ! string . IsNullOrWhiteSpace ( auth . Use ) ) ;
34
+ this . RuleFor ( auth => auth . Basic ! . Use ! )
35
+ . Must ( ReferenceAnExistingSecret )
36
+ . When ( auth => ! string . IsNullOrWhiteSpace ( auth . Basic ? . Use ) ) ;
37
+ this . RuleFor ( auth => auth . Bearer ! . Use ! )
38
+ . Must ( ReferenceAnExistingSecret )
39
+ . When ( auth => ! string . IsNullOrWhiteSpace ( auth . Bearer ? . Use ) ) ;
40
+ this . RuleFor ( auth => auth . Certificate ! . Use ! )
41
+ . Must ( ReferenceAnExistingSecret )
42
+ . When ( auth => ! string . IsNullOrWhiteSpace ( auth . Certificate ? . Use ) ) ;
43
+ this . RuleFor ( auth => auth . Digest ! . Use ! )
44
+ . Must ( ReferenceAnExistingSecret )
45
+ . When ( auth => ! string . IsNullOrWhiteSpace ( auth . Digest ? . Use ) ) ;
46
+ this . RuleFor ( auth => auth . OAuth2 ! . Use ! )
47
+ . Must ( ReferenceAnExistingSecret )
48
+ . When ( auth => ! string . IsNullOrWhiteSpace ( auth . OAuth2 ? . Use ) ) ;
49
+ this . RuleFor ( auth => auth . Oidc ! . Use ! )
50
+ . Must ( ReferenceAnExistingSecret )
51
+ . When ( auth => ! string . IsNullOrWhiteSpace ( auth . Oidc ? . Use ) ) ;
52
+ }
53
+
54
+ /// <summary>
55
+ /// Gets the current <see cref="IServiceProvider"/>
56
+ /// </summary>
57
+ protected IServiceProvider ServiceProvider { get ; }
58
+
59
+ /// <summary>
60
+ /// Gets the configured reusable components
61
+ /// </summary>
62
+ protected ComponentDefinitionCollection ? Components { get ; }
63
+
64
+ /// <summary>
65
+ /// Determines whether or not the specified authentication exists
66
+ /// </summary>
67
+ /// <param name="name">The name of the authentication to check</param>
68
+ /// <returns>A boolean indicating whether or not the specified authentication exists</returns>
69
+ protected virtual bool ReferenceAnExistingAuthentication ( string name )
70
+ {
71
+ if ( this . Components ? . Authentications ? . ContainsKey ( name ) == true ) return true ;
72
+ else return false ;
73
+ }
74
+
75
+ /// <summary>
76
+ /// Determines whether or not the specified secret exists
77
+ /// </summary>
78
+ /// <param name="name">The name of the secret to check</param>
79
+ /// <returns>A boolean indicating whether or not the specified secret exists</returns>
80
+ protected virtual bool ReferenceAnExistingSecret ( string name )
81
+ {
82
+ if ( this . Components ? . Secrets ? . Contains ( name ) == true ) return true ;
83
+ else return false ;
84
+ }
85
+
86
+ }
0 commit comments