blob: 1abc6d23aff30117e5b22183074ebb84910e918b [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 Gregorio9b8bec62012-01-17 11:35:32 -050023import os
24import stat
Joe Gregorio560b5322011-02-22 11:09:24 -050025import threading
Joe Gregorio695fdc12011-01-16 16:46:55 -050026
Joe Gregorio549230c2012-01-11 10:38:05 -050027from anyjson import simplejson
Joe Gregoriodeeb0202011-02-15 14:49:57 -050028from client import Storage as BaseStorage
Joe Gregorio562b7312011-09-15 09:06:38 -040029from client import Credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -050030
Joe Gregoriodeeb0202011-02-15 14:49:57 -050031
32class Storage(BaseStorage):
Joe Gregorio695fdc12011-01-16 16:46:55 -050033 """Store and retrieve a single credential to and from a file."""
34
35 def __init__(self, filename):
Joe Gregorio7c22ab22011-02-16 15:32:39 -050036 self._filename = filename
Joe Gregorio560b5322011-02-22 11:09:24 -050037 self._lock = threading.Lock()
Joe Gregorio695fdc12011-01-16 16:46:55 -050038
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040039 def acquire_lock(self):
40 """Acquires any lock necessary to access this Storage.
41
42 This lock is not reentrant."""
43 self._lock.acquire()
44
45 def release_lock(self):
46 """Release the Storage lock.
47
48 Trying to release a lock that isn't held will result in a
49 RuntimeError.
50 """
51 self._lock.release()
52
53 def locked_get(self):
Joe Gregorio695fdc12011-01-16 16:46:55 -050054 """Retrieve Credential from file.
55
56 Returns:
Joe Gregorio7c22ab22011-02-16 15:32:39 -050057 oauth2client.client.Credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -050058 """
Joe Gregorio562b7312011-09-15 09:06:38 -040059 credentials = None
Joe Gregoriodeeb0202011-02-15 14:49:57 -050060 try:
Joe Gregorio9b8bec62012-01-17 11:35:32 -050061 f = open(self._filename, 'rb')
Joe Gregorio562b7312011-09-15 09:06:38 -040062 content = f.read()
Joe Gregoriodeeb0202011-02-15 14:49:57 -050063 f.close()
Joe Gregorio562b7312011-09-15 09:06:38 -040064 except IOError:
Joe Gregorio562b7312011-09-15 09:06:38 -040065 return credentials
66
Joe Gregorio562b7312011-09-15 09:06:38 -040067 try:
68 credentials = Credentials.new_from_json(content)
Joe Gregorio9da2ad82011-09-11 14:04:44 -040069 credentials.set_store(self)
Joe Gregorio562b7312011-09-15 09:06:38 -040070 except ValueError:
Joe Gregorioec555842011-10-27 11:10:39 -040071 pass
Joe Gregorio7c22ab22011-02-16 15:32:39 -050072
Joe Gregorio695fdc12011-01-16 16:46:55 -050073 return credentials
74
Joe Gregorio9b8bec62012-01-17 11:35:32 -050075 def _create_file_if_needed(self):
76 """Create an empty file if necessary.
77
78 This method will not initialize the file. Instead it implements a
79 simple version of "touch" to ensure the file has been created.
80 """
81 if not os.path.exists(self._filename):
82 old_umask = os.umask(0177)
83 try:
84 open(self._filename, 'a+b').close()
85 finally:
86 os.umask(old_umask)
87
Joe Gregoriod2ee4d82011-09-15 14:32:45 -040088 def locked_put(self, credentials):
Joe Gregorio562b7312011-09-15 09:06:38 -040089 """Write Credentials to file.
Joe Gregorio695fdc12011-01-16 16:46:55 -050090
91 Args:
92 credentials: Credentials, the credentials to store.
93 """
Joe Gregorio9b8bec62012-01-17 11:35:32 -050094
95 self._create_file_if_needed()
96 f = open(self._filename, 'wb')
Joe Gregorio562b7312011-09-15 09:06:38 -040097 f.write(credentials.to_json())
Joe Gregorio695fdc12011-01-16 16:46:55 -050098 f.close()
Joe Gregorioec75dc12012-02-06 13:40:42 -050099
100 def locked_delete(self):
101 """Delete Credentials file.
102
103 Args:
104 credentials: Credentials, the credentials to store.
105 """
106
107 os.unlink(self._filename)