-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add APIs like role, enforcer, token and permission (#44)
* port role permission apis * add custom http client * fix method complexity * fix method complexity * port Enforcer Token Apis * fix casdoorToken
- Loading branch information
Showing
16 changed files
with
1,190 additions
and
17 deletions.
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
src/main/java/org/casbin/casdoor/entity/CasdoorToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
// Copyright 2023 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
package org.casbin.casdoor.entity; | ||
|
||
// Token has the same definition as https://github.com/casdoor/casdoor/blob/master/object/token.go#L45 | ||
|
||
public class CasdoorToken { | ||
|
||
private String owner; | ||
private String name; | ||
private String createdTime; | ||
private String application; | ||
private String accessToken; | ||
private String refreshToken; | ||
private String code; | ||
private String scope; | ||
private String organization; | ||
private String user; | ||
private int expiresIn; | ||
private String tokenType; | ||
private String codeChallenge; | ||
private boolean codeIsUsed; | ||
private long codeExpireIn; | ||
|
||
public CasdoorToken() { | ||
|
||
} | ||
|
||
public CasdoorToken(String owner, String name, String createdTime, String application, String accessToken, String refreshToken, String code, String scope, String organization, String user, int expiresIn, String tokenType, String codeChallenge, boolean codeIsUsed, long codeExpireIn) { | ||
this.owner = owner; | ||
this.name = name; | ||
this.createdTime = createdTime; | ||
this.application = application; | ||
this.accessToken = accessToken; | ||
this.refreshToken = refreshToken; | ||
this.code = code; | ||
this.scope = scope; | ||
this.organization = organization; | ||
this.user = user; | ||
this.expiresIn = expiresIn; | ||
this.tokenType = tokenType; | ||
this.codeChallenge = codeChallenge; | ||
this.codeIsUsed = codeIsUsed; | ||
this.codeExpireIn = codeExpireIn; | ||
} | ||
|
||
public String getOwner() { | ||
return owner; | ||
} | ||
|
||
public void setOwner(String owner) { | ||
this.owner = owner; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getCreatedTime() { | ||
return createdTime; | ||
} | ||
|
||
public void setCreatedTime(String createdTime) { | ||
this.createdTime = createdTime; | ||
} | ||
|
||
public String getApplication() { | ||
return application; | ||
} | ||
|
||
public void setApplication(String application) { | ||
this.application = application; | ||
} | ||
|
||
public String getAccessToken() { | ||
return accessToken; | ||
} | ||
|
||
public void setAccessToken(String accessToken) { | ||
this.accessToken = accessToken; | ||
} | ||
|
||
public String getRefreshToken() { | ||
return refreshToken; | ||
} | ||
|
||
public void setRefreshToken(String refreshToken) { | ||
this.refreshToken = refreshToken; | ||
} | ||
|
||
public String getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(String code) { | ||
this.code = code; | ||
} | ||
|
||
public String getScope() { | ||
return scope; | ||
} | ||
|
||
public void setScope(String scope) { | ||
this.scope = scope; | ||
} | ||
|
||
public String getOrganization() { | ||
return organization; | ||
} | ||
|
||
public void setOrganization(String organization) { | ||
this.organization = organization; | ||
} | ||
|
||
public String getUser() { | ||
return user; | ||
} | ||
|
||
public void setUser(String user) { | ||
this.user = user; | ||
} | ||
|
||
public int getExpiresIn() { | ||
return expiresIn; | ||
} | ||
|
||
public void setExpiresIn(int expiresIn) { | ||
this.expiresIn = expiresIn; | ||
} | ||
|
||
public String getTokenType() { | ||
return tokenType; | ||
} | ||
|
||
public void setTokenType(String tokenType) { | ||
this.tokenType = tokenType; | ||
} | ||
|
||
public String getCodeChallenge() { | ||
return codeChallenge; | ||
} | ||
|
||
public void setCodeChallenge(String codeChallenge) { | ||
this.codeChallenge = codeChallenge; | ||
} | ||
|
||
public boolean isCodeIsUsed() { | ||
return codeIsUsed; | ||
} | ||
|
||
public void setCodeIsUsed(boolean codeIsUsed) { | ||
this.codeIsUsed = codeIsUsed; | ||
} | ||
|
||
public long getCodeExpireIn() { | ||
return codeExpireIn; | ||
} | ||
|
||
public void setCodeExpireIn(long codeExpireIn) { | ||
this.codeExpireIn = codeExpireIn; | ||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
src/main/java/org/casbin/casdoor/service/CasdoorEnforcerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
// Copyright 2023 The casbin Authors. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing CasdoorPermissions and | ||
// limitations under the License. | ||
|
||
package org.casbin.casdoor.service; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.casbin.casdoor.config.CasdoorConfig; | ||
import org.casbin.casdoor.exception.CasdoorException; | ||
import org.casbin.casdoor.util.http.CasdoorResponse; | ||
import org.casbin.casdoor.util.http.HttpClient; | ||
import org.casbin.casdoor.util.MapToUrlUtils; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class CasdoorEnforcerService { | ||
private final CasdoorConfig casdoorConfig; | ||
private final ObjectMapper objectMapper; | ||
|
||
public CasdoorEnforcerService(CasdoorConfig casdoorConfig) { | ||
this.casdoorConfig = casdoorConfig; | ||
this.objectMapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); | ||
} | ||
public boolean enforce(String permissionId, String modelId, String resourceId, Object[] casbinRequest) throws IOException { | ||
byte[] postBytes = objectMapper.writeValueAsBytes(casbinRequest); | ||
if (postBytes == null) { | ||
throw new CasdoorException("Failed to get bytes from URL"); | ||
} | ||
CasdoorResponse response = doEnforce("enforce", permissionId, modelId, resourceId, postBytes); | ||
|
||
if (!(response.getStatus().equals("ok"))) { | ||
throw new CasdoorException("Failed to unmarshal JSON"); | ||
} | ||
|
||
List<?> results = (List<?>) response.getData(); | ||
for (Object result : results) { | ||
if (!(result instanceof Boolean)) { | ||
throw new CasdoorException("Invalid data"); | ||
} | ||
|
||
if ((Boolean) result) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
public boolean[][] batchEnforce(String permissionId, String modelId, String resourceId, Object[][] casbinRequests) throws IOException { | ||
byte[] postBytes = objectMapper.writeValueAsBytes(casbinRequests); | ||
if (postBytes == null) { | ||
throw new CasdoorException("Failed to get bytes from URL"); | ||
} | ||
CasdoorResponse response = doEnforce("batch-enforce", permissionId, modelId, resourceId, postBytes); | ||
|
||
if (!(response.getStatus().equals("ok"))) { | ||
throw new CasdoorException("Failed to unmarshal JSON"); | ||
} | ||
List<?> responseData = (List<?>) response.getData(); | ||
boolean[][] allows = new boolean[responseData.size()][]; | ||
|
||
for (int i = 0; i < responseData.size(); i++) { | ||
Object data = responseData.get(i); | ||
if (!(data instanceof List<?>)) { | ||
throw new CasdoorException("Invalid data"); | ||
} | ||
|
||
List<?> dataSublist = (List<?>) data; | ||
allows[i] = new boolean[dataSublist.size()]; | ||
|
||
for (int j = 0; j < dataSublist.size(); j++) { | ||
Object elem = dataSublist.get(j); | ||
if (!(elem instanceof Boolean)) { | ||
throw new CasdoorException("Invalid data"); | ||
} | ||
allows[i][j] = (Boolean) elem; | ||
} | ||
} | ||
|
||
return allows; | ||
} | ||
public CasdoorResponse doEnforce(String action, String permissionId, String modelId, String resourceId, byte[] postBytes) throws IOException { | ||
Map<String, String> queryMap = new HashMap<>(); | ||
queryMap.put("action", action); | ||
queryMap.put("permissionId", casdoorConfig.getOrganizationName()+"/"+permissionId); | ||
queryMap.put("modelId", modelId); | ||
queryMap.put("resourceId", resourceId); | ||
queryMap.put("clientSecret",casdoorConfig.getClientSecret()); | ||
queryMap.put("clientId", casdoorConfig.getClientId()); | ||
|
||
String url = null; | ||
if(queryMap.get("action").equals("enforce")){ | ||
url = casdoorConfig.getEndpoint() + "/api/enforce?" + MapToUrlUtils.mapToUrlParams(queryMap); | ||
} | ||
else{ | ||
url = casdoorConfig.getEndpoint() + "/api/batch-enforce?" + MapToUrlUtils.mapToUrlParams(queryMap); | ||
|
||
} | ||
String data = new String(postBytes, StandardCharsets.UTF_8); | ||
String response = HttpClient.postString(url, data); | ||
CasdoorResponse casdoorResponse = objectMapper.readValue(response, CasdoorResponse.class); | ||
if (!casdoorResponse.getStatus().equals("ok")){ | ||
throw new CasdoorException("Failed to unmarshal JSON"); | ||
} | ||
return casdoorResponse; | ||
} | ||
|
||
} |
Oops, something went wrong.