|
| 1 | +/* |
| 2 | + * TeleStax, Open Source Cloud Communications |
| 3 | + * Copyright 2011-2018, Telestax Inc and individual contributors |
| 4 | + * by the @authors tag. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * under the terms of the GNU Affero General Public License as |
| 8 | + * published by the Free Software Foundation; either version 3 of |
| 9 | + * the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU Affero General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Affero General Public License |
| 17 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 18 | + */ |
| 19 | + |
| 20 | +package org.restcomm.connect.commons; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | +import org.restcomm.connect.commons.dao.Sid; |
| 24 | +import org.restcomm.connect.commons.util.DigestAuthentication; |
| 25 | + |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.Map; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertEquals; |
| 30 | +import static org.junit.Assert.assertTrue; |
| 31 | + |
| 32 | +public class DigestAuthenticationTest { |
| 33 | + |
| 34 | + private String client = "alice0000000"; |
| 35 | + private String domain = "org0000000.restcomm.com"; |
| 36 | + private String password = "1234"; |
| 37 | + private String proxyAuthHeader = "Digest username=\"alice0000000\",realm=\"org0000000.restcomm.com\",cnonce=\"6b8b4567\",nc=00000001,qop=auth,uri=\"sip:172.31.45.30:5080\",nonce=\"61343361383534392d633237372d343\",response=\"bc322276e42a123c53c2ed6f53d5e7c7\",algorithm=MD5"; |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testAuth(){ |
| 41 | + String hashedPass = DigestAuthentication.HA1(client, domain, password, "MD5"); |
| 42 | + |
| 43 | + assertEquals("9b11a2924d0881aca84f9db97f834d99", hashedPass); |
| 44 | + |
| 45 | + assertTrue(permitted(proxyAuthHeader, "INVITE", hashedPass)); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + static boolean permitted(final String authorization, final String method, String clientPassword) { |
| 50 | + final Map<String, String> map = authHeaderToMap(authorization); |
| 51 | + String user = map.get("username"); |
| 52 | + final String algorithm = map.get("algorithm"); |
| 53 | + final String realm = map.get("realm"); |
| 54 | + final String uri = map.get("uri"); |
| 55 | + final String nonce = map.get("nonce"); |
| 56 | + final String nc = map.get("nc"); |
| 57 | + final String cnonce = map.get("cnonce"); |
| 58 | + final String qop = map.get("qop"); |
| 59 | + final String response = map.get("response"); |
| 60 | + final String password2 = clientPassword; |
| 61 | + final String result = DigestAuthentication.response(algorithm, user, realm, "", password2, nonce, nc, cnonce, |
| 62 | + method, uri, null, qop); |
| 63 | + return result.equals(response); |
| 64 | + } |
| 65 | + |
| 66 | + private static Map<String, String> authHeaderToMap(final String header) { |
| 67 | + final Map<String, String> map = new HashMap<String, String>(); |
| 68 | + final int endOfScheme = header.indexOf(" "); |
| 69 | + map.put("scheme", header.substring(0, endOfScheme).trim()); |
| 70 | + final String[] tokens = header.substring(endOfScheme + 1).split(","); |
| 71 | + for (final String token : tokens) { |
| 72 | + final String[] values = token.trim().split("=",2); //Issue #935, split only for first occurrence of "=" |
| 73 | + map.put(values[0].toLowerCase(), values[1].replace("\"", "")); |
| 74 | + } |
| 75 | + |
| 76 | + return map; |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments