Class AbstractAdapterCredentialsFilter<Credentials,Connection>
- Type Parameters:
Credentials- Type for credentials for your tool. (e.g. UsernamePasswordCredentials)Connection- Type for connection object to your tool
- All Implemented Interfaces:
Filter
Overview
Purpose: Provide a JEE Servlet filter base implementation for accepting both HTTP basic and OAuth provider authentication, connecting your tool using the credentials, and managing the connections.With this credentitals filter:
- Your Webapp can accepts HTTP Basic authentication
- Your Webapp can works as an OAuth provider
Once user entered credentials via HTTP Basic auth or OAuth, it
is passed to a callback method getCredentialsFromRequest(HttpServletRequest)
or getCredentialsForOAuth(String, String) so that your implementation
can build a Credentials object from the given data.
And then, next callback method login(Object, HttpServletRequest) is invoked for
authenticate the credentials and building connection to your back-end tool.
Concrete types of the credentials and the connection can be specified as type
parameters of this class.
While processing a request, the credentials and the connection are available
as attributes of the request. Your subsequent process such as HttpServlet.service(ServletRequest, ServletResponse)
can extract and use them for accessing your tool. You can use getConnector(HttpServletRequest)
and getCredentials(HttpServletRequest) to retrieve them from the request.
Usage
You have to subclass this class and give implementations for the following methods:
login(Object, HttpServletRequest)getCredentialsFromRequest(HttpServletRequest)getCredentialsForOAuth(String, String)isAdminSession(String, Object, HttpServletRequest)createConsumerStore()logout(Object, HttpSession)(optional)
<filter>
<display-name>[YOUR FILTER CLASS NAME (MyFilter)]</display-name>
<filter-name>[YOUR FILTER CLASS NAME (MyFilter)]</filter-name>
<filter-class>[FULLY QUALIFIED YOUR FILTER CLASS NAME (com.example.MyFilter)]</filter-class>
</filter>
<filter-mapping>
<filter-name>[YOUR FILTER CLASS NAME (MyFilter)]</filter-name>
<url-pattern>/services/*</url-pattern>
</filter-mapping>
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractAdapterCredentialsFilter(String displayName, String realm) Constructor -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ConsumerStoreInvoked from this class to createConsumerStorefor OAuth keys.voiddestroy()protected voiddoChainDoFilterWithConnector(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Connection connector) The default implementation is:voiddoFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) Check for OAuth or BasicAuth credentials and challenge if not found.static <T> TgetConnector(HttpServletRequest request) get Connector assigned to this request The connector should be placed in the session by the CredentialsFilter servlet filterstatic <T> TgetCredentials(HttpServletRequest request) Get Credentials for this sessionprotected abstract CredentialsgetCredentialsForOAuth(String id, String password) Create a Credentials object from given user id and password.protected abstract CredentialsExtract credentials from the request and return it.protected Stringprotected Stringprotected StringGets the official servlet URL in case this can differ from that in the individual requests.protected booleanhandleUnauthorizedRequest(HttpServletRequest request, HttpServletResponse response) The default implementation is to thrown an UnauthorizedException, which in turn causes sendUnauthorizedResponse() to be called.voidinit(FilterConfig filterConfig) protected abstract booleanisAdminSession(String id, Connection session, HttpServletRequest request) Tell if this is an admin session.protected booleanisProtectedResource(HttpServletRequest request) Check if the resource is protectedprotected abstract Connectionlogin(Credentials crdentials, HttpServletRequest request) Create connection to your tool using the given credentials, and returns the connection.protected voidlogout(Connection loginSession, HttpSession session) Logoutstatic <T> voidremoveConnector(HttpServletRequest request) remove Connector from this sessionstatic <T> voidremoveCredentials(HttpServletRequest request) remove Credentials from this sessionstatic <T> voidremoveToken(HttpServletRequest request) remove the mapping from the oauth token to the Connector saved in the current request.static <T> voidsetConnector(HttpServletRequest request, T connector) set Connector for this sessionstatic <T> voidsetCredentials(HttpServletRequest request, T credentials) set Credentials for this session
-
Field Details
-
CONNECTOR_ATTRIBUTE
- See Also:
-
CREDENTIALS_ATTRIBUTE
- See Also:
-
ADMIN_SESSION_ATTRIBUTE
- See Also:
-
TOKEN_TO_CONNECTION_CACHE_ATTRIBUTE
- See Also:
-
JAZZ_INVALID_EXPIRED_TOKEN_OAUTH_PROBLEM
- See Also:
-
OAUTH_EMPTY_TOKEN_KEY
-
-
Constructor Details
-
AbstractAdapterCredentialsFilter
Constructor- Parameters:
displayName- application name displayed on the login promptrealm- realm for this adapter
-
-
Method Details
-
getCredentialsFromRequest
protected abstract Credentials getCredentialsFromRequest(HttpServletRequest request) throws UnauthorizedException Extract credentials from the request and return it.- Parameters:
request-HttpServletRequest- Returns:
- credentials
- Throws:
UnauthorizedException- iff no login credentials associated to the request.
-
getCredentialsForOAuth
Create a Credentials object from given user id and password.For OAuth two-legged request, the
idis set toOAUTH_EMPTY_TOKEN_KEYobject. Implementor can compare the value using==to identify the request. In the request the consumer key is set to thepassword. So you might find a functional user associated to the consumer key with the value.- Parameters:
id- user id orOAUTH_EMPTY_TOKEN_KEYpassword- password or OAuth consumer key- Returns:
- credentials
-
login
protected abstract Connection login(Credentials crdentials, HttpServletRequest request) throws UnauthorizedException, ServletException Create connection to your tool using the given credentials, and returns the connection.- Parameters:
crdentials- credentials for loginrequest-HttpServletRequest- Returns:
- connection that represents the successful login session
- Throws:
UnauthorizedException- credentials is invalidServletException- other exceptional situation
-
logout
Logout- Parameters:
loginSession-session-
-
isAdminSession
protected abstract boolean isAdminSession(String id, Connection session, HttpServletRequest request) Tell if this is an admin session. For admin session, Lyo provides user-interface to accept provisional authentication key.- Parameters:
id-session-request-- Returns:
-
createConsumerStore
Invoked from this class to createConsumerStorefor OAuth keys. Typical implementation can be:return new FileSystemConsumerStore("YourOAuthStore.xml");- Returns:
- Throws:
Exception
-
getServletUri
Gets the official servlet URL in case this can differ from that in the individual requests.- See Also:
-
isProtectedResource
Check if the resource is protected- Returns:
- true - the resource is protected, otherwise false
-
setConnector
set Connector for this session- Parameters:
request-connector-
-
setCredentials
set Credentials for this session- Parameters:
request-credentials-
-
getConnector
get Connector assigned to this request The connector should be placed in the session by the CredentialsFilter servlet filter- Parameters:
request-- Returns:
- connector
-
getCredentials
Get Credentials for this session- Parameters:
request-- Returns:
- credentials
-
removeConnector
remove Connector from this session- Parameters:
request-
-
removeCredentials
remove Credentials from this session- Parameters:
request-
-
removeToken
remove the mapping from the oauth token to the Connector saved in the current request. This is typically needed when the application deems that the oauth token is no longer valid. The application should also remove the Connector from the session.- Parameters:
request-
-
getOAuthRealm
-
getDisplayName
-
destroy
public void destroy() -
doFilter
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException Check for OAuth or BasicAuth credentials and challenge if not found. Store the Connector in the HttpSession for retrieval in the REST services.- Specified by:
doFilterin interfaceFilter- Throws:
IOExceptionServletException
-
doChainDoFilterWithConnector
protected void doChainDoFilterWithConnector(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Connection connector) throws IOException, ServletException The default implementation is:request.setAttribute(CONNECTOR_ATTRIBUTE, connector); chain.doFilter(request, response);
Subclass may invoke thechain.doFilter()directly instead of invoking super method.- Parameters:
request-HttpServletRequestresponse-HttpServletResponsechain-FilterChainconnector-AbstractAdapterCredentialsFilterto be used for processing rest of the chain (i.e. REST request)- Throws:
IOExceptionServletException
-
handleUnauthorizedRequest
protected boolean handleUnauthorizedRequest(HttpServletRequest request, HttpServletResponse response) throws UnauthorizedException The default implementation is to thrown an UnauthorizedException, which in turn causes sendUnauthorizedResponse() to be called. This means chain.doFilter() is not called, and no filters in the chain are called.- Parameters:
request-response-- Returns:
- true if the filter is to interrupt the chain of filters. that is, the current doFilter() method should simply return, without calling chain.doFilter().
- Throws:
UnauthorizedException
-
init
- Specified by:
initin interfaceFilter- Throws:
ServletException
-