<%@ page import="org.apache.commons.codec.binary.*,java.util.Properties,java.io.*" %>
<%@ page import="org.opengion.hayabusa.resource.*,org.opengion.hayabusa.common.*" %>

<%
	String authorization = request.getHeader("Authorization");
	if( authorization == null ) {
		response.setStatus( response.SC_UNAUTHORIZED );
		response.setHeader( "WWW-Authenticate",
							"BASIC realm=\"Web App\"\r\n" );
		out.println( "<h1> Error No authorization </h1>" );
		return ;
	}
	else {
		String authName = authorization.substring( 6 ).trim();
		byte[] decodedBytes = Base64.decodeBase64(authName);
		String nameAndPasswd = new String( decodedBytes );
		int index = nameAndPasswd.indexOf( ":" );
		String userID = nameAndPasswd.substring( 0,index );
		String passwd = nameAndPasswd.substring( index+1 );

		UserInfo userInfo = (UserInfo)session.getAttribute( HybsSystem.USERINFO_KEY );
		String userID2 = null;
		if( userInfo != null ) {
			userID2 = userInfo.getUserID();
			if( !userID2.equals( userID ) ) {
	//			out.println( "<h1> Error No Match UserID [" + userID + "]</h1>" );

				return ;
			}
		}
		response.setStatus( response.SC_UNAUTHORIZED );
		response.setHeader( "WWW-Authenticate",
							"BASIC realm=\"Web App\"" );
		out.println( "<h1> Error No authorization </h1>" );
		out.println( "<h1> Authorization   UserID  [" + userID + "]</h1>" );
		out.println( "<h1> Sesion USERINFO userID2 [" + userID2 + "]</h1>" );
	}
%>
