{"action":"created","comment":{"url":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/comments/29293502","id":29293502,"diff_hunk":"@@ -0,0 +1,209 @@\n+# Copyright (c) 2015 Shotgun Software Inc.\n+#\n+# CONFIDENTIAL AND PROPRIETARY\n+#\n+# This work is provided \"AS IS\" and subject to the Shotgun Pipeline Toolkit\n+# Source Code License included in this distribution package. See LICENSE.\n+# By accessing, using, copying or modifying this work you indicate your\n+# agreement to the Shotgun Pipeline Toolkit Source Code License. All rights\n+# not expressly granted therein are reserved by Shotgun Software Inc.\n+\n+\"\"\"\n+This module will provide basic i/o to read and write session user's credentials\n+in the site's cache location.\n+\"\"\"\n+\n+import os\n+import sys\n+import urlparse\n+from ConfigParser import SafeConfigParser\n+from tank_vendor.shotgun_api3 import Shotgun, AuthenticationFault, ProtocolError\n+from tank_vendor.shotgun_api3.lib import httplib2\n+from .errors import AuthenticationError\n+\n+# FIXME: Quick hack to easily disable logging in this module while keeping the\n+# code compatible. We have to disable it by default because Maya will print all out\n+# debug strings.\n+if False:\n+    import logging\n+    # Configure logging\n+    logger = logging.getLogger(\"sgtk.session_cache\")\n+    logger.setLevel(logging.DEBUG)\n+    logger.addHandler(logging.StreamHandler())\n+else:\n+    class logger:\n+        @staticmethod\n+        def debug(*args, **kwargs):\n+            pass\n+\n+        @staticmethod\n+        def info(*args, **kwargs):\n+            pass\n+\n+        @staticmethod\n+        def warning(*args, **kwargs):\n+            pass\n+\n+        @staticmethod\n+        def error(*args, **kwargs):\n+            pass\n+\n+        @staticmethod\n+        def exception(*args, **kwargs):\n+            pass\n+\n+\n+def _get_local_site_cache_location(base_url):\n+    \"\"\"\n+    Returns the location of the site cache root based on a site.\n+\n+    :param base_url: Site we need to compute the root path for.\n+\n+    :returns: An absolute path to the site cache root.\n+    \"\"\"\n+    if sys.platform == \"darwin\":\n+        root = os.path.expanduser(\"~/Library/Caches/Shotgun\")\n+    elif sys.platform == \"win32\":\n+        root = os.path.join(os.environ[\"APPDATA\"], \"Shotgun\")\n+    elif sys.platform.startswith(\"linux\"):\n+        root = os.path.expanduser(\"~/.shotgun\")\n+\n+    # get site only; https://www.foo.com:8080 -> www.foo.com\n+    base_url = urlparse.urlparse(base_url)[1].split(\":\")[0]\n+\n+    return os.path.join(root, base_url)\n+\n+\n+def _get_session_data_location(base_url):\n+    \"\"\"\n+    Returns the location of the session file on disk for a specific site.\n+\n+    :param base_url: The site we want the login information for.\n+\n+    :returns: Path to the login information.\n+    \"\"\"\n+    return os.path.join(\n+        _get_local_site_cache_location(base_url),\n+        \"authentication\",\n+        \"login.ini\"\n+    )\n+\n+\n+def delete_session_data(host):\n+    \"\"\"\n+    Clears the session cache for the given site.\n+\n+    :param host: Site to clear the session cache for.\n+    \"\"\"\n+    if not host:\n+        logger.error(\"Current host not set, nothing to clear.\")\n+        return\n+    logger.debug(\"Clearing session cached on disk.\")\n+    try:\n+        info_path = _get_session_data_location(host)\n+        if os.path.exists(info_path):\n+            logger.debug(\"Session file found.\")\n+            os.remove(info_path)\n+            logger.debug(\"Session cleared.\")\n+        else:\n+            logger.debug(\"Session file not found: %s\", info_path)\n+    except:\n+        logger.exception(\"Couldn't delete the session cache file!\")\n+\n+\n+def get_session_data(base_url):\n+    \"\"\"\n+    Returns the cached login info if found.\n+\n+    :param base_url: The site we want the login information for.\n+\n+    :returns: Returns a dictionary with keys login and session_token or None\n+    \"\"\"\n+    # Retrieve the location of the cached info\n+    info_path = _get_session_data_location(base_url)\n+    # Nothing was cached, return an empty dictionary.\n+    if not os.path.exists(info_path):\n+        logger.debug(\"No cache found at %s\" % info_path)\n+        return None\n+    try:\n+        # Read the login information\n+        config = SafeConfigParser({\"login\": None, \"session_token\": None})\n+        config.read(info_path)\n+        if not config.has_section(\"LoginInfo\"):\n+            logger.debug(\"No Login info was found\")\n+            return None\n+\n+        login = config.get(\"LoginInfo\", \"login\", raw=True)\n+        session_token = config.get(\"LoginInfo\", \"session_token\", raw=True)\n+\n+        if not login or not session_token:\n+            logger.debug(\"Incomplete settings (login:%s, session_token:%s)\" % (login, session_token))\n+            return None\n+\n+        return {\"login\": login, \"session_token\": session_token}\n+    except Exception:\n+        logger.exception(\"Exception thrown while loading cached session info.\")\n+        return None\n+\n+\n+def cache_session_data(host, login, session_token):\n+    \"\"\"\n+    Caches the session data for a site and a user.\n+\n+    :param host: Site we want to cache a session for.\n+    :param login: User we want to cache a session for.\n+    :param session_token: Session token we want to cache.\n+    \"\"\"\n+    # Retrieve the cached info file location from the host\n+    info_path = _get_session_data_location(host)\n+\n+    # make sure the info_dir exists!\n+    info_dir, info_file = os.path.split(info_path)\n+    if not os.path.exists(info_dir):\n+        os.makedirs(info_dir, 0700)\n+\n+    logger.debug(\"Caching login info at %s...\", info_path)\n+    # Create a document with the following format:\n+    # [LoginInfo]\n+    # login=username\n+    # session_token=some_unique_id\n+    config = SafeConfigParser()\n+    config.add_section(\"LoginInfo\")\n+    config.set(\"LoginInfo\", \"login\", login)\n+    config.set(\"LoginInfo\", \"session_token\", session_token)\n+    # Write it to disk.\n+    with open(info_path, \"w\") as configfile:\n+        config.write(configfile)\n+    logger.debug(\"Cached!\")\n+\n+\n+def generate_session_token(hostname, login, password, http_proxy):\n+    \"\"\"\n+    Generates a session token for a given username/password on a given site.\n+\n+    :param hostname: The host to connect to.\n+    :param login: The user to get a session for.\n+    :param password: Password for the user.\n+    :param http_proxy: Proxy to use. Can be None.","path":"python/tank_vendor/shotgun_authentication/session_cache.py","position":354,"original_position":187,"commit_id":"6f60843d943361644b23f1b37d5825e44cf26b9a","original_commit_id":"0d80a81c6f78595719553556b93ae12ca085ce25","user":{"login":"manneohrstrom","id":337710,"avatar_url":"https://avatars.githubusercontent.com/u/337710?v=3","gravatar_id":"","url":"https://api.github.com/users/manneohrstrom","html_url":"https://github.com/manneohrstrom","followers_url":"https://api.github.com/users/manneohrstrom/followers","following_url":"https://api.github.com/users/manneohrstrom/following{/other_user}","gists_url":"https://api.github.com/users/manneohrstrom/gists{/gist_id}","starred_url":"https://api.github.com/users/manneohrstrom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/manneohrstrom/subscriptions","organizations_url":"https://api.github.com/users/manneohrstrom/orgs","repos_url":"https://api.github.com/users/manneohrstrom/repos","events_url":"https://api.github.com/users/manneohrstrom/events{/privacy}","received_events_url":"https://api.github.com/users/manneohrstrom/received_events","type":"User","site_admin":false},"body":"makes sense!","created_at":"2015-04-28T22:04:12Z","updated_at":"2015-04-28T22:04:12Z","html_url":"https://github.com/shotgunsoftware/tk-core/pull/147#discussion_r29293502","pull_request_url":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/147","_links":{"self":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/comments/29293502"},"html":{"href":"https://github.com/shotgunsoftware/tk-core/pull/147#discussion_r29293502"},"pull_request":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/147"}}},"pull_request":{"url":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/147","id":33076928,"html_url":"https://github.com/shotgunsoftware/tk-core/pull/147","diff_url":"https://github.com/shotgunsoftware/tk-core/pull/147.diff","patch_url":"https://github.com/shotgunsoftware/tk-core/pull/147.patch","issue_url":"https://api.github.com/repos/shotgunsoftware/tk-core/issues/147","number":147,"state":"open","locked":false,"title":"#27965 - Improved Toolkit Security","user":{"login":"manneohrstrom","id":337710,"avatar_url":"https://avatars.githubusercontent.com/u/337710?v=3","gravatar_id":"","url":"https://api.github.com/users/manneohrstrom","html_url":"https://github.com/manneohrstrom","followers_url":"https://api.github.com/users/manneohrstrom/followers","following_url":"https://api.github.com/users/manneohrstrom/following{/other_user}","gists_url":"https://api.github.com/users/manneohrstrom/gists{/gist_id}","starred_url":"https://api.github.com/users/manneohrstrom/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/manneohrstrom/subscriptions","organizations_url":"https://api.github.com/users/manneohrstrom/orgs","repos_url":"https://api.github.com/users/manneohrstrom/repos","events_url":"https://api.github.com/users/manneohrstrom/events{/privacy}","received_events_url":"https://api.github.com/users/manneohrstrom/received_events","type":"User","site_admin":false},"body":"Creating a pull request for all the security changes, mainly for CR purposes.","created_at":"2015-04-10T19:39:33Z","updated_at":"2015-04-28T22:04:12Z","closed_at":null,"merged_at":null,"merge_commit_sha":"dfb3a2dd13a0f440f1bf4b6af7e96b1cdc24a6dd","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/147/commits","review_comments_url":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/147/comments","review_comment_url":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/comments{/number}","comments_url":"https://api.github.com/repos/shotgunsoftware/tk-core/issues/147/comments","statuses_url":"https://api.github.com/repos/shotgunsoftware/tk-core/statuses/6f60843d943361644b23f1b37d5825e44cf26b9a","head":{"label":"shotgunsoftware:security","ref":"security","sha":"6f60843d943361644b23f1b37d5825e44cf26b9a","user":{"login":"shotgunsoftware","id":317894,"avatar_url":"https://avatars.githubusercontent.com/u/317894?v=3","gravatar_id":"","url":"https://api.github.com/users/shotgunsoftware","html_url":"https://github.com/shotgunsoftware","followers_url":"https://api.github.com/users/shotgunsoftware/followers","following_url":"https://api.github.com/users/shotgunsoftware/following{/other_user}","gists_url":"https://api.github.com/users/shotgunsoftware/gists{/gist_id}","starred_url":"https://api.github.com/users/shotgunsoftware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shotgunsoftware/subscriptions","organizations_url":"https://api.github.com/users/shotgunsoftware/orgs","repos_url":"https://api.github.com/users/shotgunsoftware/repos","events_url":"https://api.github.com/users/shotgunsoftware/events{/privacy}","received_events_url":"https://api.github.com/users/shotgunsoftware/received_events","type":"Organization","site_admin":false},"repo":{"id":4270061,"name":"tk-core","full_name":"shotgunsoftware/tk-core","owner":{"login":"shotgunsoftware","id":317894,"avatar_url":"https://avatars.githubusercontent.com/u/317894?v=3","gravatar_id":"","url":"https://api.github.com/users/shotgunsoftware","html_url":"https://github.com/shotgunsoftware","followers_url":"https://api.github.com/users/shotgunsoftware/followers","following_url":"https://api.github.com/users/shotgunsoftware/following{/other_user}","gists_url":"https://api.github.com/users/shotgunsoftware/gists{/gist_id}","starred_url":"https://api.github.com/users/shotgunsoftware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shotgunsoftware/subscriptions","organizations_url":"https://api.github.com/users/shotgunsoftware/orgs","repos_url":"https://api.github.com/users/shotgunsoftware/repos","events_url":"https://api.github.com/users/shotgunsoftware/events{/privacy}","received_events_url":"https://api.github.com/users/shotgunsoftware/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/shotgunsoftware/tk-core","description":"The Shotgun Pipeline Toolkit Core API","fork":false,"url":"https://api.github.com/repos/shotgunsoftware/tk-core","forks_url":"https://api.github.com/repos/shotgunsoftware/tk-core/forks","keys_url":"https://api.github.com/repos/shotgunsoftware/tk-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shotgunsoftware/tk-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shotgunsoftware/tk-core/teams","hooks_url":"https://api.github.com/repos/shotgunsoftware/tk-core/hooks","issue_events_url":"https://api.github.com/repos/shotgunsoftware/tk-core/issues/events{/number}","events_url":"https://api.github.com/repos/shotgunsoftware/tk-core/events","assignees_url":"https://api.github.com/repos/shotgunsoftware/tk-core/assignees{/user}","branches_url":"https://api.github.com/repos/shotgunsoftware/tk-core/branches{/branch}","tags_url":"https://api.github.com/repos/shotgunsoftware/tk-core/tags","blobs_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shotgunsoftware/tk-core/statuses/{sha}","languages_url":"https://api.github.com/repos/shotgunsoftware/tk-core/languages","stargazers_url":"https://api.github.com/repos/shotgunsoftware/tk-core/stargazers","contributors_url":"https://api.github.com/repos/shotgunsoftware/tk-core/contributors","subscribers_url":"https://api.github.com/repos/shotgunsoftware/tk-core/subscribers","subscription_url":"https://api.github.com/repos/shotgunsoftware/tk-core/subscription","commits_url":"https://api.github.com/repos/shotgunsoftware/tk-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/shotgunsoftware/tk-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/shotgunsoftware/tk-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/shotgunsoftware/tk-core/contents/{+path}","compare_url":"https://api.github.com/repos/shotgunsoftware/tk-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shotgunsoftware/tk-core/merges","archive_url":"https://api.github.com/repos/shotgunsoftware/tk-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shotgunsoftware/tk-core/downloads","issues_url":"https://api.github.com/repos/shotgunsoftware/tk-core/issues{/number}","pulls_url":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls{/number}","milestones_url":"https://api.github.com/repos/shotgunsoftware/tk-core/milestones{/number}","notifications_url":"https://api.github.com/repos/shotgunsoftware/tk-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shotgunsoftware/tk-core/labels{/name}","releases_url":"https://api.github.com/repos/shotgunsoftware/tk-core/releases{/id}","created_at":"2012-05-09T09:12:06Z","updated_at":"2015-04-28T14:27:41Z","pushed_at":"2015-04-28T14:27:47Z","git_url":"git://github.com/shotgunsoftware/tk-core.git","ssh_url":"git@github.com:shotgunsoftware/tk-core.git","clone_url":"https://github.com/shotgunsoftware/tk-core.git","svn_url":"https://github.com/shotgunsoftware/tk-core","homepage":"","size":42101,"stargazers_count":18,"watchers_count":18,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":11,"mirror_url":null,"open_issues_count":10,"forks":11,"open_issues":10,"watchers":18,"default_branch":"master"}},"base":{"label":"shotgunsoftware:master","ref":"master","sha":"607451b44e1a2d4715fd4f184450b20479ba2141","user":{"login":"shotgunsoftware","id":317894,"avatar_url":"https://avatars.githubusercontent.com/u/317894?v=3","gravatar_id":"","url":"https://api.github.com/users/shotgunsoftware","html_url":"https://github.com/shotgunsoftware","followers_url":"https://api.github.com/users/shotgunsoftware/followers","following_url":"https://api.github.com/users/shotgunsoftware/following{/other_user}","gists_url":"https://api.github.com/users/shotgunsoftware/gists{/gist_id}","starred_url":"https://api.github.com/users/shotgunsoftware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shotgunsoftware/subscriptions","organizations_url":"https://api.github.com/users/shotgunsoftware/orgs","repos_url":"https://api.github.com/users/shotgunsoftware/repos","events_url":"https://api.github.com/users/shotgunsoftware/events{/privacy}","received_events_url":"https://api.github.com/users/shotgunsoftware/received_events","type":"Organization","site_admin":false},"repo":{"id":4270061,"name":"tk-core","full_name":"shotgunsoftware/tk-core","owner":{"login":"shotgunsoftware","id":317894,"avatar_url":"https://avatars.githubusercontent.com/u/317894?v=3","gravatar_id":"","url":"https://api.github.com/users/shotgunsoftware","html_url":"https://github.com/shotgunsoftware","followers_url":"https://api.github.com/users/shotgunsoftware/followers","following_url":"https://api.github.com/users/shotgunsoftware/following{/other_user}","gists_url":"https://api.github.com/users/shotgunsoftware/gists{/gist_id}","starred_url":"https://api.github.com/users/shotgunsoftware/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/shotgunsoftware/subscriptions","organizations_url":"https://api.github.com/users/shotgunsoftware/orgs","repos_url":"https://api.github.com/users/shotgunsoftware/repos","events_url":"https://api.github.com/users/shotgunsoftware/events{/privacy}","received_events_url":"https://api.github.com/users/shotgunsoftware/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/shotgunsoftware/tk-core","description":"The Shotgun Pipeline Toolkit Core API","fork":false,"url":"https://api.github.com/repos/shotgunsoftware/tk-core","forks_url":"https://api.github.com/repos/shotgunsoftware/tk-core/forks","keys_url":"https://api.github.com/repos/shotgunsoftware/tk-core/keys{/key_id}","collaborators_url":"https://api.github.com/repos/shotgunsoftware/tk-core/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/shotgunsoftware/tk-core/teams","hooks_url":"https://api.github.com/repos/shotgunsoftware/tk-core/hooks","issue_events_url":"https://api.github.com/repos/shotgunsoftware/tk-core/issues/events{/number}","events_url":"https://api.github.com/repos/shotgunsoftware/tk-core/events","assignees_url":"https://api.github.com/repos/shotgunsoftware/tk-core/assignees{/user}","branches_url":"https://api.github.com/repos/shotgunsoftware/tk-core/branches{/branch}","tags_url":"https://api.github.com/repos/shotgunsoftware/tk-core/tags","blobs_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/refs{/sha}","trees_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/trees{/sha}","statuses_url":"https://api.github.com/repos/shotgunsoftware/tk-core/statuses/{sha}","languages_url":"https://api.github.com/repos/shotgunsoftware/tk-core/languages","stargazers_url":"https://api.github.com/repos/shotgunsoftware/tk-core/stargazers","contributors_url":"https://api.github.com/repos/shotgunsoftware/tk-core/contributors","subscribers_url":"https://api.github.com/repos/shotgunsoftware/tk-core/subscribers","subscription_url":"https://api.github.com/repos/shotgunsoftware/tk-core/subscription","commits_url":"https://api.github.com/repos/shotgunsoftware/tk-core/commits{/sha}","git_commits_url":"https://api.github.com/repos/shotgunsoftware/tk-core/git/commits{/sha}","comments_url":"https://api.github.com/repos/shotgunsoftware/tk-core/comments{/number}","issue_comment_url":"https://api.github.com/repos/shotgunsoftware/tk-core/issues/comments{/number}","contents_url":"https://api.github.com/repos/shotgunsoftware/tk-core/contents/{+path}","compare_url":"https://api.github.com/repos/shotgunsoftware/tk-core/compare/{base}...{head}","merges_url":"https://api.github.com/repos/shotgunsoftware/tk-core/merges","archive_url":"https://api.github.com/repos/shotgunsoftware/tk-core/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/shotgunsoftware/tk-core/downloads","issues_url":"https://api.github.com/repos/shotgunsoftware/tk-core/issues{/number}","pulls_url":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls{/number}","milestones_url":"https://api.github.com/repos/shotgunsoftware/tk-core/milestones{/number}","notifications_url":"https://api.github.com/repos/shotgunsoftware/tk-core/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/shotgunsoftware/tk-core/labels{/name}","releases_url":"https://api.github.com/repos/shotgunsoftware/tk-core/releases{/id}","created_at":"2012-05-09T09:12:06Z","updated_at":"2015-04-28T14:27:41Z","pushed_at":"2015-04-28T14:27:47Z","git_url":"git://github.com/shotgunsoftware/tk-core.git","ssh_url":"git@github.com:shotgunsoftware/tk-core.git","clone_url":"https://github.com/shotgunsoftware/tk-core.git","svn_url":"https://github.com/shotgunsoftware/tk-core","homepage":"","size":42101,"stargazers_count":18,"watchers_count":18,"language":"Python","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":11,"mirror_url":null,"open_issues_count":10,"forks":11,"open_issues":10,"watchers":18,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/147"},"html":{"href":"https://github.com/shotgunsoftware/tk-core/pull/147"},"issue":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/issues/147"},"comments":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/issues/147/comments"},"review_comments":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/147/comments"},"review_comment":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/pulls/147/commits"},"statuses":{"href":"https://api.github.com/repos/shotgunsoftware/tk-core/statuses/6f60843d943361644b23f1b37d5825e44cf26b9a"}}}}