Skip to content
This repository has been archived by the owner on Oct 4, 2021. It is now read-only.

Commit

Permalink
implemented a client authentication wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Fast007 committed Jun 22, 2017
1 parent 882fa27 commit e92f93b
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 24 deletions.
6 changes: 6 additions & 0 deletions lib/pom.xml
Expand Up @@ -129,5 +129,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.openbase.bco</groupId>
<artifactId>authentication.lib</artifactId>
<version>1.4-SNAPSHOT</version>
<type>jar</type>
</dependency>
</dependencies>
</project>
Expand Up @@ -22,6 +22,7 @@
* #L%
*/
import com.google.protobuf.GeneratedMessage;
import java.io.StreamCorruptedException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
Expand All @@ -42,6 +43,7 @@
import rst.domotic.service.ServiceTemplateType.ServiceTemplate.ServicePattern;
import rst.domotic.service.ServiceTemplateType.ServiceTemplate.ServiceType;
import rst.domotic.state.ContactStateType;
import rst.domotic.action.ActionFuture;

/**
*
Expand All @@ -53,8 +55,13 @@ public interface Service {
public static final Package SERVICE_STATE_PACKAGE = ContactStateType.class.getPackage();
public static final String SERVICE_LABEL = Service.class.getSimpleName();

public default Future<Void> applyAction(final ActionDescription actionDescription) throws CouldNotPerformException, InterruptedException, StreamCorruptedException {
Future<ActionFuture> applyAction = applyAction(actionDescription, true);
return new VoidFuture(applyAction);
}

@RPCMethod
public Future<Void> applyAction(final ActionDescription actionDescription) throws CouldNotPerformException, InterruptedException;
public Future<ActionFuture> applyAction(final ActionDescription actionDescription, boolean test) throws CouldNotPerformException, InterruptedException, StreamCorruptedException;

default public void addServiceStateObserver(ServiceType serviceType, Observer observer) {
}
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.openbase.jul.exception.CouldNotPerformException;
import org.openbase.jul.exception.NotSupportedException;
import rst.domotic.action.ActionDescriptionType.ActionDescription;
import rst.domotic.authentication.TicketAuthenticatorWrapperType.TicketAuthenticatorWrapper;
import rst.domotic.service.ServiceTemplateType;

/**
Expand Down Expand Up @@ -55,4 +56,9 @@ public static String getUpdateMethodName(final ServiceTemplateType.ServiceTempla
public default Future<Void> applyAction(ActionDescription actionConfig) throws CouldNotPerformException, InterruptedException {
throw new NotSupportedException("actions", ProviderService.class);
}

@Override
public default Future<TicketAuthenticatorWrapper> applyAction(ActionDescription actionConfig, boolean test) throws CouldNotPerformException, InterruptedException {
throw new NotSupportedException("actions", ProviderService.class);
}
}
Expand Up @@ -64,6 +64,7 @@
import rsb.Scope;
import rsb.converter.DefaultConverterRepository;
import rsb.converter.ProtocolBufferConverter;
import rst.domotic.action.ActionFuture;
import rst.domotic.action.ActionDescriptionType.ActionDescription;
import rst.domotic.registry.UnitRegistryDataType.UnitRegistryData;
import rst.domotic.service.ServiceDescriptionType.ServiceDescription;
Expand Down Expand Up @@ -405,7 +406,7 @@ public Method getUpdateMethod(final ServiceTemplate.ServiceType serviceType, Cla
}

@Override
public Future<Void> applyAction(final ActionDescription actionDescription) throws CouldNotPerformException, InterruptedException {
public Future<ActionFuture> applyAction(final ActionDescription actionDescription, boolean test) throws CouldNotPerformException, InterruptedException {
try {
logger.debug("applyAction: " + actionDescription.getLabel());
final Object attribute = serviceJSonProcessor.deserialize(actionDescription.getServiceStateDescription().getServiceAttribute(), actionDescription.getServiceStateDescription().getServiceAttributeType());
Expand Down
Expand Up @@ -22,6 +22,7 @@
* #L%
*/
import com.google.protobuf.GeneratedMessage;
import org.openbase.bco.authentication.lib.SessionManager;
import org.openbase.bco.dal.lib.layer.service.Service;
import org.openbase.jul.exception.InitializationException;
import org.openbase.jul.pattern.ConfigurableRemote;
Expand Down Expand Up @@ -81,4 +82,6 @@ public interface UnitRemote<M extends GeneratedMessage> extends Unit<M>, Service
* @return returns true if the unit is enabled otherwise false.
*/
boolean isEnabled();

public void setSessionManager(SessionManager sessionManager);
}
1 change: 1 addition & 0 deletions nb-configuration.xml
Expand Up @@ -22,5 +22,6 @@ Any value defined here will override the pom.xml file value but is only applicab
<org-netbeans-modules-editor-indent.CodeStyle.usedProfile>project</org-netbeans-modules-editor-indent.CodeStyle.usedProfile>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapOneLineComment>false</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapOneLineComment>
<org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapCommentText>false</org-netbeans-modules-editor-indent.text.x-java.CodeStyle.project.wrapCommentText>
<netbeans.hint.jdkPlatform>JDK_1.8</netbeans.hint.jdkPlatform>
</properties>
</project-shared-configuration>
Expand Up @@ -22,6 +22,8 @@
* #L%
*/
import com.google.protobuf.GeneratedMessage;
import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -56,6 +58,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import rst.domotic.action.ActionDescriptionType.ActionDescription;
import rst.domotic.authentication.TicketAuthenticatorWrapperType.TicketAuthenticatorWrapper;
import rst.domotic.service.ServiceTemplateType.ServiceTemplate.ServiceType;
import rst.domotic.unit.UnitConfigType.UnitConfig;
import rst.domotic.unit.UnitTemplateType.UnitTemplate.UnitType;
Expand Down Expand Up @@ -428,7 +431,7 @@ public ServiceType getServiceType() {
}

@Override
public Future<Void> applyAction(final ActionDescription actionDescription) throws CouldNotPerformException, InterruptedException {
public Future<TicketAuthenticatorWrapper> applyAction(final ActionDescription actionDescription, boolean test) throws CouldNotPerformException, InterruptedException, StreamCorruptedException {
try {
if (!actionDescription.getServiceStateDescription().getServiceType().equals(getServiceType())) {
throw new VerificationFailedException("Service type is not compatible to given action config!");
Expand All @@ -439,10 +442,10 @@ public Future<Void> applyAction(final ActionDescription actionDescription) throw
if (actionDescription.getServiceStateDescription().getUnitType() == UnitType.UNKNOWN
|| actionDescription.getServiceStateDescription().getUnitType() == unitRemote.getType()
|| UnitConfigProcessor.isBaseUnit(unitRemote.getType())) {
actionFutureList.add(unitRemote.applyAction(actionDescription));
actionFutureList.add(unitRemote.applyAction(actionDescription, test));
}
}
return GlobalCachedExecutorService.allOf((Void) null, actionFutureList);
return GlobalCachedExecutorService.allOf((TicketAuthenticatorWrapper) null, actionFutureList);
} catch (CouldNotPerformException ex) {
throw new CouldNotPerformException("Could not apply action!", ex);
}
Expand Down
Expand Up @@ -22,6 +22,8 @@
* #L%
*/
import com.google.protobuf.GeneratedMessage;
import java.io.IOException;
import java.io.StreamCorruptedException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -30,6 +32,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.openbase.bco.authentication.lib.AuthenticationClientHandler;
import org.openbase.bco.authentication.lib.SessionManager;
import org.openbase.bco.dal.lib.layer.service.Service;
import org.openbase.bco.dal.lib.layer.unit.UnitRemote;
import org.openbase.bco.dal.remote.unit.location.LocationRemote;
Expand All @@ -42,6 +45,7 @@
import org.openbase.jul.exception.InvalidStateException;
import org.openbase.jul.exception.NotAvailableException;
import org.openbase.jul.exception.printer.ExceptionPrinter;
import org.openbase.jul.exception.printer.LogLevel;
import org.openbase.jul.extension.protobuf.MessageObservable;
import org.openbase.jul.extension.protobuf.processing.GenericMessageProcessor;
import org.openbase.jul.extension.rsb.com.AbstractConfigurableRemote;
Expand All @@ -60,6 +64,8 @@
import rsb.converter.DefaultConverterRepository;
import rsb.converter.ProtocolBufferConverter;
import rst.communicationpatterns.ResourceAllocationType.ResourceAllocation;
import rst.domotic.action.ActionFuture;
import rst.domotic.action.ActionAuthorityType.ActionAuthority;
import rst.domotic.action.ActionDescriptionType.ActionDescription;
import rst.domotic.action.SnapshotType.Snapshot;
import rst.domotic.authentication.TicketAuthenticatorWrapperType.TicketAuthenticatorWrapper;
Expand All @@ -86,9 +92,12 @@ public abstract class AbstractUnitRemote<M extends GeneratedMessage> extends Abs
DefaultConverterRepository.getDefaultConverterRepository().addConverter(new ProtocolBufferConverter<>(Snapshot.getDefaultInstance()));
}

private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(AbstractUnitRemote.class);

private UnitTemplate template;
private UnitRegistry unitRegistry;
private final Map<ServiceType, MessageObservable> serviceStateObservableMap;
private SessionManager sessionManager;

public AbstractUnitRemote(final Class<M> dataClass) {
super(dataClass, UnitConfig.class);
Expand Down Expand Up @@ -387,10 +396,12 @@ public UnitTemplate getTemplate() throws NotAvailableException {
}

/**
* Method returns the transformation between the root location and this unit.
*
* Method returns the transformation between the root location and this
* unit.
*
* @return a transformation future
* @throws InterruptedException is thrown if the thread was externally interrupted.
* @throws InterruptedException is thrown if the thread was externally
* interrupted.
*/
public Future<Transform> getTransformation() throws InterruptedException {
final Future<LocationRegistryDataType.LocationRegistryData> dataFuture;
Expand Down Expand Up @@ -436,7 +447,8 @@ public ScopeType.Scope getScope() throws NotAvailableException {
* Method returns the parent location config of this unit.
*
* @return a unit config of the parent location.
* @throws NotAvailableException is thrown if the location config is currently not available.
* @throws NotAvailableException is thrown if the location config is
* currently not available.
*/
public UnitConfig getParentLocationConfig() throws NotAvailableException, InterruptedException {
try {
Expand All @@ -449,8 +461,7 @@ public UnitConfig getParentLocationConfig() throws NotAvailableException, Interr

/**
*
* @return
* @throws NotAvailableException
* @return @throws NotAvailableException
* @deprecated please use getParentLocationConfig() instead.
*/
@Deprecated
Expand All @@ -466,10 +477,13 @@ public UnitConfig getLocationConfig() throws NotAvailableException {
/**
* Method returns the parent location remote of this unit.
*
* @param waitForData flag defines if the method should block until the remote is fully synchronized.
* @param waitForData flag defines if the method should block until the
* remote is fully synchronized.
* @return a location remote instance.
* @throws NotAvailableException is thrown if the location remote is currently not available.
* @throws java.lang.InterruptedException is thrown if the current was externally interrupted.
* @throws NotAvailableException is thrown if the location remote is
* currently not available.
* @throws java.lang.InterruptedException is thrown if the current was
* externally interrupted.
*/
public LocationRemote getParentLocationRemote(final boolean waitForData) throws NotAvailableException, InterruptedException {
try {
Expand All @@ -479,6 +493,16 @@ public LocationRemote getParentLocationRemote(final boolean waitForData) throws
}
}

/**
* Sets the session Manager which is used for the authentication of the
* client/user
*
* @param sessionManager an instance of SessionManager
*/
public void setSessionManager(SessionManager sessionManager) {
this.sessionManager = sessionManager;
}

/**
* {@inheritDoc}
*
Expand All @@ -488,12 +512,20 @@ public LocationRemote getParentLocationRemote(final boolean waitForData) throws
* @throws java.lang.InterruptedException {@inheritDoc}
*/
@Override
public Future<Void> applyAction(ActionDescription actionDescription) throws CouldNotPerformException, InterruptedException {
byte[] sessionKey = null; // TODO: actionDescription.getSessionKey();
TicketAuthenticatorWrapper wrapper = null; // TODO: actionDescription.getTicketAuthenticationWrapper();
wrapper = AuthenticationClientHandler.initSSRequest(sessionKey, wrapper);
// TODO: actionDescription.setTicketAuthenticationWrapper(wrapper);
return RPCHelper.callRemoteMethod(actionDescription, this, Void.class);
public Future<ActionFuture> applyAction(ActionDescription actionDescription, boolean test) throws CouldNotPerformException, InterruptedException, StreamCorruptedException {
try {
this.sessionManager.setTicketAuthenticatorWrapper(AuthenticationClientHandler.initSSRequest(this.sessionManager.getSessionKey(), this.sessionManager.getTicketAuthenticatorWrapper()));
} catch (StreamCorruptedException ex) {
ExceptionPrinter.printHistory(ex, LOGGER, LogLevel.WARN);
throw new StreamCorruptedException(ex.getMessage());
} catch (IOException ex) {
ExceptionPrinter.printHistory(ex, LOGGER, LogLevel.ERROR);
throw new CouldNotPerformException("Internal server error. Please try again.");
}
ActionAuthority.Builder builder = actionDescription.getActionAuthority().toBuilder();
builder.setTicketAuthenticatorWrapper(this.sessionManager.getTicketAuthenticatorWrapper());
return RPCHelper.callRemoteMethod(builder.build(), this, TicketAuthenticatorWrapper.class);
// TODO: Authenticate() wird in AbstractUnitController aufgerufen
}

/**
Expand All @@ -509,7 +541,8 @@ public Future<Snapshot> recordSnapshot() throws CouldNotPerformException, Interr
}

/**
* Use if serviceType cannot be resolved from serviceAttribute. E.g. AlarmState.
* Use if serviceType cannot be resolved from serviceAttribute. E.g.
* AlarmState.
*
* @param actionDescription
* @param serviceAttribute
Expand Down
17 changes: 14 additions & 3 deletions remote/src/main/java/org/openbase/bco/dal/remote/unit/Units.java
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.openbase.bco.authentication.lib.SessionManager;
import org.openbase.bco.dal.remote.unit.agent.AgentRemote;
import org.openbase.bco.dal.remote.unit.app.AppRemote;
import org.openbase.bco.dal.remote.unit.connection.ConnectionRemote;
Expand Down Expand Up @@ -191,7 +192,9 @@ public class Units {
private static RemoteControllerRegistry<String, org.openbase.bco.dal.lib.layer.unit.UnitRemote<? extends GeneratedMessage>> unitRemoteRegistry;

public static final SyncObject UNIT_POOL_LOCK = new SyncObject("UnitPoolLock");


private static SessionManager sessionManager;

static {
try {
unitRemoteRegistry = new RemoteControllerRegistry<>();
Expand Down Expand Up @@ -222,7 +225,8 @@ public String toString() {
} catch (CouldNotPerformException ex) {
ExceptionPrinter.printHistory(new FatalImplementationErrorException(Units.class, new org.openbase.jul.exception.InstantiationException(Units.class, ex)), LOGGER);
}


sessionManager = new SessionManager();
}

/**
Expand Down Expand Up @@ -274,7 +278,7 @@ public synchronized static UnitRegistry getUnitRegistry() throws InterruptedExce
Registries.getUnitRegistry().waitForData();
return Registries.getUnitRegistry();
}

/**
* Returns the unit remote of the unit identified by the given unit id.
*
Expand Down Expand Up @@ -313,6 +317,13 @@ private static UnitRemote<?> getUnitRemote(final String unitId) throws NotAvaila
unitRemote.activate();
unitRemote.lock(unitRemoteRegistry);
}

// TODO: if user is not logged in, login with public rights
if (!sessionManager.isLoggedIn()) {
throw new NotAvailableException("User is not logged in. Must be logged in before trying to access server.");
}
// appends a sessionManager to unitRemote
unitRemote.setSessionManager(sessionManager);
return unitRemote;
} catch (CouldNotPerformException | NullPointerException ex) {
throw new NotAvailableException("UnitRemote[" + unitId + "]", ex);
Expand Down

0 comments on commit e92f93b

Please sign in to comment.