blob: f35da8f99d7ccf8d03a4e90ed51e5c172bab366c [file] [log] [blame]
Joe Gregorio695fdc12011-01-16 16:46:55 -05001# 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.
14
15"""Utilities for Google App Engine
16
17Utilities for making it easier to use OAuth 2.0
18on Google App Engine.
19"""
20
21__author__ = 'jcgregorio@google.com (Joe Gregorio)'
22
23import pickle
24
25from google.appengine.ext import db
26from client import Credentials
27from client import Flow
Joe Gregoriodeeb0202011-02-15 14:49:57 -050028from client import Storage
Joe Gregorio695fdc12011-01-16 16:46:55 -050029
30
31class FlowProperty(db.Property):
32 """Utility property that allows easy
33 storage and retreival of an
34 oauth2client.Flow"""
35
36 # Tell what the user type is.
37 data_type = Flow
38
39 # For writing to datastore.
40 def get_value_for_datastore(self, model_instance):
41 flow = super(FlowProperty,
42 self).get_value_for_datastore(model_instance)
43 return db.Blob(pickle.dumps(flow))
44
45 # For reading from datastore.
46 def make_value_from_datastore(self, value):
47 if value is None:
48 return None
49 return pickle.loads(value)
50
51 def validate(self, value):
52 if value is not None and not isinstance(value, Flow):
53 raise BadValueError('Property %s must be convertible '
54 'to a FlowThreeLegged instance (%s)' %
55 (self.name, value))
56 return super(FlowProperty, self).validate(value)
57
58 def empty(self, value):
59 return not value
60
61
62class CredentialsProperty(db.Property):
63 """Utility property that allows easy
64 storage and retrieval of
65 oath2client.Credentials
66 """
67
68 # Tell what the user type is.
69 data_type = Credentials
70
71 # For writing to datastore.
72 def get_value_for_datastore(self, model_instance):
73 cred = super(CredentialsProperty,
74 self).get_value_for_datastore(model_instance)
75 return db.Blob(pickle.dumps(cred))
76
77 # For reading from datastore.
78 def make_value_from_datastore(self, value):
79 if value is None:
80 return None
81 return pickle.loads(value)
82
83 def validate(self, value):
84 if value is not None and not isinstance(value, Credentials):
85 raise BadValueError('Property %s must be convertible '
86 'to an Credentials instance (%s)' %
87 (self.name, value))
88 return super(CredentialsProperty, self).validate(value)
89
90 def empty(self, value):
91 return not value
92
93
Joe Gregoriodeeb0202011-02-15 14:49:57 -050094class StorageByKeyName(Storage):
Joe Gregorio695fdc12011-01-16 16:46:55 -050095 """Store and retrieve a single credential to and from
96 the App Engine datastore.
97
98 This Storage helper presumes the Credentials
99 have been stored as a CredenialsProperty
100 on a datastore model class, and that entities
101 are stored by key_name.
102 """
103
104 def __init__(self, model, key_name, property_name):
105 """Constructor for Storage.
106
107 Args:
108 model: db.Model, model class
109 key_name: string, key name for the entity that has the credentials
110 property_name: string, name of the property that is an CredentialsProperty
111 """
112 self.model = model
113 self.key_name = key_name
114 self.property_name = property_name
115
116 def get(self):
117 """Retrieve Credential from datastore.
118
119 Returns:
120 oauth2client.Credentials
121 """
122 entity = self.model.get_or_insert(self.key_name)
123 credential = getattr(entity, self.property_name)
124 if credential and hasattr(credential, 'set_store'):
125 credential.set_store(self.put)
126 return credential
127
128 def put(self, credentials):
129 """Write a Credentials to the datastore.
130
131 Args:
132 credentials: Credentials, the credentials to store.
133 """
134 entity = self.model.get_or_insert(self.key_name)
135 setattr(entity, self.property_name, credentials)
136 entity.put()