View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.sra.security.cas;
20  
21  import org.jasig.cas.client.Protocol;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.springframework.http.HttpMethod;
25  import org.springframework.security.web.server.util.matcher.ServerWebExchangeMatcher;
26  import org.springframework.web.server.ServerWebExchange;
27  import org.springframework.web.util.UriComponentsBuilder;
28  import reactor.core.publisher.Mono;
29  
30  public final class CASUtils {
31  
32      private static final Logger LOG = LoggerFactory.getLogger(CASUtils.class);
33  
34      public static Mono<String> safeGetParameter(final ServerWebExchange exchange, final String parameter) {
35          if (exchange.getRequest().getMethod() == HttpMethod.POST) {
36              LOG.debug(
37                      "safeGetParameter called on a POST ServerHttpRequest for Restricted Parameters. "
38                      + "Cannot complete check safely. "
39                      + "Reverting to standard behavior for this Parameter");
40              return exchange.getFormData().
41                      flatMap(form -> Mono.justOrEmpty(form.getFirst(parameter)));
42          }
43          return Mono.justOrEmpty(exchange.getRequest().getQueryParams().getFirst(parameter));
44      }
45  
46      public static Mono<String> retrieveTicketFromRequest(final ServerWebExchange exchange, final Protocol protocol) {
47          return safeGetParameter(exchange, protocol.getArtifactParameterName());
48      }
49  
50      public static ServerWebExchangeMatcher ticketAvailable(final Protocol protocol) {
51          return exchange -> retrieveTicketFromRequest(exchange, protocol).
52                  flatMap(ticket -> ServerWebExchangeMatcher.MatchResult.match()).
53                  switchIfEmpty(ServerWebExchangeMatcher.MatchResult.notMatch());
54      }
55  
56      public static String constructServiceUrl(final ServerWebExchange exchange, final Protocol protocol) {
57          return UriComponentsBuilder.fromHttpRequest(exchange.getRequest()).
58                  replaceQueryParam(protocol.getArtifactParameterName()).
59                  build().
60                  toUriString();
61      }
62  
63      private CASUtils() {
64          // private constructor for static utility class
65      }
66  }