<commit_before>import jwt
import logging
from flask_oauthlib.client import OAuth

from aleph import signals, settings

oauth = OAuth()
log = logging.getLogger(__name__)


def configure_oauth(app):
    if settings.OAUTH:
        oauth.provider = oauth.remote_app(
            name=settings.OAUTH_NAME,
            consumer_key=settings.OAUTH_KEY,
            consumer_secret=settings.OAUTH_SECRET,
            request_token_params={
                'scope': settings.OAUTH_SCOPE
            },
            base_url=settings.OAUTH_BASE_URL,
            request_token_url=settings.OAUTH_REQUEST_TOKEN_URL,
            access_token_method=settings.OAUTH_TOKEN_METHOD,
            access_token_url=settings.OAUTH_TOKEN_URL,
            authorize_url=settings.OAUTH_AUTHORIZE_URL
        )
    oauth.init_app(app)
    return oauth


@signals.handle_oauth_session.connect
def handle_azure_oauth(sender, provider=None, oauth=None):
    from aleph.model import Role
    if 'login.microsoftonline.com' not in provider.base_url:
        return
    token = (oauth.get('access_token'), '')
    me = provider.get('me?fields=id,name,email', token=token)
    user_id = 'azure:%s' % me.data.get('id')
    return Role.load_or_create(user_id, Role.USER, me.data.get('name'),
                               email=me.data.get('email'))

@signals.handle_oauth_session.connect
def handle_google_oauth(sender, provider=None, oauth=None):
    from aleph.model import Role
    if 'googleapis.com' not in provider.base_url:
        return

    token = (oauth.get('access_token'), '')
    me = provider.get('userinfo', token=token)
    user_id = 'google:%s' % me.data.get('id')
    return Role.load_or_create(user_id, Role.USER, me.data.get('name'),
                               email=me.data.get('email'))


@signals.handle_oauth_session.connect
def handle_facebook_oauth(sender, provider=None, oauth=None):
    from aleph.model import Role
    if 'facebook.com' not in provider.base_url:<commit_msg>Working support for OAuth 2.0 authentication against Azure/AD, todo: fix ID verification<commit_after>import jwt
import logging
from flask_oauthlib.client import OAuth

from aleph import signals, settings

oauth = OAuth()
log = logging.getLogger(__name__)


def configure_oauth(app):
    if settings.OAUTH:
        oauth.provider = oauth.remote_app(
            name=settings.OAUTH_NAME,
            consumer_key=settings.OAUTH_KEY,
            consumer_secret=settings.OAUTH_SECRET,
            request_token_params={
                'scope': settings.OAUTH_SCOPE
            },
            base_url=settings.OAUTH_BASE_URL,
            request_token_url=settings.OAUTH_REQUEST_TOKEN_URL,
            access_token_method=settings.OAUTH_TOKEN_METHOD,
            access_token_url=settings.OAUTH_TOKEN_URL,
            authorize_url=settings.OAUTH_AUTHORIZE_URL
        )
    oauth.init_app(app)
    return oauth


@signals.handle_oauth_session.connect
def handle_azure_oauth(sender, provider=None, oauth=None):
    from aleph.model import Role
    if 'login.microsoftonline.com' not in provider.base_url:
        return

    token_data = jwt.decode(oauth.get('id_token'), verify=False) # TODO: verify=True


    user_id = 'azure:%s' % token_data['upn']
    return Role.load_or_create(user_id, Role.USER, token_data['name'],
                               email=token_data['upn'])

@signals.handle_oauth_session.connect
def handle_google_oauth(sender, provider=None, oauth=None):
    from aleph.model import Role
    if 'googleapis.com' not in provider.base_url:
        return

    token = (oauth.get('access_token'), '')
    me = provider.get('userinfo', token=token)
    user_id = 'google:%s' % me.data.get('id')
    return Role.load_or_create(user_id, Role.USER, me.data.get('name'),
                               email=me.data.get('email'))


@signals.handle_oauth_session.connect
def handle_facebook_oauth(sender, provider=None, oauth=None):
    from aleph.model import Role
    if 'facebook.com' not in provider.base_url:
{'fraction_non_alphanumeric': 0.10267299525355983, 'fraction_numerical': 0.0004996252810392206, 'mean_word_length': 3.1449275362318843, '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': '7608562', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1263, 'n_words': 224}