| OLD | NEW |
| 1 # Copyright (C) 2010 Google Inc. | 1 # Copyright 2014 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Licensed under the Apache License, Version 2.0 (the "License"); | 3 # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 # you may not use this file except in compliance with the License. | 4 # you may not use this file except in compliance with the License. |
| 5 # You may obtain a copy of the License at | 5 # You may obtain a copy of the License at |
| 6 # | 6 # |
| 7 # http://www.apache.org/licenses/LICENSE-2.0 | 7 # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 # | 8 # |
| 9 # Unless required by applicable law or agreed to in writing, software | 9 # Unless required by applicable law or agreed to in writing, software |
| 10 # distributed under the License is distributed on an "AS IS" BASIS, | 10 # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 # See the License for the specific language governing permissions and | 12 # See the License for the specific language governing permissions and |
| 13 # limitations under the License. | 13 # limitations under the License. |
| 14 | 14 |
| 15 """Utilities for OAuth. | 15 """Utilities for OAuth. |
| 16 | 16 |
| 17 Utilities for making it easier to work with OAuth 2.0 | 17 Utilities for making it easier to work with OAuth 2.0 |
| 18 credentials. | 18 credentials. |
| 19 """ | 19 """ |
| 20 | 20 |
| 21 __author__ = 'jcgregorio@google.com (Joe Gregorio)' | 21 __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 22 | 22 |
| 23 import os | 23 import os |
| 24 import stat | |
| 25 import threading | 24 import threading |
| 26 | 25 |
| 27 from anyjson import simplejson | 26 from client import Credentials |
| 28 from client import Storage as BaseStorage | 27 from client import Storage as BaseStorage |
| 29 from client import Credentials | |
| 30 | 28 |
| 31 | 29 |
| 32 class CredentialsFileSymbolicLinkError(Exception): | 30 class CredentialsFileSymbolicLinkError(Exception): |
| 33 """Credentials files must not be symbolic links.""" | 31 """Credentials files must not be symbolic links.""" |
| 34 | 32 |
| 35 | 33 |
| 36 class Storage(BaseStorage): | 34 class Storage(BaseStorage): |
| 37 """Store and retrieve a single credential to and from a file.""" | 35 """Store and retrieve a single credential to and from a file.""" |
| 38 | 36 |
| 39 def __init__(self, filename): | 37 def __init__(self, filename): |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 | 83 |
| 86 return credentials | 84 return credentials |
| 87 | 85 |
| 88 def _create_file_if_needed(self): | 86 def _create_file_if_needed(self): |
| 89 """Create an empty file if necessary. | 87 """Create an empty file if necessary. |
| 90 | 88 |
| 91 This method will not initialize the file. Instead it implements a | 89 This method will not initialize the file. Instead it implements a |
| 92 simple version of "touch" to ensure the file has been created. | 90 simple version of "touch" to ensure the file has been created. |
| 93 """ | 91 """ |
| 94 if not os.path.exists(self._filename): | 92 if not os.path.exists(self._filename): |
| 95 old_umask = os.umask(0177) | 93 old_umask = os.umask(0o177) |
| 96 try: | 94 try: |
| 97 open(self._filename, 'a+b').close() | 95 open(self._filename, 'a+b').close() |
| 98 finally: | 96 finally: |
| 99 os.umask(old_umask) | 97 os.umask(old_umask) |
| 100 | 98 |
| 101 def locked_put(self, credentials): | 99 def locked_put(self, credentials): |
| 102 """Write Credentials to file. | 100 """Write Credentials to file. |
| 103 | 101 |
| 104 Args: | 102 Args: |
| 105 credentials: Credentials, the credentials to store. | 103 credentials: Credentials, the credentials to store. |
| 106 | 104 |
| 107 Raises: | 105 Raises: |
| 108 CredentialsFileSymbolicLinkError if the file is a symbolic link. | 106 CredentialsFileSymbolicLinkError if the file is a symbolic link. |
| 109 """ | 107 """ |
| 110 | 108 |
| 111 self._create_file_if_needed() | 109 self._create_file_if_needed() |
| 112 self._validate_file() | 110 self._validate_file() |
| 113 f = open(self._filename, 'wb') | 111 f = open(self._filename, 'w') |
| 114 f.write(credentials.to_json()) | 112 f.write(credentials.to_json()) |
| 115 f.close() | 113 f.close() |
| 116 | 114 |
| 117 def locked_delete(self): | 115 def locked_delete(self): |
| 118 """Delete Credentials file. | 116 """Delete Credentials file. |
| 119 | 117 |
| 120 Args: | 118 Args: |
| 121 credentials: Credentials, the credentials to store. | 119 credentials: Credentials, the credentials to store. |
| 122 """ | 120 """ |
| 123 | 121 |
| 124 os.unlink(self._filename) | 122 os.unlink(self._filename) |
| OLD | NEW |