Пример #1
0
    def get_app_links_data(jira_host):
        data = {
            'consumer_key': jira_host.get('consumer_key'),
            'public_key': utils.read_rsa_key(config('PUBLIC_KEY_PATH')),
            'application_url': config('OAUTH_SERVICE_URL'),
            'application_name': 'JiraTelegramBot',
        }

        with open(os.path.join(config('DOCS_PATH'), 'app_links.txt')) as file:
            src_text = Template(file.read())

        return src_text.substitute(data)
Пример #2
0
    def authorization(self, telegram_id):
        """
        Gets the user data and tries to log in according to the specified authorization method.
        Output of messages according to missing information
        :param telegram_id: user id telegram
        :return: returns a namedtuple for further authorization or bool and messages
        TODO: make refactoring in the future
        """
        user_data = self.db.get_user_data(telegram_id)
        auth_method = user_data.get('auth_method')

        if not auth_method:
            raise BotAuthError(
                'You are not authorized by any of the methods (user/pass or OAuth)'
            )
        else:
            if auth_method == 'basic':
                credentials = (user_data.get('username'),
                               utils.decrypt_password(
                                   user_data.get('auth')['basic']['password']))
            else:
                host_data = self.db.get_host_data(user_data.get('host_url'))
                if not host_data:
                    raise BotAuthError(
                        'In database there are no data on the {} host'.format(
                            user_data.get('host_url')))
                credentials = {
                    'access_token':
                    user_data.get('auth')['oauth']['access_token'],
                    'access_token_secret':
                    user_data.get('auth')['oauth']['access_token_secret'],
                    'consumer_key':
                    host_data.get('consumer_key'),
                    'key_cert':
                    utils.read_rsa_key(config('PRIVATE_KEY_PATH'))
                }

            auth_data = self.AuthData(auth_method, user_data.get('host_url'),
                                      user_data.get('username'), credentials)
            self.jira.check_authorization(auth_data.auth_method,
                                          auth_data.jira_host,
                                          auth_data.credentials,
                                          base_check=True)

            return auth_data
Пример #3
0
    def dispatch_request(self):
        transaction_status = None

        try:
            resp = self.jira_app.authorized_response()
        except OAuthException as e:
            # if the user declined an authorization request
            message = 'Access denied: {}'.format(e.message)
            answer = e.data.get('oauth_problem')

            if answer and answer == 'permission_denied':
                message = 'Authorization request declined by user'

            self.send_to_chat(session['telegram_id'], message)
            return redirect(bot_url)

        oauth_dict = {
            'access_token': resp.get('oauth_token'),
            'access_token_secret': resp.get('oauth_token_secret'),
            'consumer_key': self.jira_app.consumer_key,
            'key_cert': read_rsa_key(self.jira_app.rsa_key_path)
        }

        jira_host = db.get_host_data(session['host'])
        user_exists = db.is_user_exists(session['telegram_id'])

        if not jira_host:
            message = 'No settings found for {} in the database'.format(session['host'])
            logger.exception(message)
            self.send_to_chat(session['telegram_id'], message)
            return redirect(bot_url)

        try:
            authed_jira = jira.JIRA(self.jira_app.base_server_url, oauth=oauth_dict)
        except jira.JIRAError as e:
            logger.exception('Status: {}, message: {}'.format(e.status_code, e.text))
        else:
            username = authed_jira.myself().get('name')
            data = self.get_auth_data(
                session['host'],
                username,
                oauth_dict['access_token'],
                oauth_dict['access_token_secret']
            )
            if not user_exists:
                self.send_to_chat(session['telegram_id'], 'You are not in the database. Just call the /start command')
                return redirect(bot_url)
            else:
                transaction_status = db.update_user(session['telegram_id'], data)

            # host verified
            jira_host.update({'is_confirmed': True})
            db.update_host(host_url=jira_host.get('url'), host_data=jira_host)

        if not transaction_status:
            message = 'Impossible to save data into the database. Please try again later.'
            logger.exception(
                "Data didn't save into DB. "
                "telegram_id: {}, jira_host: {}".format(session['telegram_id'], jira_host['url'])
            )
            self.send_to_chat(session['telegram_id'], message)
            return redirect(bot_url)

        self.send_to_chat(
            session['telegram_id'],
            'You were successfully authorized in {}'.format(session.get('host', 'Jira'))
        )
        return redirect(bot_url)