Skip to content

Instantly share code, notes, and snippets.

@jamesfalkner
Created January 23, 2012 16:50
Show Gist options
  • Save jamesfalkner/1664208 to your computer and use it in GitHub Desktop.
Save jamesfalkner/1664208 to your computer and use it in GitHub Desktop.
Custom
package pt.agap2.custom_login;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import pt.agap2.util.EncryptConstants;
import pt.agap2.util.TripleDesImpl;
import com.liferay.portal.model.User;
import com.liferay.portal.security.auth.AutoLogin;
import com.liferay.portal.security.auth.AutoLoginException;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
public class Agap2CustomLogin implements AutoLogin {
Log _log = LogFactory.getLog(Agap2CustomLogin.class);
private final long TICKS_AT_EPOCH = 621355968000000000L;
private final long TICKS_PER_MILLISECOND = 10000L;
private final long DELTA = 30000L;
public String[] login(HttpServletRequest request,
HttpServletResponse response) throws AutoLoginException {
String[] credentials = null;
try {
long companyId = PortalUtil.getCompanyId(request);
String authEncStr = request.getParameter("auth");
Long currentTimeInMilliseconds = System.currentTimeMillis();
String authDEncStr = TripleDesImpl.decrypt(authEncStr, EncryptConstants.CRYPT_KEY);
String userName = null;
if ( authDEncStr != null && Pattern.compile(Pattern.quote(EncryptConstants.SHARED_EXTERNAL_SITES_GUID), Pattern.CASE_INSENSITIVE).matcher(authDEncStr).find()){
authDEncStr = authDEncStr.substring(EncryptConstants.SHARED_EXTERNAL_SITES_GUID.length());
String strTicks = authDEncStr.substring(authDEncStr.indexOf("-")+1);
long ticks = Long.valueOf(strTicks);
long requestTimeInMiliseconds = (ticks - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND;
// a string de autenticação apenas é válida por DELTA segundos depois de gerada.
if( (currentTimeInMilliseconds-requestTimeInMiliseconds) <= DELTA)
userName = authDEncStr.substring(0, authDEncStr.indexOf("-"));
else
throw new AutoLoginException("Time Exceeded");
}
User user = UserLocalServiceUtil.getUserByScreenName(companyId,
userName);
long userId = user.getUserId();
String password = user.getPassword();
credentials = new String[3];
credentials[0] = Long.toString(userId);
credentials[1] = password;
credentials[2] = Boolean.TRUE.toString();
} catch (Exception e) {
_log.warn(e.getMessage());
throw new AutoLoginException(e);
}
return credentials;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment