com.github.scribejava
scribejava-apis
6.9.0
com.github.scribejava
scribejava-core
6.9.0
import com.github.scribejava.apis.LinkedInApi20;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.model.OAuth2AccessToken;
import com.github.scribejava.core.oauth.OAuth20Service;
import com.github.scribejava.core.model.OAuthRequest;
import com.github.scribejava.core.model.Response;
import com.github.scribejava.core.model.Verb;
public class OAuthLinkedinFilter implements org.exoplatform.web.filter.Filter{
public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
HttpSession session = req.getSession(true);
LinkedinAuthHelper helper = new LinkedinAuthHelper();
String code = request.getParameter("code");
String stateRes = request.getParameter("state");
String stateReq = (String)session.getAttribute("state");
if(code == null || stateReq == null || stateRes == null || "".equals(code) || "".equals(stateReq) || "".equals(stateRes) || !stateRes.equals(stateReq)){
session.setAttribute("state", helper.getStateToken());
String loginUrl = helper.getLoginUrl();
res.sendRedirect(loginUrl);
return;
}else if(code != null && stateRes != null && stateReq != null && stateRes.equals(stateReq)){
session.removeAttribute("state");
String username = helper.getUsernameFromTokenCode(code);
if(username != null){
SecureRandom random = new SecureRandom();
String password = new BigInteger(130, random).toString(32);
session.setAttribute("oauth_username",username);
session.setAttribute("oauth_password",password);
res.sendRedirect("/portal/login?username="+username+"&password="+password);
return;
}
}
}
org.exoplatform.web.filter.ExtensibleFilter
Sample Filter Definition Plugin
addFilterDefinitions
org.exoplatform.web.filter.FilterDefinitionPlugin
Sample Filter Definition
package org.exoplatform.community.service.sso;
public class ORGLoginModule extends AbstractLoginModule {
public boolean login() throws LoginException {
try {
HttpServletRequest request = getCurrentHttpServletRequest();
ExoContainer container = getContainer();
HttpSession session = request.getSession(true);
String username = null;
String password = null;
if(session != null) {
if(session.getAttribute("oauth_username") != null) {
username = (String) session.getAttribute("oauth_username");
session.removeAttribute("oauth_username");
}
if(session.getAttribute("oauth_password") != null) {
password = (String) session.getAttribute("oauth_password");
session.removeAttribute("oauth_password");
}
}
if(username != null){
establishSecurityContext(container, username);
if (log.isTraceEnabled()) {
log.trace("Successfully established security context for user " + username);
}
return true;
}
return false;
}catch (Exception e) {
if (log.isTraceEnabled()) {
log.trace("Exception in login module", e);
}
return false;
}
protected void establishSecurityContext(ExoContainer container, String username) throws Exception {
Authenticator authenticator = (Authenticator) container.getComponentInstanceOfType(Authenticator.class);
if (authenticator == null) {
throw new LoginException("No Authenticator component found, check your configuration");
}
Identity identity = authenticator.createIdentity(username);
sharedState.put("exo.security.identity", identity);
sharedState.put("javax.security.auth.login.name", username);
UsernameCredential usernameCredential = new UsernameCredential(username);
subject.getPublicCredentials().add(usernameCredential);
}
}
gatein-domain {
org.exoplatform.community.service.sso.ORGLoginModule required;
org.gatein.sso.integration.SSODelegateLoginModule required
enabled="#{gatein.sso.login.module.enabled}"
delegateClassName="#{gatein.sso.login.module.class}"
portalContainerName=portal
realmName=gatein-domain
password-stacking=useFirstPass;
org.exoplatform.services.security.j2ee.TomcatLoginModule required
portalContainerName=portal
realmName=gatein-domain;
};
( Your e-mail address will not be published)