예제 #1
0
 def initialize(self, request, response):
     """ Authenticate through Google account.
     """
     webapp.RequestHandler.initialize(self, request, response)
     from google.appengine.api import users
     user = users.get_current_user()
     if not user:
         logging.debug(
             'GoogleRequest.initialize: not login, redirect to /gg')
         self.redirect(users.create_login_url("/gg/home"))
         self.get = (lambda *args: None)
         self.post = (lambda *args: None)
     else:
         #user logged in google account,check our cookie
         sns = 'gg'  #Google: how to make use of GMail contact, chat etc? via OAuth
         uid = '%s_%s' % (sns, user.user_id())
         logging.debug(
             'GoogleRequest.initialize: %s visit via Google, try login' %
             uid)
         su = helper.from_cache(uid)
         if not su:
             su = helper.get_user_by_key(uid, False)  #no memcache
             if su is None:
                 logging.debug(
                     'GoogleRequest.initialize: New user, try create')
                 em = user.email()
                 name = em[:em.find('@')]
                 su = helper.create_user(uid, name,
                                         em)  #auto cached if successful
                 if su is None:
                     logging.error(
                         'GoogleRequest.initialize: create_user(%s,%s,%s) failed'
                         % (uid, name, em))
                     self.response.out.write(
                         'Server in maintenance, please come back later. Thank you.'
                     )
                     self.get = self.post = (
                         lambda *args: None)  #stop calling request handler
                     return
             else:
                 logging.debug(
                     'GoogleRequest.initialize: new session today, try cache'
                 )
                 su._cache_time = datetime.utcnow()
                 if helper.to_cache(uid, su, helper.PLAYER_CACHE_SECS):
                     logging.debug(
                         'GoogleRequest.initialize: Memcached, task dau and send email to admin'
                     )
                     taskqueue.add(url='/task/dau',
                                   params={
                                       'usr': uid,
                                       'act': 'login'
                                   })
                     #if uid not in ['gg_109722387073140662444','gg_108772542023352813713']:
                     helper.send_email(
                         'Login SuiComics: %s(%s) @ %s' %
                         (su.name, uid, datetime.utcnow()), 'OK')
         else:
             #in memcache
             logging.debug('GoogleRequest.initialize: in memcache, revisit')
             if hasattr(su, '_cache_time'):
                 if (datetime.utcnow() - su._cache_time).seconds >= 3600:
                     su._cache_time = datetime.utcnow()
                     helper.to_cache(uid, su, helper.PLAYER_CACHE_SECS)
         self.tempvars = {
             'user': su,
             'sns': 'gg',
             'login': True,
             'uname': su.name,
             'onFacebook': False
         }
         args = get_session_from_cookie(self.request.cookies)
         if not args:
             put_cookie(self.response.headers, uid, su.token,
                        sns)  #a generated random token
         else:
             self.tempvars.update(args)  #['sns','uid','token']
         self.sns = sns
         self.user = su
예제 #2
0
 def login(self, uid, access_token):
     """ Login routine.
         From FacebookRequest: login(facebook_uid
     """
     self.sns = 'fb'
     ukey = '%s_%s' % (self.sns, uid)
     u = helper.from_cache(ukey)
     if not u:
         u = helper.get_user_by_key(ukey, False)  #memcache=False
         if not u:
             graph = facebook.GraphAPI(access_token)
             profile = graph.get_object('me')
             u = helper.create_user(ukey, profile['name'],
                                    profile.get('email', None),
                                    False)  #save=False
             if not u:
                 logging.error(
                     'FacebookRequest.login: helper.create_user failed')
                 self.redirect_with_msg(
                     'Server in maintenance, please try later, thank you.')
                 self.get = self.post = (lambda *args: None)
                 return
             u.access_token = access_token
             u.save()
             logging.debug('FacebookRequest.login: New User %s saved' %
                           ukey)
         else:
             now = datetime.utcnow()
             u._cache_time = now
             if helper.to_cache(
                     ukey, u, helper.PLAYER_CACHE_SECS
             ):  #2 hours, if memcache fails, do not task/dau or send_email
                 #                    if u.lastime.day != now.day or u.lastime.month != now.month:
                 #                        taskqueue.add(url='/task/dau',params={'usr':ukey,'act':'login'})
                 taskqueue.add(url='/task/dau',
                               params={
                                   'usr': ukey,
                                   'act': 'login'
                               })
                 if ukey not in [
                         'fb_669391906', 'fb_1464710918', 'fb_1842536962',
                         'fb_1831016858'
                 ]:
                     helper.send_email(
                         'Login SuiComics: %s(%s) @ %s' %
                         (u.name, ukey, now), 'OK')
                 else:
                     helper.send_email(
                         'Login SuiComics: %s(%s) @ %s' %
                         (u.name, ukey, now), 'OK - to remove this')
     else:
         #got user from memcache
         if u.access_token != access_token:
             u.access_token = access_token
             u.save()
             logging.debug(
                 'FacebookRequest.login: access_token updated while %s still in memcache'
                 % ukey)
         elif hasattr(u, '_cache_time'):
             if (datetime.utcnow() - u._cache_time).seconds >= 3600:
                 u._cache_time = datetime.utcnow()
                 helper.to_cache(ukey, u, helper.PLAYER_CACHE_SECS)
     if self.request.headers.get('User-Agent', '').find('MSIE') >= 0:
         #logging.debug('addHeader P3P for MSIE')
         #self.response.headers.add_header('P3P','CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"')
         self.response.headers.add_header('P3P', 'CP="SuiComics"')
     args = get_session_from_cookie(self.request.cookies)
     if not args or args['uid'] != ukey:
         put_cookie(self.response.headers, ukey, u.token, self.sns)
     self.user = u
     self.tempvars = {
         'user': u,
         'sns': 'fb',
         'login': True,
         'uname': u.name,
         'onFacebook': True
     }
     if self.request.get('ref') == 'bookmarks':
         c = self.request.get('count')
         if c != '0':
             helper.clear_fb_count(uid)