diff --git a/setup.py b/setup.py index 4b5369f..6068493 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,3 @@ - from setuptools import setup setup() diff --git a/src/casdoor/adapter.py b/src/casdoor/adapter.py new file mode 100644 index 0000000..80801e9 --- /dev/null +++ b/src/casdoor/adapter.py @@ -0,0 +1,36 @@ +# Copyright 2021 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. + + +class Adapter: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.type = "string" + self.databaseType = "string" + self.host = "string" + self.port = 0 + self.user = "string" + self.password = "string" + self.database = "string" + self.table = "string" + self.tableNamePrefix = "string" + self.isEnabled = True + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/application.py b/src/casdoor/application.py new file mode 100644 index 0000000..95ea077 --- /dev/null +++ b/src/casdoor/application.py @@ -0,0 +1,98 @@ +# Copyright 2021 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. + +from .organization import Organization, ThemeData +from .provider import Provider + + +class ProviderItem: + def __init__(self): + self.owner = "string" + self.name = "string" + self.canSignUp = True + self.canSignIn = True + self.canUnlink = True + self.prompted = True + self.alertType = "string" + self.rule = "string" + self.provider = Provider + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ + + +class SignupItem: + def __init__(self): + self.name = "string" + self.visible = True + self.required = True + self.prompted = True + self.rule = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ + + +class Application: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.logo = "string" + self.homepageUrl = "string" + self.description = "string" + self.organization = "string" + self.cert = "string" + self.enablePassword = True + self.enableSignUp = True + self.enableSigninSession = True + self.enableAutoSignin = True + self.enableCodeSignin = True + self.enableSamlCompress = True + self.enableWebAuthn = True + self.enableLinkWithEmail = True + self.orgChoiceMode = "string" + self.samlReplyUrl = "string" + self.providers = [ProviderItem] + self.signupItems = [SignupItem] + self.grantTypes = ["string"] + self.organizationObj = Organization + self.tags = ["string"] + self.clientId = "string" + self.clientSecret = "string" + self.redirectUris = ["string"] + self.tokenFormat = "string" + self.expireInHours = 0 + self.refreshExpireInHours = 0 + self.signupUrl = "string" + self.signinUrl = "string" + self.forgetUrl = "string" + self.affiliationUrl = "string" + self.termsOfUse = "string" + self.signupHtml = "string" + self.signinHtml = "string" + self.themeData = ThemeData + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/cert.py b/src/casdoor/cert.py new file mode 100644 index 0000000..ce99ba4 --- /dev/null +++ b/src/casdoor/cert.py @@ -0,0 +1,36 @@ +# Copyright 2021 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. + + +class Cert: + def __init__(self): + self.name = "string" + self.owner = "string" + self.createdTime = "string" + self.displayName = "string" + self.type = "string" + self.scope = "string" + self.cryptoAlgorithm = "string" + self.bitSize = 0 + self.expireInYears = 0 + self.certificate = "string" + self.privateKey = "string" + self.authorityPublicKey = "string" + self.authorityRootPublicKey = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/enforcer.py b/src/casdoor/enforcer.py new file mode 100644 index 0000000..07b95f1 --- /dev/null +++ b/src/casdoor/enforcer.py @@ -0,0 +1,32 @@ +# Copyright 2021 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. + + +class Enforcer: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.updatedTime = "string" + self.displayName = "string" + self.description = "string" + self.model = "string" + self.adapter = "string" + self.isEnabled = True + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/group.py b/src/casdoor/group.py new file mode 100644 index 0000000..c0519a2 --- /dev/null +++ b/src/casdoor/group.py @@ -0,0 +1,40 @@ +# Copyright 2021 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. + +from .user import User + + +class Group: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.updatedTime = "string" + self.displayName = "string" + self.manager = "string" + self.contactEmail = "string" + self.type = "string" + self.parentId = "string" + self.isTopGroup = True + self.users = [User] + self.title = "string" + self.key = "string" + self.children = [Group] + self.isEnabled = True + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/main.py b/src/casdoor/main.py index 0624c1a..cf7e6c1 100644 --- a/src/casdoor/main.py +++ b/src/casdoor/main.py @@ -20,7 +20,27 @@ from cryptography import x509 from cryptography.hazmat.backends import default_backend +from .adapter import Adapter +from .application import Application +from .cert import Cert +from .enforcer import Enforcer +from .group import Group +from .model import Model +from .organization import Organization +from .payment import Payment +from .permisssion import Permission +from .plan import Plan +from .pricing import Pricing +from .product import Product +from .provider import Provider +from .resource import Resource +from .role import Role +from .session import Session +from .subscription import Subscription +from .syncer import Syncer +from .token import Token from .user import User +from .webhook import Webhook class CasdoorSDK: @@ -50,7 +70,7 @@ def __init__( @property def certification(self) -> bytes: - if type(self.certificate) is not str: + if not isinstance(self.certificate, str): raise TypeError("certificate field must be str type") return self.certificate.encode("utf-8") @@ -374,3 +394,1170 @@ def update_user(self, user: User) -> Dict: def delete_user(self, user: User) -> Dict: response = self.modify_user("delete-user", user) return response + + def get_certs(self) -> List[Dict]: + """ + Get the certs from Casdoor. + + :return: a list of dicts containing cert info + """ + url = self.endpoint + "/api/get-certs" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + certs = r.json() + return certs + + def get_cert(self, cert_id: str) -> Dict: + """ + Get the cert from Casdoor providing the cert_id. + + :param cert_id: the id of the cert + :return: a dict that contains cert's info + """ + url = self.endpoint + "/api/get-cert" + params = { + "id": f"{self.org_name}/{cert_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + cert = r.json() + return cert + + def modify_cert(self, method: str, cert: Cert) -> Dict: + url = self.endpoint + f"/api/{method}" + cert.owner = self.org_name + params = { + "id": f"{cert.owner}/{cert.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + cert_info = json.dumps(cert.to_dict()) + r = requests.post(url, params=params, data=cert_info) + response = r.json() + return response + + def add_cert(self, cert: Cert) -> Dict: + response = self.modify_cert("add-cert", cert) + return response + + def update_cert(self, cert: Cert) -> Dict: + response = self.modify_cert("update-cert", cert) + return response + + def delete_cert(self, cert: Cert) -> Dict: + response = self.modify_cert("delete-cert", cert) + return response + + def get_adapters(self) -> List[Dict]: + """ + Get the adapters from Casdoor. + + :return: a list of dicts containing adapter info + """ + url = self.endpoint + "/api/get-adapters" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + adapters = r.json() + return adapters + + def get_adapter(self, adapter_id: str) -> Dict: + """ + Get the adapter from Casdoor providing the adapter_id. + + :param adapter_id: the id of the adapter + :return: a dict that contains adapter's info + """ + url = self.endpoint + "/api/get-adapter" + params = { + "id": f"{self.org_name}/{adapter_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + adapter = r.json() + return adapter + + def modify_adapter(self, method: str, adapter: Adapter) -> Dict: + url = self.endpoint + f"/api/{method}" + adapter.owner = self.org_name + params = { + "id": f"{adapter.owner}/{adapter.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + adapter_info = json.dumps(adapter.to_dict()) + r = requests.post(url, params=params, data=adapter_info) + response = r.json() + return response + + def add_adapter(self, adapter: Adapter) -> Dict: + response = self.modify_adapter("add-adapter", adapter) + return response + + def update_adapter(self, adapter: Adapter) -> Dict: + response = self.modify_adapter("update-adapter", adapter) + return response + + def delete_adapter(self, adapter: Adapter) -> Dict: + response = self.modify_adapter("delete-adapter", adapter) + return response + + def get_groups(self) -> List[Dict]: + """ + Get the groups from Casdoor. + + :return: a list of dicts containing group info + """ + url = self.endpoint + "/api/get-groups" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + groups = r.json() + return groups + + def get_group(self, group_id: str) -> Dict: + """ + Get the group from Casdoor providing the group_id. + + :param group_id: the id of the group + :return: a dict that contains group's info + """ + url = self.endpoint + "/api/get-group" + params = { + "id": f"{self.org_name}/{group_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + group = r.json() + return group + + def modify_group(self, method: str, group: Group) -> Dict: + url = self.endpoint + f"/api/{method}" + group.owner = self.org_name + params = { + "id": f"{group.owner}/{group.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + group_info = json.dumps(group.to_dict()) + r = requests.post(url, params=params, data=group_info) + response = r.json() + return response + + def add_group(self, group: Group) -> Dict: + response = self.modify_group("add-group", group) + return response + + def update_group(self, group: Group) -> Dict: + response = self.modify_group("update-group", group) + return response + + def delete_group(self, group: Group) -> Dict: + response = self.modify_group("delete-group", group) + return response + + def get_roles(self) -> List[Dict]: + """ + Get the roles from Casdoor. + + :return: a list of dicts containing role info + """ + url = self.endpoint + "/api/get-roles" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + roles = r.json() + return roles + + def get_role(self, role_id: str) -> Dict: + """ + Get the role from Casdoor providing the role_id. + + :param role_id: the id of the role + :return: a dict that contains role's info + """ + url = self.endpoint + "/api/get-role" + params = { + "id": f"{self.org_name}/{role_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + role = r.json() + return role + + def modify_role(self, method: str, role: Role) -> Dict: + url = self.endpoint + f"/api/{method}" + role.owner = self.org_name + params = { + "id": f"{role.owner}/{role.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + role_info = json.dumps(role.to_dict()) + r = requests.post(url, params=params, data=role_info) + response = r.json() + return response + + def add_role(self, role: Role) -> Dict: + response = self.modify_role("add-role", role) + return response + + def update_role(self, role: Role) -> Dict: + response = self.modify_role("update-role", role) + return response + + def delete_role(self, role: Role) -> Dict: + response = self.modify_role("delete-role", role) + return response + + def get_organizations(self) -> List[Dict]: + """ + Get the organizations from Casdoor. + + :return: a list of dicts containing organization info + """ + url = self.endpoint + "/api/get-organizations" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + organizations = r.json() + return organizations + + def get_organization(self, organization_id: str) -> Dict: + """ + Get the organization from Casdoor providing the organization_id. + + :param organization_id: the id of the organization + :return: a dict that contains organization's info + """ + url = self.endpoint + "/api/get-organization" + params = { + "id": f"{self.org_name}/{organization_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + organization = r.json() + return organization + + def modify_organization(self, method: str, organization: Organization) -> Dict: + url = self.endpoint + f"/api/{method}" + organization.owner = self.org_name + params = { + "id": f"{organization.owner}/{organization.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + organization_info = json.dumps(organization.to_dict()) + r = requests.post(url, params=params, data=organization_info) + response = r.json() + return response + + def add_organization(self, organization: Organization) -> Dict: + response = self.modify_organization("add-organization", organization) + return response + + def update_organization(self, organization: Organization) -> Dict: + response = self.modify_organization("update-organization", organization) + return response + + def delete_organization(self, organization: Organization) -> Dict: + response = self.modify_organization("delete-organization", organization) + return response + + def get_payments(self) -> List[Dict]: + """ + Get the payments from Casdoor. + + :return: a list of dicts containing payment info + """ + url = self.endpoint + "/api/get-payments" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + payments = r.json() + return payments + + def get_payment(self, payment_id: str) -> Dict: + """ + Get the payment from Casdoor providing the payment_id. + + :param payment_id: the id of the payment + :return: a dict that contains payment's info + """ + url = self.endpoint + "/api/get-payment" + params = { + "id": f"{self.org_name}/{payment_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + payment = r.json() + return payment + + def modify_payment(self, method: str, payment: Payment) -> Dict: + url = self.endpoint + f"/api/{method}" + payment.owner = self.org_name + params = { + "id": f"{payment.owner}/{payment.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + payment_info = json.dumps(payment.to_dict()) + r = requests.post(url, params=params, data=payment_info) + response = r.json() + return response + + def add_payment(self, payment: Payment) -> Dict: + response = self.modify_payment("add-payment", payment) + return response + + def update_payment(self, payment: Payment) -> Dict: + response = self.modify_payment("update-payment", payment) + return response + + def delete_payment(self, payment: Payment) -> Dict: + response = self.modify_payment("delete-payment", payment) + return response + + def get_providers(self) -> List[Dict]: + """ + Get the providers from Casdoor. + + :return: a list of dicts containing provider info + """ + url = self.endpoint + "/api/get-providers" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + providers = r.json() + return providers + + def get_provider(self, provider_id: str) -> Dict: + """ + Get the provider from Casdoor providing the provider_id. + + :param provider_id: the id of the provider + :return: a dict that contains provider's info + """ + url = self.endpoint + "/api/get-provider" + params = { + "id": f"{self.org_name}/{provider_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + provider = r.json() + return provider + + def modify_provider(self, method: str, provider: Provider) -> Dict: + url = self.endpoint + f"/api/{method}" + provider.owner = self.org_name + params = { + "id": f"{provider.owner}/{provider.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + provider_info = json.dumps(provider.to_dict()) + r = requests.post(url, params=params, data=provider_info) + response = r.json() + return response + + def add_provider(self, provider: Provider) -> Dict: + response = self.modify_provider("add-provider", provider) + return response + + def update_provider(self, provider: Provider) -> Dict: + response = self.modify_provider("update-provider", provider) + return response + + def delete_provider(self, provider: Provider) -> Dict: + response = self.modify_provider("delete-provider", provider) + return response + + def get_applications(self) -> List[Dict]: + """ + Get the applications from Casdoor. + + :return: a list of dicts containing application info + """ + url = self.endpoint + "/api/get-applications" + params = { + "owner": "admin", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + applications = r.json() + return applications + + def get_application(self, application_id: str) -> Dict: + """ + Get the application from Casdoor providing the application_id. + + :param application_id: the id of the application + :return: a dict that contains application's info + """ + url = self.endpoint + "/api/get-application" + params = { + "id": f"{self.org_name}/{application_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + application = r.json() + return application + + def modify_application(self, method: str, application: Application) -> Dict: + url = self.endpoint + f"/api/{method}" + application.owner = self.org_name + params = { + "id": f"{application.owner}/{application.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + application_info = json.dumps(application.to_dict()) + r = requests.post(url, params=params, data=application_info) + response = r.json() + return response + + def add_application(self, application: Application) -> Dict: + response = self.modify_application("add-application", application) + return response + + def update_application(self, application: Application) -> Dict: + response = self.modify_application("update-application", application) + return response + + def delete_application(self, application: Application) -> Dict: + response = self.modify_application("delete-application", application) + return response + + def get_models(self) -> List[Dict]: + """ + Get the models from Casdoor. + + :return: a list of dicts containing model info + """ + url = self.endpoint + "/api/get-models" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + models = r.json() + return models + + def get_model(self, model_id: str) -> Dict: + """ + Get the model from Casdoor providing the model_id. + + :param model_id: the id of the model + :return: a dict that contains model's info + """ + url = self.endpoint + "/api/get-model" + params = { + "id": f"{self.org_name}/{model_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + model = r.json() + return model + + def modify_model(self, method: str, model: Model) -> Dict: + url = self.endpoint + f"/api/{method}" + model.owner = self.org_name + params = { + "id": f"{model.owner}/{model.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + model_info = json.dumps(model.to_dict()) + r = requests.post(url, params=params, data=model_info) + response = r.json() + return response + + def add_model(self, model: Model) -> Dict: + response = self.modify_model("add-model", model) + return response + + def update_model(self, model: Model) -> Dict: + response = self.modify_model("update-model", model) + return response + + def delete_model(self, model: Model) -> Dict: + response = self.modify_model("delete-model", model) + return response + + def get_plans(self) -> List[Dict]: + """ + Get the plans from Casdoor. + + :return: a list of dicts containing plan info + """ + url = self.endpoint + "/api/get-plans" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + plans = r.json() + return plans + + def get_plan(self, plan_id: str) -> Dict: + """ + Get the plan from Casdoor providing the plan_id. + + :param plan_id: the id of the plan + :return: a dict that contains plan's info + """ + url = self.endpoint + "/api/get-plan" + params = { + "id": f"{self.org_name}/{plan_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + plan = r.json() + return plan + + def modify_plan(self, method: str, plan: Plan) -> Dict: + url = self.endpoint + f"/api/{method}" + plan.owner = self.org_name + params = { + "id": f"{plan.owner}/{plan.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + plan_info = json.dumps(plan.to_dict()) + r = requests.post(url, params=params, data=plan_info) + response = r.json() + return response + + def add_plan(self, plan: Plan) -> Dict: + response = self.modify_plan("add-plan", plan) + return response + + def update_plan(self, plan: Plan) -> Dict: + response = self.modify_plan("update-plan", plan) + return response + + def delete_plan(self, plan: Plan) -> Dict: + response = self.modify_plan("delete-plan", plan) + return response + + def get_permissions(self) -> List[Dict]: + """ + Get the permissions from Casdoor. + + :return: a list of dicts containing permission info + """ + url = self.endpoint + "/api/get-permissions" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + permissions = r.json() + return permissions + + def get_permission(self, permission_id: str) -> Dict: + """ + Get the permission from Casdoor providing the permission_id. + + :param permission_id: the id of the permission + :return: a dict that contains permission's info + """ + url = self.endpoint + "/api/get-permission" + params = { + "id": f"{self.org_name}/{permission_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + permission = r.json() + return permission + + def modify_permission(self, method: str, permission: Permission) -> Dict: + url = self.endpoint + f"/api/{method}" + permission.owner = self.org_name + params = { + "id": f"{permission.owner}/{permission.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + permission_info = json.dumps(permission.to_dict()) + r = requests.post(url, params=params, data=permission_info) + response = r.json() + return response + + def add_permission(self, permission: Permission) -> Dict: + response = self.modify_permission("add-permission", permission) + return response + + def update_permission(self, permission: Permission) -> Dict: + response = self.modify_permission("update-permission", permission) + return response + + def delete_permission(self, permission: Permission) -> Dict: + response = self.modify_permission("delete-permission", permission) + return response + + def get_enforcers(self) -> List[Dict]: + """ + Get the enforcers from Casdoor. + + :return: a list of dicts containing enforcer info + """ + url = self.endpoint + "/api/get-enforcers" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + enforcers = r.json() + return enforcers + + def get_enforcer(self, enforcer_id: str) -> Dict: + """ + Get the enforcer from Casdoor providing the enforcer_id. + + :param enforcer_id: the id of the enforcer + :return: a dict that contains enforcer's info + """ + url = self.endpoint + "/api/get-enforcer" + params = { + "id": f"{self.org_name}/{enforcer_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + enforcer = r.json() + return enforcer + + def modify_enforcer(self, method: str, enforcer: Enforcer) -> Dict: + url = self.endpoint + f"/api/{method}" + enforcer.owner = self.org_name + params = { + "id": f"{enforcer.owner}/{enforcer.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + enforcer_info = json.dumps(enforcer.to_dict()) + r = requests.post(url, params=params, data=enforcer_info) + response = r.json() + return response + + def add_enforcer(self, enforcer: Enforcer) -> Dict: + response = self.modify_enforcer("add-enforcer", enforcer) + return response + + def update_enforcer(self, enforcer: Enforcer) -> Dict: + response = self.modify_enforcer("update-enforcer", enforcer) + return response + + def delete_enforcer(self, enforcer: Enforcer) -> Dict: + response = self.modify_enforcer("delete-enforcer", enforcer) + return response + + def get_resources(self, owner, user, field, value, sort_field, sort_order) -> List[Dict]: + """ + Get the resources from Casdoor. + + :return: a list of dicts containing resource info + """ + url = self.endpoint + "/api/get-resources" + params = { + "owner": owner, + "user": user, + "field": field, + "value": value, + "sortField": sort_field, + "sortOrder": sort_order, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + resources = r.json() + return resources + + def get_resource(self, resource_id: str) -> Dict: + """ + Get the resource from Casdoor providing the resource_id. + + :param resource_id: the id of the resource + :return: a dict that contains resource's info + """ + url = self.endpoint + "/api/get-resource" + params = { + "id": f"{self.org_name}/{resource_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + resource = r.json() + return resource + + def modify_resource(self, method: str, resource: Resource) -> Dict: + url = self.endpoint + f"/api/{method}" + resource.owner = self.org_name + params = { + "id": f"{resource.owner}/{resource.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + resource_info = json.dumps(resource.to_dict()) + r = requests.post(url, params=params, data=resource_info) + response = r.json() + return response + + def add_resource(self, resource: Resource) -> Dict: + response = self.modify_resource("add-resource", resource) + return response + + def update_resource(self, resource: Resource) -> Dict: + response = self.modify_resource("update-resource", resource) + return response + + def delete_resource(self, resource: Resource) -> Dict: + response = self.modify_resource("delete-resource", resource) + return response + + def get_tokens(self, p, page_size) -> List[Dict]: + """ + Get the tokens from Casdoor. + + :return: a list of dicts containing token info + """ + url = self.endpoint + "/api/get-tokens" + params = { + "owner": self.org_name, + "p": str(p), + "pageSize": str(page_size), + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + tokens = r.json() + return tokens + + def get_token(self, token_id: str) -> Dict: + """ + Get the token from Casdoor providing the token_id. + + :param token_id: the id of the token + :return: a dict that contains token's info + """ + url = self.endpoint + "/api/get-token" + params = { + "id": f"{self.org_name}/{token_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + token = r.json() + return token + + def modify_token(self, method: str, token: Token) -> Dict: + url = self.endpoint + f"/api/{method}" + token.owner = self.org_name + params = { + "id": f"{token.owner}/{token.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + token_info = json.dumps(token.to_dict()) + r = requests.post(url, params=params, data=token_info) + response = r.json() + return response + + def add_token(self, token: Token) -> Dict: + response = self.modify_token("add-token", token) + return response + + def update_token(self, token: Token) -> Dict: + response = self.modify_token("update-token", token) + return response + + def delete_token(self, token: Token) -> Dict: + response = self.modify_token("delete-token", token) + return response + + def get_sessions(self) -> List[Dict]: + """ + Get the sessions from Casdoor. + + :return: a list of dicts containing session info + """ + url = self.endpoint + "/api/get-sessions" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + sessions = r.json() + return sessions + + def get_session(self, session_id: str) -> Dict: + """ + Get the session from Casdoor providing the session_id. + + :param session_id: the id of the session + :return: a dict that contains session's info + """ + url = self.endpoint + "/api/get-session" + params = { + "id": f"{self.org_name}/{session_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + session = r.json() + return session + + def modify_session(self, method: str, session: Session) -> Dict: + url = self.endpoint + f"/api/{method}" + session.owner = self.org_name + params = { + "id": f"{session.owner}/{session.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + session_info = json.dumps(session.to_dict()) + r = requests.post(url, params=params, data=session_info) + response = r.json() + return response + + def add_session(self, session: Session) -> Dict: + response = self.modify_session("add-session", session) + return response + + def update_session(self, session: Session) -> Dict: + response = self.modify_session("update-session", session) + return response + + def delete_session(self, session: Session) -> Dict: + response = self.modify_session("delete-session", session) + return response + + def get_syncers(self) -> List[Dict]: + """ + Get the syncers from Casdoor. + + :return: a list of dicts containing syncer info + """ + url = self.endpoint + "/api/get-syncers" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + syncers = r.json() + return syncers + + def get_syncer(self, syncer_id: str) -> Dict: + """ + Get the syncer from Casdoor providing the syncer_id. + + :param syncer_id: the id of the syncer + :return: a dict that contains syncer's info + """ + url = self.endpoint + "/api/get-syncer" + params = { + "id": f"{self.org_name}/{syncer_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + syncer = r.json() + return syncer + + def modify_syncer(self, method: str, syncer: Syncer) -> Dict: + url = self.endpoint + f"/api/{method}" + syncer.owner = self.org_name + params = { + "id": f"{syncer.owner}/{syncer.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + syncer_info = json.dumps(syncer.to_dict()) + r = requests.post(url, params=params, data=syncer_info) + response = r.json() + return response + + def add_syncer(self, syncer: Syncer) -> Dict: + response = self.modify_syncer("add-syncer", syncer) + return response + + def update_syncer(self, syncer: Syncer) -> Dict: + response = self.modify_syncer("update-syncer", syncer) + return response + + def delete_syncer(self, syncer: Syncer) -> Dict: + response = self.modify_syncer("delete-syncer", syncer) + return response + + def get_webhooks(self) -> List[Dict]: + """ + Get the webhooks from Casdoor. + + :return: a list of dicts containing webhook info + """ + url = self.endpoint + "/api/get-webhooks" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + webhooks = r.json() + return webhooks + + def get_webhook(self, webhook_id: str) -> Dict: + """ + Get the webhook from Casdoor providing the webhook_id. + + :param webhook_id: the id of the webhook + :return: a dict that contains webhook's info + """ + url = self.endpoint + "/api/get-webhook" + params = { + "id": f"{self.org_name}/{webhook_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + webhook = r.json() + return webhook + + def modify_webhook(self, method: str, webhook: Webhook) -> Dict: + url = self.endpoint + f"/api/{method}" + webhook.owner = self.org_name + params = { + "id": f"{webhook.owner}/{webhook.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + webhook_info = json.dumps(webhook.to_dict()) + r = requests.post(url, params=params, data=webhook_info) + response = r.json() + return response + + def add_webhook(self, webhook: Webhook) -> Dict: + response = self.modify_webhook("add-webhook", webhook) + return response + + def update_webhook(self, webhook: Webhook) -> Dict: + response = self.modify_webhook("update-webhook", webhook) + return response + + def delete_webhook(self, webhook: Webhook) -> Dict: + response = self.modify_webhook("delete-webhook", webhook) + return response + + def get_subscriptions(self) -> List[Dict]: + """ + Get the subscriptions from Casdoor. + + :return: a list of dicts containing subscription info + """ + url = self.endpoint + "/api/get-subscriptions" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + subscriptions = r.json() + return subscriptions + + def get_subscription(self, subscription_id: str) -> Dict: + """ + Get the subscription from Casdoor providing the subscription_id. + + :param subscription_id: the id of the subscription + :return: a dict that contains subscription's info + """ + url = self.endpoint + "/api/get-subscription" + params = { + "id": f"{self.org_name}/{subscription_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + subscription = r.json() + return subscription + + def modify_subscription(self, method: str, subscription: Subscription) -> Dict: + url = self.endpoint + f"/api/{method}" + subscription.owner = self.org_name + params = { + "id": f"{subscription.owner}/{subscription.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + subscription_info = json.dumps(subscription.to_dict()) + r = requests.post(url, params=params, data=subscription_info) + response = r.json() + return response + + def add_subscription(self, subscription: Subscription) -> Dict: + response = self.modify_subscription("add-subscription", subscription) + return response + + def update_subscription(self, subscription: Subscription) -> Dict: + response = self.modify_subscription("update-subscription", subscription) + return response + + def delete_subscription(self, subscription: Subscription) -> Dict: + response = self.modify_subscription("delete-subscription", subscription) + return response + + def get_pricings(self) -> List[Dict]: + """ + Get the pricings from Casdoor. + + :return: a list of dicts containing pricing info + """ + url = self.endpoint + "/api/get-pricings" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + pricings = r.json() + return pricings + + def get_pricing(self, pricing_id: str) -> Dict: + """ + Get the pricing from Casdoor providing the pricing_id. + + :param pricing_id: the id of the pricing + :return: a dict that contains pricing's info + """ + url = self.endpoint + "/api/get-pricing" + params = { + "id": f"{self.org_name}/{pricing_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + pricing = r.json() + return pricing + + def modify_pricing(self, method: str, pricing: Pricing) -> Dict: + url = self.endpoint + f"/api/{method}" + pricing.owner = self.org_name + params = { + "id": f"{pricing.owner}/{pricing.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + pricing_info = json.dumps(pricing.to_dict()) + r = requests.post(url, params=params, data=pricing_info) + response = r.json() + return response + + def add_pricing(self, pricing: Pricing) -> Dict: + response = self.modify_pricing("add-pricing", pricing) + return response + + def update_pricing(self, pricing: Pricing) -> Dict: + response = self.modify_pricing("update-pricing", pricing) + return response + + def delete_pricing(self, pricing: Pricing) -> Dict: + response = self.modify_pricing("delete-pricing", pricing) + return response + + def get_products(self) -> List[Dict]: + """ + Get the products from Casdoor. + + :return: a list of dicts containing product info + """ + url = self.endpoint + "/api/get-products" + params = { + "owner": self.org_name, + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + products = r.json() + return products + + def get_product(self, product_id: str) -> Dict: + """ + Get the product from Casdoor providing the product_id. + + :param product_id: the id of the product + :return: a dict that contains product's info + """ + url = self.endpoint + "/api/get-product" + params = { + "id": f"{self.org_name}/{product_id}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + r = requests.get(url, params) + product = r.json() + return product + + def modify_product(self, method: str, product: Product) -> Dict: + url = self.endpoint + f"/api/{method}" + product.owner = self.org_name + params = { + "id": f"{product.owner}/{product.name}", + "clientId": self.client_id, + "clientSecret": self.client_secret, + } + product_info = json.dumps(product.to_dict()) + r = requests.post(url, params=params, data=product_info) + response = r.json() + return response + + def add_product(self, product: Product) -> Dict: + response = self.modify_product("add-product", product) + return response + + def update_product(self, product: Product) -> Dict: + response = self.modify_product("update-product", product) + return response + + def delete_product(self, product: Product) -> Dict: + response = self.modify_product("delete-product", product) + return response diff --git a/src/casdoor/model.py b/src/casdoor/model.py new file mode 100644 index 0000000..5606176 --- /dev/null +++ b/src/casdoor/model.py @@ -0,0 +1,40 @@ +# Copyright 2021 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. + +from .user import User + + +class Model: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.updatedTime = "string" + self.displayName = "string" + self.manager = "string" + self.contactEmail = "string" + self.type = "string" + self.parentId = "string" + self.isTopModel = True + self.users = [User] + self.title = "string" + self.key = "string" + self.children = [Model] + self.isEnabled = True + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/organization.py b/src/casdoor/organization.py new file mode 100644 index 0000000..5c854d7 --- /dev/null +++ b/src/casdoor/organization.py @@ -0,0 +1,85 @@ +# Copyright 2021 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. + + +class AccountItem: + def __init__(self): + self.name = "string" + self.visible = False + self.viewRule = "string" + self.modifyRule = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ + + +class ThemeData: + def __init__(self): + self.themeType = "string" + self.colorPrimary = "string" + self.borderRadius = 0 + self.isCompact = False + self.isEnabled = False + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ + + +class MfaItem: + def __init__(self): + self.name = "string" + self.rule = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ + + +class Organization: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.websiteUrl = "string" + self.favicon = "string" + self.passwordType = "string" + self.passwordSalt = "string" + self.passwordOptions = ["string"] + self.countryCodes = ["string"] + self.defaultAvatar = "string" + self.defaultApplication = "string" + self.tags = ["string"] + self.languages = ["string"] + self.themeData = ThemeData + self.masterPassword = "string" + self.initScore = 0 + self.enableSoftDeletion = True + self.isProfilePublic = True + self.mfaItems = [MfaItem] + self.accountItems = [AccountItem] + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/payment.py b/src/casdoor/payment.py new file mode 100644 index 0000000..7692859 --- /dev/null +++ b/src/casdoor/payment.py @@ -0,0 +1,50 @@ +# Copyright 2021 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. + + +class Payment: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.provider = "string" + self.type = "string" + self.productName = "string" + self.productDisplayName = "string" + self.detail = "string" + self.tag = "string" + self.currency = "string" + self.price = 0.0 + self.returnUrl = "string" + self.user = "string" + self.personName = "string" + self.personIdCard = "string" + self.personEmail = "string" + self.personPhone = "string" + self.invoiceType = "string" + self.invoiceTitle = "string" + self.invoiceTaxId = "string" + self.invoiceRemark = "string" + self.invoiceUrl = "string" + self.outOrderId = "string" + self.payUrl = "string" + self.state = "string" + self.message = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/permisssion.py b/src/casdoor/permisssion.py new file mode 100644 index 0000000..089a46e --- /dev/null +++ b/src/casdoor/permisssion.py @@ -0,0 +1,42 @@ +# Copyright 2021 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. + + +class Permission: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.description = "string" + self.users = ["string"] + self.roles = ["string"] + self.domains = ["string"] + self.model = "string" + self.adapter = "string" + self.resourceType = "string" + self.resources = ["string"] + self.actions = ["string"] + self.effect = "string" + self.isEnabled = True + self.submitter = "string" + self.approver = "string" + self.approveTime = "string" + self.state = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/plan.py b/src/casdoor/plan.py new file mode 100644 index 0000000..36efbd4 --- /dev/null +++ b/src/casdoor/plan.py @@ -0,0 +1,34 @@ +# Copyright 2021 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. + + +class Plan: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.description = "string" + self.pricePerMonth = 0.0 + self.pricePerYear = 0.0 + self.currency = "string" + self.isEnabled = True + self.role = "string" + self.options = ["string"] + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/pricing.py b/src/casdoor/pricing.py new file mode 100644 index 0000000..fbbf583 --- /dev/null +++ b/src/casdoor/pricing.py @@ -0,0 +1,36 @@ +# Copyright 2021 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. + + +class Pricing: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.description = "string" + self.plans = ["string"] + self.isEnabled = False + self.trialDuration = 0 + self.application = "string" + self.submitter = "string" + self.approver = "string" + self.approveTime = "string" + self.state = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/product.py b/src/casdoor/product.py new file mode 100644 index 0000000..caf5632 --- /dev/null +++ b/src/casdoor/product.py @@ -0,0 +1,41 @@ +# Copyright 2021 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. + +from .provider import Provider + + +class Product: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.image = "string" + self.detail = "string" + self.description = "string" + self.tag = "string" + self.currency = "string" + self.price = 0.0 + self.quantity = 0 + self.sold = 0 + self.providers = ["string"] + self.returnUrl = "string" + self.state = "string" + self.providerObjs = [Provider] + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/provider.py b/src/casdoor/provider.py new file mode 100644 index 0000000..7474bab --- /dev/null +++ b/src/casdoor/provider.py @@ -0,0 +1,62 @@ +# Copyright 2021 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. + + +class Provider: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.category = "string" + self.type = "string" + self.subType = "string" + self.method = "string" + self.clientId = "string" + self.clientSecret = "string" + self.clientId2 = "string" + self.clientSecret2 = "string" + self.cert = "string" + self.customAuthUrl = "string" + self.customTokenUrl = "string" + self.customUserInfoUrl = "string" + self.customLogo = "string" + self.scopes = "string" + self.userMapping = {"string": "string"} + self.host = "string" + self.port = 0 + self.disableSsl = True + self.title = "string" + self.content = "string" + self.receiver = "string" + self.regionId = "string" + self.signName = "string" + self.templateCode = "string" + self.appId = "string" + self.endpoint = "string" + self.intranetEndpoint = "string" + self.domain = "string" + self.bucket = "string" + self.pathPrefix = "string" + self.metadata = "string" + self.idP = "string" + self.issuerUrl = "string" + self.enableSignAuthnRequest = False + self.providerUrl = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/resource.py b/src/casdoor/resource.py new file mode 100644 index 0000000..0ffdb02 --- /dev/null +++ b/src/casdoor/resource.py @@ -0,0 +1,37 @@ +# Copyright 2021 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. + + +class Resource: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.user = "string" + self.provider = "string" + self.application = "string" + self.tag = "string" + self.parent = "string" + self.fileName = "string" + self.fileType = "string" + self.fileFormat = "string" + self.fileSize = 0 + self.url = "string" + self.description = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/role.py b/src/casdoor/role.py new file mode 100644 index 0000000..1bef968 --- /dev/null +++ b/src/casdoor/role.py @@ -0,0 +1,32 @@ +# Copyright 2021 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. + + +class Role: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.description = "string" + self.users = ["string"] + self.roles = ["string"] + self.domains = ["string"] + self.isEnabled = True + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/session.py b/src/casdoor/session.py new file mode 100644 index 0000000..7207f83 --- /dev/null +++ b/src/casdoor/session.py @@ -0,0 +1,28 @@ +# Copyright 2021 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. + + +class Session: + def __init__(self): + self.owner = "string" + self.name = "string" + self.application = "string" + self.createdTime = "string" + self.sessionId = ["string"] + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/subscription.py b/src/casdoor/subscription.py new file mode 100644 index 0000000..045bdc6 --- /dev/null +++ b/src/casdoor/subscription.py @@ -0,0 +1,40 @@ +# Copyright 2021 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. + +from datetime import datetime + + +class Subscription: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.displayName = "string" + self.startDate = datetime.now() + self.endDate = datetime.now() + self.duration = 0 + self.description = "string" + self.user = "string" + self.plan = "string" + self.isEnabled = True + self.submitter = "string" + self.approver = "string" + self.approveTime = "string" + self.state = "string" + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/syncer.py b/src/casdoor/syncer.py new file mode 100644 index 0000000..c4e9d64 --- /dev/null +++ b/src/casdoor/syncer.py @@ -0,0 +1,59 @@ +# Copyright 2021 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. + + +class TableColumn: + def __init__(self): + self.name = "string" + self.type = "string" + self.casdoorName = "string" + self.isKey = True + self.isHashed = True + self.values = ["string"] + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ + + +class Syncer: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.organization = "string" + self.type = "string" + self.host = "string" + self.port = 0 + self.user = "string" + self.password = "string" + self.databaseType = "string" + self.database = "string" + self.table = "string" + self.tablePrimaryKey = "string" + self.tableColumns = [TableColumn] + self.affiliationTable = "string" + self.avatarBaseUrl = "string" + self.errorText = "string" + self.syncInterval = 0 + self.isReadOnly = True + self.isEnabled = True + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/token.py b/src/casdoor/token.py new file mode 100644 index 0000000..18e459d --- /dev/null +++ b/src/casdoor/token.py @@ -0,0 +1,38 @@ +# Copyright 2021 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. + + +class Token: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.application = "string" + self.organization = "string" + self.user = "string" + self.code = "string" + self.accessToken = "string" + self.refreshToken = "string" + self.expiresIn = 0 + self.scope = "string" + self.tokenType = "string" + self.codeChallenge = "string" + self.codeIsUsed = True + self.codeExpireIn = 0 + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/casdoor/user.py b/src/casdoor/user.py index 7e8fb77..22cf189 100644 --- a/src/casdoor/user.py +++ b/src/casdoor/user.py @@ -12,6 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. + class User: def __init__(self): self.address = ["string"] diff --git a/src/casdoor/webhook.py b/src/casdoor/webhook.py new file mode 100644 index 0000000..0a2e8c2 --- /dev/null +++ b/src/casdoor/webhook.py @@ -0,0 +1,45 @@ +# Copyright 2021 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. + +from .syncer import TableColumn + + +class Webhook: + def __init__(self): + self.owner = "string" + self.name = "string" + self.createdTime = "string" + self.organization = "string" + self.type = "string" + self.host = "string" + self.port = 0 + self.user = "string" + self.password = "string" + self.databaseType = "string" + self.database = "string" + self.table = "string" + self.tablePrimaryKey = "string" + self.tableColumns = [TableColumn] + self.affiliationTable = "string" + self.avatarBaseUrl = "string" + self.errorText = "string" + self.syncInterval = 0 + self.isReadOnly = True + self.isEnabled = True + + def __str__(self): + return str(self.__dict__) + + def to_dict(self) -> dict: + return self.__dict__ diff --git a/src/tests/test_oauth.py b/src/tests/test_oauth.py index aecadb1..943e938 100644 --- a/src/tests/test_oauth.py +++ b/src/tests/test_oauth.py @@ -38,14 +38,13 @@ class TestOAuth(TestCase): @staticmethod def get_sdk(): - sdk = CasdoorSDK( endpoint="http://test.casbin.com:8000", client_id="3267f876b11e7d1cb217", client_secret="3f0d1f06d28d65309c8f38b505cb9dcfa487754d", certificate="CasdoorSecret", org_name="built-in", - application_name="app-built-in" + application_name="app-built-in", ) return sdk @@ -62,9 +61,7 @@ def test__oauth_token_request(self): def test__get_payload_for_authorization_code(self): sdk = self.get_sdk() - result = sdk._CasdoorSDK__get_payload_for_authorization_code( # noqa: It's private method - code=self.code - ) + result = sdk._CasdoorSDK__get_payload_for_authorization_code(code=self.code) # noqa: It's private method self.assertEqual("authorization_code", result.get("grant_type")) def test__get_payload_for_client_credentials(self): @@ -75,8 +72,7 @@ def test__get_payload_for_client_credentials(self): def test__get_payload_for_password_credentials(self): sdk = self.get_sdk() result = sdk._CasdoorSDK__get_payload_for_password_credentials( # noqa: It's private method - username="test", - password="test" + username="test", password="test" ) self.assertEqual("password", result.get("grant_type")) @@ -92,10 +88,7 @@ def test__get_payload_for_access_token_request_with_client_cred(self): def test__get_payload_for_access_token_request_with_cred(self): sdk = self.get_sdk() - result = sdk._get_payload_for_access_token_request( - username="test", - password="test" - ) + result = sdk._get_payload_for_access_token_request(username="test", password="test") self.assertEqual("password", result.get("grant_type")) def test_get_oauth_token_with_client_cred(self): @@ -112,10 +105,7 @@ def test_get_oauth_token_with_code(self): def test_get_oauth_token_with_password(self): sdk = self.get_sdk() - token = sdk.get_oauth_token( - username=self.username, - password=self.password - ) + token = sdk.get_oauth_token(username=self.username, password=self.password) access_token = token.get("access_token") self.assertIsInstance(access_token, str) @@ -147,58 +137,47 @@ def test_parse_jwt_token(self): def test_enforce(self): sdk = self.get_sdk() - status = sdk.enforce( - "built-in/permission-built-in", "admin", "a", "ac" - ) + status = sdk.enforce("built-in/permission-built-in", "admin", "a", "ac") self.assertIsInstance(status, bool) def mocked_enforce_requests_post(*args, **kwargs): class MockResponse: - def __init__(self, - json_data, - status_code=200, - headers={'content-type': 'json'}): - self.json_data = json_data - self.status_code = status_code - self.headers = headers + def __init__(self, json_data, status_code=200, headers=None): + if headers is None: + headers = {"content-type": "json"} + self.json_data = json_data + self.status_code = status_code + self.headers = headers def json(self): return self.json_data + result = True for i in range(0, 5): - if kwargs.get('json').get(f"v{i}") != f"v{i}": + if kwargs.get("json").get(f"v{i}") != f"v{i}": result = False return MockResponse(result) - @mock.patch("requests.post", - side_effect=mocked_enforce_requests_post) + @mock.patch("requests.post", side_effect=mocked_enforce_requests_post) def test_enforce_parmas(self, mock_post): sdk = self.get_sdk() - status = sdk.enforce( - "built-in/permission-built-in", - "v0", - "v1", - "v2", - v3='v3', - v4='v4', - v5='v5' - ) + status = sdk.enforce("built-in/permission-built-in", "v0", "v1", "v2", v3="v3", v4="v4", v5="v5") self.assertEqual(status, True) def mocked_batch_enforce_requests_post(*args, **kwargs): class MockResponse: - def __init__(self, - json_data, - status_code=200, - headers={'content-type': 'json'}): - self.json_data = json_data - self.status_code = status_code - self.headers = headers + def __init__(self, json_data, status_code=200, headers=None): + if headers is None: + headers = {"content-type": "json"} + self.json_data = json_data + self.status_code = status_code + self.headers = headers def json(self): return self.json_data - json = kwargs.get('json') + + json = kwargs.get("json") result = [True for i in range(0, len(json))] for k in range(0, len(json)): for i in range(0, len(json[k]) - 1): @@ -207,34 +186,22 @@ def json(self): return MockResponse(result) - @mock.patch("requests.post", - side_effect=mocked_batch_enforce_requests_post) + @mock.patch("requests.post", side_effect=mocked_batch_enforce_requests_post) def test_batch_enforce(self, mock_post): sdk = self.get_sdk() status = sdk.batch_enforce( - "built-in/permission-built-in", - [ - ["v0", "v1", "v2", "v3", "v4", 'v5'], - ["v0", "v1", "v2", "v3", "v4", "v1"] - ] + "built-in/permission-built-in", [["v0", "v1", "v2", "v3", "v4", "v5"], ["v0", "v1", "v2", "v3", "v4", "v1"]] ) self.assertEqual(len(status), 2) self.assertEqual(status[0], True) self.assertEqual(status[1], False) - @mock.patch("requests.post", - side_effect=mocked_batch_enforce_requests_post) + @mock.patch("requests.post", side_effect=mocked_batch_enforce_requests_post) def test_batch_enforce_raise(self, mock_post): sdk = self.get_sdk() with self.assertRaises(ValueError) as context: - sdk.batch_enforce( - "built-in/permission-built-in", - [ - ["v0", "v1"] - ] - ) - self.assertEqual("Invalid permission rule[0]: ['v0', 'v1']", - str(context.exception)) + sdk.batch_enforce("built-in/permission-built-in", [["v0", "v1"]]) + self.assertEqual("Invalid permission rule[0]: ['v0', 'v1']", str(context.exception)) def test_get_users(self): sdk = self.get_sdk()