def get_auth(): slack_code = request.args.get('code') if not slack_code: logging.error('Missing "code" query string parameter') return 'Invalid request.', 400 try: state = request.args.get('state') if state: data = json.loads( security.decrypt(config.SLACK_ENCRYPTION_KEY, state)) if data['account_id']: account = models.Account.get_by_id(int(data['account_id'])) else: account = None else: account = None handler, auth = _exchange_code('reactioncam', slack_code, account=account) except: logging.exception('Error exchanging Slack code') return 'Invalid request.', 400 # Build the Roger authentication code for the client. session = handler.create_session() auth_code = session.create_auth_code('reactioncam') # Redirect to an app URI which will complete the login flow. redirect_uri = 'reactioncam://login/code/%s' % (auth_code.key.id(), ) resp = make_response(RESPONSE_TEMPLATE % {'uri': redirect_uri}) resp.headers['Location'] = redirect_uri return resp
def from_refresh_token(cls, token): data = proto.RefreshToken() try: data.ParseFromString(security.decrypt(config.ENCRYPTION_KEY, token)) except: raise errors.InvalidAccessToken() return cls(data.account_id, scopes=data.scopes)
def from_access_token(cls, token): data = proto.AccessToken() try: data.ParseFromString(security.decrypt(config.ENCRYPTION_KEY, token)) except: raise errors.InvalidAccessToken() return cls(data.account_id, access_token=token, created=datetime.fromtimestamp(data.created), ttl=data.ttl, scopes=data.scopes)
def get_exchange(): # Only reaction.cam can use this endpoint. try: payload = security.decrypt(config.WEB_ENCRYPTION_KEY, request.args['payload'], block_segments=True) handler, auth = _exchange_code('reactioncamweb', **json.loads(payload)) except: logging.exception('Error exchanging Slack code') return 'Invalid request.', 400 g.public_options['view_account'] = handler.account return handler.create_session(skip_activation=True)
def post_chunk_played(): # Only internal websites can use this endpoint. try: payload = security.decrypt(config.WEB_ENCRYPTION_KEY, flask_extras.get_parameter('payload'), block_segments=True) data = json.loads(payload) fingerprint = data['fingerprint'] stream_key = ndb.Key('Stream', data['stream_id']) chunk_key = ndb.Key('Chunk', data['chunk_id'], parent=stream_key) except: raise errors.InvalidArgument('Invalid payload') cache_key = 'external_plays:%d:%d:%s' % (stream_key.id(), chunk_key.id(), fingerprint) if memcache.get(cache_key): logging.debug( 'Repeat chunk play for fingerprint %s (stream %d chunk %d)', fingerprint, stream_key.id(), chunk_key.id()) return {'success': True} memcache.set(cache_key, True, 172800) stream, chunk = ndb.get_multi([stream_key, chunk_key]) if not stream or not chunk: raise errors.ResourceNotFound('That chunk does not exist') chunk.external_plays += 1 chunk.put() for local_chunk in stream.chunks: if local_chunk.chunk_id == chunk.key.id(): local_chunk.external_plays = chunk.external_plays stream.put() break logging.debug('New chunk play for fingerprint %s (stream %d chunk %d)', fingerprint, stream_key.id(), chunk_key.id()) logging.debug('Total external chunk plays is now %d', chunk.external_plays) handler = streams.MutableStream(chunk.sender, stream) if chunk.external_plays == 1: handler.notify_first_play(chunk) handler.notify(notifs.ON_STREAM_CHUNK_EXTERNAL_PLAY, add_stream=True, chunk=chunk) return {'success': True}