Generate the client_id on the client, so we don't need a round trip to get it before we can send arbitrary RPCs.
This changes the Profile/Client relationship; we never needed to be transactional with them, so split them up entirely so we can reparent Clients.
This commit is contained in:
@@ -25,7 +25,8 @@ import utils
|
||||
|
||||
|
||||
# Profile
|
||||
# ↳ Client
|
||||
#
|
||||
# Client (⤴︎ Profile)
|
||||
#
|
||||
# Instance
|
||||
#
|
||||
@@ -73,20 +74,20 @@ class Profile(db.Model):
|
||||
|
||||
|
||||
class Client(db.Model):
|
||||
# parent=Profile
|
||||
|
||||
profile = db.ReferenceProperty(reference_class=Profile)
|
||||
first_seen = db.DateTimeProperty(required=True, auto_now_add=True)
|
||||
|
||||
@classmethod
|
||||
def FromProfile(cls, profile):
|
||||
client = cls(parent=profile)
|
||||
def FromProfile(cls, client_id, profile):
|
||||
client = cls(key_name=client_id, profile=profile)
|
||||
client.put()
|
||||
return client
|
||||
|
||||
@classmethod
|
||||
def FromGoogleUser(cls, google_user):
|
||||
def FromGoogleUser(cls, client_id, google_user):
|
||||
profile = Profile.FromGoogleUser(google_user)
|
||||
return cls.FromProfile(profile)
|
||||
return cls.FromProfile(client_id, profile)
|
||||
|
||||
|
||||
class Instance(db.Model):
|
||||
@@ -308,7 +309,7 @@ class Subscription(db.Model):
|
||||
readable_only_by = (
|
||||
Subject.readable_only_by.get_value_for_datastore(subject))
|
||||
if (readable_only_by and
|
||||
readable_only_by != client.parent_key()):
|
||||
readable_only_by != Client.profile.get_value_for_datastore(client)):
|
||||
raise AccessDenied
|
||||
|
||||
subscriptions = (
|
||||
|
||||
@@ -23,15 +23,16 @@ from cosmopolite.lib import models
|
||||
def _CheckClientAndGoogleUser(client, google_user):
|
||||
if not google_user:
|
||||
# Nothing to check. If there's a user on the profile, it can stay there.
|
||||
return client
|
||||
return
|
||||
|
||||
client_profile_google_user = client.parent().google_user
|
||||
client_profile_google_user = client.profile.google_user
|
||||
if client_profile_google_user:
|
||||
if client_profile_google_user == google_user:
|
||||
return client
|
||||
return
|
||||
else:
|
||||
# Shared computer? Google account wins.
|
||||
return models.Client.FromGoogleUser(google_user)
|
||||
client.profile = models.Profile.FromGoogleUser(google_user)
|
||||
client.put()
|
||||
return
|
||||
|
||||
# User just signed in. Their anonymous profile gets permanently
|
||||
# associated with this Google account.
|
||||
@@ -44,14 +45,17 @@ def _CheckClientAndGoogleUser(client, google_user):
|
||||
# profile.
|
||||
# TODO(flamingcow): Fetch-then-store uniqueness is a race.
|
||||
google_profile = profiles[0]
|
||||
google_profile.MergeFrom(client.parent_key())
|
||||
return models.Client.FromProfile(google_profile)
|
||||
google_profile.MergeFrom(
|
||||
models.Client.profile.get_value_for_datastore(client))
|
||||
client.profile = google_profile
|
||||
client.put()
|
||||
return
|
||||
|
||||
# First time signin.
|
||||
client_profile = client.parent()
|
||||
client_profile = client.profile
|
||||
client_profile.google_user = google_user
|
||||
client_profile.put()
|
||||
return client
|
||||
return
|
||||
|
||||
|
||||
def session_required(handler):
|
||||
@@ -65,21 +69,16 @@ def session_required(handler):
|
||||
|
||||
@functools.wraps(handler)
|
||||
def FindOrCreateSession(self):
|
||||
client_key = auth.ParseKey(self.request_json.get('client_id', None))
|
||||
client_id = self.request_json['client_id']
|
||||
|
||||
# The hunt for a Profile begins.
|
||||
if client_key:
|
||||
self.client = _CheckClientAndGoogleUser(
|
||||
models.Client.get(client_key),
|
||||
self.verified_google_user)
|
||||
self.client = models.Client.get_by_key_name(client_id)
|
||||
|
||||
if self.client:
|
||||
_CheckClientAndGoogleUser(self.client, self.verified_google_user)
|
||||
else:
|
||||
self.client = models.Client.FromGoogleUser(self.verified_google_user)
|
||||
self.client = models.Client.FromGoogleUser(
|
||||
client_id, self.verified_google_user)
|
||||
|
||||
ret = handler(self)
|
||||
if client_key != self.client.key():
|
||||
# Tell the client that this changed
|
||||
ret['client_id'] = auth.Sign(self.client.key())
|
||||
|
||||
return ret
|
||||
return handler(self)
|
||||
|
||||
return FindOrCreateSession
|
||||
|
||||
Reference in New Issue
Block a user