001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020package org.apache.mina.proxy; 021 022import static org.junit.Assert.assertEquals; 023 024import java.util.HashMap; 025 026import org.apache.mina.core.session.DummySession; 027import org.apache.mina.proxy.handlers.http.basic.HttpBasicAuthLogicHandler; 028import org.apache.mina.proxy.handlers.http.digest.DigestUtilities; 029import org.junit.Test; 030 031/** 032 * HttpAuthTest.java - JUNIT tests of the HTTP Basic & Digest authentication mechanisms. 033 * See <a href="http://www.ietf.org/rfc/rfc2617.txt">RFC 2617</a> . 034 * 035 * @author <a href="http://mina.apache.org">Apache MINA Project</a> 036 * @since MINA 2.0.0-M3 037 */ 038public class HttpAuthTest { 039 040 /** 041 * The charset in use. 042 */ 043 private final static String CHARSET_IN_USE = "ISO-8859-1"; 044 045 /** 046 * Tests Basic authentication mechanism. 047 */ 048 @Test 049 public void testBasicAuthResponse() { 050 String USER = "Aladdin"; 051 String PWD = "open sesame"; 052 053 assertEquals("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", HttpBasicAuthLogicHandler.createAuthorization(USER, PWD)); 054 } 055 056 /** 057 * Tests Http Digest authentication mechanism. 058 */ 059 @Test 060 public void testDigestAuthResponse() { 061 String USER = "Mufasa"; 062 String PWD = "Circle Of Life"; 063 String METHOD = "GET"; 064 065 HashMap<String, String> map = new HashMap<String, String>(); 066 067 map.put("realm", "testrealm@host.com"); 068 map.put("qop", "auth"); 069 map.put("nc", "00000001"); 070 071 map.put("cnonce", "0a4f113b"); 072 073 map.put("nonce", "dcd98b7102dd2f0e8b11d0f600bfb0c093"); 074 map.put("opaque", "5ccc069c403ebaf9f0171e9517f40e41"); 075 map.put("uri", "/dir/index.html"); 076 map.put("username", USER); 077 078 String response = null; 079 try { 080 response = DigestUtilities.computeResponseValue(new DummySession(), map, METHOD, PWD, CHARSET_IN_USE, null); 081 assertEquals("6629fae49393a05397450978507c4ef1", response); 082 writeResponse(map, response); 083 } catch (Exception e) { 084 //e.printStackTrace(); 085 } 086 } 087 088 /** 089 * Pretty prints the digest response header . 090 * 091 * @param map the map holding the authentication parameters 092 * @param response the built digest response string 093 */ 094 private void writeResponse(HashMap<String, String> map, String response) { 095 map.put("response", response); 096 StringBuilder sb = new StringBuilder("Digest "); 097 boolean addSeparator = false; 098 099 for (String key : map.keySet()) { 100 101 if (addSeparator) { 102 sb.append(",\n\t\t\t "); 103 } else { 104 addSeparator = true; 105 } 106 107 boolean quotedValue = !"qop".equals(key) && !"nc".equals(key); 108 sb.append(key); 109 if (quotedValue) { 110 sb.append("=\"").append(map.get(key)).append('\"'); 111 } else { 112 sb.append('=').append(map.get(key)); 113 } 114 } 115 116 //System.out.println("Proxy-Authorization: " + sb.toString()); 117 } 118}