generated from nextcloud/app-skeleton-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from cloud-py-api/feat/nextcloud-auth-resource
added creation of Nextcloud `auth` resource
- Loading branch information
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -502,8 +502,93 @@ def delete_listener(registered_listener: dict) -> bool: | |
return r | ||
|
||
|
||
def create_nextcloud_auth_variable() -> bool: | ||
r = httpx.post( | ||
url="http://127.0.0.1:8000/api/w/nextcloud/variables/create", | ||
cookies={"token": USERS_STORAGE["[email protected]"]["token"]}, | ||
json={ | ||
"path": "u/admin/exapp_token", | ||
"value": os.environ["APP_SECRET"], | ||
"is_secret": True, | ||
"is_oauth": False, | ||
"description": "ExApp Authentication Variable", | ||
}, | ||
) | ||
if r.status_code >= 400: | ||
LOGGER.critical("Can not create Nextcloud Auth Variable: %s %s", r.status_code, r.text) | ||
return False | ||
return True | ||
|
||
|
||
def update_nextcloud_auth_variable() -> bool: | ||
r = httpx.post( | ||
url="http://127.0.0.1:8000/api/w/nextcloud/variables/update/u/admin/exapp_token", | ||
cookies={"token": USERS_STORAGE["[email protected]"]["token"]}, | ||
json={"value": os.environ["APP_SECRET"]}, | ||
) | ||
if r.status_code >= 400: | ||
LOGGER.critical("Can not update Nextcloud Auth Variable: %s %s", r.status_code, r.text) | ||
return False | ||
return True | ||
|
||
|
||
def create_nextcloud_auth_resource() -> bool: | ||
r = httpx.post( | ||
url="http://127.0.0.1:8000/api/w/nextcloud/resources/create", | ||
cookies={"token": USERS_STORAGE["[email protected]"]["token"]}, | ||
json={ | ||
"path": "u/admin/exapp_resource", | ||
"resource_type": "nextcloud", | ||
"baseUrl": "unknown", | ||
"description": "ExApp Authentication Resource", | ||
"value": {"username": "flow_app", "password": "$var:u/admin/exapp_token"}, | ||
}, | ||
) | ||
if r.status_code >= 400: | ||
LOGGER.critical("Can not create Nextcloud Auth Resource: %s %s", r.status_code, r.text) | ||
return False | ||
return True | ||
|
||
|
||
def create_nextcloud_resource(): | ||
r = httpx.get( | ||
url="http://127.0.0.1:8000/api/w/nextcloud/variables/exists/u/admin/exapp_token", | ||
cookies={"token": USERS_STORAGE["[email protected]"]["token"]}, | ||
) | ||
if r.status_code >= 400: | ||
LOGGER.critical("Can not check for Nextcloud Auth Variable: %s %s", r.status_code, r.text) | ||
return | ||
if r.text.lower() == "false": | ||
LOGGER.info("Creating Nextcloud Auth variable") | ||
if create_nextcloud_auth_variable() is False: | ||
return | ||
else: | ||
r = httpx.get( | ||
url="http://127.0.0.1:8000/api/w/nextcloud/variables/get_value/u/admin/exapp_token", | ||
cookies={"token": USERS_STORAGE["[email protected]"]["token"]}, | ||
) | ||
if r.status_code >= 400: | ||
LOGGER.critical("Can not get Nextcloud Auth Variable value: %s %s", r.status_code, r.text) | ||
return | ||
if r.text != os.environ["APP_SECRET"]: | ||
LOGGER.info("Updating Nextcloud Auth variable") | ||
if update_nextcloud_auth_variable() is False: | ||
return | ||
|
||
r = httpx.get( | ||
url="http://127.0.0.1:8000/api/w/nextcloud/resources/exists/u/admin/exapp_resource", | ||
cookies={"token": USERS_STORAGE["[email protected]"]["token"]}, | ||
) | ||
if r.status_code >= 400: | ||
LOGGER.critical("Can not check for Nextcloud Auth Resource: %s %s", r.status_code, r.text) | ||
if r.text.lower() == "false": | ||
LOGGER.info("Creating Nextcloud Auth Resource") | ||
create_nextcloud_auth_resource() | ||
|
||
|
||
if __name__ == "__main__": | ||
initialize_windmill() | ||
create_nextcloud_resource() | ||
# Current working dir is set for the Service we are wrapping, so change we first for ExApp default one | ||
os.chdir(Path(__file__).parent) | ||
run_app(APP, log_level="info") # Calling wrapper around `uvicorn.run`. |