Skip to content

Instantly share code, notes, and snippets.

@mifrazmurthaja
Last active September 1, 2021 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mifrazmurthaja/bbc9512ac73295967453af3ff7846ab4 to your computer and use it in GitHub Desktop.
Save mifrazmurthaja/bbc9512ac73295967453af3ff7846ab4 to your computer and use it in GitHub Desktop.
5.10-retry.jsp
<%--
~ Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
--%>
<%@ page import="org.owasp.encoder.Encode" %>
<%@ page import="java.io.File" %>
<%@ page import="org.wso2.carbon.idp.mgt.IdentityProviderManager" %>
<%@ page import="org.wso2.carbon.utils.multitenancy.MultitenantUtils" %>
<%@ page import="org.wso2.carbon.identity.application.common.model.IdentityProvider" %>
<%@ page import="org.wso2.carbon.identity.application.common.model.IdentityProviderProperty" %>
<%@ page import="org.wso2.carbon.context.PrivilegedCarbonContext" %>
<%@ page import="org.wso2.carbon.identity.core.util.IdentityTenantUtil" %>
<%@ page import="org.wso2.carbon.user.core.service.RealmService" %>
<%@ page import="org.wso2.carbon.user.core.UserStoreManager" %>
<%@ page import="org.wso2.carbon.user.api.UserRealm" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="includes/localize.jsp" %>
<%
String stat = request.getParameter("status");
String statusMessage = request.getParameter("statusMsg");
if (stat == null || statusMessage == null) {
stat = AuthenticationEndpointUtil.i18n(resourceBundle, "authentication.error");
statusMessage = AuthenticationEndpointUtil.i18n(resourceBundle,
"something.went.wrong.during.authentication");
} else {
stat = AuthenticationEndpointUtil.customi18n(resourceBundle, stat);
statusMessage = AuthenticationEndpointUtil.customi18n(resourceBundle, statusMessage);
}
session.invalidate();
%>
<%
String failedUsername = request.getParameter("failedUsername");
String tenantAwareUsername = MultitenantUtils.getTenantAwareUsername(failedUsername);
String errorCode = request.getParameter("errorCode");
String maxfailedAttempts = "";
String unlockTime = "";
String MAX_TIMESTAMP = "8640000000000000";
if (errorCode != null && errorCode.equals("17003") && failedUsername != null) {
int usersTenantId = IdentityTenantUtil.getTenantIdOfUser(failedUsername);
RealmService realmService =
(RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService
(RealmService.class, null);
UserRealm userRealm = realmService.getTenantUserRealm(usersTenantId);
String claimUri = "http://wso2.org/claims/identity/unlockTime";
UserStoreManager currentUserStoreManager = (UserStoreManager) userRealm.getUserStoreManager();
// Iterate through all the userstore managers
while (currentUserStoreManager != null) {
if(currentUserStoreManager.isExistingUser(tenantAwareUsername)) {
break;
}
currentUserStoreManager = currentUserStoreManager.getSecondaryUserStoreManager();
}
Map<String, String> claimValues = currentUserStoreManager
.getUserClaimValues(tenantAwareUsername, new String[]{claimUri}, null);
unlockTime = claimValues.get(claimUri);
if(StringUtils.isNotEmpty(unlockTime))
{
long unlockTimeLong = Long.parseLong(unlockTime);
Date unlockDate = new Date((long) unlockTimeLong);
unlockTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(unlockDate);
} else {
unlockTime = MAX_TIMESTAMP;
}
}
String callback = request.getHeader("referer");
if (callback != null) {
callback = callback.replace("authFailure=true&", "");
}
try {
IdentityProviderManager identityProviderManager = IdentityProviderManager.getInstance();
IdentityProvider residentIdP =
identityProviderManager.getResidentIdP(MultitenantUtils.getTenantDomain(failedUsername));
for (IdentityProviderProperty identityProviderProperty : residentIdP.getIdpProperties()) {
if (identityProviderProperty.getName().equals("account.lock.handler.On.Failure.Max.Attempts")) {
maxfailedAttempts = identityProviderProperty.getValue();
}
}
} catch (Exception e) {
errorCode = e.getMessage();
}
%>
<!doctype html>
<html>
<head>
<!-- header -->
<%
File headerFile = new File(getServletContext().getRealPath("extensions/header.jsp"));
if (headerFile.exists()) {
%>
<jsp:include page="extensions/header.jsp"/>
<% } else { %>
<jsp:include page="includes/header.jsp"/>
<% } %>
</head>
<body class="login-portal layout authentication-portal-layout">
<main class="center-segment">
<div class="ui container medium center aligned middle aligned">
<!-- product-title -->
<%
File productTitleFile = new File(getServletContext().getRealPath("extensions/product-title.jsp"));
if (productTitleFile.exists()) {
%>
<jsp:include page="extensions/product-title.jsp"/>
<% } else { %>
<jsp:include page="includes/product-title.jsp"/>
<% } %>
<div class="ui segment">
<% if (errorCode != null && errorCode.equals("17003")) {%>
<span class="mainIcons mainIcon4"></span>
<h3 class="title">Sorry, your account is locked</h3>
<p class="infoText">
You have entered your password wrong more than
<%=maxfailedAttempts%> times. You will have to wait for
<span id="time"></span> to continue.
</p>
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 form-group">
<div class="form-actions">
<button disabled class="ui primary large button" type="button" id="btn-login"
onclick="location.href='<%=callback%>';"><%=AuthenticationEndpointUtil.i18n(resourceBundle, "login")%>
</button>
</div>
</div>
<%
} else {
%>
<div class="segment-form">
<div class="ui visible negative message">
<div class="header"><%=Encode.forHtmlContent(stat)%></div>
<p><%=Encode.forHtmlContent(statusMessage)%></p>
</div>
</div>
<%
}
%>
</div>
</div>
</main>
<!-- product-footer -->
<%
File productFooterFile = new File(getServletContext().getRealPath("extensions/product-footer.jsp"));
if (productFooterFile.exists()) {
%>
<jsp:include page="extensions/product-footer.jsp"/>
<% } else { %>
<jsp:include page="includes/product-footer.jsp"/>
<% } %>
<!-- footer -->
<%
File footerFile = new File(getServletContext().getRealPath("extensions/footer.jsp"));
if (footerFile.exists()) {
%>
<jsp:include page="extensions/footer.jsp"/>
<% } else { %>
<jsp:include page="includes/footer.jsp"/>
<% } %>
<script>
function startTimer(duration, display) {
var timer = duration,
minutes, seconds;
setInterval(function() {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
var mUnit = " minutes";
var sUnit = " seconds";
if (minutes == "01" || minutes == "00")
mUnit = " minute";
if (seconds == "01" || seconds == "00")
sUnit = " second";
display.text(minutes + mUnit + " and " + seconds + sUnit);
if (--timer < 0) {
display.text("0 minute and 0 second");
$('#btn-login').prop("disabled", false);
$('#btn-login').click();
}
}, 1000);
}
var unlockTime = new Date('<%=unlockTime%>').getTime();
var currentTime = new Date().getTime();
var Difference_In_Time = unlockTime - currentTime;
startTimer(Difference_In_Time / 1000, $('#time'));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment