<commit_before>"""
Custom Authenticator to use generic OAuth2 with JupyterHub
"""
######
# packages for jupyter setup
from base64 import urlsafe_b64decode
import re
import ast
from subprocess import check_output
######

import json
import os
import base64
import urllib

from tornado.auth import OAuth2Mixin
from tornado import gen, web

from tornado.httputil import url_concat
from tornado.httpclient import HTTPRequest, AsyncHTTPClient

from jupyterhub.auth import LocalAuthenticator

from traitlets import Unicode, Dict, Bool, List

from .oauth2 import OAuthLoginHandler, OAuthenticator


class OpenIDEnvMixin(OAuth2Mixin):

    oauth_access_token_url = Unicode(
        os.environ.get('OAUTH2_TOKEN_URL', ''),
        config=True,
        help="OpenID Connect endpoint for access token"
    )

    oauth_authorize_url = Unicode(
        os.environ.get('OAUTH2_AUTHORIZE_URL', ''),
        config=True,
        help="OpenID Connect enpoint for authorization"
    )

class OpenIDLoginHandler(OAuthLoginHandler, OpenIDEnvMixin):
    @property
    def scope(self):
        return self.authenticator.scope
    pass


class OpenIDOAuthenticator(OAuthenticator, OpenIDEnvMixin):

    login_handler = OpenIDLoginHandler

    scope =  ['openid', 'profile', 'email', 'groups']

    login_service = Unicode(
        os.environ.get('LOGIN_SERVICE', ''),
        config=True,
        help="String to be displayed in Login-Button"
    )

    userdata_url = Unicode(
        os.environ.get('OAUTH2_USERDATA_URL', ''),
        config=True,
        help="Userdata url to get user data login information"
    )
    token_url = Unicode(
        os.environ.get('OAUTH2_TOKEN_URL', ''),
        config=True,
        help="Access token endpoint URL"
    )
    extra_params = Dict(
        os.environ.get('OAUTH2_AUTHENTICATION_PARAMS', {}),
        help="Extra parameters for first POST request"
    ).tag(config=True)

    username_key = Unicode(
        os.environ.get('OAUTH2_USERNAME_KEY', 'username'),
        config=True,
        help="Userdata username key from returned json for USERDATA_URL"
    )
    userdata_params = {'scope':['openid','profile','email']}
    #Dict(
    #    os.environ.get('OAUTH2_USERDATA_PARAMS', {}),
    #    help="Userdata params to get user data login information"
    #).tag(config=True)

    userdata_method = Unicode(
        os.environ.get('OAUTH2_USERDATA_METHOD', 'GET'),
        config=True,
        help="Userdata method to get user data login information"
    )

    tls_verify = Bool(
        os.environ.get('OAUTH2_TLS_VERIFY', 'True').lower() in {'true', '1'},
        config=True,
        help="Disable TLS verification on http request"
    )

    connectors = List(
        os.environ.get('CONNECTOR_LIST',''),
        config=True,
        help="List of allowed IDP endpoints"
    )

    @gen.coroutine<commit_msg>wip chg default value, and syntax<commit_after>"""
Custom Authenticator to use generic OAuth2 with JupyterHub
"""
######
# packages for jupyter setup
from base64 import urlsafe_b64decode
import re
import ast
from subprocess import check_output
######

import json
import os
import base64
import urllib

from tornado.auth import OAuth2Mixin
from tornado import gen, web

from tornado.httputil import url_concat
from tornado.httpclient import HTTPRequest, AsyncHTTPClient

from jupyterhub.auth import LocalAuthenticator

from traitlets import Unicode, Dict, Bool, List

from .oauth2 import OAuthLoginHandler, OAuthenticator


class OpenIDEnvMixin(OAuth2Mixin):

    oauth_access_token_url = Unicode(
        os.environ.get('OAUTH2_TOKEN_URL', ''),
        config=True,
        help="OpenID Connect endpoint for access token"
    )

    oauth_authorize_url = Unicode(
        os.environ.get('OAUTH2_AUTHORIZE_URL', ''),
        config=True,
        help="OpenID Connect enpoint for authorization"
    )

class OpenIDLoginHandler(OAuthLoginHandler, OpenIDEnvMixin):
    @property
    def scope(self):
        return self.authenticator.scope
    pass


class OpenIDOAuthenticator(OAuthenticator, OpenIDEnvMixin):

    login_handler = OpenIDLoginHandler

    scope =  ['openid', 'profile', 'email', 'groups']

    login_service = Unicode(
        os.environ.get('LOGIN_SERVICE', ''),
        config=True,
        help="String to be displayed in Login-Button"
    )

    userdata_url = Unicode(
        os.environ.get('OAUTH2_USERDATA_URL', ''),
        config=True,
        help="Userdata url to get user data login information"
    )
    token_url = Unicode(
        os.environ.get('OAUTH2_TOKEN_URL', ''),
        config=True,
        help="Access token endpoint URL"
    )
    extra_params = Dict(
        os.environ.get('OAUTH2_AUTHENTICATION_PARAMS', {}),
        help="Extra parameters for first POST request"
    ).tag(config=True)

    username_key = Unicode(
        os.environ.get('OAUTH2_USERNAME_KEY', 'username'),
        config=True,
        help="Userdata username key from returned json for USERDATA_URL"
    )
    userdata_params = {'scope':['openid','profile','email']}
    #Dict(
    #    os.environ.get('OAUTH2_USERDATA_PARAMS', {}),
    #    help="Userdata params to get user data login information"
    #).tag(config=True)

    userdata_method = Unicode(
        os.environ.get('OAUTH2_USERDATA_METHOD', 'GET'),
        config=True,
        help="Userdata method to get user data login information"
    )

    tls_verify = Bool(
        os.environ.get('OAUTH2_TLS_VERIFY', 'True').lower() in {'true', '1'},
        config=True,
        help="Disable TLS verification on http request"
    )

    connectors = List(
        help="List of allowed IDP endpoints"
    ).tag(config=True)

    @gen.coroutine
{'fraction_non_alphanumeric': 0.10436164577079286, 'fraction_numerical': 0.0070633939607981635, 'mean_word_length': 3.2618510158013545, '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': '205410', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1740, 'n_words': 423}