Skip to content

Commit

Permalink
Merge pull request #145 from cuadradek/tokenPasswordReset
Browse files Browse the repository at this point in the history
Password reset using token parameter
  • Loading branch information
zlamalp authored Mar 30, 2021
2 parents bcec54c + f639aa6 commit ff55f8a
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,23 @@ public static Request checkPasswordResetRequestIsValid(String i, String m, JsonE

}

/**
* Checks if the password reset request link is valid. The request is valid, if it
* was created, never used and hasn't expired yet.
*
* @param token Token param
* @param events Events done on callback
*
* @return Request unique request
*/
public static Request checkPasswordResetRequestIsValid(String token, JsonEvents events) {

JsonClient client = new JsonClient(events);
if (token != null && !token.isEmpty()) client.put("token", token);
return client.call(USERS_MANAGER + "checkPasswordResetRequestIsValid");

}

/**
* Reset users password in selected namespace by non-authz call using secret token
*
Expand All @@ -320,6 +337,26 @@ public static Request resetNonAuthzPassword(String i, String m, String newPass,

}

/**
* Reset users password in selected namespace by non-authz call using secret token
*
* @param token Token param
* @param newPass New password set for the user
* @param lang Language to get notification in
* @param events Events done on callback
*
* @return Request unique request
*/
public static Request resetNonAuthzPassword(String token, String newPass, String lang, JsonEvents events){

JsonClient client = new JsonClient(events);
if (token != null && !token.isEmpty()) client.put("token", token);
if (newPass!= null && !newPass.isEmpty()) client.put("password", newPass);
if (lang!= null && !lang.isEmpty()) client.put("lang", lang);
return client.call(USERS_MANAGER + "changeNonAuthzPassword");

}

/**
* Returns all groups of specific user in given vo where the given user is admin.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ protected void onBind() {
} else if (PerunSession.getInstance().getUser() == null &&
PerunSession.getInstance().getRpcServer().equals("non") &&
!Window.Location.getParameterMap().keySet().contains("m") &&
!Window.Location.getParameterMap().keySet().contains("i")) {
!Window.Location.getParameterMap().keySet().contains("i") &&
!Window.Location.getParameterMap().keySet().contains("token")) {
placeManager.revealPlace(new PlaceRequest.Builder().nameToken(PlaceTokens.NOT_USER).build());
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ public void onKeyUp(KeyUpEvent event) {

setUpNonAuthz(i, m);

} else if (Window.Location.getParameterMap().containsKey("token") &&
PerunSession.getInstance().getRpcServer().equals("non")) {

final String token = Window.Location.getParameter("token");

drawNonAuthz();

setUpNonAuthz(token);

} else {

draw();
Expand Down Expand Up @@ -295,6 +304,100 @@ public void onLoadingStart() {
});
}

private void setUpNonAuthz(String token) {
UsersManager.checkPasswordResetRequestIsValid(token, new JsonEvents() {
@Override
public void onFinished(JavaScriptObject result) {
form.setVisible(true);

setUpNonAuthzSubmitButton(token);
}

@Override
public void onError(PerunException error) {
submit.setProcessing(false);
form.setVisible(false);
alert.setVisible(true);
if ("PasswordResetLinkExpiredException".equals(error.getName())) {
alert.setType(AlertType.WARNING);
} else {
alert.setType(AlertType.DANGER);
}
error.setNamespace(namespace);
alert.setHTML(ErrorTranslator.getTranslatedMessage(error));
if (Window.Location.getParameterMap().containsKey("target_url")) {
continueButton.setVisible(true);
}
}

@Override
public void onLoadingStart() {
form.setVisible(false);
}
});
}

private void setUpNonAuthzSubmitButton(String token) {
submit.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {

if (validate()) {

UsersManager.resetNonAuthzPassword(token, passwordTextBox.getValue(), PerunConfiguration.getCurrentLocaleName(), new JsonEvents() {

final JsonEvents events = this;

@Override
public void onFinished(JavaScriptObject result) {
submit.setProcessing(false);
form.setVisible(false);
alert.setType(AlertType.SUCCESS);
alert.setText((isAccountActivation) ? translation.activateSuccess() : translation.resetSuccess());
alert.setVisible(true);
if (Window.Location.getParameterMap().containsKey("target_url")) {
alert.getToolbar().setVisible(true);
continueButton.setType(ButtonType.SUCCESS);
continueButton.setVisible(true);
}

}

@Override
public void onError(PerunException error) {
submit.setProcessing(false);
form.setVisible(false);
if ("PasswordResetLinkExpiredException".equals(error.getName())) {
alert.setType(AlertType.WARNING);
} else {
alert.setType(AlertType.DANGER);
alert.setRetryHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
drawNonAuthz();
}
});
}
alert.setVisible(true);
alert.setHTML(ErrorTranslator.getTranslatedMessage(error));
alert.setReportInfo(error);
if (Window.Location.getParameterMap().containsKey("target_url")) {
continueButton.setVisible(true);
}
}

@Override
public void onLoadingStart() {
submit.setProcessing(true);
}
});

}

}
});
}

public void draw() {

alert.setVisible(false);
Expand Down

0 comments on commit ff55f8a

Please sign in to comment.