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 | |
| 23 | import pickle |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 24 | import threading |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 25 | |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 26 | |
| 27 | try: # pragma: no cover |
| 28 | import simplejson |
| 29 | except ImportError: # pragma: no cover |
| 30 | try: |
| 31 | # Try to import from django, should work on App Engine |
| 32 | from django.utils import simplejson |
| 33 | except ImportError: |
| 34 | # Should work for Python2.6 and higher. |
| 35 | import json as simplejson |
| 36 | |
| 37 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 38 | from client import Storage as BaseStorage |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 39 | from client import Credentials |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 40 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 41 | |
| 42 | class Storage(BaseStorage): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 43 | """Store and retrieve a single credential to and from a file.""" |
| 44 | |
| 45 | def __init__(self, filename): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 46 | self._filename = filename |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 47 | self._lock = threading.Lock() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 48 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame^] | 49 | def acquire_lock(self): |
| 50 | """Acquires any lock necessary to access this Storage. |
| 51 | |
| 52 | This lock is not reentrant.""" |
| 53 | self._lock.acquire() |
| 54 | |
| 55 | def release_lock(self): |
| 56 | """Release the Storage lock. |
| 57 | |
| 58 | Trying to release a lock that isn't held will result in a |
| 59 | RuntimeError. |
| 60 | """ |
| 61 | self._lock.release() |
| 62 | |
| 63 | def locked_get(self): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 64 | """Retrieve Credential from file. |
| 65 | |
| 66 | Returns: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 67 | oauth2client.client.Credentials |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 68 | """ |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 69 | credentials = None |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 70 | try: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 71 | f = open(self._filename, 'r') |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 72 | content = f.read() |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 73 | f.close() |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 74 | except IOError: |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 75 | return credentials |
| 76 | |
| 77 | # First try reading as JSON, and if that fails fall back to pickle. |
| 78 | try: |
| 79 | credentials = Credentials.new_from_json(content) |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame] | 80 | credentials.set_store(self) |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 81 | except ValueError: |
| 82 | # TODO(jcgregorio) On a future release remove this path to finally remove |
| 83 | # all pickle support. |
| 84 | try: |
| 85 | credentials = pickle.loads(content) |
| 86 | credentials.set_store(self) |
| 87 | except: |
| 88 | pass |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 89 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 90 | return credentials |
| 91 | |
Joe Gregorio | d2ee4d8 | 2011-09-15 14:32:45 -0400 | [diff] [blame^] | 92 | def locked_put(self, credentials): |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 93 | """Write Credentials to file. |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 94 | |
| 95 | Args: |
| 96 | credentials: Credentials, the credentials to store. |
| 97 | """ |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 98 | f = open(self._filename, 'w') |
Joe Gregorio | 562b731 | 2011-09-15 09:06:38 -0400 | [diff] [blame] | 99 | f.write(credentials.to_json()) |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 100 | f.close() |