1 | package com.jsql.util; | |
2 | ||
3 | import com.jsql.model.InjectionModel; | |
4 | import org.apache.commons.codec.digest.DigestUtils; | |
5 | import org.apache.commons.lang3.StringUtils; | |
6 | import org.apache.logging.log4j.LogManager; | |
7 | import org.apache.logging.log4j.Logger; | |
8 | ||
9 | import java.net.MalformedURLException; | |
10 | import java.net.URI; | |
11 | import java.net.URISyntaxException; | |
12 | import java.net.URL; | |
13 | import java.net.http.HttpRequest.Builder; | |
14 | import java.util.AbstractMap.SimpleEntry; | |
15 | import java.util.Arrays; | |
16 | import java.util.Map; | |
17 | import java.util.stream.Collectors; | |
18 | ||
19 | public class DigestUtil { | |
20 | ||
21 | /** | |
22 | * Log4j logger sent to view. | |
23 | */ | |
24 | private static final Logger LOGGER = LogManager.getRootLogger(); | |
25 | ||
26 | private String tokenDigest = null; | |
27 | ||
28 | private final InjectionModel injectionModel; | |
29 | ||
30 | public DigestUtil(InjectionModel injectionModel) { | |
31 | this.injectionModel = injectionModel; | |
32 | } | |
33 | ||
34 | public void parseWwwAuthenticate(Map<String, String> mapResponse) { | |
35 | ||
36 | if ( | |
37 |
1
1. parseWwwAuthenticate : negated conditional → NO_COVERAGE |
mapResponse.containsKey(HeaderUtil.WWW_AUTHENTICATE_RESPONSE) |
38 |
1
1. parseWwwAuthenticate : negated conditional → NO_COVERAGE |
&& mapResponse.get(HeaderUtil.WWW_AUTHENTICATE_RESPONSE).trim().startsWith("Digest") |
39 | ) { | |
40 | ||
41 | String[] digestParts = StringUtils.split( | |
42 | mapResponse.get(HeaderUtil.WWW_AUTHENTICATE_RESPONSE).replaceAll("(?i)^\\s*Digest", ""), | |
43 | "," | |
44 | ); | |
45 | ||
46 | Map<String, String> cookieValues = Arrays.stream(digestParts) | |
47 | .map(cookie -> { | |
48 | String[] cookieEntry = StringUtils.split(cookie, "="); | |
49 |
1
1. lambda$parseWwwAuthenticate$0 : replaced return value with null for com/jsql/util/DigestUtil::lambda$parseWwwAuthenticate$0 → NO_COVERAGE |
return new SimpleEntry<>( |
50 | cookieEntry[0].trim(), | |
51 | cookieEntry[1].trim() | |
52 | ); | |
53 | }) | |
54 | .collect( | |
55 | Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue) | |
56 | ); | |
57 | ||
58 | String realm = cookieValues.get("realm").replace("\"", ""); | |
59 | String qop = cookieValues.get("qop").replace("\"", ""); | |
60 | String nonce = cookieValues.get("nonce").replace("\"", ""); | |
61 | ||
62 | try { | |
63 | String username = this.injectionModel.getMediatorUtils().getAuthenticationUtil().getUsernameAuthentication(); | |
64 | String password = this.injectionModel.getMediatorUtils().getAuthenticationUtil().getPasswordAuthentication(); | |
65 | String nc = "00000001"; | |
66 | String cnonce = "2ecb0e39da79fcb5aa6ffb1bd45cb3bb"; | |
67 | ||
68 | URL url = new URI(this.injectionModel.getMediatorUtils().getConnectionUtil().getUrlByUser()).toURL(); | |
69 | String path = url.getFile(); | |
70 | ||
71 | String ha1 = DigestUtils.md5Hex( | |
72 | String.format("%s:%s:%s", username, realm, password) | |
73 | ); | |
74 | String ha2 = DigestUtils.md5Hex( | |
75 | String.format("%s:%s", this.injectionModel.getMediatorUtils().getConnectionUtil().getTypeRequest(), path) | |
76 | ); | |
77 | String response = DigestUtils.md5Hex( | |
78 | String.format("%s:%s:%s:%s:%s:%s", ha1, nonce, nc, cnonce, qop, ha2) | |
79 | ); | |
80 | ||
81 | this.tokenDigest = String.format( | |
82 | "Digest username=\"%s\",realm=\"%s\",nonce=\"%s\",uri=\"%s\",cnonce=\"%s\",nc=%s,response=\"%s\",qop=\"%s\"", | |
83 | username, realm, nonce, path, cnonce, nc, response, qop | |
84 | ); | |
85 | } catch (MalformedURLException | URISyntaxException e) { | |
86 | LOGGER.error("Incorrect URL", e); | |
87 | } | |
88 | } | |
89 | } | |
90 | ||
91 | public void addHeaderToken(Builder httpRequest) { | |
92 | | |
93 |
1
1. addHeaderToken : negated conditional → NO_COVERAGE |
if (this.tokenDigest == null) { |
94 | return; | |
95 | } | |
96 | ||
97 | httpRequest.setHeader("Authorization", this.tokenDigest); | |
98 | } | |
99 | ||
100 | public boolean isDigest() { | |
101 |
2
1. isDigest : negated conditional → NO_COVERAGE 2. isDigest : replaced boolean return with true for com/jsql/util/DigestUtil::isDigest → NO_COVERAGE |
return this.tokenDigest != null; |
102 | } | |
103 | ||
104 | public void setTokenDigest(String tokenDigest) { | |
105 | this.tokenDigest = tokenDigest; | |
106 | } | |
107 | ||
108 | public String getTokenDigest() { | |
109 |
1
1. getTokenDigest : replaced return value with "" for com/jsql/util/DigestUtil::getTokenDigest → NO_COVERAGE |
return tokenDigest; |
110 | } | |
111 | } | |
Mutations | ||
37 |
1.1 |
|
38 |
1.1 |
|
49 |
1.1 |
|
93 |
1.1 |
|
101 |
1.1 2.2 |
|
109 |
1.1 |