| OLD | NEW |
| 1 # Copyright 2014 Google Inc. All rights reserved. | 1 # Copyright (C) 2010 Google Inc. |
| 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 |
| 24 import threading | 25 import threading |
| 25 | 26 |
| 27 from anyjson import simplejson |
| 28 from client import Storage as BaseStorage |
| 26 from client import Credentials | 29 from client import Credentials |
| 27 from client import Storage as BaseStorage | |
| 28 | 30 |
| 29 | 31 |
| 30 class CredentialsFileSymbolicLinkError(Exception): | 32 class CredentialsFileSymbolicLinkError(Exception): |
| 31 """Credentials files must not be symbolic links.""" | 33 """Credentials files must not be symbolic links.""" |
| 32 | 34 |
| 33 | 35 |
| 34 class Storage(BaseStorage): | 36 class Storage(BaseStorage): |
| 35 """Store and retrieve a single credential to and from a file.""" | 37 """Store and retrieve a single credential to and from a file.""" |
| 36 | 38 |
| 37 def __init__(self, filename): | 39 def __init__(self, filename): |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 85 |
| 84 return credentials | 86 return credentials |
| 85 | 87 |
| 86 def _create_file_if_needed(self): | 88 def _create_file_if_needed(self): |
| 87 """Create an empty file if necessary. | 89 """Create an empty file if necessary. |
| 88 | 90 |
| 89 This method will not initialize the file. Instead it implements a | 91 This method will not initialize the file. Instead it implements a |
| 90 simple version of "touch" to ensure the file has been created. | 92 simple version of "touch" to ensure the file has been created. |
| 91 """ | 93 """ |
| 92 if not os.path.exists(self._filename): | 94 if not os.path.exists(self._filename): |
| 93 old_umask = os.umask(0o177) | 95 old_umask = os.umask(0177) |
| 94 try: | 96 try: |
| 95 open(self._filename, 'a+b').close() | 97 open(self._filename, 'a+b').close() |
| 96 finally: | 98 finally: |
| 97 os.umask(old_umask) | 99 os.umask(old_umask) |
| 98 | 100 |
| 99 def locked_put(self, credentials): | 101 def locked_put(self, credentials): |
| 100 """Write Credentials to file. | 102 """Write Credentials to file. |
| 101 | 103 |
| 102 Args: | 104 Args: |
| 103 credentials: Credentials, the credentials to store. | 105 credentials: Credentials, the credentials to store. |
| 104 | 106 |
| 105 Raises: | 107 Raises: |
| 106 CredentialsFileSymbolicLinkError if the file is a symbolic link. | 108 CredentialsFileSymbolicLinkError if the file is a symbolic link. |
| 107 """ | 109 """ |
| 108 | 110 |
| 109 self._create_file_if_needed() | 111 self._create_file_if_needed() |
| 110 self._validate_file() | 112 self._validate_file() |
| 111 f = open(self._filename, 'w') | 113 f = open(self._filename, 'wb') |
| 112 f.write(credentials.to_json()) | 114 f.write(credentials.to_json()) |
| 113 f.close() | 115 f.close() |
| 114 | 116 |
| 115 def locked_delete(self): | 117 def locked_delete(self): |
| 116 """Delete Credentials file. | 118 """Delete Credentials file. |
| 117 | 119 |
| 118 Args: | 120 Args: |
| 119 credentials: Credentials, the credentials to store. | 121 credentials: Credentials, the credentials to store. |
| 120 """ | 122 """ |
| 121 | 123 |
| 122 os.unlink(self._filename) | 124 os.unlink(self._filename) |
| OLD | NEW |