mirror of
https://github.com/ynput/ayon-core.git
synced 2026-01-01 16:34:53 +01:00
use OpenPypeRegistry to set, get and remove ftrack credentials
This commit is contained in:
parent
5a7d19337c
commit
153160f85d
1 changed files with 27 additions and 67 deletions
|
|
@ -1,21 +1,16 @@
|
|||
import os
|
||||
import json
|
||||
import ftrack_api
|
||||
import appdirs
|
||||
import getpass
|
||||
|
||||
try:
|
||||
from urllib.parse import urlparse
|
||||
except ImportError:
|
||||
from urlparse import urlparse
|
||||
|
||||
|
||||
CONFIG_PATH = os.path.normpath(appdirs.user_data_dir("pype-app", "pype"))
|
||||
CREDENTIALS_FILE_NAME = "ftrack_cred.json"
|
||||
CREDENTIALS_PATH = os.path.join(CONFIG_PATH, CREDENTIALS_FILE_NAME)
|
||||
CREDENTIALS_FOLDER = os.path.dirname(CREDENTIALS_PATH)
|
||||
from openpype.lib import OpenPypeSettingsRegistry
|
||||
|
||||
if not os.path.isdir(CREDENTIALS_FOLDER):
|
||||
os.makedirs(CREDENTIALS_FOLDER)
|
||||
USERNAME_KEY = "username"
|
||||
API_KEY_KEY = "api_key"
|
||||
|
||||
|
||||
def get_ftrack_hostname(ftrack_server=None):
|
||||
|
|
@ -34,84 +29,49 @@ def _get_ftrack_secure_key(hostname):
|
|||
|
||||
|
||||
def get_credentials(ftrack_server=None):
|
||||
credentials = {}
|
||||
if not os.path.exists(CREDENTIALS_PATH):
|
||||
with open(CREDENTIALS_PATH, "w") as file:
|
||||
file.write(json.dumps(credentials))
|
||||
file.close()
|
||||
return credentials
|
||||
|
||||
with open(CREDENTIALS_PATH, "r") as file:
|
||||
content = file.read()
|
||||
|
||||
hostname = get_ftrack_hostname(ftrack_server)
|
||||
secure_key = _get_ftrack_secure_key(hostname)
|
||||
|
||||
content_json = json.loads(content or "{}")
|
||||
credentials = content_json.get(hostname) or {}
|
||||
|
||||
return credentials
|
||||
|
||||
|
||||
def save_credentials(ft_user, ft_api_key, ftrack_server=None):
|
||||
hostname = get_ftrack_hostname(ftrack_server)
|
||||
|
||||
with open(CREDENTIALS_PATH, "r") as file:
|
||||
content = file.read()
|
||||
|
||||
content_json = json.loads(content or "{}")
|
||||
|
||||
content_json[hostname] = {
|
||||
"username": ft_user,
|
||||
"api_key": ft_api_key
|
||||
registry = OpenPypeSettingsRegistry(secure_key)
|
||||
return {
|
||||
USERNAME_KEY: registry.get_secure_item(USERNAME_KEY, None),
|
||||
API_KEY_KEY: registry.get_secure_item(API_KEY_KEY, None)
|
||||
}
|
||||
|
||||
# Deprecated keys
|
||||
if "username" in content_json:
|
||||
content_json.pop("username")
|
||||
if "apiKey" in content_json:
|
||||
content_json.pop("apiKey")
|
||||
|
||||
with open(CREDENTIALS_PATH, "w") as file:
|
||||
file.write(json.dumps(content_json, indent=4))
|
||||
|
||||
|
||||
def clear_credentials(ft_user=None, ftrack_server=None):
|
||||
if not ft_user:
|
||||
ft_user = os.environ.get("FTRACK_API_USER")
|
||||
|
||||
if not ft_user:
|
||||
return
|
||||
|
||||
def save_credentials(username, api_key, ftrack_server=None):
|
||||
hostname = get_ftrack_hostname(ftrack_server)
|
||||
with open(CREDENTIALS_PATH, "r") as file:
|
||||
content = file.read()
|
||||
secure_key = _get_ftrack_secure_key(hostname)
|
||||
|
||||
content_json = json.loads(content or "{}")
|
||||
if hostname not in content_json:
|
||||
content_json[hostname] = {}
|
||||
|
||||
content_json.pop(hostname, None)
|
||||
|
||||
with open(CREDENTIALS_PATH, "w") as file:
|
||||
file.write(json.dumps(content_json))
|
||||
registry = OpenPypeSettingsRegistry(secure_key)
|
||||
registry.set_secure_item(USERNAME_KEY, username)
|
||||
registry.set_secure_item(API_KEY_KEY, api_key)
|
||||
|
||||
|
||||
def check_credentials(ft_user, ft_api_key, ftrack_server=None):
|
||||
def clear_credentials(ftrack_server=None):
|
||||
hostname = get_ftrack_hostname(ftrack_server)
|
||||
secure_key = _get_ftrack_secure_key(hostname)
|
||||
|
||||
registry = OpenPypeSettingsRegistry(secure_key)
|
||||
registry.delete_secure_item(USERNAME_KEY)
|
||||
registry.delete_secure_item(API_KEY_KEY)
|
||||
|
||||
|
||||
def check_credentials(username, api_key, ftrack_server=None):
|
||||
if not ftrack_server:
|
||||
ftrack_server = os.environ["FTRACK_SERVER"]
|
||||
|
||||
if not ft_user or not ft_api_key:
|
||||
if not username or not api_key:
|
||||
return False
|
||||
|
||||
try:
|
||||
session = ftrack_api.Session(
|
||||
server_url=ftrack_server,
|
||||
api_key=ft_api_key,
|
||||
api_user=ft_user
|
||||
api_key=api_key,
|
||||
api_user=username
|
||||
)
|
||||
session.close()
|
||||
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue