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 stat
25 import threading
26
27 from anyjson import simplejson
28 from client import Storage as BaseStorage
29 from client import Credentials
30
31
33 """Credentials files must not be symbolic links."""
34
35
37 """Store and retrieve a single credential to and from a file."""
38
42
47
49 """Acquires any lock necessary to access this Storage.
50
51 This lock is not reentrant."""
52 self._lock.acquire()
53
55 """Release the Storage lock.
56
57 Trying to release a lock that isn't held will result in a
58 RuntimeError.
59 """
60 self._lock.release()
61
87
89 """Create an empty file if necessary.
90
91 This method will not initialize the file. Instead it implements a
92 simple version of "touch" to ensure the file has been created.
93 """
94 if not os.path.exists(self._filename):
95 old_umask = os.umask(0177)
96 try:
97 open(self._filename, 'a+b').close()
98 finally:
99 os.umask(old_umask)
100
102 """Write Credentials to file.
103
104 Args:
105 credentials: Credentials, the credentials to store.
106
107 Raises:
108 CredentialsFileSymbolicLinkError if the file is a symbolic link.
109 """
110
111 self._create_file_if_needed()
112 self._validate_file()
113 f = open(self._filename, 'wb')
114 f.write(credentials.to_json())
115 f.close()
116
118 """Delete Credentials file.
119
120 Args:
121 credentials: Credentials, the credentials to store.
122 """
123
124 os.unlink(self._filename)
125