|
| 1 | +/* |
| 2 | + * Copyright 2014-2018 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package docs.security; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import javax.servlet.http.HttpServletResponse; |
| 23 | + |
| 24 | +import org.springframework.http.HttpHeaders; |
| 25 | +import org.springframework.http.ResponseCookie; |
| 26 | +import org.springframework.lang.NonNull; |
| 27 | + |
| 28 | +final class ResponseCookieParser { |
| 29 | + |
| 30 | + private ResponseCookieParser() { |
| 31 | + } |
| 32 | + |
| 33 | + static List<ResponseCookie> parse(HttpServletResponse response) { |
| 34 | + return doParse(response, null); |
| 35 | + } |
| 36 | + |
| 37 | + static ResponseCookie parse(HttpServletResponse response, String cookieName) { |
| 38 | + List<ResponseCookie> responseCookies = doParse(response, cookieName); |
| 39 | + return (!responseCookies.isEmpty() ? responseCookies.get(0) : null); |
| 40 | + } |
| 41 | + |
| 42 | + @NonNull |
| 43 | + private static List<ResponseCookie> doParse(HttpServletResponse response, |
| 44 | + String cookieName) { |
| 45 | + List<ResponseCookie> responseCookies = new ArrayList<>(); |
| 46 | + for (String setCookieHeader : response.getHeaders(HttpHeaders.SET_COOKIE)) { |
| 47 | + String[] cookieParts = setCookieHeader.split("\\s*=\\s*", 2); |
| 48 | + if (cookieParts.length != 2) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + String name = cookieParts[0]; |
| 52 | + if (cookieName != null && !name.equals(cookieName)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + String[] valueAndDirectives = cookieParts[1].split("\\s*;\\s*", 2); |
| 56 | + String value = valueAndDirectives[0]; |
| 57 | + String[] directives = valueAndDirectives[1].split("\\s*;\\s*"); |
| 58 | + String domain = null; |
| 59 | + int maxAge = -1; |
| 60 | + String path = null; |
| 61 | + boolean secure = false; |
| 62 | + boolean httpOnly = false; |
| 63 | + String sameSite = null; |
| 64 | + for (String directive : directives) { |
| 65 | + if (directive.startsWith("Domain")) { |
| 66 | + domain = directive.split("=")[1]; |
| 67 | + } |
| 68 | + if (directive.startsWith("Max-Age")) { |
| 69 | + maxAge = Integer.parseInt(directive.split("=")[1]); |
| 70 | + } |
| 71 | + if (directive.startsWith("Path")) { |
| 72 | + path = directive.split("=")[1]; |
| 73 | + } |
| 74 | + if (directive.startsWith("Secure")) { |
| 75 | + secure = true; |
| 76 | + } |
| 77 | + if (directive.startsWith("HttpOnly")) { |
| 78 | + httpOnly = true; |
| 79 | + } |
| 80 | + if (directive.startsWith("SameSite")) { |
| 81 | + sameSite = directive.split("=")[1]; |
| 82 | + } |
| 83 | + } |
| 84 | + responseCookies.add(ResponseCookie.from(name, value).maxAge(maxAge).path(path) |
| 85 | + .domain(domain).secure(secure).httpOnly(httpOnly).sameSite(sameSite) |
| 86 | + .build()); |
| 87 | + } |
| 88 | + return responseCookies; |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments