OLD | NEW |
(Empty) | |
| 1 # Copyright 2014 Google Inc. All rights reserved. |
| 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 """OAuth 2.0 utilities for Django. |
| 16 |
| 17 Utilities for using OAuth 2.0 in conjunction with |
| 18 the Django datastore. |
| 19 """ |
| 20 |
| 21 import oauth2client |
| 22 import base64 |
| 23 import pickle |
| 24 import six |
| 25 |
| 26 from django.db import models |
| 27 from django.utils.encoding import smart_bytes, smart_text |
| 28 from oauth2client.client import Storage as BaseStorage |
| 29 |
| 30 |
| 31 __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 32 |
| 33 |
| 34 class CredentialsField(six.with_metaclass(models.SubfieldBase, models.Field)): |
| 35 |
| 36 def __init__(self, *args, **kwargs): |
| 37 if 'null' not in kwargs: |
| 38 kwargs['null'] = True |
| 39 super(CredentialsField, self).__init__(*args, **kwargs) |
| 40 |
| 41 def get_internal_type(self): |
| 42 return 'TextField' |
| 43 |
| 44 def to_python(self, value): |
| 45 if value is None: |
| 46 return None |
| 47 if isinstance(value, oauth2client.client.Credentials): |
| 48 return value |
| 49 return pickle.loads(base64.b64decode(smart_bytes(value))) |
| 50 |
| 51 def get_prep_value(self, value): |
| 52 if value is None: |
| 53 return None |
| 54 return smart_text(base64.b64encode(pickle.dumps(value))) |
| 55 |
| 56 def value_to_string(self, obj): |
| 57 """Convert the field value from the provided model to a string. |
| 58 |
| 59 Used during model serialization. |
| 60 |
| 61 Args: |
| 62 obj: db.Model, model object |
| 63 |
| 64 Returns: |
| 65 string, the serialized field value |
| 66 """ |
| 67 value = self._get_val_from_obj(obj) |
| 68 return self.get_prep_value(value) |
| 69 |
| 70 |
| 71 class FlowField(six.with_metaclass(models.SubfieldBase, models.Field)): |
| 72 |
| 73 def __init__(self, *args, **kwargs): |
| 74 if 'null' not in kwargs: |
| 75 kwargs['null'] = True |
| 76 super(FlowField, self).__init__(*args, **kwargs) |
| 77 |
| 78 def get_internal_type(self): |
| 79 return 'TextField' |
| 80 |
| 81 def to_python(self, value): |
| 82 if value is None: |
| 83 return None |
| 84 if isinstance(value, oauth2client.client.Flow): |
| 85 return value |
| 86 return pickle.loads(base64.b64decode(value)) |
| 87 |
| 88 def get_prep_value(self, value): |
| 89 if value is None: |
| 90 return None |
| 91 return smart_text(base64.b64encode(pickle.dumps(value))) |
| 92 |
| 93 def value_to_string(self, obj): |
| 94 """Convert the field value from the provided model to a string. |
| 95 |
| 96 Used during model serialization. |
| 97 |
| 98 Args: |
| 99 obj: db.Model, model object |
| 100 |
| 101 Returns: |
| 102 string, the serialized field value |
| 103 """ |
| 104 value = self._get_val_from_obj(obj) |
| 105 return self.get_prep_value(value) |
| 106 |
| 107 |
| 108 class Storage(BaseStorage): |
| 109 """Store and retrieve a single credential to and from the Django datastore. |
| 110 |
| 111 This Storage helper presumes the Credentials |
| 112 have been stored as a CredenialsField |
| 113 on a db model class. |
| 114 """ |
| 115 |
| 116 def __init__(self, model_class, key_name, key_value, property_name): |
| 117 """Constructor for Storage. |
| 118 |
| 119 Args: |
| 120 model: db.Model, model class |
| 121 key_name: string, key name for the entity that has the credentials |
| 122 key_value: string, key value for the entity that has the |
| 123 credentials |
| 124 property_name: string, name of the property that is an |
| 125 CredentialsProperty |
| 126 """ |
| 127 self.model_class = model_class |
| 128 self.key_name = key_name |
| 129 self.key_value = key_value |
| 130 self.property_name = property_name |
| 131 |
| 132 def locked_get(self): |
| 133 """Retrieve stored credential. |
| 134 |
| 135 Returns: |
| 136 oauth2client.Credentials |
| 137 """ |
| 138 credential = None |
| 139 |
| 140 query = {self.key_name: self.key_value} |
| 141 entities = self.model_class.objects.filter(**query) |
| 142 if len(entities) > 0: |
| 143 credential = getattr(entities[0], self.property_name) |
| 144 if credential and hasattr(credential, 'set_store'): |
| 145 credential.set_store(self) |
| 146 return credential |
| 147 |
| 148 def locked_put(self, credentials, overwrite=False): |
| 149 """Write a Credentials to the Django datastore. |
| 150 |
| 151 Args: |
| 152 credentials: Credentials, the credentials to store. |
| 153 overwrite: Boolean, indicates whether you would like these |
| 154 credentials to overwrite any existing stored |
| 155 credentials. |
| 156 """ |
| 157 args = {self.key_name: self.key_value} |
| 158 |
| 159 if overwrite: |
| 160 (entity, |
| 161 unused_is_new) = self.model_class.objects.get_or_create(**args) |
| 162 else: |
| 163 entity = self.model_class(**args) |
| 164 |
| 165 setattr(entity, self.property_name, credentials) |
| 166 entity.save() |
| 167 |
| 168 def locked_delete(self): |
| 169 """Delete Credentials from the datastore.""" |
| 170 |
| 171 query = {self.key_name: self.key_value} |
| 172 entities = self.model_class.objects.filter(**query).delete() |
OLD | NEW |