From c60488c51b2335a3848290e4a8e50b325b18ff5d Mon Sep 17 00:00:00 2001 From: Robert Jaros Date: Fri, 25 May 2018 19:22:39 +0200 Subject: Some fixes to pac4j security components. --- .../kotlin/pl/treksoft/kvision/remote/Security.kt | 42 +++++++++++++--------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'src/main/kotlin/pl') diff --git a/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt b/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt index 6373b27a..4e89f558 100644 --- a/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt +++ b/src/main/kotlin/pl/treksoft/kvision/remote/Security.kt @@ -47,11 +47,15 @@ class LoginService { * Login with Pac4j FormClient. * @param credentials username and password credentials */ - fun login(credentials: Credentials): Deferred = - loginAgent.remoteCall("callback?client_name=FormClient", obj { - this.username = credentials.username - this.password = credentials.password - }, HttpMethod.POST, "application/x-www-form-urlencoded").then { _: dynamic -> true }.asDeferred() + fun login(credentials: Credentials?): Deferred = + if (credentials?.username != null) { + loginAgent.remoteCall("callback?client_name=FormClient", obj { + this.username = credentials.username + this.password = credentials.password + }, HttpMethod.POST, "application/x-www-form-urlencoded").then { _: dynamic -> true }.asDeferred() + } else { + throw SecurityException("Credentials cannot be empty") + } } /** @@ -74,19 +78,23 @@ abstract class SecurityMgr { afterLogin() } } - } catch (e: SecurityException) { - afterError() - isLoggedIn = false - while (!isLoggedIn) { - try { - login().await() - isLoggedIn = true - afterLogin() - } catch (e: SecurityException) { - console.log(e) + } catch (e: Exception) { + if (e is SecurityException || !isLoggedIn) { + afterError() + isLoggedIn = false + while (!isLoggedIn) { + try { + login() + isLoggedIn = true + afterLogin() + } catch (e: Throwable) { + console.log(e) + } } + block() + } else { + throw e } - block() } } @@ -95,7 +103,7 @@ abstract class SecurityMgr { * @return true if login is successful * @throws SecurityException if login is not successful */ - abstract suspend fun login(): Deferred + abstract suspend fun login(): Boolean /** * Method called after successful login. -- cgit