Esempio n. 1
0
    def get(self):
        try:
            error = self.request.get('error')
            if error == "access_denied":
                raise OAuthConnectDeniedException
            elif error:
                raise OAuthConnectErrorException

            code = self.request.get('code')
            if not code:
                raise OAuthConnectErrorException

            client = utils.makeFoursquareClient()
            access_token = client.oauth.get_token(code)
            if not access_token:
                raise OAuthConnectErrorException
            client.set_access_token(access_token)
        except OAuthConnectDeniedException:
            self.redirect(CONFIG['auth_denied_uri'])
            return
        except OAuthConnectErrorException:
            path = os.path.join(os.path.dirname(__file__),
                                'templates/connect_error.html')
            self.response.out.write(
                template.render(path, {'name': CONFIG['site_name']}))
            return

        user = client.users()  # returns the auth'd users info
        fs_user_id = user['user']['id']

        existing_token = UserToken.get_by_fs_id(fs_user_id)

        if existing_token:
            token = existing_token
        else:
            token = UserToken()

        token.token = access_token
        token.fs_id = fs_user_id
        token.put()

        session = UserSession.get_or_create_session(fs_user_id)
        cookie = Cookie.SimpleCookie()
        cookie['session'] = session.session
        cookie['session']['path'] = '/'
        cookie['session']['expires'] = email.utils.formatdate(time.time() +
                                                              (14 * 86400),
                                                              localtime=False,
                                                              usegmt=True)
        self.response.headers.add_header("Set-Cookie", cookie.output()[12:])
        isMobile = utils.isMobileUserAgent(self.request.headers['User-Agent'])
        redirect_uri = CONFIG[
            'auth_success_uri_mobile'] if isMobile else CONFIG[
                'auth_success_uri_desktop']
        self.redirect(redirect_uri)
Esempio n. 2
0
  def get(self):
    user_token = UserToken.get_from_cookie(self.request.cookies.get('session', None))
    is_authd = 'false'
    if user_token and user_token.fs_id:
      client = utils.makeFoursquareClient(user_token.token)
      try:
        client.users()
        is_authd = 'true'
      except InvalidAuth:
        user_token.delete()

    self.response.out.write(is_authd)
Esempio n. 3
0
  def get(self):
    try:
      error = self.request.get('error')
      if error == "access_denied":
        raise OAuthConnectDeniedException
      elif error:
        raise OAuthConnectErrorException

      code = self.request.get('code')
      if not code:
        raise OAuthConnectErrorException

      client = utils.makeFoursquareClient()
      access_token = client.oauth.get_token(code)
      if not access_token:
        raise OAuthConnectErrorException
      client.set_access_token(access_token)
    except OAuthConnectDeniedException:
      self.redirect(CONFIG['auth_denied_uri'])
      return
    except Exception:
      path = os.path.join(os.path.dirname(__file__),
                          'templates/connect_error.html')
      self.response.out.write(template.render(path, {'name': CONFIG['site_name']}))
      return

    user = client.users()  # returns the auth'd users info
    fs_user_id = user['user']['id']

    existing_token = UserToken.get_by_fs_id(fs_user_id)

    if existing_token:
      token = existing_token
    else:
      token = UserToken()

    token.token = access_token
    token.fs_id = fs_user_id
    token.put()

    if not memcache.set('token:%s' % fs_user_id, access_token, 86400):
      logging.error('Memcache set during oauth on token for %s' % fs_user_id)
      memcache.delete('token:%s' % fs_user_id)

    session = UserSession.get_or_create_session(fs_user_id)
    cookie = Cookie.SimpleCookie()
    cookie['session'] = session.session
    cookie['session']['path'] = '/'
    cookie['session']['expires'] = email.utils.formatdate(time.time() + (14 * 86400), localtime=False, usegmt=True)
    self.response.headers.add_header("Set-Cookie", cookie.output()[12:])
    isMobile = utils.isMobileUserAgent(self.request.headers['User-Agent'])
    redirect_uri = CONFIG['auth_success_uri_mobile'] if isMobile else CONFIG['auth_success_uri_desktop']
    self.redirect(redirect_uri)
Esempio n. 4
0
    def get(self):
        user_token = UserToken.get_from_cookie(
            self.request.cookies.get('session', None))
        is_authd = 'false'
        if user_token and user_token.fs_id:
            client = utils.makeFoursquareClient(user_token.token)
            try:
                client.users()
                is_authd = 'true'
            except InvalidAuth:
                user_token.delete()

        self.response.out.write(is_authd)