Search in sources :

Example 1 with AuthEnabledLinkedDataSource

use of won.auth.linkeddata.AuthEnabledLinkedDataSource in project webofneeds by researchstudio-sat.

the class AclTests method testImplicitOwnerToken.

@Test(timeout = 60 * 1000)
public void testImplicitOwnerToken() throws Exception {
    runTest(ctx -> {
        EventBus bus = ctx.getEventBus();
        final URI wonNodeUri = ctx.getNodeURISource().getNodeURI();
        final URI atomUri = ctx.getWonNodeInformationService().generateAtomURI(wonNodeUri);
        final String atomUriString = atomUri.toString();
        final AtomContent atomContent = AtomContent.builder(atomUri).addTitle("Unit test for implicit owner token").addSocket(Socket.builder(atomUriString + "#holderSocket").setSocketDefinition(WXHOLD.HolderSocket.asURI()).build()).addSocket(Socket.builder(atomUriString + "#buddySocket").setSocketDefinition(WXBUDDY.BuddySocket.asURI()).build()).addType(URI.create(WON.Atom.getURI())).build();
        // create an acl allowing only the atom itself to read everything
        Authorization auth = Authorization.builder().addGrant(ase -> ase.addOperationsSimpleOperationExpression(OP_READ)).addGranteesAtomExpression(ae -> ae.addAtomsURI(URI.create("https://example.com/nobody"))).build();
        WonMessage createMessage = WonMessageBuilder.createAtom().atom(atomUri).content().graph(RdfOutput.toGraph(atomContent)).content().aclGraph(// add the acl graph
        won.auth.model.RdfOutput.toGraph(auth)).build();
        createMessage = ctx.getWonMessageSender().prepareMessage(createMessage);
        ctx.getBotContextWrapper().rememberAtomUri(atomUri);
        final String action = "Create Atom action";
        EventListener successCallback = event -> {
            URI connContainerUri = uriService.createConnectionContainerURIForAtom(atomUri);
            URI createMessageUri = ((SuccessResponseEvent) event).getOriginalMessageURI();
            boolean passed = true;
            URI tokenQuery = uriService.createTokenRequestURIForAtomURIWithScopes(atomUri, WONAUTH.OwnerToken.toString());
            passed = testTokenRequest(ctx, bus, null, false, atomUri, null, tokenQuery, "test1.1 - obtain token");
            Set<String> tokens = ((AuthEnabledLinkedDataSource) ctx.getLinkedDataSource()).getAuthTokens(tokenQuery, atomUri);
            String token = tokens.stream().findFirst().get();
            passed = passed && testTokenRequest(ctx, bus, null, true, null, token, tokenQuery, "test1.2 - request token using only token");
            passed = passed && testLinkedDataRequest(ctx, bus, null, false, null, null, atomUri, "test1.3 - request atom data without any auth (continues session with token)");
            passed = passed && testLinkedDataRequest(ctx, bus, null, true, atomUri, null, atomUri, "test1.4 - request atom data with webid");
            passed = passed && testLinkedDataRequest(ctx, bus, null, true, null, token, atomUri, "test1.5 - request atom data with token");
            if (passed) {
                passTest(bus);
            }
        };
        EventListener failureCallback = makeFailureCallbackToFailTest(bot, ctx, bus, action);
        EventBotActionUtils.makeAndSubscribeResponseListener(createMessage, successCallback, failureCallback, ctx);
        ctx.getWonMessageSender().sendMessage(createMessage);
    });
}
Also used : EventBotAction(won.bot.framework.eventbot.action.EventBotAction) SuccessResponseEvent(won.bot.framework.eventbot.event.impl.wonmessage.SuccessResponseEvent) LoggerFactory(org.slf4j.LoggerFactory) won.auth.model(won.auth.model) EventBus(won.bot.framework.eventbot.bus.EventBus) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConnectFromOtherAtomEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherAtomEvent) BehaviourBarrier(won.bot.framework.eventbot.behaviour.BehaviourBarrier) ContentUtils(won.utils.content.ContentUtils) AtomicReference(java.util.concurrent.atomic.AtomicReference) WonMessage(won.protocol.message.WonMessage) HashSet(java.util.HashSet) WonMessageBuilder(won.protocol.message.builder.WonMessageBuilder) MessageFromOtherAtomEvent(won.bot.framework.eventbot.event.impl.wonmessage.MessageFromOtherAtomEvent) ActionOnceAfterNEventsListener(won.bot.framework.eventbot.listener.impl.ActionOnceAfterNEventsListener) Shacl2JavaInstanceFactory(won.shacl2java.Shacl2JavaInstanceFactory) Individuals(won.auth.model.Individuals) URI(java.net.URI) Dataset(org.apache.jena.query.Dataset) Socket(won.utils.content.model.Socket) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) BotBehaviour(won.bot.framework.eventbot.behaviour.BotBehaviour) MethodHandles(java.lang.invoke.MethodHandles) RdfOutput(won.utils.content.model.RdfOutput) Set(java.util.Set) Test(org.junit.Test) BaseEventBotAction(won.bot.framework.eventbot.action.BaseEventBotAction) EventBotActionUtils(won.bot.framework.eventbot.action.EventBotActionUtils) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) AtomContent(won.utils.content.model.AtomContent) AuthEnabledLinkedDataSource(won.auth.linkeddata.AuthEnabledLinkedDataSource) CountDownLatch(java.util.concurrent.CountDownLatch) Event(won.bot.framework.eventbot.event.Event) won.protocol.vocabulary(won.protocol.vocabulary) Optional(java.util.Optional) EventFilter(won.bot.framework.eventbot.filter.EventFilter) EventListener(won.bot.framework.eventbot.listener.EventListener) Assert(org.junit.Assert) GraphFactory(org.apache.jena.sparql.graph.GraphFactory) HashSet(java.util.HashSet) Set(java.util.Set) WonMessage(won.protocol.message.WonMessage) EventBus(won.bot.framework.eventbot.bus.EventBus) EventListener(won.bot.framework.eventbot.listener.EventListener) AtomContent(won.utils.content.model.AtomContent) URI(java.net.URI) Test(org.junit.Test)

Aggregations

MethodHandles (java.lang.invoke.MethodHandles)1 URI (java.net.URI)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Optional (java.util.Optional)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Dataset (org.apache.jena.query.Dataset)1 GraphFactory (org.apache.jena.sparql.graph.GraphFactory)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 AuthEnabledLinkedDataSource (won.auth.linkeddata.AuthEnabledLinkedDataSource)1 won.auth.model (won.auth.model)1 Individuals (won.auth.model.Individuals)1 BaseEventBotAction (won.bot.framework.eventbot.action.BaseEventBotAction)1 EventBotAction (won.bot.framework.eventbot.action.EventBotAction)1