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 | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 26 | from client import Storage as BaseStorage |
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 | |
| 29 | class Storage(BaseStorage): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 30 | """Store and retrieve a single credential to and from a file.""" |
| 31 | |
| 32 | def __init__(self, filename): |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 33 | self._filename = filename |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 34 | self._lock = threading.Lock() |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 35 | |
| 36 | def get(self): |
| 37 | """Retrieve Credential from file. |
| 38 | |
| 39 | Returns: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 40 | oauth2client.client.Credentials |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 41 | """ |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 42 | self._lock.acquire() |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 43 | try: |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 44 | f = open(self._filename, 'r') |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 45 | credentials = pickle.loads(f.read()) |
| 46 | f.close() |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame^] | 47 | credentials.set_store(self) |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 48 | except: |
| 49 | credentials = None |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 50 | self._lock.release() |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 51 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 52 | return credentials |
| 53 | |
| 54 | def put(self, credentials): |
| 55 | """Write a pickled Credentials to file. |
| 56 | |
| 57 | Args: |
| 58 | credentials: Credentials, the credentials to store. |
| 59 | """ |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 60 | self._lock.acquire() |
Joe Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 61 | f = open(self._filename, 'w') |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 62 | f.write(pickle.dumps(credentials)) |
| 63 | f.close() |
Joe Gregorio | 560b532 | 2011-02-22 11:09:24 -0500 | [diff] [blame] | 64 | self._lock.release() |