blob: d20cf6e56aeb09e98773459bc5eb237f5b9baa8b [file] [log] [blame]
Joe Gregorio20a5aa92011-04-01 17:44:25 -04001# 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 Gregorio695fdc12011-01-16 16:46:55 -050014
15"""Utilities for OAuth.
16
17Utilities for making it easier to work with OAuth 2.0
18credentials.
19"""
20
21__author__ = 'jcgregorio@google.com (Joe Gregorio)'
22
Joe Gregorio560b5322011-02-22 11:09:24 -050023import threading
Joe Gregorio695fdc12011-01-16 16:46:55 -050024
Joe Gregorio549230c2012-01-11 10:38:05 -050025from anyjson import simplejson
Joe Gregoriodeeb0202011-02-15 14:49:57 -050026from client import Storage as BaseStorage
Joe Gregorio562b7312011-09-15 09:06:38 -040027from client import Credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -050028
Joe Gregoriodeeb0202011-02-15 14:49:57 -050029
30class Storage(BaseStorage):
Joe Gregorio695fdc12011-01-16 16:46:55 -050031 """Store and retrieve a single credential to and from a file."""
32
33 def __init__(self, filename):
Joe Gregorio7c22ab22011-02-16 15:32:39 -050034 self._filename = filename
Joe Gregorio560b5322011-02-22 11:09:24 -050035 self._lock = threading.Lock()
Joe Gregorio695fdc12011-01-16 16:46:55 -050036
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040037 def acquire_lock(self):
38 """Acquires any lock necessary to access this Storage.
39
40 This lock is not reentrant."""
41 self._lock.acquire()
42
43 def release_lock(self):
44 """Release the Storage lock.
45
46 Trying to release a lock that isn't held will result in a
47 RuntimeError.
48 """
49 self._lock.release()
50
51 def locked_get(self):
Joe Gregorio695fdc12011-01-16 16:46:55 -050052 """Retrieve Credential from file.
53
54 Returns:
Joe Gregorio7c22ab22011-02-16 15:32:39 -050055 oauth2client.client.Credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -050056 """
Joe Gregorio562b7312011-09-15 09:06:38 -040057 credentials = None
Joe Gregoriodeeb0202011-02-15 14:49:57 -050058 try:
Joe Gregorio7c22ab22011-02-16 15:32:39 -050059 f = open(self._filename, 'r')
Joe Gregorio562b7312011-09-15 09:06:38 -040060 content = f.read()
Joe Gregoriodeeb0202011-02-15 14:49:57 -050061 f.close()
Joe Gregorio562b7312011-09-15 09:06:38 -040062 except IOError:
Joe Gregorio562b7312011-09-15 09:06:38 -040063 return credentials
64
Joe Gregorio562b7312011-09-15 09:06:38 -040065 try:
66 credentials = Credentials.new_from_json(content)
Joe Gregorio9da2ad82011-09-11 14:04:44 -040067 credentials.set_store(self)
Joe Gregorio562b7312011-09-15 09:06:38 -040068 except ValueError:
Joe Gregorioec555842011-10-27 11:10:39 -040069 pass
Joe Gregorio7c22ab22011-02-16 15:32:39 -050070
Joe Gregorio695fdc12011-01-16 16:46:55 -050071 return credentials
72
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040073 def locked_put(self, credentials):
Joe Gregorio562b7312011-09-15 09:06:38 -040074 """Write Credentials to file.
Joe Gregorio695fdc12011-01-16 16:46:55 -050075
76 Args:
77 credentials: Credentials, the credentials to store.
78 """
Joe Gregorio7c22ab22011-02-16 15:32:39 -050079 f = open(self._filename, 'w')
Joe Gregorio562b7312011-09-15 09:06:38 -040080 f.write(credentials.to_json())
Joe Gregorio695fdc12011-01-16 16:46:55 -050081 f.close()