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.pac4j;
20
21 import java.net.URI;
22 import org.pac4j.core.exception.http.RedirectionAction;
23 import org.pac4j.core.exception.http.WithContentAction;
24 import org.pac4j.core.exception.http.WithLocationAction;
25 import org.springframework.http.HttpStatus;
26 import org.springframework.http.MediaType;
27 import reactor.core.publisher.Mono;
28
29 public final class RedirectionActionUtils {
30
31 public static Mono<Void> handle(
32 final RedirectionAction action,
33 final ServerWebExchangeContext swec) {
34
35 if (action instanceof WithLocationAction) {
36 WithLocationAction withLocationAction = (WithLocationAction) action;
37 swec.getNative().getResponse().setStatusCode(HttpStatus.FOUND);
38 swec.getNative().getResponse().getHeaders().setLocation(URI.create(withLocationAction.getLocation()));
39 return swec.getNative().getResponse().setComplete();
40 } else if (action instanceof WithContentAction) {
41 WithContentAction withContentAction = (WithContentAction) action;
42 String content = withContentAction.getContent();
43
44 if (content == null) {
45 throw new IllegalArgumentException("No content set for POST AuthnRequest");
46 }
47
48 return Mono.defer(() -> {
49 swec.getNative().getResponse().getHeaders().setContentType(MediaType.TEXT_HTML);
50 return swec.getNative().getResponse().
51 writeWith(Mono.just(swec.getNative().getResponse().bufferFactory().wrap(content.getBytes())));
52 });
53 } else {
54 throw new IllegalArgumentException("Unsupported Action: " + action.getClass().getName());
55 }
56 }
57
58 private RedirectionActionUtils() {
59
60 }
61 }