View Javadoc
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      private static final Logger LOGGER = LogManager.getRootLogger();
22  
23      private String tokenDigest = null;
24  
25      private final InjectionModel injectionModel;
26  
27      public DigestUtil(InjectionModel injectionModel) {
28          this.injectionModel = injectionModel;
29      }
30  
31      public void parseWwwAuthenticate(Map<String, String> mapResponse) {
32          if (
33              mapResponse.containsKey(HeaderUtil.WWW_AUTHENTICATE_RESPONSE)
34              && mapResponse.get(HeaderUtil.WWW_AUTHENTICATE_RESPONSE).trim().startsWith("Digest")
35          ) {
36              String[] digestParts = StringUtils.split(
37                  mapResponse.get(HeaderUtil.WWW_AUTHENTICATE_RESPONSE).replaceAll("(?i)^\\s*Digest", StringUtils.EMPTY),
38                  ","
39              );
40  
41              Map<String, String> cookieValues = Arrays.stream(digestParts)
42                  .map(cookie -> {
43                      String[] cookieEntry = StringUtils.split(cookie, "=");
44                      return new SimpleEntry<>(
45                          cookieEntry[0].trim(),
46                          cookieEntry[1].trim()
47                      );
48                  })
49                  .collect(
50                      Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue)
51                  );
52  
53              String realm = cookieValues.get("realm").replace("\"", StringUtils.EMPTY);
54              String qop = cookieValues.get("qop").replace("\"", StringUtils.EMPTY);
55              String nonce = cookieValues.get("nonce").replace("\"", StringUtils.EMPTY);
56  
57              try {
58                  String username = this.injectionModel.getMediatorUtils().getAuthenticationUtil().getUsernameAuthentication();
59                  String password = this.injectionModel.getMediatorUtils().getAuthenticationUtil().getPasswordAuthentication();
60                  String nc = "00000001";
61                  String cnonce = "2ecb0e39da79fcb5aa6ffb1bd45cb3bb";
62  
63                  URL url = new URI(this.injectionModel.getMediatorUtils().getConnectionUtil().getUrlByUser()).toURL();
64                  String path = url.getFile();
65  
66                  String ha1 = DigestUtils.md5Hex(
67                      String.format("%s:%s:%s", username, realm, password)
68                  );
69                  String ha2 = DigestUtils.md5Hex(
70                      String.format("%s:%s", this.injectionModel.getMediatorUtils().getConnectionUtil().getTypeRequest(), path)
71                  );
72                  String response = DigestUtils.md5Hex(
73                      String.format("%s:%s:%s:%s:%s:%s", ha1, nonce, nc, cnonce, qop, ha2)
74                  );
75  
76                  this.tokenDigest = String.format(
77                      "Digest username=\"%s\",realm=\"%s\",nonce=\"%s\",uri=\"%s\",cnonce=\"%s\",nc=%s,response=\"%s\",qop=\"%s\"",
78                      username, realm, nonce, path, cnonce, nc, response, qop
79                  );
80              } catch (MalformedURLException | URISyntaxException e) {
81                  LOGGER.error("Incorrect URL", e);
82              }
83          }
84      }
85  
86      public void addHeaderToken(Builder httpRequest) {
87          if (this.tokenDigest == null) {
88               return;
89          }
90          httpRequest.setHeader("Authorization", this.tokenDigest);
91      }
92  
93      public boolean isDigest() {
94          return this.tokenDigest != null;
95      }
96  
97      public void setTokenDigest(String tokenDigest) {
98          this.tokenDigest = tokenDigest;
99      }
100 
101     public String getTokenDigest() {
102         return this.tokenDigest;
103     }
104 }