Search in sources :

Example 1 with GroupAgentImpl

use of i5.las2peer.security.GroupAgentImpl in project Distributed-Noracle-Backend by Distributed-Noracle.

the class NoracleSpaceService method createSpace.

@Override
public Space createSpace(String name) throws ServiceInvocationException {
    Agent mainAgent = Context.get().getMainAgent();
    if (mainAgent instanceof AnonymousAgent) {
        throw new ServiceNotAuthorizedException("You have to be logged in to create a space");
    }
    String spaceId = buildSpaceId();
    String spaceSecret = generateSpaceSecret();
    SpaceInviteAgent spaceInviteAgent;
    try {
        spaceInviteAgent = new SpaceInviteAgent(spaceSecret);
        spaceInviteAgent.unlock(spaceSecret);
        Context.get().storeAgent(spaceInviteAgent);
    } catch (AgentOperationFailedException | CryptoException e) {
        throw new InternalServiceException("Could not create space invite agent", e);
    } catch (AgentAccessDeniedException | AgentAlreadyExistsException | AgentLockedException e) {
        throw new InternalServiceException("Could not store space invite agent", e);
    }
    try {
        Envelope envInviteMapping = Context.get().createEnvelope(getInviteMappingIdentifier(spaceId));
        envInviteMapping.setPublic();
        envInviteMapping.setContent(spaceInviteAgent.getIdentifier());
        Context.get().storeEnvelope(envInviteMapping);
    } catch (EnvelopeOperationFailedException | EnvelopeAccessDeniedException e) {
        throw new InternalServiceException("Could not store space invite mapping", e);
    }
    GroupAgentImpl spaceMemberGroupAgent;
    try {
        spaceMemberGroupAgent = GroupAgentImpl.createGroupAgent(new Agent[] { spaceInviteAgent, mainAgent });
        spaceMemberGroupAgent.unlock(mainAgent);
        Context.get().storeAgent(spaceMemberGroupAgent);
    } catch (AgentOperationFailedException | CryptoException | SerializationException e) {
        throw new InternalServiceException("Could not create space member group agent", e);
    } catch (AgentAccessDeniedException | AgentAlreadyExistsException | AgentLockedException e) {
        throw new InternalServiceException("Could not store space member group agent", e);
    }
    try {
        Envelope envGroupMapping = Context.get().createEnvelope(getMemberMappingIdentifier(spaceId));
        envGroupMapping.setPublic();
        envGroupMapping.setContent(spaceMemberGroupAgent.getIdentifier());
        Context.get().storeEnvelope(envGroupMapping);
    } catch (EnvelopeOperationFailedException | EnvelopeAccessDeniedException e) {
        throw new InternalServiceException("Could not store space group member mapping", e);
    }
    Envelope env;
    try {
        env = Context.get().createEnvelope(getSpaceEnvelopeIdentifier(spaceId), mainAgent);
    } catch (EnvelopeOperationFailedException | EnvelopeAccessDeniedException e) {
        throw new InternalServiceException("Could not create envelope for space", e);
    }
    env.addReader(spaceMemberGroupAgent);
    Space space = new Space(spaceId, spaceSecret, name, mainAgent.getIdentifier(), spaceMemberGroupAgent.getIdentifier());
    env.setContent(space);
    try {
        Context.get().storeEnvelope(env, mainAgent);
    } catch (EnvelopeAccessDeniedException | EnvelopeOperationFailedException e) {
        throw new InternalServiceException("Could not store space envelope", e);
    }
    return space;
}
Also used : Space(i5.las2peer.services.noracleService.model.Space) SpaceInviteAgent(i5.las2peer.services.noracleService.model.SpaceInviteAgent) SerializationException(i5.las2peer.serialization.SerializationException) EnvelopeOperationFailedException(i5.las2peer.api.persistency.EnvelopeOperationFailedException) SpaceInviteAgent(i5.las2peer.services.noracleService.model.SpaceInviteAgent) EnvelopeAccessDeniedException(i5.las2peer.api.persistency.EnvelopeAccessDeniedException) Envelope(i5.las2peer.api.persistency.Envelope) GroupAgentImpl(i5.las2peer.security.GroupAgentImpl) CryptoException(i5.las2peer.tools.CryptoException)

Aggregations

Envelope (i5.las2peer.api.persistency.Envelope)1 EnvelopeAccessDeniedException (i5.las2peer.api.persistency.EnvelopeAccessDeniedException)1 EnvelopeOperationFailedException (i5.las2peer.api.persistency.EnvelopeOperationFailedException)1 GroupAgentImpl (i5.las2peer.security.GroupAgentImpl)1 SerializationException (i5.las2peer.serialization.SerializationException)1 Space (i5.las2peer.services.noracleService.model.Space)1 SpaceInviteAgent (i5.las2peer.services.noracleService.model.SpaceInviteAgent)1 CryptoException (i5.las2peer.tools.CryptoException)1