1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """Utilities for OAuth.
16
17 Utilities for making it easier to work with OAuth 2.0
18 credentials.
19 """
20
21 __author__ = 'jcgregorio@google.com (Joe Gregorio)'
22
23 import os
24 import threading
25
26 from oauth2client.client import Credentials
27 from oauth2client.client import Storage as BaseStorage
28
29
31 """Credentials files must not be symbolic links."""
32
33
35 """Store and retrieve a single credential to and from a file."""
36
40
45
47 """Acquires any lock necessary to access this Storage.
48
49 This lock is not reentrant."""
50 self._lock.acquire()
51
53 """Release the Storage lock.
54
55 Trying to release a lock that isn't held will result in a
56 RuntimeError.
57 """
58 self._lock.release()
59
85
87 """Create an empty file if necessary.
88
89 This method will not initialize the file. Instead it implements a
90 simple version of "touch" to ensure the file has been created.
91 """
92 if not os.path.exists(self._filename):
93 old_umask = os.umask(0o177)
94 try:
95 open(self._filename, 'a+b').close()
96 finally:
97 os.umask(old_umask)
98
100 """Write Credentials to file.
101
102 Args:
103 credentials: Credentials, the credentials to store.
104
105 Raises:
106 CredentialsFileSymbolicLinkError if the file is a symbolic link.
107 """
108
109 self._create_file_if_needed()
110 self._validate_file()
111 f = open(self._filename, 'w')
112 f.write(credentials.to_json())
113 f.close()
114
116 """Delete Credentials file.
117
118 Args:
119 credentials: Credentials, the credentials to store.
120 """
121
122 os.unlink(self._filename)
123