Skip to content

credentials

check_user_permissions(shotgrid_url, username, password=None, api_key=None, script_name=None, proxy=None)

Check if the provided user can access the Shotgrid API.

Parameters:

Name Type Description Default
shotgrid_url str

The Shotgun server URL.

required
username str

The Shotgrid username to use the Session as.

required
password Optional[str]

The Shotgrid password to use the Session as.

None
api_key Optional[str]

The Shotgrid API key to use the Session as.

None
script_name Optional[str]

The Shotgrid API script name to use the Session as.

None
proxy Optional[str]

The proxy to use for the connection.

None

Returns:

Name Type Description
tuple (bool, str)

Whether the connection was successful or not, and a string message with the result.

Source code in client/ayon_shotgrid/lib/credentials.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def check_user_permissions(
    shotgrid_url,
    username,
    password: str = None,
    api_key: str = None,
    script_name: str = None,
    proxy: str = None,
):
    """Check if the provided user can access the Shotgrid API.

    Args:
        shotgrid_url (str): The Shotgun server URL.
        username (str): The Shotgrid username to use the Session as.
        password (Optional[str]): The Shotgrid password to use the Session as.
        api_key (Optional[str]): The Shotgrid API key to use the Session as.
        script_name (Optional[str]): The Shotgrid API script name to use the
            Session as.
        proxy (Optional[str]): The proxy to use for the connection.

    Returns:
        tuple(bool, str): Whether the connection was successful or not, and a
            string message with the result.
     """

    if not any([shotgrid_url, username]):
        return (False, "Missing a field.")

    kwargs = {}

    if api_key:
        if not script_name:
            return (
                False,
                (
                    "'script_name' input arg should be used in "
                    "combination with 'api_key'."
                )
            )
        kwargs.update({
            "api_key": api_key,
            "script_name": script_name,
        })
    if password:
        kwargs["password"] = password
    if proxy:
        kwargs["proxy"] = proxy

    try:
        session = create_sg_session(shotgrid_url, username, **kwargs)
        session.close()
    except AuthenticationFault as e:
        return (False, str(e))

    return (True, "Successfully logged in.")

clear_local_login()

Clear the Shotgrid Login entry from the local registry.

Source code in client/ayon_shotgrid/lib/credentials.py
144
145
146
147
148
149
150
151
def clear_local_login():
    """Clear the Shotgrid Login entry from the local registry. """
    reg = AYONSecureRegistry("shotgrid/user")
    if reg.get_item("value", None) is not None:
        reg.delete_item("value")
    reg = AYONSecureRegistry("shotgrid/pass")
    if reg.get_item("value", None) is not None:
        reg.delete_item("value")

create_sg_session(shotgrid_url, username, password=None, api_key=None, script_name=None, proxy=None)

Attempt to create a Shotgun Session

Parameters:

Name Type Description Default
shotgrid_url str

The Shotgun server URL.

required
username str

The Shotgrid username to use the Session as.

required
password Optional[str]

The Shotgrid password to use the Session as.

None
api_key Optional[str]

The Shotgrid API key to use the Session as.

None
script_name Optional[str]

The Shotgrid API script name to use the Session as.

None
proxy Optional[str]

The proxy to use for the connection.

None

Returns:

Name Type Description
session Shotgun

A Shotgrid API Session.

Raises:

Type Description
AuthenticationFault

If the authentication with Shotgrid fails.

Source code in client/ayon_shotgrid/lib/credentials.py
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def create_sg_session(
    shotgrid_url,
    username,
    password: str = None,
    api_key: str = None,
    script_name: str = None,
    proxy: str = None,
):
    """Attempt to create a Shotgun Session

    Args:
        shotgrid_url (str): The Shotgun server URL.
        username (str): The Shotgrid username to use the Session as.
        password (Optional[str]): The Shotgrid password to use the Session as.
        api_key (Optional[str]): The Shotgrid API key to use the Session as.
        script_name (Optional[str]): The Shotgrid API script name to use the
            Session as.
        proxy (Optional[str]): The proxy to use for the connection.

    Returns:
        session (shotgun_api3.Shotgun): A Shotgrid API Session.

    Raises:
        AuthenticationFault: If the authentication with Shotgrid fails.
    """
    if not any([shotgrid_url, username]):
        return (False, "Missing a field.")

    kwargs = {
        "base_url": shotgrid_url
    }

    if api_key:
        if not script_name:
            return (
                False,
                (
                    "'script_name' input arg should be used in "
                    "combination with 'api_key'."
                )
            )
        kwargs.update({
            "api_key": api_key,
            "script_name": script_name,
            "sudo_as_login": username,
        })
    if password:
        kwargs.update({
            "password": password,
            "login": username,
        })
    if proxy:
        kwargs["http_proxy"] = proxy

    session = shotgun_api3.Shotgun(**kwargs)

    session.preferences_read()

    return session

get_local_login()

Get the Shotgrid Login entry from the local registry.

Source code in client/ayon_shotgrid/lib/credentials.py
124
125
126
127
128
129
130
131
132
133
def get_local_login():
    """Get the Shotgrid Login entry from the local registry. """
    try:
        reg = AYONSecureRegistry("shotgrid/user")
        username = reg.get_item("value")
        reg = AYONSecureRegistry("shotgrid/pass")
        password = reg.get_item("value")
        return username, password
    except Exception:
        return (None, None)

save_local_login(username, password)

Save the Shotgrid Login entry from the local registry.

Source code in client/ayon_shotgrid/lib/credentials.py
136
137
138
139
140
141
def save_local_login(username, password):
    """Save the Shotgrid Login entry from the local registry. """
    reg = AYONSecureRegistry("shotgrid/user")
    reg.set_item("value", username)
    reg = AYONSecureRegistry("shotgrid/pass")
    reg.set_item("value", password or "")