from appconfig import OPENSSO_HOST_NAME,OPENSSO_PORT,APPLICATION_HOST_NAME,APPLICATION_PORT
from orangescape.model.server.Context import getApplication
from orangescape.model.server.UserAuthentication import UserAuthentication
from EnterpriseAuth import EnterpriseAbstractAuth
import logging
import urllib
import appconfig as config

class EnterpriseDevAuthenticator(EnterpriseAbstractAuth):
    def authenticate(self, controller):
        resultDict = {"authenticated":False}
        userEmail = controller.request.get("userid")
        if userEmail:
            resultDict["authenticated"] = True
            resultDict["userEmail"] = userEmail
        return resultDict

class LdapAuthenticator(EnterpriseAbstractAuth):
    def authenticate(self, controller):
        resultDict = {"authenticated":False}
        if controller.jeerequest.getUserPrincipal():
            resultDict["authenticated"] = True
            userName = controller.jeerequest.getUserPrincipal().getName()
            if config.COMPANYID:
                userName = userName +"@" + config.COMPANYID
            resultDict["userEmail"] = userName
        return resultDict

    def doPreRequest(self,controller):
        session = controller.session
        if session.getUser():
            return session.getUser().getSheetId()
        elif controller.jeerequest.getUserPrincipal():
            userName = controller.jeerequest.getUserPrincipal().getName()
            if config.COMPANYID:
                userName = userName + "@" + config.COMPANYID
            userinitialization=UserAuthentication(controller.session,userName)
            userSurrogateId=userinitialization.authenticateUser()
            return userSurrogateId
        else:
            logging.debug('no user in session!')
            currentUrl=urllib.quote(controller.request.path+"?"+controller.request.query_string)
            controller.redirect("/"+getApplication().getSheetId()+"/1/signin/?forward="+currentUrl,True)
            return False

    def getLogoutUrl(self):
        #ldap_logout_url = '/'+getApplication().getSheetId()+'/j_spring_security_logout'
        ldap_logout_url = '/'+getApplication().getSheetId()+'/logout.jsp?appId='+getApplication().getSheetId()
        return ldap_logout_url

class OpenSSOAuthenticator:

    def authenticate(self):
        return {"result":True}

    def doPreRequest(self,controller):
#        request=controller.request
#        emailId = request._environ_getter()['HTTP_USERID']
#        session = controller.session
#        userinitialization=UserAuthentication(session,emailId)
#        userId=userinitialization.authenticateUser()
#        return userId
        session = controller.session
        if session.getUser():
            return session.getUser().getSheetId()
        else:
            currentUrl=urllib.quote(controller.request.path+"?"+controller.request.query_string)
            controller.redirect("/"+getApplication().getSheetId()+"/1/signin/?forward="+currentUrl,True)
            return False

    def getLogoutUrl(self):
        opensso_logout_url_list=['http://',OPENSSO_HOST_NAME,':',OPENSSO_PORT,'/opensso/UI/Logout?goto==http://',APPLICATION_HOST_NAME,':',APPLICATION_PORT,'/',getApplication().getSheetId(),'/1/worktop']
        return ''.join(opensso_logout_url_list)
