Class AbstractAdapterCredentialsFilter<Credentials,Connection>
- Type Parameters:
Connection
- Type for connection object to your toolCredentials
- Type for credentials for your tool. (e.g. UsernamePasswordCredentials)
- 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
ConstructorsModifierConstructorDescriptionprotected
AbstractAdapterCredentialsFilter
(String displayName, String realm) Constructor -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract ConsumerStore
Invoked from this class to createConsumerStore
for OAuth keys.void
destroy()
protected void
doChainDoFilterWithConnector
(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Connection connector) The default implementation is:void
doFilter
(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) Check for OAuth or BasicAuth credentials and challenge if not found.static <T> T
getConnector
(HttpServletRequest request) get Connector assigned to this request The connector should be placed in the session by the CredentialsFilter servlet filterstatic <T> T
getCredentials
(HttpServletRequest request) Get Credentials for this sessionprotected abstract Credentials
getCredentialsForOAuth
(String id, String password) Create a Credentials object from given user id and password.protected abstract Credentials
Extract credentials from the request and return it.protected String
protected String
protected String
Gets the official servlet URL in case this can differ from that in the individual requests.protected boolean
handleUnauthorizedRequest
(HttpServletRequest request, HttpServletResponse response) The default implementation is to thrown an UnauthorizedException, which in turn causes sendUnauthorizedResponse() to be called.void
init
(FilterConfig filterConfig) protected abstract boolean
isAdminSession
(String id, Connection session, HttpServletRequest request) Tell if this is an admin session.protected boolean
isProtectedResource
(HttpServletRequest request) Check if the resource is protectedprotected abstract Connection
login
(Credentials crdentials, HttpServletRequest request) Create connection to your tool using the given credentials, and returns the connection.protected void
logout
(Connection loginSession, HttpSession session) Logoutstatic <T> void
removeConnector
(HttpServletRequest request) remove Connector from this sessionstatic <T> void
removeCredentials
(HttpServletRequest request) remove Credentials from this sessionstatic <T> void
removeToken
(HttpServletRequest request) remove the mapping from the oauth token to the Connector saved in the current request.static <T> void
setConnector
(HttpServletRequest request, T connector) set Connector for this sessionstatic <T> void
setCredentials
(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
id
is set toOAUTH_EMPTY_TOKEN_KEY
object. 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_KEY
password
- 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 createConsumerStore
for 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. -
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:
doFilter
in interfaceFilter
- Throws:
IOException
ServletException
-
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
-HttpServletRequest
response
-HttpServletResponse
chain
-FilterChain
connector
-AbstractAdapterCredentialsFilter
to be used for processing rest of the chain (i.e. REST request)- Throws:
IOException
ServletException
-
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:
response
-request
-- 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:
init
in interfaceFilter
- Throws:
ServletException
-