Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Append enforce function to sdk #37

Merged
merged 6 commits into from
Jan 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '18'

- name: Setup
run: npm install -g semantic-release @semantic-release/github @semantic-release/changelog @semantic-release/commit-analyzer @semantic-release/git @semantic-release/release-notes-generator semantic-release-pypi
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,4 @@ casdoor-python-sdk support basic user operations, like:
- `get_users()`, get all users.
- `modify_user(method: str, user: User)/add_user(user: User)/update_user(user: User)/delete_user(user: User)`, write user to database.
- `refresh_token_request(refresh_token: str, scope: str)`, refresh access token
- `enforce(self, permission_model_name: str, sub: str, obj: str, act: str)`, check permission from model
47 changes: 45 additions & 2 deletions src/casdoor/async_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def __del__(self):
@property
def certification(self) -> bytes:
if type(self.certificate) is not str:
raise TypeError('certificate field must be str type')
return self.certificate.encode('utf-8')
raise TypeError("certificate field must be str type")
return self.certificate.encode("utf-8")

async def get_auth_link(
self,
Expand Down Expand Up @@ -165,6 +165,49 @@ def parse_jwt_token(self, token: str) -> dict:
)
return return_json

async def enforce(
self,
permission_model_name: str,
sub: str,
obj: str,
act: str
) -> bool:
"""
Send data to Casdoor enforce API
:param permission_model_name: Name permission model
:param sub: sub from Casbin
:param obj: obj from Casbin
:param act: act from Casbin
"""
url = self.endpoint + "/api/enforce"
query_params = {
"clientId": self.client_id,
"clientSecret": self.client_secret
}
params = {
"id": permission_model_name,
"v0": sub,
"v1": obj,
"v2": act,
}
async with self._session.post(
url, params=query_params, json=params
) as response:
if (
response.status != 200 or
"json" not in response.headers["content-type"]
):
error_str = "Casdoor response error:\n" + str(response.text)
raise ValueError(error_str)

has_permission = await response.json()

if not isinstance(has_permission, bool):
error_str = "Casdoor response error:\n" + await response.text()
raise ValueError(error_str)

return has_permission

async def get_users(self) -> List[dict]:
"""
Get the users from Casdoor.
Expand Down
42 changes: 40 additions & 2 deletions src/casdoor/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def __init__(
@property
def certification(self) -> bytes:
if type(self.certificate) is not str:
raise TypeError('certificate field must be str type')
return self.certificate.encode('utf-8')
raise TypeError("certificate field must be str type")
return self.certificate.encode("utf-8")

def get_auth_link(
self,
Expand Down Expand Up @@ -154,6 +154,44 @@ def parse_jwt_token(self, token: str) -> dict:
)
return return_json

def enforce(
self,
permission_model_name: str,
sub: str,
obj: str,
act: str
) -> bool:
"""
Send data to Casdoor enforce API
:param permission_model_name: Name permission model
:param sub: sub from Casbin
:param obj: obj from Casbin
:param act: act from Casbin
"""
url = self.endpoint + "/api/enforce"
query_params = {
"clientId": self.client_id,
"clientSecret": self.client_secret
}
params = {
"id": permission_model_name,
"v0": sub,
"v1": obj,
"v2": act,
}
r = requests.post(url, json=params, params=query_params)
if r.status_code != 200 or "json" not in r.headers["content-type"]:
error_str = "Casdoor response error:\n" + str(r.text)
raise ValueError(error_str)

has_permission = r.json()

if not isinstance(has_permission, bool):
error_str = "Casdoor response error:\n" + r.text
raise ValueError(error_str)

return has_permission

def get_users(self) -> List[dict]:
"""
Get the users from Casdoor.
Expand Down
7 changes: 7 additions & 0 deletions src/tests/test_async_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ async def test_parse_jwt_token(self):
decoded_msg = sdk.parse_jwt_token(access_token)
self.assertIsInstance(decoded_msg, dict)

async def test_enforce(self):
sdk = self.get_sdk()
status = await sdk.enforce(
"built-in/permission-built-in", "admin", "a", "ac"
)
self.assertIsInstance(status, bool)

async def test_get_users(self):
sdk = self.get_sdk()
users = await sdk.get_users()
Expand Down
7 changes: 7 additions & 0 deletions src/tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ def test_parse_jwt_token(self):
decoded_msg = sdk.parse_jwt_token(access_token)
self.assertIsInstance(decoded_msg, dict)

def test_enforce(self):
sdk = self.get_sdk()
status = sdk.enforce(
"built-in/permission-built-in", "admin", "a", "ac"
)
self.assertIsInstance(status, bool)

def test_get_users(self):
sdk = self.get_sdk()
users = sdk.get_users()
Expand Down