Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 1 | # Copyright 2010 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | """Utilities for OAuth. |
| 4 | |
| 5 | Utilities for making it easier to work with OAuth 2.0 |
| 6 | credentials. |
| 7 | """ |
| 8 | |
| 9 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 10 | |
| 11 | import pickle |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 12 | import threading |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 13 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 14 | from client import Storage as BaseStorage |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 15 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 16 | |
| 17 | class Storage(BaseStorage): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 18 | """Store and retrieve a single credential to and from a file.""" |
| 19 | |
| 20 | def __init__(self, filename): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 21 | self._filename = filename |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 22 | self._lock = threading.Lock() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 23 | |
| 24 | def get(self): |
| 25 | """Retrieve Credential from file. |
| 26 | |
| 27 | Returns: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 28 | oauth2client.client.Credentials |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 29 | """ |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 30 | self._lock.acquire() |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 31 | try: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 32 | f = open(self._filename, 'r') |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 33 | credentials = pickle.loads(f.read()) |
| 34 | f.close() |
| 35 | credentials.set_store(self.put) |
| 36 | except: |
| 37 | credentials = None |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 38 | self._lock.release() |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 39 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 40 | return credentials |
| 41 | |
| 42 | def put(self, credentials): |
| 43 | """Write a pickled Credentials to file. |
| 44 | |
| 45 | Args: |
| 46 | credentials: Credentials, the credentials to store. |
| 47 | """ |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 48 | self._lock.acquire() |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 49 | f = open(self._filename, 'w') |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 50 | f.write(pickle.dumps(credentials)) |
| 51 | f.close() |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 52 | self._lock.release() |