def get_caminata_user(self, _):
   user = user.get_current_user()
   if not user:
     raise remote.ApplicationError('User not logged in.')
     
   # Look for a user
   caminata_user_key = ndb.Key(
       caminata_users_models.CaminataUser, user.user_id())
   caminata_user = caminata_user_key.get()
   
   # If user not there, maybe they are invited, look them up
   # by email.
   if not caminata_user:
     invited_caminata_user_key = ndb.Key(
       caminata_users_models.InvitedCaminataUser, user.email().lower())
     invited_caminata_user = invited_caminata_user_key.get()
     if not invited_caminata_user:
       raise remote.ApplicationError('User not authorized.')
     caminata_user = caminata_users_models.CaminataUser.from_invited_user(
         invited_caminata_user, user.user_id())
     caminata_user.key = ndb.Key(caminata_users_models.CaminataUser, user.user_id())
     caminata_user.put()
     
   return GetCaminataUserResponse(
       email=caminata_user.email,
       nickname=caminata_user.nickname,
       dashboard_access=caminata_user.dashboard_access,
       students_access=caminata_user.students_access,
       laps_access=caminata_user.laps_access)
    def _validate_authentication(self):
        import tba_config
        # Allow all requests in debug mode
        if tba_config.DEBUG:
            return

        incoming_app_id = self.request_state.headers.get('X-Appengine-Inbound-Appid', None)
        if incoming_app_id is None:
            raise remote.ApplicationError('Unauthenticated')

        from google.appengine.api.app_identity import app_identity
        if not app_identity.get_application_id() == incoming_app_id:
            raise remote.ApplicationError('Unauthenticated')
Example #3
0
    def _validate_authentication(self):
        # Allow all requests in debug mode
        if tba_config.DEBUG:
            return

        # Ignore auth check during tests
        if self.testing:
            return

        incoming_app_id = self.request_state.headers.get('X-Appengine-Inbound-Appid', None)
        if incoming_app_id is None:
            raise remote.ApplicationError('Unauthenticated')

        if not app_identity.get_application_id() == incoming_app_id:
            raise remote.ApplicationError('Unauthenticated')
    def ping(self, request):
        """ Immediately dispatch a Ping to either FCM or a webhook """
        if not self._authenticated:
            raise remote.ApplicationError('Unauthenticated')

        if request.fcm and request.webhook:
            return TBANSResponse(code=400, message='Cannot ping both FCM and webhook')

        from tbans.models.notifications.ping import PingNotification
        notification = PingNotification()

        if request.fcm:
            from tbans.models.messages.fcm_message import FCMMessage
            message = FCMMessage(notification, token=request.fcm.token, topic=request.fcm.topic, condition=request.fcm.condition)
            logging.info('Ping - {}'.format(str(message)))

            response = message.send()
            logging.info('Ping Response - {}'.format(str(response)))
            return TBANSResponse(code=response.status_code, message=response.content)
        elif request.webhook:
            from tbans.models.messages.webhook_message import WebhookMessage
            message = WebhookMessage(notification, request.webhook.url, request.webhook.secret)
            logging.info('Ping - {}'.format(str(message)))

            response = message.send()
            logging.info('Ping Response - {}'.format(str(response)))
            return TBANSResponse(code=response.status_code, message=response.content)
        else:
            return TBANSResponse(code=400, message='Did not specify FCM or webhook to ping')
Example #5
0
 def search(self, request):
     if not request.query:
         raise remote.ApplicationError('Missing: query')
     user = users.User.get_from_environ()
     persistent_user = user and user.get_persistent()
     docs, cursor = execute_search(request.query)
     if not IS_DEV:
         docs = clean_docs(persistent_user, docs)
     resp = SearchResponse()
     if IS_DEV or persistent_user:
         resp.documents = docs
     resp.cursor = cursor
     return resp
    def verification(self, request):
        """ Immediately dispatch a Verification to a webhook """
        if not self._authenticated:
            raise remote.ApplicationError('Unauthenticated')

        from tbans.models.notifications.verification import VerificationNotification
        notification = VerificationNotification(request.webhook.url, request.webhook.secret)

        from tbans.models.messages.webhook_message import WebhookMessage
        message = WebhookMessage(notification, request.webhook.url, request.webhook.secret)
        logging.info('Verification - {}'.format(str(message)))

        response = message.send()
        logging.info('Verification Response - {}'.format(str(response)))
        return VerificationResponse(code=response.status_code, message=response.content, verification_key=notification.verification_key)
Example #7
0
    def check_quota(self, request):
        """Perform a quota check for a user."""
        state = self.__get_state(request.user)

        response = QuotaResponse(all_status=CheckResult.Status.OK)
        response.denied = False

        state.begin_transaction()
        try:
            for quota in request.quotas:
                if quota.mode in (QuotaCheck.Mode.CHECK_ALL,
                                  QuotaCheck.Mode.CHECK_SOME):
                    func = state.check_quota
                else:
                    func = state.deduct_quota

                available = func(quota.name, quota.tokens)
                if available is None:
                    raise remote.ApplicationError(
                        'Unknown quota %s requested' % quota.name)

                result = CheckResult(available=available)
                response.results.append(result)
                if available == quota.tokens:
                    result.status = CheckResult.Status.OK
                    if response.all_status == CheckResult.Status.NONE:
                        result.status = CheckResult.Status.SOME
                elif available == 0:
                    result.status = CheckResult.Status.NONE
                    if response.all_status == CheckResult.Status.OK:
                        response.all_status = CheckResult.Status.NONE
                    response.denied = True
                else:
                    result.status = CheckResult.Status.SOME
                    response.all_status = CheckResult.Status.SOME
                    if quota.mode in (QuotaCheck.Mode.ALL,
                                      QuotaCheck.Mode.CHECK_ALL):
                        response.denied = True

            if response.denied:
                state.abort_transaction()
            else:
                state.commit_transaction()
        except:
            state.abort_transaction()
            raise
        return response
Example #8
0
 def RaiseNormalApplicationError(self, unused_request):
   raise remote.ApplicationError('whatever')
Example #9
0
  def testRepr(self):
    self.assertEquals("ApplicationError('an error', 1)",
                      repr(remote.ApplicationError('an error', 1)))

    self.assertEquals("ApplicationError('an error')",
                      repr(remote.ApplicationError('an error')))
Example #10
0
 def testStr(self):
   self.assertEquals('an error', str(remote.ApplicationError('an error', 1)))
Example #11
0
 def testErrorCode(self):
   self.assertEquals('blam',
                     remote.ApplicationError('an error', 'blam').error_name)
Example #12
0
 def raise_application_error(self, request):
   raise remote.ApplicationError('This is an application error', 'ERROR_NAME')
Example #13
0
 def raise_application_error(self, request):
     raise remote.ApplicationError('App error', 10)