Skip to content

Commit a846d5b

Browse files
committed
JCL-479: Add acp module
1 parent 76522f7 commit a846d5b

File tree

16 files changed

+860
-0
lines changed

16 files changed

+860
-0
lines changed

acp/pom.xml

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?xml version="1.0"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>com.inrupt.client</groupId>
6+
<artifactId>inrupt-client</artifactId>
7+
<version>2.0.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>inrupt-client-acp</artifactId>
11+
<name>Inrupt Java Client Libraries - Access Control Policies</name>
12+
<description>
13+
Access Control Policy support for the Inrupt Client Libraries.
14+
</description>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>com.inrupt.client</groupId>
19+
<artifactId>inrupt-client-api</artifactId>
20+
<version>${project.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.inrupt.client</groupId>
24+
<artifactId>inrupt-client-vocabulary</artifactId>
25+
<version>${project.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.slf4j</groupId>
29+
<artifactId>slf4j-api</artifactId>
30+
<version>${slf4j.version}</version>
31+
</dependency>
32+
33+
<!-- test dependencies -->
34+
<dependency>
35+
<groupId>org.junit.jupiter</groupId>
36+
<artifactId>junit-jupiter-engine</artifactId>
37+
<version>${junit.version}</version>
38+
<scope>test</scope>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.junit.jupiter</groupId>
42+
<artifactId>junit-jupiter-params</artifactId>
43+
<version>${junit.version}</version>
44+
<scope>test</scope>
45+
</dependency>
46+
<dependency>
47+
<groupId>org.wiremock</groupId>
48+
<artifactId>wiremock</artifactId>
49+
<version>${wiremock.version}</version>
50+
<scope>test</scope>
51+
</dependency>
52+
<dependency>
53+
<groupId>com.inrupt.client</groupId>
54+
<artifactId>inrupt-client-jackson</artifactId>
55+
<version>${project.version}</version>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>com.inrupt.client</groupId>
60+
<artifactId>inrupt-client-guava</artifactId>
61+
<version>${project.version}</version>
62+
<scope>test</scope>
63+
</dependency>
64+
<dependency>
65+
<groupId>com.inrupt.client</groupId>
66+
<artifactId>inrupt-client-core</artifactId>
67+
<version>${project.version}</version>
68+
<scope>test</scope>
69+
</dependency>
70+
<dependency>
71+
<groupId>com.inrupt.client</groupId>
72+
<artifactId>inrupt-client-httpclient</artifactId>
73+
<version>${project.version}</version>
74+
<scope>test</scope>
75+
</dependency>
76+
<dependency>
77+
<groupId>com.inrupt.client</groupId>
78+
<artifactId>inrupt-client-jena</artifactId>
79+
<version>${project.version}</version>
80+
<scope>test</scope>
81+
</dependency>
82+
<dependency>
83+
<groupId>com.inrupt.client</groupId>
84+
<artifactId>inrupt-client-solid</artifactId>
85+
<version>${project.version}</version>
86+
<scope>test</scope>
87+
</dependency>
88+
<dependency>
89+
<groupId>org.slf4j</groupId>
90+
<artifactId>slf4j-simple</artifactId>
91+
<version>${slf4j.version}</version>
92+
<scope>test</scope>
93+
</dependency>
94+
</dependencies>
95+
96+
<build>
97+
<plugins>
98+
<plugin>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-surefire-plugin</artifactId>
101+
</plugin>
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-failsafe-plugin</artifactId>
105+
<configuration>
106+
<systemPropertyVariables />
107+
</configuration>
108+
</plugin>
109+
<plugin>
110+
<groupId>org.jacoco</groupId>
111+
<artifactId>jacoco-maven-plugin</artifactId>
112+
</plugin>
113+
</plugins>
114+
</build>
115+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright Inrupt Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to use,
7+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8+
* Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
package com.inrupt.client.acp;
22+
23+
import static com.inrupt.client.vocabulary.RDF.type;
24+
25+
import com.inrupt.client.spi.RDFFactory;
26+
import com.inrupt.client.vocabulary.ACP;
27+
import com.inrupt.rdf.wrapping.commons.ValueMappings;
28+
import com.inrupt.rdf.wrapping.commons.WrapperIRI;
29+
30+
import java.util.Set;
31+
32+
import org.apache.commons.rdf.api.Graph;
33+
import org.apache.commons.rdf.api.IRI;
34+
import org.apache.commons.rdf.api.RDF;
35+
import org.apache.commons.rdf.api.RDFTerm;
36+
37+
public class AccessControl extends WrapperIRI {
38+
39+
static final RDF rdf = RDFFactory.getInstance();
40+
41+
public AccessControl(final RDFTerm original, final Graph graph) {
42+
super(original, graph);
43+
graph.add((IRI) original, rdf.createIRI(type.toString()), rdf.createIRI(ACP.AccessControl.toString()));
44+
}
45+
46+
public static IRI asResource(final AccessControl accessControl, final Graph graph) {
47+
graph.add(accessControl, rdf.createIRI(type.toString()), rdf.createIRI(ACP.AccessControl.toString()));
48+
accessControl.apply().forEach(policy -> {
49+
graph.add(accessControl, rdf.createIRI(ACP.apply.toString()), policy);
50+
Policy.asResource(policy, graph);
51+
});
52+
return accessControl;
53+
}
54+
55+
public Set<Policy> apply() {
56+
return objects(rdf.createIRI(ACP.apply.toString()),
57+
Policy::asResource, ValueMappings.as(Policy.class));
58+
}
59+
}
60+
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Copyright Inrupt Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to use,
7+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8+
* Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
package com.inrupt.client.acp;
22+
23+
import com.inrupt.client.RDFSource;
24+
import com.inrupt.client.vocabulary.ACP;
25+
import com.inrupt.client.vocabulary.RDF;
26+
import com.inrupt.rdf.wrapping.commons.ValueMappings;
27+
import com.inrupt.rdf.wrapping.commons.WrapperIRI;
28+
29+
import java.net.URI;
30+
import java.util.Set;
31+
import java.util.UUID;
32+
import java.util.function.Consumer;
33+
34+
import org.apache.commons.rdf.api.Dataset;
35+
import org.apache.commons.rdf.api.Graph;
36+
import org.apache.commons.rdf.api.RDFTerm;
37+
38+
/**
39+
* An Access Control Resource type.
40+
*/
41+
public class AccessControlResource extends RDFSource {
42+
43+
private static final URI SOLID_ACCESS_GRANT = URI.create("http://www.w3.org/ns/solid/vc#SolidAccessGrant");
44+
45+
public AccessControlResource(final URI identifier, final Dataset dataset) {
46+
super(identifier, dataset);
47+
dataset.add(null, rdf.createIRI(identifier.toString()), rdf.createIRI(RDF.type.toString()),
48+
rdf.createIRI(ACP.AccessControlResource.toString()));
49+
}
50+
51+
public Set<AccessControl> accessControl() {
52+
return new ACPNode(rdf.createIRI(getIdentifier().toString()), getGraph()).accessControl();
53+
}
54+
55+
public Set<AccessControl> memberAccessControl() {
56+
return new ACPNode(rdf.createIRI(getIdentifier().toString()), getGraph()).memberAccessControl();
57+
}
58+
59+
static class ACPNode extends WrapperIRI {
60+
public ACPNode(final RDFTerm original, final Graph graph) {
61+
super(original, graph);
62+
}
63+
64+
public Set<AccessControl> memberAccessControl() {
65+
return objects(rdf.createIRI(ACP.memberAccessControl.toString()),
66+
AccessControl::asResource, ValueMappings.as(AccessControl.class));
67+
}
68+
69+
public Set<AccessControl> accessControl() {
70+
return objects(rdf.createIRI(ACP.accessControl.toString()),
71+
AccessControl::asResource, ValueMappings.as(AccessControl.class));
72+
}
73+
}
74+
75+
public AccessControl accessControl(final Policy... policies) {
76+
final var baseUri = getIdentifier().getScheme() + ":" + getIdentifier().getSchemeSpecificPart();
77+
final var ac = new AccessControl(rdf.createIRI(baseUri + "#" + UUID.randomUUID()), getGraph());
78+
for (final var policy : policies) {
79+
ac.apply().add(policy);
80+
}
81+
return ac;
82+
}
83+
84+
public Policy authenticatedAgentPolicy(final URI... access) {
85+
return agentPolicy(ACP.AuthenticatedAgent, access);
86+
}
87+
88+
public Policy anyAgentPolicy(final URI... access) {
89+
return agentPolicy(ACP.PublicAgent, access);
90+
}
91+
92+
public Policy anyClientPolicy(final URI... access) {
93+
return clientPolicy(ACP.PublicClient, access);
94+
}
95+
96+
public Policy anyIssuerPolicy(final URI... access) {
97+
return issuerPolicy(ACP.PublicIssuer, access);
98+
}
99+
100+
public Policy accessGrantsPolicy(final URI... access) {
101+
return simplePolicy(matcher -> matcher.vc().add(SOLID_ACCESS_GRANT), access);
102+
}
103+
104+
public Policy agentPolicy(final URI agent, final URI... access) {
105+
return simplePolicy(matcher -> matcher.agent().add(agent), access);
106+
}
107+
108+
public Policy clientPolicy(final URI client, final URI... access) {
109+
return simplePolicy(matcher -> matcher.client().add(client), access);
110+
}
111+
112+
public Policy issuerPolicy(final URI issuer, final URI... access) {
113+
return simplePolicy(matcher -> matcher.issuer().add(issuer), access);
114+
}
115+
116+
Policy simplePolicy(final Consumer<Matcher> handler, final URI... access) {
117+
final var baseUri = getIdentifier().getScheme() + ":" + getIdentifier().getSchemeSpecificPart();
118+
final var matcher = new Matcher(rdf.createIRI(baseUri + "#" + UUID.randomUUID()), getGraph());
119+
handler.accept(matcher);
120+
121+
final var policy = new Policy(rdf.createIRI(baseUri + "#" + UUID.randomUUID()), getGraph());
122+
for (final var item : access) {
123+
policy.allow().add(item);
124+
}
125+
policy.allOf().add(matcher);
126+
return policy;
127+
}
128+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Inrupt Inc.
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal in
6+
* the Software without restriction, including without limitation the rights to use,
7+
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8+
* Software, and to permit persons to whom the Software is furnished to do so,
9+
* subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
package com.inrupt.client.acp;
22+
23+
import static com.inrupt.client.vocabulary.RDF.type;
24+
25+
import com.inrupt.client.spi.RDFFactory;
26+
import com.inrupt.client.vocabulary.ACP;
27+
import com.inrupt.rdf.wrapping.commons.TermMappings;
28+
import com.inrupt.rdf.wrapping.commons.ValueMappings;
29+
import com.inrupt.rdf.wrapping.commons.WrapperIRI;
30+
31+
import java.net.URI;
32+
import java.util.Set;
33+
34+
import org.apache.commons.rdf.api.Graph;
35+
import org.apache.commons.rdf.api.IRI;
36+
import org.apache.commons.rdf.api.RDF;
37+
import org.apache.commons.rdf.api.RDFTerm;
38+
39+
public class Matcher extends WrapperIRI {
40+
41+
static final RDF rdf = RDFFactory.getInstance();
42+
43+
public Matcher(final RDFTerm original, final Graph graph) {
44+
super(original, graph);
45+
graph.add((IRI) original, rdf.createIRI(type.toString()), rdf.createIRI(ACP.Matcher.toString()));
46+
}
47+
48+
static RDFTerm asResource(final Matcher matcher, final Graph graph) {
49+
graph.add(matcher, rdf.createIRI(type.toString()), rdf.createIRI(ACP.Matcher.toString()));
50+
matcher.vc().forEach(vc ->
51+
graph.add(matcher, rdf.createIRI(ACP.vc.toString()), rdf.createIRI(vc.toString())));
52+
matcher.agent().forEach(agent ->
53+
graph.add(matcher, rdf.createIRI(ACP.agent.toString()), rdf.createIRI(agent.toString())));
54+
matcher.client().forEach(client ->
55+
graph.add(matcher, rdf.createIRI(ACP.client.toString()), rdf.createIRI(client.toString())));
56+
matcher.issuer().forEach(issuer ->
57+
graph.add(matcher, rdf.createIRI(ACP.issuer.toString()), rdf.createIRI(issuer.toString())));
58+
return matcher;
59+
}
60+
61+
public Set<URI> vc() {
62+
return objects(rdf.createIRI(ACP.vc.toString()),
63+
TermMappings::asIri, ValueMappings::iriAsUri);
64+
}
65+
66+
public Set<URI> agent() {
67+
return objects(rdf.createIRI(ACP.agent.toString()),
68+
TermMappings::asIri, ValueMappings::iriAsUri);
69+
}
70+
71+
public Set<URI> client() {
72+
return objects(rdf.createIRI(ACP.client.toString()),
73+
TermMappings::asIri, ValueMappings::iriAsUri);
74+
}
75+
76+
public Set<URI> issuer() {
77+
return objects(rdf.createIRI(ACP.issuer.toString()),
78+
TermMappings::asIri, ValueMappings::iriAsUri);
79+
}
80+
}
81+

0 commit comments

Comments
 (0)