From 88df75c6dc00dd734fd5860e9c4d8d35a7e69c33 Mon Sep 17 00:00:00 2001 From: anhefti Date: Mon, 2 Sep 2019 13:21:16 +0200 Subject: [PATCH] SEBSERV-34 fixed --- .../seb/sebserver/gui/content/LoginPage.java | 80 +++++++++++++------ .../OAuth2AuthorizationContextHolder.java | 5 +- .../auth/SEBServerAuthorizationContext.java | 2 +- 3 files changed, 59 insertions(+), 28 deletions(-) diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/content/LoginPage.java b/src/main/java/ch/ethz/seb/sebserver/gui/content/LoginPage.java index a8e2a97b..bd60d783 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/content/LoginPage.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/content/LoginPage.java @@ -8,6 +8,7 @@ package ch.ethz.seb.sebserver.gui.content; +import org.apache.commons.lang3.StringUtils; import org.eclipse.rap.rwt.RWT; import org.eclipse.swt.SWT; import org.eclipse.swt.layout.GridData; @@ -83,43 +84,72 @@ public class LoginPage implements TemplateComposer { .getAuthorizationContext(RWT.getUISession().getHttpSession()); button.addListener(SWT.Selection, event -> { - final String username = loginName.getText(); - try { - - final boolean loggedIn = authorizationContext.login( - username, - loginPassword.getText()); - - if (loggedIn) { - // Set users locale on page after successful login - this.i18nSupport.setSessionLocale( - authorizationContext - .getLoggedInUser() - .getOrThrow().language); - - pageContext.forwardToMainPage(); - - } else { - loginError(pageContext, "sebserver.login.failed.message"); - } - } catch (final Exception e) { - log.error("Unexpected error while trying to login with user: {}", username, e); - loginError(pageContext, "Unexpected Error. Please call an Administrator"); - } + login( + pageContext, + loginName.getText(), + loginPassword.getText(), + authorizationContext); }); loginName.addListener(SWT.KeyDown, event -> { if (event.character == '\n' || event.character == '\r') { - loginPassword.setFocus(); + if (StringUtils.isNotBlank(loginPassword.getText())) { + login( + pageContext, + loginName.getText(), + loginPassword.getText(), + authorizationContext); + } else { + loginPassword.setFocus(); + } } }); loginPassword.addListener(SWT.KeyDown, event -> { if (event.character == '\n' || event.character == '\r') { - button.setFocus(); + if (StringUtils.isNotBlank(loginName.getText())) { + login( + pageContext, + loginName.getText(), + loginPassword.getText(), + authorizationContext); + } else { + loginName.setFocus(); + } } }); } + private void login( + final PageContext pageContext, + final String loginName, + final CharSequence loginPassword, + final SEBServerAuthorizationContext authorizationContext) { + + final String username = loginName; + try { + + final boolean loggedIn = authorizationContext.login( + username, + loginPassword); + + if (loggedIn) { + // Set users locale on page after successful login + this.i18nSupport.setSessionLocale( + authorizationContext + .getLoggedInUser() + .getOrThrow().language); + + pageContext.forwardToMainPage(); + + } else { + loginError(pageContext, "sebserver.login.failed.message"); + } + } catch (final Exception e) { + log.error("Unexpected error while trying to login with user: {}", username, e); + loginError(pageContext, "Unexpected Error. Please call an Administrator"); + } + } + private void loginError( final PageContext pageContext, final String message) { diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/auth/OAuth2AuthorizationContextHolder.java b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/auth/OAuth2AuthorizationContextHolder.java index 4d985629..13bc3f81 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/auth/OAuth2AuthorizationContextHolder.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/auth/OAuth2AuthorizationContextHolder.java @@ -48,6 +48,7 @@ import ch.ethz.seb.sebserver.gbl.model.user.UserInfo; import ch.ethz.seb.sebserver.gbl.model.user.UserRole; import ch.ethz.seb.sebserver.gbl.profile.GuiProfile; import ch.ethz.seb.sebserver.gbl.util.Result; +import ch.ethz.seb.sebserver.gbl.util.Utils; @Lazy @Component @@ -183,13 +184,13 @@ public class OAuth2AuthorizationContextHolder implements AuthorizationContextHol } @Override - public boolean login(final String username, final String password) { + public boolean login(final String username, final CharSequence password) { if (!this.valid || this.isLoggedIn()) { return false; } this.resource.setUsername(username); - this.resource.setPassword(password); + this.resource.setPassword(Utils.toString(password)); log.debug("Trying to login for user: {}", username); diff --git a/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/auth/SEBServerAuthorizationContext.java b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/auth/SEBServerAuthorizationContext.java index 55c60abf..a4485014 100644 --- a/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/auth/SEBServerAuthorizationContext.java +++ b/src/main/java/ch/ethz/seb/sebserver/gui/service/remote/webservice/auth/SEBServerAuthorizationContext.java @@ -35,7 +35,7 @@ public interface SEBServerAuthorizationContext { * @param username the username for login * @param password the password for login * @return */ - boolean login(String username, String password); + boolean login(String username, CharSequence password); /** Requests a logout on SEB Server webservice if a user is currently logged in * This uses OAuth 2 and Springs OAuth2RestTemplate to make a revoke token request for the