<commit_before>from org.jboss.seam.contexts import Context, Contexts
from org.jboss.seam.security import Identity
from org.jboss.seam import Component
from javax.faces.context import FacesContext
from org.jboss.seam import Component
from org.apache.http.entity import ContentType 
from org.xdi.oxauth.service.python.interfaces import ExternalAuthenticatorType
from org.xdi.oxauth.service import UserService
from org.xdi.oxauth.service import AuthenticationService
from org.xdi.oxauth.service.net import HttpService
from org.xdi.util.security import StringEncrypter 
from org.xdi.util import StringHelper
from org.xdi.util import ArrayHelper
from org.apache.http.params import CoreConnectionPNames
from java.util import Arrays
from java.util import HashMap

import java
import sys

class ExternalAuthenticator(ExternalAuthenticatorType):
    def __init__(self, currentTimeMillis):
        self.currentTimeMillis = currentTimeMillis

    def init(self, configurationAttributes):
        print "CAS2 initialization"
        print "CAS2 initialized successfully"
        return True   

    def isValidAuthenticationMethod(self, usageType, configurationAttributes):
        print "CAS2 Rest API authenticate isValidAuthenticationMethod"

        if (not (configurationAttributes.containsKey("cas_validation_uri") and
            configurationAttributes.containsKey("cas_validation_pattern") and
            configurationAttributes.containsKey("cas_validation_timeout"))):
            return True

        cas_validation_uri = configurationAttributes.get("cas_validation_uri").getValue2()
        cas_validation_pattern = configurationAttributes.get("cas_validation_pattern").getValue2()
        cas_validation_timeout = int(configurationAttributes.get("cas_validation_timeout").getValue2()) * 1000

        httpService = HttpService.instance();

        http_client = httpService.getHttpsClientTrustAll();
        http_client_params = http_client.getParams();
        http_client_params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, cas_validation_timeout);

        try:
            http_response = httpService.executeGet(http_client, cas_validation_uri)
        except:
            print "CAS2 Rest API authenticate isValidAuthenticationMethod. Exception: ", sys.exc_info()[1]
            return False

        if (http_response.getStatusLine().getStatusCode() != 200):
            print "CAS2 Rest API authenticate isValidAuthenticationMethod. Get invalid response from CAS2 server: ", str(http_response_ticket.getStatusLine().getStatusCode())
            httpService.consume(http_response)
            return False

        validation_response_bytes = httpService.getResponseContent(http_response)
        validation_response_string = httpService.convertEntityToString(validation_response_bytes)
        httpService.consume(http_response)

        if (validation_response_string == None or validation_response_string.find(cas_validation_pattern) == -1):
            print "CAS2 Rest API authenticate isValidAuthenticationMethod. Get invalid login page from CAS2 server:"
            return False

        return True

    def getAlternativeAuthenticationMethod(self, usageType, configurationAttributes):
        cas_alt_auth_mode = configurationAttributes.get("cas_alt_auth_mode").getValue2()
        return cas_alt_auth_mode

    def authenticate(self, configurationAttributes, requestParameters, step):
        context = Contexts.getEventContext()
        authenticationService = AuthenticationService.instance()
        userService = UserService.instance()
        httpService = HttpService.instance();

        stringEncrypter = StringEncrypter.defaultInstance()

        cas_host = configurationAttributes.get("cas_host").getValue2()
        cas_extra_opts = configurationAttributes.get("cas_extra_opts").getValue2()
        cas_map_user = StringHelper.toBoolean(configurationAttributes.get("cas_map_user").getValue2(), False)
        cas_renew_opt = StringHelper.toBoolean(configurationAttributes.get("cas_renew_opt").getValue2(), False)

        if (step == 1):
            print "CAS2 authenticate for step 1"
            ticket_array = requestParameters.get("ticket")
            if ArrayHelper.isEmpty(ticket_array):
                print "CAS2 authenticate for step 1. ticket is empty"
                return False

            ticket = ticket_array[0]
            print "CAS2 authenticate for step 1. ticket: " + ticket

            if (StringHelper.isEmptyString(ticket)):
                print "CAS2 authenticate for step 1. ticket is invalid"
                return False

            # Validate ticket
            request = FacesContext.getCurrentInstance().getExternalContext().getRequest()

            parametersMap = HashMap()
            parametersMap.put("service", httpService.constructServerUrl(request) + "/postlogin")
            if (cas_renew_opt):
                parametersMap.put("renew", "true")
            parametersMap.put("ticket", ticket)
            cas_service_request_uri = authenticationService.parametersAsString(parametersMap)
            cas_service_request_uri = cas_host + "/serviceValidate?" + cas_service_request_uri
            if StringHelper.isNotEmpty(cas_extra_opts):
                cas_service_request_uri = cas_service_request_uri + "&" + cas_extra_opts

            print "CAS2 authenticate for step 1. cas_service_request_uri: " + cas_service_request_uri

            http_client = httpService.getHttpsClientTrustAll();
            http_response = httpService.executeGet(http_client, cas_service_request_uri)
            validation_content = httpService.convertEntityToString(httpService.getResponseContent(http_response))<commit_msg>cas_extra_opts in CAS module should be optional<commit_after>from org.jboss.seam.contexts import Context, Contexts
from org.jboss.seam.security import Identity
from org.jboss.seam import Component
from javax.faces.context import FacesContext
from org.jboss.seam import Component
from org.apache.http.entity import ContentType 
from org.xdi.oxauth.service.python.interfaces import ExternalAuthenticatorType
from org.xdi.oxauth.service import UserService
from org.xdi.oxauth.service import AuthenticationService
from org.xdi.oxauth.service.net import HttpService
from org.xdi.util.security import StringEncrypter 
from org.xdi.util import StringHelper
from org.xdi.util import ArrayHelper
from org.apache.http.params import CoreConnectionPNames
from java.util import Arrays
from java.util import HashMap

import java
import sys

class ExternalAuthenticator(ExternalAuthenticatorType):
    def __init__(self, currentTimeMillis):
        self.currentTimeMillis = currentTimeMillis

    def init(self, configurationAttributes):
        print "CAS2 initialization"
        print "CAS2 initialized successfully"
        return True   

    def isValidAuthenticationMethod(self, usageType, configurationAttributes):
        print "CAS2 Rest API authenticate isValidAuthenticationMethod"

        if (not (configurationAttributes.containsKey("cas_validation_uri") and
            configurationAttributes.containsKey("cas_validation_pattern") and
            configurationAttributes.containsKey("cas_validation_timeout"))):
            return True

        cas_validation_uri = configurationAttributes.get("cas_validation_uri").getValue2()
        cas_validation_pattern = configurationAttributes.get("cas_validation_pattern").getValue2()
        cas_validation_timeout = int(configurationAttributes.get("cas_validation_timeout").getValue2()) * 1000

        httpService = HttpService.instance();

        http_client = httpService.getHttpsClientTrustAll();
        http_client_params = http_client.getParams();
        http_client_params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, cas_validation_timeout);

        try:
            http_response = httpService.executeGet(http_client, cas_validation_uri)
        except:
            print "CAS2 Rest API authenticate isValidAuthenticationMethod. Exception: ", sys.exc_info()[1]
            return False

        if (http_response.getStatusLine().getStatusCode() != 200):
            print "CAS2 Rest API authenticate isValidAuthenticationMethod. Get invalid response from CAS2 server: ", str(http_response_ticket.getStatusLine().getStatusCode())
            httpService.consume(http_response)
            return False

        validation_response_bytes = httpService.getResponseContent(http_response)
        validation_response_string = httpService.convertEntityToString(validation_response_bytes)
        httpService.consume(http_response)

        if (validation_response_string == None or validation_response_string.find(cas_validation_pattern) == -1):
            print "CAS2 Rest API authenticate isValidAuthenticationMethod. Get invalid login page from CAS2 server:"
            return False

        return True

    def getAlternativeAuthenticationMethod(self, usageType, configurationAttributes):
        cas_alt_auth_mode = configurationAttributes.get("cas_alt_auth_mode").getValue2()
        return cas_alt_auth_mode

    def authenticate(self, configurationAttributes, requestParameters, step):
        context = Contexts.getEventContext()
        authenticationService = AuthenticationService.instance()
        userService = UserService.instance()
        httpService = HttpService.instance();

        stringEncrypter = StringEncrypter.defaultInstance()

        cas_host = configurationAttributes.get("cas_host").getValue2()
        cas_map_user = StringHelper.toBoolean(configurationAttributes.get("cas_map_user").getValue2(), False)
        cas_renew_opt = StringHelper.toBoolean(configurationAttributes.get("cas_renew_opt").getValue2(), False)

        cas_extra_opts = None
        if (configurationAttributes.containsKey("cas_extra_opts")):
            cas_extra_opts = configurationAttributes.get("cas_extra_opts").getValue2()

        if (step == 1):
            print "CAS2 authenticate for step 1"
            ticket_array = requestParameters.get("ticket")
            if ArrayHelper.isEmpty(ticket_array):
                print "CAS2 authenticate for step 1. ticket is empty"
                return False

            ticket = ticket_array[0]
            print "CAS2 authenticate for step 1. ticket: " + ticket

            if (StringHelper.isEmptyString(ticket)):
                print "CAS2 authenticate for step 1. ticket is invalid"
                return False

            # Validate ticket
            request = FacesContext.getCurrentInstance().getExternalContext().getRequest()

            parametersMap = HashMap()
            parametersMap.put("service", httpService.constructServerUrl(request) + "/postlogin")
            if (cas_renew_opt):
                parametersMap.put("renew", "true")
            parametersMap.put("ticket", ticket)
            cas_service_request_uri = authenticationService.parametersAsString(parametersMap)
            cas_service_request_uri = cas_host + "/serviceValidate?" + cas_service_request_uri
            if (cas_extra_opts != None):
                cas_service_request_uri = cas_service_request_uri + "&" + cas_extra_opts

            print "CAS2 authenticate for step 1. cas_service_request_uri: " + cas_service_request_uri

            http_client = httpService.getHttpsClientTrustAll();
            http_response = httpService.executeGet(http_client, cas_service_request_uri)
            validation_content = httpService.convertEntityToString(httpService.getResponseContent(http_response))
{'fraction_non_alphanumeric': 0.07374631268436578, 'fraction_numerical': 0.006420267221933021, 'mean_word_length': 3.6126450580232095, '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': '6642270', 'n_tokens_mistral': 2971, 'n_tokens_neox': 2824, 'n_words': 620}