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.util.Collection;
22 import java.util.HashMap;
23 import java.util.LinkedHashSet;
24 import java.util.Map;
25 import java.util.Optional;
26 import org.apache.commons.lang3.ArrayUtils;
27 import org.pac4j.core.context.Cookie;
28 import org.pac4j.core.context.WebContext;
29 import org.pac4j.core.util.CommonHelper;
30 import org.springframework.http.HttpCookie;
31 import org.springframework.http.HttpHeaders;
32 import org.springframework.http.ResponseCookie;
33 import org.springframework.util.MultiValueMap;
34 import org.springframework.web.server.ServerWebExchange;
35 import org.springframework.web.util.UriComponentsBuilder;
36
37 public class ServerWebExchangeContext implements WebContext {
38
39 private final ServerWebExchange exchange;
40
41 private MultiValueMap<String, String> form;
42
43 private String body;
44
45
46
47
48
49
50 public ServerWebExchangeContext(final ServerWebExchange exchange) {
51 CommonHelper.assertNotNull("exchange", exchange);
52 this.exchange = exchange;
53 }
54
55 @Override
56 public Optional<String> getRequestAttribute(final String name) {
57 return Optional.ofNullable(exchange.getAttribute(name));
58 }
59
60 @Override
61 public void setRequestAttribute(final String name, final Object value) {
62 exchange.getAttributes().put(name, value);
63 }
64
65 @Override
66 public Optional<String> getRequestParameter(final String name) {
67 Map<String, String[]> params = getRequestParameters();
68 if (params.containsKey(name)) {
69 String[] values = params.get(name);
70 if (!ArrayUtils.isEmpty(values)) {
71 return Optional.of(values[0]);
72 }
73 }
74 return Optional.empty();
75 }
76
77 public ServerWebExchangeContext setForm(final MultiValueMap<String, String> form) {
78 this.form = form;
79 return this;
80 }
81
82 @Override
83 public Map<String, String[]> getRequestParameters() {
84 Map<String, String[]> params = new HashMap<>();
85
86 this.exchange.getRequest().getQueryParams().
87 forEach((key, value) -> params.put(key, new String[] { value.toString() }));
88
89 if (this.form != null) {
90 form.forEach((key, values) -> params.put(key, values.toArray(String[]::new)));
91 }
92
93 return params;
94 }
95
96 @Override
97 public Optional<String> getRequestHeader(final String name) {
98 return Optional.ofNullable(exchange.getRequest().getHeaders().getFirst(name));
99 }
100
101 @Override
102 public String getRequestMethod() {
103 return this.exchange.getRequest().getMethodValue();
104 }
105
106 @Override
107 public String getRemoteAddr() {
108 return this.exchange.getRequest().getRemoteAddress().getHostString();
109 }
110
111
112
113
114
115
116 public ServerWebExchange getNative() {
117 return this.exchange;
118 }
119
120 @Override
121 public void setResponseHeader(final String name, final String value) {
122
123 }
124
125 @Override
126 public Optional<String> getResponseHeader(final String s) {
127 return Optional.ofNullable(this.exchange.getResponse().getHeaders().getFirst(s));
128 }
129
130 @Override
131 public void setResponseContentType(final String content) {
132 this.exchange.getResponse().getHeaders().set(HttpHeaders.CONTENT_TYPE, content);
133 }
134
135 @Override
136 public String getProtocol() {
137 return isSecure() ? "https" : "http";
138 }
139
140 @Override
141 public String getServerName() {
142 return UriComponentsBuilder.fromHttpRequest(exchange.getRequest()).build().getHost();
143 }
144
145 @Override
146 public int getServerPort() {
147 return UriComponentsBuilder.fromHttpRequest(exchange.getRequest()).build().getPort();
148 }
149
150 @Override
151 public String getScheme() {
152 return UriComponentsBuilder.fromHttpRequest(exchange.getRequest()).build().getScheme();
153 }
154
155 @Override
156 public boolean isSecure() {
157 return this.exchange.getRequest().getSslInfo() != null;
158 }
159
160 @Override
161 public String getFullRequestURL() {
162 return UriComponentsBuilder.fromHttpRequest(exchange.getRequest()).build().toUriString();
163 }
164
165 @Override
166 public Collection<Cookie> getRequestCookies() {
167 MultiValueMap<String, HttpCookie> cookies = this.exchange.getRequest().getCookies();
168
169 Collection<Cookie> pac4jCookies = new LinkedHashSet<>();
170 cookies.toSingleValueMap().values().forEach(c -> {
171 Cookie cookie = new Cookie(c.getName(), c.getValue());
172 pac4jCookies.add(cookie);
173 });
174 return pac4jCookies;
175 }
176
177 @Override
178 public void addResponseCookie(final Cookie cookie) {
179 ResponseCookie.ResponseCookieBuilder c = ResponseCookie.from(cookie.getName(), cookie.getValue());
180 c.secure(cookie.isSecure());
181 c.path(cookie.getPath());
182 c.maxAge(cookie.getMaxAge());
183 c.httpOnly(cookie.isHttpOnly());
184 c.domain(cookie.getDomain());
185 this.exchange.getResponse().addCookie(c.build());
186 }
187
188 @Override
189 public String getPath() {
190 return exchange.getRequest().getPath().value();
191 }
192
193 public ServerWebExchangeContext setBody(final String body) {
194 this.body = body;
195 return this;
196 }
197
198 @Override
199 public String getRequestContent() {
200 return body;
201 }
202 }