You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2020/12/06 09:58:17 UTC
[isis] branch master updated: ISIS-2464: some cleaning up of
authentication code
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push:
new c665e98 ISIS-2464: some cleaning up of authentication code
c665e98 is described below
commit c665e986324dde6aa65a5b4d370c61cd8ffcaca4
Author: Andi Huber <ah...@apache.org>
AuthorDate: Sun Dec 6 10:58:02 2020 +0100
ISIS-2464: some cleaning up of authentication code
---
.../core/runtime/context/RuntimeContextBase.java | 3 --
.../manager/AuthenticationManager.java | 61 ++++++++++++----------
.../restfulobjects/rendering/IResourceContext.java | 7 ++-
3 files changed, 35 insertions(+), 36 deletions(-)
diff --git a/core/runtime/src/main/java/org/apache/isis/core/runtime/context/RuntimeContextBase.java b/core/runtime/src/main/java/org/apache/isis/core/runtime/context/RuntimeContextBase.java
index 43565f8..3098315 100644
--- a/core/runtime/src/main/java/org/apache/isis/core/runtime/context/RuntimeContextBase.java
+++ b/core/runtime/src/main/java/org/apache/isis/core/runtime/context/RuntimeContextBase.java
@@ -34,7 +34,6 @@ import org.apache.isis.core.security.authentication.AuthenticationContext;
import org.apache.isis.core.security.authentication.manager.AuthenticationManager;
import lombok.Getter;
-import lombok.val;
/**
*
@@ -89,9 +88,7 @@ public abstract class RuntimeContextBase implements RuntimeContext {
.currentAuthentication()
.ifPresent(authentication->{
- val authenticationManager = getServiceRegistry().lookupServiceElseFail(AuthenticationManager.class);
authenticationManager.closeSession(authentication);
-
interactionFactory.closeSessionStack();
});
diff --git a/core/security/src/main/java/org/apache/isis/core/security/authentication/manager/AuthenticationManager.java b/core/security/src/main/java/org/apache/isis/core/security/authentication/manager/AuthenticationManager.java
index 42a7869..479bf6a 100644
--- a/core/security/src/main/java/org/apache/isis/core/security/authentication/manager/AuthenticationManager.java
+++ b/core/security/src/main/java/org/apache/isis/core/security/authentication/manager/AuthenticationManager.java
@@ -21,8 +21,8 @@ package org.apache.isis.core.security.authentication.manager;
import java.util.List;
import java.util.Map;
-import java.util.stream.Collectors;
+import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Named;
@@ -33,15 +33,14 @@ import org.springframework.stereotype.Service;
import org.apache.isis.applib.annotation.OrderPrecedence;
import org.apache.isis.applib.util.ToString;
-import org.apache.isis.commons.internal.base._NullSafe;
-import org.apache.isis.commons.internal.collections._Lists;
+import org.apache.isis.commons.collections.Can;
+import org.apache.isis.commons.internal.base._Timing;
import org.apache.isis.commons.internal.collections._Maps;
import org.apache.isis.core.security.authentication.Authentication;
-import org.apache.isis.core.security.authentication.standard.NoAuthenticatorException;
-import org.apache.isis.core.security.authentication.standard.RandomCodeGenerator;
-import org.apache.isis.core.security.authentication.standard.SimpleAuthentication;
import org.apache.isis.core.security.authentication.AuthenticationRequest;
import org.apache.isis.core.security.authentication.standard.Authenticator;
+import org.apache.isis.core.security.authentication.standard.NoAuthenticatorException;
+import org.apache.isis.core.security.authentication.standard.RandomCodeGenerator;
import org.apache.isis.core.security.authentication.standard.Registrar;
import lombok.Getter;
@@ -54,27 +53,24 @@ import lombok.val;
@Qualifier("Default")
public class AuthenticationManager {
+ @Getter private final Can<Authenticator> authenticators;
+
private final Map<String, String> userByValidationCode = _Maps.newHashMap();
-
private final RandomCodeGenerator randomCodeGenerator;
- @Getter
- private final List<Authenticator> authenticators;
- private final List<Registrar> registrars;
+ private final Can<Registrar> registrars;
@Inject
public AuthenticationManager(
- final List<Authenticator> authenticators,
+ final List<Authenticator> injectedAuthenticators,
final RandomCodeGenerator randomCodeGenerator) {
this.randomCodeGenerator = randomCodeGenerator;
- this.authenticators = authenticators;
+ this.authenticators = Can.ofCollection(injectedAuthenticators);
if (authenticators.isEmpty()) {
throw new NoAuthenticatorException("No authenticators specified");
}
-
- registrars = authenticators.stream()
+ this.registrars = authenticators
.filter(Registrar.class::isInstance)
- .map(Registrar.class::cast)
- .collect(_Lists.toUnmodifiable());
+ .map(Registrar.class::cast);
}
// -- SESSION MANAGEMENT (including authenticate)
@@ -85,15 +81,15 @@ public class AuthenticationManager {
return null;
}
- val compatibleAuthenticators = _NullSafe.stream(authenticators)
- .filter(authenticator->authenticator.canAuthenticate(request.getClass()))
- .collect(Collectors.toList());
+ val compatibleAuthenticators = authenticators
+ .filter(authenticator->authenticator.canAuthenticate(request.getClass()));
- if (compatibleAuthenticators.size() == 0) {
- throw new NoAuthenticatorException("No authenticator available for processing " + request.getClass().getName());
+ if (compatibleAuthenticators.isEmpty()) {
+ throw new NoAuthenticatorException(
+ "No authenticator available for processing " + request.getClass().getName());
}
- for (final Authenticator authenticator : compatibleAuthenticators) {
+ for (val authenticator : compatibleAuthenticators) {
val authentication = authenticator.authenticate(request, getUnusedRandomCode());
if (authentication != null) {
userByValidationCode.put(authentication.getValidationCode(), authentication.getUserName());
@@ -105,8 +101,18 @@ public class AuthenticationManager {
}
private String getUnusedRandomCode() {
+
+ val stopWatch = _Timing.now();
+
String code;
do {
+
+ // guard against infinite loop when unique code generation for some reason fails
+ if(stopWatch.getMillis()>3000L) {
+ throw new NoAuthenticatorException(
+ "RandomCodeGenerator failed to produce a unique code within 3s.");
+ }
+
code = randomCodeGenerator.generateRandomCode();
} while (userByValidationCode.containsKey(code));
@@ -114,15 +120,12 @@ public class AuthenticationManager {
}
- public final boolean isSessionValid(final Authentication authentication) {
+ public final boolean isSessionValid(final @Nullable Authentication authentication) {
if(authentication==null) {
return false;
}
- if(authentication instanceof SimpleAuthentication) {
- final SimpleAuthentication simpleSession = (SimpleAuthentication) authentication;
- if(simpleSession.getType() == Authentication.Type.EXTERNAL) {
- return true;
- }
+ if(authentication.getType() == Authentication.Type.EXTERNAL) {
+ return true;
}
final String userName = userByValidationCode.get(authentication.getValidationCode());
return authentication.getUser().isCurrentUser(userName);
@@ -130,7 +133,7 @@ public class AuthenticationManager {
public void closeSession(Authentication authentication) {
- for (Authenticator authenticator : authenticators) {
+ for (val authenticator : authenticators) {
authenticator.logout(authentication);
}
userByValidationCode.remove(authentication.getValidationCode());
diff --git a/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/IResourceContext.java b/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/IResourceContext.java
index fb9a4aa..b25e027 100644
--- a/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/IResourceContext.java
+++ b/viewers/restfulobjects/rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/IResourceContext.java
@@ -79,11 +79,10 @@ public interface IResourceContext {
RepresentationService.Intent getIntent();
AuthenticationContext getAuthenticationContext();
-
SpecificationLoader getSpecificationLoader();
- MetaModelContext getMetaModelContext(); // TODO derive from specLoader
- ServiceRegistry getServiceRegistry(); // TODO derive from specLoader
- IsisConfiguration getConfiguration(); // TODO derive from specLoader
+ MetaModelContext getMetaModelContext();
+ ServiceRegistry getServiceRegistry();
+ IsisConfiguration getConfiguration();
// -- UTILITY