2929import org .apache .commons .lang3 .StringUtils ;
3030import org .apache .commons .lang3 .tuple .Pair ;
3131import org .springframework .security .core .Authentication ;
32- import org .springframework .security .core .context .SecurityContextHolder ;
3332import org .springframework .stereotype .Service ;
3433
3534import javax .annotation .PostConstruct ;
36- import javax .inject .Inject ;
3735import java .util .ArrayList ;
3836import java .util .Comparator ;
3937import java .util .HashMap ;
4745@ Service
4846public class ParametersService {
4947
50- @ Inject
51- private IProxySpecProvider baseSpecProvider ;
48+ private final IProxySpecProvider baseSpecProvider ;
5249
53- @ Inject
54- private AccessControlEvaluationService accessControlEvaluationService ;
50+ private final AccessControlEvaluationService accessControlEvaluationService ;
5551
5652 private static final Pattern PARAMETER_ID_PATTERN = Pattern .compile ("[a-zA-Z\\ d_-]*" );
5753
54+ public ParametersService (IProxySpecProvider baseSpecProvider , AccessControlEvaluationService accessControlEvaluationService ) {
55+ this .baseSpecProvider = baseSpecProvider ;
56+ this .accessControlEvaluationService = accessControlEvaluationService ;
57+ }
58+
5859 @ PostConstruct
5960 public void init () {
6061 for (ProxySpec spec : baseSpecProvider .getSpecs ()) {
@@ -117,7 +118,7 @@ private void validateSpec(ProxySpec spec) {
117118
118119 }
119120
120- public boolean validateRequest (ProxySpec resolvedSpec , ProvidedParameters providedParameters ) throws InvalidParametersException {
121+ public boolean validateRequest (Authentication auth , ProxySpec resolvedSpec , ProvidedParameters providedParameters ) throws InvalidParametersException {
121122 Parameters parameters = resolvedSpec .getParameters ();
122123 if (parameters == null ) {
123124 return false ;
@@ -139,8 +140,6 @@ public boolean validateRequest(ProxySpec resolvedSpec, ProvidedParameters provid
139140 }
140141 }
141142
142- Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
143-
144143 // check if the combination of values is allowed
145144 for (Parameters .ValueSet valueSet : parameters .getValueSets ()) {
146145 if (!accessControlEvaluationService .checkAccess (auth , resolvedSpec , valueSet .getAccessControl ())) {
@@ -168,16 +167,15 @@ private boolean areParametersAllowedByValueSet(List<String> parameterIds, Parame
168167 return true ;
169168 }
170169
171- public AllowedParametersForUser calculateAllowedParametersForUser (ProxySpec proxySpec ) {
170+ public AllowedParametersForUser calculateAllowedParametersForUser (Authentication auth , ProxySpec proxySpec ) {
172171 Parameters parameters = proxySpec .getParameters ();
173172 if (parameters == null ) {
174173 return new AllowedParametersForUser (new HashMap <>(), new HashSet <>());
175174 }
176175 List <String > parameterIds = parameters .getIds ();
177176
178177 // 1. check which ValueSets are allowed for this
179- Authentication auth = SecurityContextHolder .getContext ().getAuthentication ();
180- List < Parameters .ValueSet > allowedValueSets = parameters .getValueSets ().stream ()
178+ List <Parameters .ValueSet > allowedValueSets = parameters .getValueSets ().stream ()
181179 .filter (v -> accessControlEvaluationService .checkAccess (auth , proxySpec , v .getAccessControl ()))
182180 .collect (Collectors .toList ());
183181
0 commit comments