Beispiel #1
0
class ServiceAccountStorage(ndb.Model):
    """
    Tracks access tokens in the database. The key is
    based on the scopes, user, and clientid
    """
    credentials = CredentialsNDBProperty()

    @classmethod
    def _get_kind(cls):
        return '_ferris_OAuth2ServiceAccountStorage'
Beispiel #2
0
class Auth(ndb.Model):
    """Store approximately one record, the oauth token for calendar access."""
    credentials = CredentialsNDBProperty('c')

    @classmethod
    def get(cls):
        creds = cls.query().get()
        if creds:
            return creds.credentials
        else:
            return None
Beispiel #3
0
class User(ndb.Model):
    """
    Datastore model to keep all relevant information about a user

    Properties:
        displayName     Name of the user as returned by the Google+ API
        imageUrl        Avatar image of the user as returned by the Google+ API
        verifyToken     Random token generated for each user to check validity of incoming notifications
        credentials     OAuth2 Access and refresh token to be used for requests against the Mirror API
        sources         List of tracked sources
    """

    displayName = ndb.StringProperty()
    imageUrl = ndb.StringProperty()
    verifyToken = ndb.StringProperty()
    credentials = CredentialsNDBProperty()
    sources = ndb.KeyProperty(kind=Source, repeated=True)
Beispiel #4
0
class Account(ndb.Model):
    """ Identifies a user of the service. Keyed by gae user_id """
    # last seen email address
    email = ndb.StringProperty()
    credentials = CredentialsNDBProperty(required=True)

    def user_id(self):
        """ return user_id part of key """
        return self.key.id()

    @classmethod
    def get_key(cls, user_id):
        """ Get Key by given user id """
        return ndb.Key(Account, user_id)

    def _pre_put_hook(self):
        if not self.key.id() and not hasattr(self, 'id'):
            raise Error('no key or id. Provide key or user_id as id')
Beispiel #5
0
class User(ndb.Model):
    """Datastore model to keep all relevant information about a user

    Properties:
        displayName     Name of the user as returned by the Google+ API
        imageUrl        Avatar image of the user as returned by the Google+ API
        verifyToken     Random token generated for each user to check validity of incoming notifications
        credentials     OAuth2 Access and refresh token to be used for requests against the Mirror API
        latitude        Latest recorded latitude of the user
        longitude       Latest recorded longitude of the user
        locationUpdate  DateTime at which the location of the user was last update
        friends         List of Google+ friends id, as returned by the Google+ API
    """

    displayName = ndb.StringProperty()
    imageUrl = ndb.StringProperty()
    verifyToken = ndb.StringProperty()
    credentials = CredentialsNDBProperty()
    latitude = ndb.FloatProperty()
    longitude = ndb.FloatProperty()
    locationUpdate = ndb.DateTimeProperty()
    friends = ndb.StringProperty(repeated=True)
class UserCredentials(Model):

    user = ndb.UserProperty(indexed=True)
    scopes = ndb.StringProperty(repeated=True, indexed=False)
    admin = ndb.BooleanProperty(indexed=True)
    credentials = CredentialsNDBProperty(indexed=False)
    filter_scopes = ndb.ComputedProperty(lambda x: ','.join(sorted(x.scopes)),
                                         indexed=True)
    updated = ndb.DateTimeProperty(indexed=False, auto_now=True)

    def authorize(self, http):
        return self.credentials.authorize(http)

    @classmethod
    def _get_kind(cls):
        return '_ferris_oauth2_user_credentials'

    @classmethod
    def after_get(cls, key, item):
        if item and item.credentials:
            storage = StorageByKeyName(UserCredentials, key.id(),
                                       'credentials')
            item.credentials.set_store(storage)

    @classmethod
    def _get_key(cls, user, scopes, admin):
        scopes_hash = hashlib.sha1(','.join(sorted(scopes))).hexdigest()
        return ndb.Key(
            cls, '%s:%s:%s' % (user, scopes_hash, True if admin else False))

    @classmethod
    def create(cls, user, scopes, credentials, admin):
        key = cls._get_key(user, scopes, admin)
        item = cls(key=key,
                   user=user,
                   scopes=scopes,
                   credentials=credentials,
                   admin=admin)
        item.put()
        return item

    @classmethod
    def find(cls, user=None, scopes=None, admin=False):
        if user and scopes:
            key = cls._get_key(user, scopes, admin)
            x = key.get()
        else:
            q = cls.query()
            if user:
                q = q.filter(cls.user == user)
            if scopes:
                q = q.filter(cls.filter_scopes == ','.join(sorted(scopes)))
            if admin:
                q = q.filter(cls.admin == admin)
            x = q.get()

        if x:
            cls.after_get(x.key, x)
        return x

    @classmethod
    def delete_all(cls, user):
        c = cls.query().filter(user=user)
        for x in c:
            x.key.delete()
Beispiel #7
0
class User(ndb.Model):
    verifyToken = ndb.StringProperty()
    credentials = CredentialsNDBProperty()
class GcpCredentials(ndb.Model):
    credentials = CredentialsNDBProperty()