blob: 89140b899e6594482182ef6434d4c3434651a532 [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
23import pickle
Joe Gregorio560b5322011-02-22 11:09:24 -050024import threading
Joe Gregorio695fdc12011-01-16 16:46:55 -050025
Joe Gregorio562b7312011-09-15 09:06:38 -040026
27try: # pragma: no cover
28 import simplejson
29except ImportError: # pragma: no cover
30 try:
31 # Try to import from django, should work on App Engine
32 from django.utils import simplejson
33 except ImportError:
34 # Should work for Python2.6 and higher.
35 import json as simplejson
36
37
Joe Gregoriodeeb0202011-02-15 14:49:57 -050038from client import Storage as BaseStorage
Joe Gregorio562b7312011-09-15 09:06:38 -040039from client import Credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -050040
Joe Gregoriodeeb0202011-02-15 14:49:57 -050041
42class Storage(BaseStorage):
Joe Gregorio695fdc12011-01-16 16:46:55 -050043 """Store and retrieve a single credential to and from a file."""
44
45 def __init__(self, filename):
Joe Gregorio7c22ab22011-02-16 15:32:39 -050046 self._filename = filename
Joe Gregorio560b5322011-02-22 11:09:24 -050047 self._lock = threading.Lock()
Joe Gregorio695fdc12011-01-16 16:46:55 -050048
49 def get(self):
50 """Retrieve Credential from file.
51
52 Returns:
Joe Gregorio7c22ab22011-02-16 15:32:39 -050053 oauth2client.client.Credentials
Joe Gregorio695fdc12011-01-16 16:46:55 -050054 """
Joe Gregorio560b5322011-02-22 11:09:24 -050055 self._lock.acquire()
Joe Gregorio562b7312011-09-15 09:06:38 -040056 credentials = None
Joe Gregoriodeeb0202011-02-15 14:49:57 -050057 try:
Joe Gregorio7c22ab22011-02-16 15:32:39 -050058 f = open(self._filename, 'r')
Joe Gregorio562b7312011-09-15 09:06:38 -040059 content = f.read()
Joe Gregoriodeeb0202011-02-15 14:49:57 -050060 f.close()
Joe Gregorio562b7312011-09-15 09:06:38 -040061 except IOError:
62 self._lock.release()
63 return credentials
64
65 # First try reading as JSON, and if that fails fall back to pickle.
66 try:
67 credentials = Credentials.new_from_json(content)
Joe Gregorio9da2ad82011-09-11 14:04:44 -040068 credentials.set_store(self)
Joe Gregorio562b7312011-09-15 09:06:38 -040069 except ValueError:
70 # TODO(jcgregorio) On a future release remove this path to finally remove
71 # all pickle support.
72 try:
73 credentials = pickle.loads(content)
74 credentials.set_store(self)
75 except:
76 pass
77 finally:
78 self._lock.release()
Joe Gregorio7c22ab22011-02-16 15:32:39 -050079
Joe Gregorio695fdc12011-01-16 16:46:55 -050080 return credentials
81
82 def put(self, credentials):
Joe Gregorio562b7312011-09-15 09:06:38 -040083 """Write Credentials to file.
Joe Gregorio695fdc12011-01-16 16:46:55 -050084
85 Args:
86 credentials: Credentials, the credentials to store.
87 """
Joe Gregorio560b5322011-02-22 11:09:24 -050088 self._lock.acquire()
Joe Gregorio7c22ab22011-02-16 15:32:39 -050089 f = open(self._filename, 'w')
Joe Gregorio562b7312011-09-15 09:06:38 -040090 f.write(credentials.to_json())
Joe Gregorio695fdc12011-01-16 16:46:55 -050091 f.close()
Joe Gregorio560b5322011-02-22 11:09:24 -050092 self._lock.release()