Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 1 | # 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 Gregorio | 7c22ab2 | 2011-02-16 15:32:39 -0500 | [diff] [blame] | 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 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 22 | |
Joe Gregorio | fc1e278 | 2011-02-15 16:04:03 -0500 | [diff] [blame] | 23 | import oauth2client |
| 24 | import base64 |
| 25 | import pickle |
| 26 | |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 27 | from django.db import models |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 28 | from oauth2client.client import Storage as BaseStorage |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 29 | |
| 30 | class CredentialsField(models.Field): |
| 31 | |
| 32 | __metaclass__ = models.SubfieldBase |
| 33 | |
Joe Gregorio | 7ad707e | 2011-05-03 08:14:24 -0400 | [diff] [blame] | 34 | def db_type(self, connection=None): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 35 | return 'VARCHAR' |
| 36 | |
| 37 | def to_python(self, value): |
Joe Gregorio | fc1e278 | 2011-02-15 16:04:03 -0500 | [diff] [blame] | 38 | if not value: |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 39 | return None |
Joe Gregorio | fc1e278 | 2011-02-15 16:04:03 -0500 | [diff] [blame] | 40 | if isinstance(value, oauth2client.client.Credentials): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 41 | return value |
| 42 | return pickle.loads(base64.b64decode(value)) |
| 43 | |
| 44 | def get_db_prep_value(self, value): |
| 45 | return base64.b64encode(pickle.dumps(value)) |
| 46 | |
| 47 | |
| 48 | class FlowField(models.Field): |
| 49 | |
| 50 | __metaclass__ = models.SubfieldBase |
| 51 | |
Joe Gregorio | 7ad707e | 2011-05-03 08:14:24 -0400 | [diff] [blame] | 52 | def db_type(self, connection=None): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 53 | return 'VARCHAR' |
| 54 | |
| 55 | def to_python(self, value): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 56 | if value is None: |
| 57 | return None |
Joe Gregorio | fc1e278 | 2011-02-15 16:04:03 -0500 | [diff] [blame] | 58 | if isinstance(value, oauth2client.client.Flow): |
Joe Gregorio | 695fdc1 | 2011-01-16 16:46:55 -0500 | [diff] [blame] | 59 | return value |
| 60 | return pickle.loads(base64.b64decode(value)) |
| 61 | |
| 62 | def get_db_prep_value(self, value): |
| 63 | return base64.b64encode(pickle.dumps(value)) |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 64 | |
| 65 | |
| 66 | class Storage(BaseStorage): |
| 67 | """Store and retrieve a single credential to and from |
| 68 | the datastore. |
| 69 | |
| 70 | This Storage helper presumes the Credentials |
| 71 | have been stored as a CredenialsField |
| 72 | on a db model class. |
| 73 | """ |
| 74 | |
| 75 | def __init__(self, model_class, key_name, key_value, property_name): |
| 76 | """Constructor for Storage. |
| 77 | |
| 78 | Args: |
| 79 | model: db.Model, model class |
| 80 | key_name: string, key name for the entity that has the credentials |
| 81 | key_value: string, key value for the entity that has the credentials |
| 82 | property_name: string, name of the property that is an CredentialsProperty |
| 83 | """ |
| 84 | self.model_class = model_class |
| 85 | self.key_name = key_name |
| 86 | self.key_value = key_value |
| 87 | self.property_name = property_name |
| 88 | |
| 89 | def get(self): |
| 90 | """Retrieve Credential from datastore. |
| 91 | |
| 92 | Returns: |
| 93 | oauth2client.Credentials |
| 94 | """ |
Joe Gregorio | fc1e278 | 2011-02-15 16:04:03 -0500 | [diff] [blame] | 95 | credential = None |
| 96 | |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 97 | query = {self.key_name: self.key_value} |
Joe Gregorio | fc1e278 | 2011-02-15 16:04:03 -0500 | [diff] [blame] | 98 | entities = self.model_class.objects.filter(**query) |
| 99 | if len(entities) > 0: |
| 100 | credential = getattr(entities[0], self.property_name) |
| 101 | if credential and hasattr(credential, 'set_store'): |
Joe Gregorio | 9da2ad8 | 2011-09-11 14:04:44 -0400 | [diff] [blame^] | 102 | credential.set_store(self) |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 103 | return credential |
| 104 | |
| 105 | def put(self, credentials): |
| 106 | """Write a Credentials to the datastore. |
| 107 | |
| 108 | Args: |
| 109 | credentials: Credentials, the credentials to store. |
| 110 | """ |
Joe Gregorio | fc1e278 | 2011-02-15 16:04:03 -0500 | [diff] [blame] | 111 | args = {self.key_name: self.key_value} |
| 112 | entity = self.model_class(**args) |
Joe Gregorio | deeb020 | 2011-02-15 14:49:57 -0500 | [diff] [blame] | 113 | setattr(entity, self.property_name, credentials) |
| 114 | entity.save() |