/* * Copyright 2006-2008 Sxip Identity Corporation */ package org.openid4java.message.pape; import org.openid4java.message.MessageException; import org.openid4java.message.Parameter; import org.openid4java.message.ParameterList; import org.openid4java.OpenIDException; import java.util.*; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * Implements the extension for OpenID Provider Authentication Policy requests. * * @see PapeMessage Message * @author Marius Scurtescu, Johnny Bufu */ public class PapeRequest extends PapeMessage { private static Log _log = LogFactory.getLog(PapeRequest.class); private static final boolean DEBUG = _log.isDebugEnabled(); protected final static List PAPE_FIELDS = Arrays.asList( new String[] { "preferred_auth_policies", "preferred_auth_level_types", "max_auth_age" }); /** * Constructs a Pape Request with an empty parameter list. */ protected PapeRequest() { set("preferred_auth_policies", ""); if (DEBUG) _log.debug("Created empty Pape request."); } /** * Constructs a Pape Request with an empty parameter list. */ public static PapeRequest createPapeRequest() { return new PapeRequest(); } /** * Constructs a PapeRequest from a parameter list. *
* The parameter list can be extracted from a received message with the
* getExtensionParams method of the Message class, and MUST NOT contain
* the "openid.
* The parameter list can be extracted from a received message with the
* getExtensionParams method of the Message class, and MUST NOT contain
* the "openid.
* Used when constructing a extension from a parameter list.
*
* @throws MessageException if the PapeRequest is not valid.
*/
public void validate() throws MessageException
{
if (! _parameters.hasParameter("preferred_auth_policies"))
{
throw new MessageException(
"preferred_auth_policies is required in a PAPE request.",
OpenIDException.PAPE_ERROR);
}
Iterator it = _parameters.getParameters().iterator();
while (it.hasNext())
{
String paramName = ((Parameter) it.next()).getKey();
if (! PAPE_FIELDS.contains(paramName) && ! paramName.startsWith(PapeMessage.AUTH_LEVEL_NS_PREFIX))
{
throw new MessageException(
"Invalid parameter name in PAPE request: " + paramName,
OpenIDException.PAPE_ERROR);
}
}
}
public void addPreferredCustomAuthLevel(String authLevelTypeUri)
{
String alias = addAuthLevelExtension(authLevelTypeUri);
String preferred = getParameterValue("preferred_auth_level_types");
set("preferred_auth_level_types", preferred == null ? alias : preferred + " " + alias);
}
}