カスタム・メディエーター
OAuth 2.0 メディエーターは、カスタマイズされた後処理を実行するために、OAuth 2.0 メッセージ処理でコールバックとして使用されます。
OAuth20 メディエーターの作成
メディエーターを作成するには、com.ibm.oauth.core.api.oauth20.mediator.OAuth20Mediator というインターフェースを実装する必要があります。1 つ以上の mediate* メソッドを実装してカスタム後処理を実行することができます。
void init(OAuthComponentConfiguration config)
このメソッドは、このオブジェクトのインスタンスの作成時にファクトリーによって呼び出されます。
void mediateAuthorize(AttributeList attributeList)
このメソッドは、基本メッセージ検証および処理の後に、processAuthorization メソッドのコンポーネント・コンシューマーでカスタム後処理を行えるようにするために、コア・コンポーネントによって呼び出されます。
void mediateAuthorizeException(AttributeList attributeList, OAuthException exception)
このメソッドは、プロトコル例外の発生時に、processAuthorization メソッドのコンポーネント・コンシューマーでカスタム後処理を行えるようにするために、コア・コンポーネントによって呼び出されます。
void mediateResource(AttributeList attributeList)
このメソッドは、基本メッセージ検証および処理の後に、processResourceRequest メソッドのコンポーネント・コンシューマーでカスタム後処理を行えるようにするために、コア・コンポーネントによって呼び出されます。
void mediateResourceException(AttributeList attributeList, OAuthException exception)
このメソッドは、プロトコル例外の発生時に、processResourceRequest メソッドのコンポーネント・コンシューマーでカスタム後処理を行えるようにするために、コア・コンポーネントによって呼び出されます。
void mediateToken(AttributeList attributeList)
このメソッドは、基本メッセージ検証および処理の後に、processTokenRequest メソッドのコンポーネント・コンシューマーでカスタム後処理を行えるようにするために、コア・コンポーネントによって呼び出されます。
void mediateTokenException(AttributeList attributeList, OAuthException exception)
このメソッドは、プロトコル例外の発生時に、processTokenRequest メソッドのコンポーネント・コンシューマーでカスタム後処理を行えるようにするために、コア・コンポーネントによって呼び出されます。OAuth プロバイダーに対する OAuth20 メディエーターの使用可能化
カスタマイズしたメディエーターを特定の OAuth20 サービス・プロバイダーに追加するには、そのサービス・プロバイダーの構成ファイル (例えば OauthConfigSample.xml) を更新します。
oauth20.mediator.classnames パラメーターを見つけて、メディエーターのクラス名を追加します。
oauth20.mediator.classnames パラメーターでメディエーターのクラス名を複数指定することもできます。
メディエーターが複数指定されると、それらのメディエーターはこのパラメーターで指定された順に開始されます。
次の例は、プロバイダー構成ファイル内のサンプル・カスタム・メディエーター・エントリーを示しています。
<parameter name="oauth20.mediator.classnames" type="cc" customizable="false">
<value>org.acme.oauth2.sampleMediator</value>
<value>org.acme.oauth2.sampleMediator2</value>
</parameter>
次のコード・サンプルは、リソース所有者パスワード・クレデンシャル・フローで WebSphere® Application Server ユーザー・レジストリーを使用してクレデンシャル検証を実装するものです。
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);
}
}