<commit_before>from __future__ import unicode_literals, print_function

import sys
from flask import request, url_for, redirect
from urlobject import URLObject
from requests_oauthlib import OAuth1Session
from oauthlib.oauth1 import SIGNATURE_HMAC, SIGNATURE_TYPE_AUTH_HEADER
from oauthlib.common import to_unicode
from .base import BaseOAuthConsumerBlueprint


class OAuth1SessionWithBaseURL(OAuth1Session):
    def __init__(self, base_url=None, *args, **kwargs):
        super(OAuth1SessionWithBaseURL, self).__init__(*args, **kwargs)
        self.base_url = URLObject(base_url)

    def prepare_request(self, request):
        if self.base_url:
            request.url = self.base_url.relative(request.url)
        print("URL = {}".format(request.url), file=sys.stderr)
        return super(OAuth1SessionWithBaseURL, self).prepare_request(request)


class OAuth1ConsumerBlueprint(BaseOAuthConsumerBlueprint):
    def __init__(self, name, import_name,
            client_key=None,
            client_secret=None,
            signature_method=SIGNATURE_HMAC,
            signature_type=SIGNATURE_TYPE_AUTH_HEADER,
            rsa_key=None,
            client_class=None,
            force_include_body=False,

            static_folder=None, static_url_path=None, template_folder=None,
            url_prefix=None, subdomain=None, url_defaults=None, root_path=None,

            login_url=None,
            authorized_url=None,
            base_url=None,
            request_token_url=None,
            access_token_url=None,
            authorization_url=None,
            redirect_url=None,
            redirect_to=None,

            **kwargs):

        if not redirect_url and not redirect_to:
            raise AttributeError("One of redirect_url or redirect_to must be defined")

        BaseOAuthConsumerBlueprint.__init__(
            self, name, import_name,
            static_folder=static_folder,
            static_url_path=static_url_path,
            template_folder=template_folder,
            url_prefix=url_prefix, subdomain=subdomain,
            url_defaults=url_defaults, root_path=root_path,
            login_url=login_url,
            authorized_url=authorized_url,
        )

        self.session = OAuth1SessionWithBaseURL(
            client_key=client_key,
            client_secret=client_secret,
            signature_method=signature_method,
            signature_type=signature_type,
            rsa_key=rsa_key,
            client_class=client_class,
            force_include_body=force_include_body,
            base_url=base_url,
            **kwargs
        )

        self.request_token_url = request_token_url
        self.access_token_url = access_token_url
        self.authorization_url = authorization_url
        self.redirect_url = redirect_url
        self.redirect_to = redirect_to

    def login(self):
        callback_uri = url_for(
            ".authorized", next=request.args.get('next'), _external=True,
        )
        response = self.session.fetch_request_token(self.request_token_url)
        url = self.session.authorization_url(
            self.authorization_url, oauth_callback=callback_uri,
        )
        return redirect(url)

    def authorized(self):
        next_url = request.args.get('next') or self.redirect_url or url_for(self.redirect_to)<commit_msg>Set callback_uri before getting access token<commit_after>from __future__ import unicode_literals, print_function

import sys
from flask import request, url_for, redirect
from urlobject import URLObject
from requests_oauthlib import OAuth1Session
from oauthlib.oauth1 import SIGNATURE_HMAC, SIGNATURE_TYPE_AUTH_HEADER
from oauthlib.common import to_unicode
from .base import BaseOAuthConsumerBlueprint


class OAuth1SessionWithBaseURL(OAuth1Session):
    def __init__(self, base_url=None, *args, **kwargs):
        super(OAuth1SessionWithBaseURL, self).__init__(*args, **kwargs)
        self.base_url = URLObject(base_url)

    def prepare_request(self, request):
        if self.base_url:
            request.url = self.base_url.relative(request.url)
        print("URL = {}".format(request.url), file=sys.stderr)
        return super(OAuth1SessionWithBaseURL, self).prepare_request(request)


class OAuth1ConsumerBlueprint(BaseOAuthConsumerBlueprint):
    def __init__(self, name, import_name,
            client_key=None,
            client_secret=None,
            signature_method=SIGNATURE_HMAC,
            signature_type=SIGNATURE_TYPE_AUTH_HEADER,
            rsa_key=None,
            client_class=None,
            force_include_body=False,

            static_folder=None, static_url_path=None, template_folder=None,
            url_prefix=None, subdomain=None, url_defaults=None, root_path=None,

            login_url=None,
            authorized_url=None,
            base_url=None,
            request_token_url=None,
            access_token_url=None,
            authorization_url=None,
            redirect_url=None,
            redirect_to=None,

            **kwargs):

        if not redirect_url and not redirect_to:
            raise AttributeError("One of redirect_url or redirect_to must be defined")

        BaseOAuthConsumerBlueprint.__init__(
            self, name, import_name,
            static_folder=static_folder,
            static_url_path=static_url_path,
            template_folder=template_folder,
            url_prefix=url_prefix, subdomain=subdomain,
            url_defaults=url_defaults, root_path=root_path,
            login_url=login_url,
            authorized_url=authorized_url,
        )

        self.session = OAuth1SessionWithBaseURL(
            client_key=client_key,
            client_secret=client_secret,
            signature_method=signature_method,
            signature_type=signature_type,
            rsa_key=rsa_key,
            client_class=client_class,
            force_include_body=force_include_body,
            base_url=base_url,
            **kwargs
        )

        self.request_token_url = request_token_url
        self.access_token_url = access_token_url
        self.authorization_url = authorization_url
        self.redirect_url = redirect_url
        self.redirect_to = redirect_to

    def login(self):
        callback_uri = url_for(
            ".authorized", next=request.args.get('next'), _external=True,
        )
        self.session._client.client.callback_uri = to_unicode(callback_uri)
        self.session.fetch_request_token(self.request_token_url)
        url = self.session.authorization_url(self.authorization_url)
        return redirect(url)

    def authorized(self):
        next_url = request.args.get('next') or self.redirect_url or url_for(self.redirect_to)
{'fraction_non_alphanumeric': 0.07125784284433821, 'fraction_numerical': 0.0023902001792650133, 'mean_word_length': 2.773957158962796, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5191721', 'n_tokens_mistral': 1915, 'n_tokens_neox': 1884, 'n_words': 323}