def test_echo_bot(self): self.anna.streams.send(['echo'], 'echome.mp3', 1000) recents, _ = self.anna.streams.get_recent() chunk = recents[0].chunks[-1] self.assertEqual(chunk.sender, accounts.get_handler('echo').account.key) self.assertEqual(chunk.payload, 'echome.mp3')
def grant_type_refresh_token(client): refresh_token = flask_extras.get_parameter('refresh_token') if not refresh_token: raise errors.MissingArgument('A refresh token is required') try: session = auth.Session.from_refresh_token(refresh_token) except ValueError: logging.exception('Failed to restore refresh token') raise errors.InvalidCredentials() return accounts.get_handler(session.account)
def grant_type_authorization_code(client): code = flask_extras.get_parameter('code') if not code: raise errors.MissingArgument('A code is required') redirect_uri = flask_extras.get_parameter('redirect_uri') if not redirect_uri: redirect_uri = None if redirect_uri and redirect_uri not in client.redirect_uris: raise errors.InvalidArgument('Invalid redirect_uri value') session = auth.Session.from_auth_code(code, client.key.id(), redirect_uri) return accounts.get_handler(session.account)
def grant_type_password(client): username = flask_extras.get_parameter('username') password = flask_extras.get_parameter('password') if not username: raise errors.MissingArgument('A username is required') if not password: raise errors.MissingArgument('A password is required') try: user = accounts.get_handler(username) except errors.ResourceNotFound: # Return a 401 instead of a 404 when the account does not exist. raise errors.InvalidCredentials() if not user.validate_password(password): raise errors.InvalidCredentials() report.user_logged_in(user.account_id, auth_identifier=username, challenge='password') return user
def _exchange_code(client, code, account=None, **kwargs): # Fetch Slack session info with the Slack authorization code. auth = slack_api.exchange_code(code, **kwargs) if not auth: raise ValueError('Invalid code') # Try to put together the two very different responses from Slack. team_properties = {} if 'user' in auth and 'team' in auth: team_id = auth['team']['id'] team_properties['name'] = auth['team']['name'] team_properties['slug'] = auth['team']['domain'] user_id = auth['user']['id'] # Warning: This is real_name, *not* same as name in users.info. name = auth['user']['name'] email = auth['user']['email'] # This endpoint doesn't provide image_original. for key in ['image_512', 'image_192']: if key in auth['user']: image = auth['user'][key] break else: image = None else: # TODO: Potentially import team domain from auth.test endpoint? team_id = auth['team_id'] team_properties['name'] = auth['team_name'] user_id = auth['user_id'] info = slack_api.get_user_info(user_id, auth['access_token']) if not info: raise ValueError('Failed to get Slack user info') name = info['user']['real_name'] or info['user']['name'] email = info['user']['profile']['email'] image = info['user']['profile'].get('image_original') # Use the Slack user id to lookup or create a user. identifier = models.Service.build_identifier('slack', team_id, user_id) # Attempt to get an existing account by either email or Slack identifier. # TODO: Use transaction? if account: handler = accounts.get_handler(account) if not handler.has_identifier(identifier): logging.debug('Adding Slack account to existing account') handler.add_identifier(identifier, notify_change=False) if not handler.has_identifier(email): try: logging.debug('Adding %s to existing account', email) handler.add_identifier(email, notify_change=False, notify_connect=False) except errors.AlreadyExists: logging.warning('%s already belongs to another account', email) else: ids = [identifier, email] handler = accounts.get_or_create(*ids, display_name=name, image=image) # Connect the user to the Slack team. auth = handler.connect_service('slack', team_id, user_id, access_token=auth['access_token'], client=client, team_properties=team_properties, token_type='bearer') return handler, auth