Skip to content

Commit 1cfb854

Browse files
committed
Add model classes required for search APIs.
1 parent a1ae579 commit 1cfb854

13 files changed

+989
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.identity.authz.spicedb.handler.model;
20+
21+
import org.wso2.carbon.identity.authorization.framework.model.AuthorizationResource;
22+
import org.wso2.carbon.identity.authorization.framework.model.AuthorizationSubject;
23+
import org.wso2.carbon.identity.authorization.framework.model.SearchResourcesResponse;
24+
import org.wso2.carbon.identity.authorization.framework.model.SearchSubjectsResponse;
25+
import org.wso2.carbon.identity.authz.spicedb.handler.util.JsonUtil;
26+
27+
import java.util.ArrayList;
28+
29+
/**
30+
* The {@code LookupObjectsResponseHolder} class holds the results stream returned from a Lookup subject or Lookup
31+
* resource response. The results stream is converted into a {@code String[]} to extract each result.
32+
*/
33+
public class LookupObjectsResponseHolder {
34+
35+
private final String[] results;
36+
private final String resultType;
37+
38+
public LookupObjectsResponseHolder(String response, String resultType) {
39+
40+
this.results = response.split("(?<=})\\s*(?=\\{)");
41+
this.resultType = resultType;
42+
}
43+
44+
public String[] getResults () {
45+
46+
return results;
47+
}
48+
49+
/**
50+
* Converts the response to a {@link SearchResourcesResponse} object from a lookup resources response.
51+
*
52+
* @return The {@link SearchResourcesResponse} object.
53+
*/
54+
public SearchResourcesResponse toSearchResourcesResponse() {
55+
56+
ArrayList<AuthorizationResource> resourceArrayList = new ArrayList<>();
57+
for (String result : this.results) {
58+
LookupResourcesResult lookUpResourcesResult = JsonUtil.jsonToResponseModel(result,
59+
LookupResourcesResult.class);
60+
AuthorizationResource authorizationResource = new AuthorizationResource(this.resultType,
61+
lookUpResourcesResult.getLookupResourcesResult().getResourceId());
62+
authorizationResource.setProperties(lookUpResourcesResult.getLookupResourcesResult()
63+
.getPartialCaveatInfo());
64+
resourceArrayList.add(authorizationResource);
65+
}
66+
return new SearchResourcesResponse(resourceArrayList);
67+
}
68+
69+
/**
70+
* Converts the response to a {@link SearchSubjectsResponse} object from a lookup subjects response.
71+
*
72+
* @return The {@link SearchSubjectsResponse} object.
73+
*/
74+
public SearchSubjectsResponse toSearchSubjectsResponse() {
75+
76+
ArrayList<AuthorizationSubject> listObjectsResults = new ArrayList<>();
77+
for (String result : this.results) {
78+
LookupSubjectsResult lookUpSubjectsResult = JsonUtil.jsonToResponseModel(result,
79+
LookupSubjectsResult.class);
80+
AuthorizationSubject searchObjectsResult = new AuthorizationSubject(this.resultType,
81+
lookUpSubjectsResult.getLookupSubjectsResult().getSubjectId());
82+
searchObjectsResult.setProperties(lookUpSubjectsResult.getLookupSubjectsResult().getPartialCaveatInfo());
83+
listObjectsResults.add(searchObjectsResult);
84+
}
85+
return new SearchSubjectsResponse(listObjectsResults);
86+
}
87+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.identity.authz.spicedb.handler.model;
20+
21+
import com.google.gson.annotations.Expose;
22+
import com.google.gson.annotations.SerializedName;
23+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24+
import org.wso2.carbon.identity.authorization.framework.model.SearchResourcesRequest;
25+
import org.wso2.carbon.identity.authz.spicedb.constants.SpiceDbModelConstants;
26+
27+
import java.util.Map;
28+
29+
/**
30+
* The {@code LookupResourcesRequest} class represents the request body of a lookup resources request sent to SpiceDB.
31+
*/
32+
@SuppressFBWarnings(value = "URF_UNREAD_FIELD",
33+
justification = "All fields are accessed via Gson serialization")
34+
public class LookupResourcesRequest {
35+
36+
@SerializedName(SpiceDbModelConstants.RESOURCE_OBJECT_TYPE)
37+
@Expose
38+
private String resourceType;
39+
@SerializedName(SpiceDbModelConstants.PERMISSION)
40+
@Expose
41+
private String permission;
42+
@SerializedName(SpiceDbModelConstants.SUBJECT)
43+
@Expose
44+
private Subject subject;
45+
@SerializedName(SpiceDbModelConstants.OPTIONAL_LIMIT)
46+
@Expose
47+
private long optionalLimit;
48+
@SerializedName(SpiceDbModelConstants.OPTIONAL_CURSOR)
49+
@Expose
50+
private String optionalCursor;
51+
@SerializedName(SpiceDbModelConstants.CONTEXT)
52+
@Expose
53+
private Map<String, Object> context;
54+
55+
public LookupResourcesRequest(SearchResourcesRequest searchResourcesRequest) {
56+
57+
if (searchResourcesRequest.getResource() == null ||
58+
searchResourcesRequest.getAction() == null ||
59+
searchResourcesRequest.getSubject() == null) {
60+
throw new IllegalArgumentException("Invalid request. Resource with type, action, and subject " +
61+
"must be provided to lookup resources.");
62+
}
63+
if (searchResourcesRequest.getSubject().getSubjectId() == null) {
64+
throw new IllegalArgumentException("Invalid request. Subject id must be provided to lookup resources.");
65+
}
66+
this.resourceType = searchResourcesRequest.getResource().getResourceType();
67+
this.permission = searchResourcesRequest.getAction().getAction();
68+
this.subject = new Subject(searchResourcesRequest.getSubject().getSubjectType(),
69+
searchResourcesRequest.getSubject().getSubjectId());
70+
}
71+
72+
public void setContext(Map<String, Object> context) {
73+
74+
this.context = context;
75+
}
76+
77+
public void setOptionalRelation (String optionalRelation) {
78+
79+
this.subject.setOptionalRelation(optionalRelation);
80+
}
81+
82+
public void setOptionalLimit (Long optionalLimit) {
83+
84+
this.optionalLimit = optionalLimit;
85+
}
86+
87+
public void setOptionalCursor (String optionalCursor) {
88+
89+
this.optionalCursor = optionalCursor;
90+
}
91+
92+
public Map<String, Object> getContext() {
93+
94+
return context;
95+
}
96+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.identity.authz.spicedb.handler.model;
20+
21+
import com.google.gson.annotations.SerializedName;
22+
import org.wso2.carbon.identity.authz.spicedb.constants.SpiceDbModelConstants;
23+
24+
/**
25+
* The {@code LookupResourcesResult} class represents a single element in the results stream returned in a lookup
26+
* resources response.
27+
*/
28+
public class LookupResourcesResult {
29+
30+
@SerializedName(SpiceDbModelConstants.RESULT)
31+
private LookupResourcesResultBody lookupResourcesResultBody;
32+
33+
public LookupResourcesResultBody getLookupResourcesResult() {
34+
35+
return lookupResourcesResultBody;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.identity.authz.spicedb.handler.model;
20+
21+
import com.google.gson.annotations.Expose;
22+
import com.google.gson.annotations.SerializedName;
23+
import org.wso2.carbon.identity.authz.spicedb.constants.SpiceDbModelConstants;
24+
25+
import java.util.Map;
26+
27+
/**
28+
* The {@code LookupResourcesResultBody} class holds all the fields in a single element in a results stream returned
29+
* from a lookup resources request.
30+
*/
31+
public class LookupResourcesResultBody {
32+
33+
@SerializedName(SpiceDbModelConstants.LOOKED_AT)
34+
@Expose
35+
private ZedToken lookedAt;
36+
@SerializedName(SpiceDbModelConstants.RESOURCE_ID)
37+
@Expose
38+
private String resourceId;
39+
@SerializedName(SpiceDbModelConstants.PERMISSION_RESULT)
40+
@Expose
41+
private String permissionship;
42+
@SerializedName(SpiceDbModelConstants.PARTIAL_CAVEAT_INFO)
43+
@Expose
44+
private Map<String, Object> partialCaveatInfo;
45+
@SerializedName(SpiceDbModelConstants.AFTER_RESULT_CURSOR)
46+
@Expose
47+
private String afterResultCursor;
48+
49+
public String lookedAt() {
50+
51+
return lookedAt.getToken();
52+
}
53+
54+
public String getResourceId() {
55+
56+
return resourceId;
57+
}
58+
59+
public String getPermissionship() {
60+
61+
return permissionship;
62+
}
63+
64+
public Map<String, Object> getPartialCaveatInfo() {
65+
66+
return partialCaveatInfo;
67+
}
68+
69+
public String getAfterResultCursor() {
70+
71+
return afterResultCursor;
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
package org.wso2.carbon.identity.authz.spicedb.handler.model;
20+
21+
import com.google.gson.annotations.Expose;
22+
import com.google.gson.annotations.SerializedName;
23+
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
24+
import org.wso2.carbon.identity.authorization.framework.model.SearchSubjectsRequest;
25+
import org.wso2.carbon.identity.authz.spicedb.constants.SpiceDbModelConstants;
26+
27+
import java.util.Map;
28+
29+
/**
30+
* The {@code LookupSubjectsRequest} class represents the request body of a lookup subjects request sent to SpiceDB.
31+
*/
32+
@SuppressFBWarnings(value = "URF_UNREAD_FIELD",
33+
justification = "All fields are accessed via Gson serialization")
34+
public class LookupSubjectsRequest {
35+
36+
@SerializedName(SpiceDbModelConstants.SUBJECT_OBJECT_TYPE)
37+
@Expose
38+
private String subjectType;
39+
@SerializedName(SpiceDbModelConstants.PERMISSION)
40+
@Expose
41+
private String permission;
42+
@SerializedName(SpiceDbModelConstants.RESOURCE)
43+
@Expose
44+
private Resource resource;
45+
@SerializedName(SpiceDbModelConstants.CONTEXT)
46+
@Expose
47+
private Map<String, Object> context;
48+
@SerializedName(SpiceDbModelConstants.OPTIONAL_SUBJECT_RELATION)
49+
@Expose
50+
private String optionalSubjectRelation;
51+
@SerializedName(SpiceDbModelConstants.OPTIONAL_CONCRETE_LIMIT)
52+
@Expose
53+
private long optionalConcreteLimit;
54+
@SerializedName(SpiceDbModelConstants.OPTIONAL_CURSOR)
55+
@Expose
56+
private String optionalCursor;
57+
@SerializedName(SpiceDbModelConstants.WILD_CARD_OPTION)
58+
@Expose
59+
private String wildCardOption;
60+
61+
public LookupSubjectsRequest(SearchSubjectsRequest searchSubjectsRequest) {
62+
63+
if (searchSubjectsRequest.getResource() == null ||
64+
searchSubjectsRequest.getAction() == null ||
65+
searchSubjectsRequest.getSubject() == null) {
66+
throw new IllegalArgumentException("Invalid request. Resource, action, and subject with type " +
67+
"must be provided to lookup subjects.");
68+
}
69+
if (searchSubjectsRequest.getResource().getResourceId() == null) {
70+
throw new IllegalArgumentException("Invalid request. Resource id must be provided to lookup subjects.");
71+
}
72+
this.subjectType = searchSubjectsRequest.getSubject().getSubjectType();
73+
this.permission = searchSubjectsRequest.getAction().getAction();
74+
this.resource = new Resource(searchSubjectsRequest.getResource().getResourceType(),
75+
searchSubjectsRequest.getResource().getResourceId());
76+
}
77+
78+
public void setContext(Map<String, Object> context) {
79+
80+
this.context = context;
81+
}
82+
83+
public void setOptionalSubjectRelation (String optionalSubjectRelation) {
84+
85+
this.optionalSubjectRelation = optionalSubjectRelation;
86+
}
87+
88+
public void setOptionalConcreteLimit (Long optionalConcreteLimit) {
89+
90+
this.optionalConcreteLimit = optionalConcreteLimit;
91+
}
92+
93+
public void setOptionalCursor (String optionalCursor) {
94+
95+
this.optionalCursor = optionalCursor;
96+
}
97+
98+
public void setWildCardOption (String wildCardOption) {
99+
100+
this.wildCardOption = wildCardOption;
101+
}
102+
}

0 commit comments

Comments
 (0)