CallControlHelper { public static boolean permitted(final String authorization, final String method, DaoManager daoManager, final Sid organizationSid) { final Map<String, String> map = authHeaderToMap(authorization); final String user = map.get("username"); String authHeaderAlgorithm = map.get("algorithm"); final String realm = map.get("realm"); final String uri = map.get("uri"); final String nonce = map.get("nonce"); final String nc = map.get("nc"); final String cnonce = map.get("cnonce"); final String qop = map.get("qop"); final String response = map.get("response"); final ClientsDao clients = daoManager.getClientsDao(); final Client client = clients.getClient(user, organizationSid); if (client != null && daoManager.getAccountsDao().getAccount(client.getAccountSid()).getStatus() != Account.Status.ACTIVE) { return false; } if (client != null && Client.ENABLED == client.getStatus()) { final String password = client.getPassword(); final String clientPasswordAlgorithm = client.getPasswordAlgorithm(); if (authHeaderAlgorithm == null) { authHeaderAlgorithm = clientPasswordAlgorithm; } final String result = DigestAuthentication.response(authHeaderAlgorithm, user, realm, password,clientPasswordAlgorithm, nonce, nc, cnonce, method, uri, null, qop); if (logger.isDebugEnabled()) { String msg = String.format("Provided response [%s], generated response [%s]", response, result); logger.debug(msg); } return result.equals(response); } else { if (logger.isDebugEnabled()) { String msg = String.format("Authorization check failed, [if(client!=null) evaluates %s] or [if(client.getStatus()==Client.ENABLED) evaluates %s]", client!=null, client != null ? Client.ENABLED == client.getStatus() : "client is null"); logger.debug(msg); } return false; } }  static boolean permitted(final String authorization, final String method, DaoManager daoManager, final Sid organizationSid); static boolean checkAuthentication(SipServletRequest request, DaoManager storage, Sid organizationSid); static String getUserSipId(final SipServletRequest request, boolean useTo);  }
@Test public void testPermitted() { String auth = "Digest username=\"alice\",realm=\"127.0.0.1\",nonce=\"64353235316666342d306434392d343\",uri=\"sip:127.0.0.1:5080\",response=\"31927ed6bc4b0c3796fd2240f8315fc7\",cnonce=\"416ba85980000000\",nc=00000001,qop=auth,algorithm=MD5"; String method = "REGISTER"; Sid organization = Sid.generate(Sid.Type.ORGANIZATION); Sid accountSid = Sid.generate(Sid.Type.ACCOUNT); Account.Builder builder = Account.builder(); builder.setSid(accountSid); builder.setStatus(Account.Status.ACTIVE); Account account = builder.build(); Client.Builder builder2 = Client.builder(); builder2.setSid(Sid.generate(Sid.Type.CLIENT)); builder2.setAccountSid(accountSid); builder2.setPassword("alice", "2ea216cdda59f04ef9ba21fead1bca10", "127.0.0.1", ""); builder2.setPasswordAlgorithm("MD5"); builder2.setStatus(Client.ENABLED); Client client = builder2.build(); DaoManager manager = Mockito.mock(DaoManager.class); ClientsDao clients = Mockito.mock(ClientsDao.class); AccountsDao accounts = Mockito.mock(AccountsDao.class); when(manager.getClientsDao()).thenReturn(clients); when(clients.getClient("alice", organization)).thenReturn(client); when(manager.getAccountsDao()).thenReturn(accounts); when(accounts.getAccount(accountSid)).thenReturn(account); boolean permitted = CallControlHelper.permitted(auth, method, manager, organization); assertTrue(permitted); }
@Test public void testPermittedNoAlgorithm() { String auth = "Digest username=\"alice\",realm=\"127.0.0.1\",nonce=\"64353235316666342d306434392d343\",uri=\"sip:127.0.0.1:5080\",response=\"31927ed6bc4b0c3796fd2240f8315fc7\",cnonce=\"416ba85980000000\",nc=00000001,qop=auth"; String method = "REGISTER"; Sid organization = Sid.generate(Sid.Type.ORGANIZATION); Sid accountSid = Sid.generate(Sid.Type.ACCOUNT); Account.Builder builder = Account.builder(); builder.setSid(accountSid); builder.setStatus(Account.Status.ACTIVE); Account account = builder.build(); Client.Builder builder2 = Client.builder(); builder2.setSid(Sid.generate(Sid.Type.CLIENT)); builder2.setAccountSid(accountSid); builder2.setPassword("alice", "2ea216cdda59f04ef9ba21fead1bca10", "127.0.0.1", ""); builder2.setPasswordAlgorithm("MD5"); builder2.setStatus(Client.ENABLED); Client client = builder2.build(); DaoManager manager = Mockito.mock(DaoManager.class); ClientsDao clients = Mockito.mock(ClientsDao.class); AccountsDao accounts = Mockito.mock(AccountsDao.class); when(manager.getClientsDao()).thenReturn(clients); when(clients.getClient("alice", organization)).thenReturn(client); when(manager.getAccountsDao()).thenReturn(accounts); when(accounts.getAccount(accountSid)).thenReturn(account); boolean permitted = CallControlHelper.permitted(auth, method, manager, organization); assertTrue(permitted); }