1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
65 }
66 }