Mediador personalizado
Se utiliza un mediador OAuth 2.0 como devolución de llamada durante el proceso de mensajes OAuth 2.0 para realizar el proceso posterior personalizado.
Escribir un mediador OAuth 2.0
void init(OAuthComponentConfiguration config)
Una fábrica invoca este método cuando se crea una instancia de este objeto. void mediateAuthorize(AttributeList attributeList)
El componente principal invoca este método después de la validación de mensajes básica y el proceso para permitir cualquier proceso posterior personalizado por parte del consumidor del componente en el método processAuthorization. void mediateAuthorizeException(AttributeList attributeList, OAuthException exception)
El componente principal invoca este método cuando se produce la excepción de protocolo para permitir cualquier proceso posterior personalizado por parte del consumidor del componente en el método processAuthorization. void mediateResource(AttributeList attributeList)
El componente principal invoca este método después de la validación de mensajes básica y el proceso para permitir cualquier proceso posterior personalizado por parte del consumidor del componente en el método processResourceRequest. void mediateResourceException(AttributeList attributeList, OAuthException exception)
El componente principal invoca este método cuando se produce la excepción de protocolo para permitir cualquier proceso posterior personalizado por parte del consumidor del componente en el método processResourceRequest. void mediateToken(AttributeList attributeList)
El componente principal invoca este método después de la validación de mensajes básica y el proceso para permitir cualquier proceso posterior personalizado por parte del consumidor del componente en el método processTokenRequest. void mediateTokenException(AttributeList attributeList, OAuthException exception)
El componente principal invoca este método cuando se produce la excepción de protocolo para permitir cualquier proceso posterior personalizado por parte del consumidor del componente en el método processTokenRequest. Habilitar el mmediador OAuth 2.0 para un proveedor de OAuth
Para añadir un mediador personalizado a un proveedor de servicios OAuth 2.0 específico, actualice la definición de proveedor en el archivo server.xml. Añadir el atributo mediatorClassname del elemento oauthProvider y especifique el nombre de clase del mediador. También puede especificar varios nombres de clase para los mediadores utilizando el subelemento mediatorClassname del elemento oauthProvider. Si se especifican varios mediadores, los mediadores se inician en el orden en el que se especifican. También debe definir un elemento library que contenga la clase de mediador y hacer referencia a la biblioteca con el atributo libraryRef.
<oauthProvider id="OAuthConfigSample" libraryRef="myLib"
mediatorClassname="com.ibm.ws.security.oauth20.mediator.ResourceOwnerValidationMediator" ...>
...
</oauthProvider>
<library id="myLib">
<fileset dir="C:\mydir" includes="myLib.jar" />
</library>
package com.ibm.ws.security.oauth20.mediator;
import com.ibm.oauth.core.api.attributes.AttributeList;
import com.ibm.oauth.core.api.config.OAuthComponentConfiguration;
import com.ibm.oauth.core.api.error.OAuthException;
import com.ibm.oauth.core.api.error.oauth20.OAuth20MediatorException;
import com.ibm.oauth.core.api.oauth20.mediator.OAuth20Mediator;
import com.ibm.oauth.core.internal.oauth20.OAuth20Constants;
import com.ibm.websphere.security.CustomRegistryException;
import com.ibm.websphere.security.PasswordCheckFailedException;
import com.ibm.websphere.security.UserRegistry;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class ResourceOwnerValidationMedidator implements OAuth20Mediator {
private static final String CLASS = ResourceOwnerValidationMedidator.class.getName();
private static final Logger LOG = Logger.getLogger(CLASS);
private UserRegistry reg = null;
public void init(OAuthComponentConfiguration config) {
try {
InitialContext ctx = new InitialContext();
reg = (UserRegistry) ctx.lookup("UserRegistry");
} catch(NamingException ne) {
LOG.log(Level.SEVERE, "Cannot lookup UserRegistry", ne);
}
}
public void mediateAuthorize(AttributeList attributeList)
throws OAuth20MediatorException {
// nothing to do here
}
public void mediateAuthorizeException(AttributeList attributeList,
OAuthException exception)
throws OAuth20MediatorException {
// nothing to do here
}
public void mediateResource(AttributeList attributeList)
throws OAuth20MediatorException {
// nothing to do here
}
public void mediateResourceException(AttributeList attributeList,
OAuthException exception)
throws OAuth20MediatorException {
// nothing to do here
}
public void mediateToken(AttributeList attributeList)
throws OAuth20MediatorException {
final String methodName = "mediateToken";
LOG.entering(CLASS, methodName, attributeList);
if("password".equals(attributeList.getAttributeValueByName("grant_type"))) {
String username = attributeList.getAttributeValueByName("username");
String password = attributeList.getAttributeValueByName("password");
try {
reg.checkPassword(username, password);
} catch (PasswordCheckFailedException e) {
throw new OAuth20MediatorException("User doesn't exist or the
password doesn't match.", e);
} catch (CustomRegistryException e) {
throw new OAuth20MediatorException("Cannot validate resource owner.", e);
} catch (RemoteException e) {
throw new OAuth20MediatorException("Cannot validate resource owner.", e);
}
}
LOG.exiting(CLASS, methodName);
}
public void mediateTokenException(AttributeList attributeList,
OAuthException exception)
throws OAuth20MediatorException {
final String methodName = "mediateTokenException";
LOG.entering(CLASS, methodName, new Object[] {attributeList, exception});
if("password".equals(attributeList.getAttributeValueByName("grant_type"))) {
// clear sensitive data
attributeList.setAttribute("access_token",
OAuth20Constants.ATTRTYPE_RESPONSE_ATTRIBUTE,
new String[0]);
attributeList.setAttribute("refresh_token",
OAuth20Constants.ATTRTYPE_RESPONSE_ATTRIBUTE,
new String[0]);
}
LOG.exiting(CLASS, methodName);
}
}