liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
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
61
62
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
# Copyright 2014 Google Inc. 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.
 
"""Utilities for OAuth.
 
Utilities for making it easier to work with OAuth 2.0
credentials.
"""
 
import os
import threading
 
from oauth2client import client
 
 
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
 
 
class CredentialsFileSymbolicLinkError(Exception):
    """Credentials files must not be symbolic links."""
 
 
class Storage(client.Storage):
    """Store and retrieve a single credential to and from a file."""
 
    def __init__(self, filename):
        super(Storage, self).__init__(lock=threading.Lock())
        self._filename = filename
 
    def _validate_file(self):
        if os.path.islink(self._filename):
            raise CredentialsFileSymbolicLinkError(
                'File: {0} is a symbolic link.'.format(self._filename))
 
    def locked_get(self):
        """Retrieve Credential from file.
 
        Returns:
            oauth2client.client.Credentials
 
        Raises:
            CredentialsFileSymbolicLinkError if the file is a symbolic link.
        """
        credentials = None
        self._validate_file()
        try:
            f = open(self._filename, 'rb')
            content = f.read()
            f.close()
        except IOError:
            return credentials
 
        try:
            credentials = client.Credentials.new_from_json(content)
            credentials.set_store(self)
        except ValueError:
            pass
 
        return credentials
 
    def _create_file_if_needed(self):
        """Create an empty file if necessary.
 
        This method will not initialize the file. Instead it implements a
        simple version of "touch" to ensure the file has been created.
        """
        if not os.path.exists(self._filename):
            old_umask = os.umask(0o177)
            try:
                open(self._filename, 'a+b').close()
            finally:
                os.umask(old_umask)
 
    def locked_put(self, credentials):
        """Write Credentials to file.
 
        Args:
            credentials: Credentials, the credentials to store.
 
        Raises:
            CredentialsFileSymbolicLinkError if the file is a symbolic link.
        """
        self._create_file_if_needed()
        self._validate_file()
        f = open(self._filename, 'w')
        f.write(credentials.to_json())
        f.close()
 
    def locked_delete(self):
        """Delete Credentials file.
 
        Args:
            credentials: Credentials, the credentials to store.
        """
        os.unlink(self._filename)