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 |
| 12 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 13 | from client import Storage as BaseStorage |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 14 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 15 | |
| 16 | class Storage(BaseStorage): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 17 | """Store and retrieve a single credential to and from a file.""" |
| 18 | |
| 19 | def __init__(self, filename): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 20 | self._filename = filename |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 21 | |
| 22 | def get(self): |
| 23 | """Retrieve Credential from file. |
| 24 | |
| 25 | Returns: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 26 | oauth2client.client.Credentials |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 27 | """ |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 28 | try: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 29 | f = open(self._filename, 'r') |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 30 | credentials = pickle.loads(f.read()) |
| 31 | f.close() |
| 32 | credentials.set_store(self.put) |
| 33 | except: |
| 34 | credentials = None |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 35 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 36 | return credentials |
| 37 | |
| 38 | def put(self, credentials): |
| 39 | """Write a pickled Credentials to file. |
| 40 | |
| 41 | Args: |
| 42 | credentials: Credentials, the credentials to store. |
| 43 | """ |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 44 | f = open(self._filename, 'w') |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 45 | f.write(pickle.dumps(credentials)) |
| 46 | f.close() |