1 18 19 package sync4j.framework.core; 20 21 28 29 public class HMACAuthentication extends Authentication { 30 31 private final String DEFAULT_ALGORITHM = "MD5"; 33 34 35 private String userMac; 37 private String algorithm; 38 private String calculatedMac; 39 40 41 43 44 protected HMACAuthentication() {} 45 46 47 48 public HMACAuthentication(final String data) { 49 super(Constants.AUTH_TYPE_HMAC, Constants.FORMAT_B64, data); 50 } 51 52 53 55 60 public String getUserMac() { 61 return this.userMac; 62 } 63 64 65 69 public void setUserMac(String userMac) { 70 this.userMac = userMac; 71 } 72 73 78 public String getCalculatedMac() { 79 return this.calculatedMac; 80 }; 81 82 83 87 public void setCalculatedMac(String calculatedMac) { 88 this.calculatedMac = calculatedMac; 89 }; 90 91 96 public String getAlgorithm() { 97 return this.algorithm; 98 }; 99 100 101 107 public void setData(String data) { 108 if (data == null) { 109 throw new IllegalArgumentException ("data cannot be null"); 110 } 111 112 113 116 117 119 int indexAlgorithm = data.indexOf("algorithm"); 120 int indexEndAlgorithm = -1; 121 122 if (indexAlgorithm == -1) { 123 algorithm = DEFAULT_ALGORITHM; 124 } else { 125 indexEndAlgorithm = data.indexOf(",", indexAlgorithm); 126 algorithm = data.substring(indexAlgorithm + 10, indexEndAlgorithm); 127 } 128 129 131 int indexUsername = data.indexOf("username", indexEndAlgorithm + 1); 132 133 if (indexUsername == -1) { 134 throw new IllegalArgumentException ("Username missing in hmac header"); 135 } 136 137 int indexEndUsername = data.indexOf("\"", indexUsername + 10); 138 139 if (indexEndUsername == -1) { 140 throw new IllegalArgumentException ("Unable to get username from hmac header [" + data + 141 "]"); 142 } 143 144 while (data.charAt(indexEndUsername - 1) == '\\') { 146 indexEndUsername = data.indexOf("\"", indexEndUsername + 1); 147 } 148 149 if (indexEndUsername == -1) { 150 throw new IllegalArgumentException ("Unable to get username from hmac header [" + data + 151 "]"); 152 } 153 154 setUsername(data.substring(indexUsername + 10, indexEndUsername)); 155 156 158 int indexMac = data.indexOf("mac", indexEndUsername); 159 160 if (indexMac == -1) { 161 throw new IllegalArgumentException ("Mac value missing in hmac header"); 162 } else { 163 userMac = data.substring(indexMac + 4); 164 } 165 166 } 167 168 } | Popular Tags |