Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 1 | # Copyright (C) 2010 Google Inc. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (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 |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 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 | |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 23 | import os |
| 24 | import stat |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 25 | import threading |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 26 | |
Joe Gregorio | 549230c | 2012-01-11 10:38:05 -0500 | [diff] [blame] | 27 | from anyjson import simplejson |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 28 | from client import Storage as BaseStorage |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 29 | from client import Credentials |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 30 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 31 | |
Joe Gregorio | 0fd1853 | 2012-08-24 15:54:40 -0400 | [diff] [blame^] | 32 | class CredentialsFileSymbolicLinkError(Exception): |
| 33 | """Credentials files must not be symbolic links.""" |
| 34 | |
| 35 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 36 | class Storage(BaseStorage): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 37 | """Store and retrieve a single credential to and from a file.""" |
| 38 | |
| 39 | def __init__(self, filename): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 40 | self._filename = filename |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 41 | self._lock = threading.Lock() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 42 | |
Joe Gregorio | 0fd1853 | 2012-08-24 15:54:40 -0400 | [diff] [blame^] | 43 | def _validate_file(self): |
| 44 | if os.path.islink(self._filename): |
| 45 | raise CredentialsFileSymbolicLinkError( |
| 46 | 'File: %s is a symbolic link.' % self._filename) |
| 47 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 48 | def acquire_lock(self): |
| 49 | """Acquires any lock necessary to access this Storage. |
| 50 | |
| 51 | This lock is not reentrant.""" |
| 52 | self._lock.acquire() |
| 53 | |
| 54 | def release_lock(self): |
| 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 | |
| 62 | def locked_get(self): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 63 | """Retrieve Credential from file. |
| 64 | |
| 65 | Returns: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 66 | oauth2client.client.Credentials |
Joe Gregorio | 0fd1853 | 2012-08-24 15:54:40 -0400 | [diff] [blame^] | 67 | |
| 68 | Raises: |
| 69 | CredentialsFileSymbolicLinkError if the file is a symbolic link. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 70 | """ |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 71 | credentials = None |
Joe Gregorio | 0fd1853 | 2012-08-24 15:54:40 -0400 | [diff] [blame^] | 72 | self._validate_file() |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 73 | try: |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 74 | f = open(self._filename, 'rb') |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 75 | content = f.read() |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 76 | f.close() |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 77 | except IOError: |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 78 | return credentials |
| 79 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 80 | try: |
| 81 | credentials = Credentials.new_from_json(content) |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 82 | credentials.set_store(self) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 83 | except ValueError: |
Joe Gregorio | ec55584 | 2011-10-27 11:10:39 -0400 | [diff] [blame] | 84 | pass |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 85 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 86 | return credentials |
| 87 | |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 88 | def _create_file_if_needed(self): |
| 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 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame] | 101 | def locked_put(self, credentials): |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 102 | """Write Credentials to file. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 103 | |
| 104 | Args: |
| 105 | credentials: Credentials, the credentials to store. |
Joe Gregorio | 0fd1853 | 2012-08-24 15:54:40 -0400 | [diff] [blame^] | 106 | |
| 107 | Raises: |
| 108 | CredentialsFileSymbolicLinkError if the file is a symbolic link. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 109 | """ |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 110 | |
| 111 | self._create_file_if_needed() |
Joe Gregorio | 0fd1853 | 2012-08-24 15:54:40 -0400 | [diff] [blame^] | 112 | self._validate_file() |
Joe Gregorio | 9b8bec6 | 2012-01-17 11:35:32 -0500 | [diff] [blame] | 113 | f = open(self._filename, 'wb') |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 114 | f.write(credentials.to_json()) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 115 | f.close() |
Joe Gregorio | ec75dc1 | 2012-02-06 13:40:42 -0500 | [diff] [blame] | 116 | |
| 117 | def locked_delete(self): |
| 118 | """Delete Credentials file. |
| 119 | |
| 120 | Args: |
| 121 | credentials: Credentials, the credentials to store. |
| 122 | """ |
| 123 | |
| 124 | os.unlink(self._filename) |