Esempio n. 1
0
    def init(self, configurationAttributes):
        print "Toopher. Initialization"
        toopher_creds_file = configurationAttributes.get(
            "toopher_creds_file").getValue2()

        # Load credentials from file
        f = open(toopher_creds_file, 'r')
        try:
            creds = json.loads(f.read())
        except:
            return False
        finally:
            f.close()

        consumer_key = creds["CONSUMER_KEY"]
        consumer_secret = creds["CONSUMER_SECRET"]
        try:
            stringEncrypter = StringEncrypter.defaultInstance()
            consumer_secret = stringEncrypter.decrypt(consumer_secret)
        except:
            return False

        self.tapi = ToopherAPI(consumer_key, consumer_secret)
        print "Toopher. Initialized successfully"

        return True
    def init(self, configurationAttributes):
        print "Toopher initialization"
        toopher_creds_file = configurationAttributes.get("toopher_creds_file").getValue2()

        # Load credentials from file
        f = open(toopher_creds_file, 'r')
        try:
            creds = json.loads(f.read())
        except:
            return False
        finally:
            f.close()

        consumer_key = creds["CONSUMER_KEY"]
        consumer_secret = creds["CONSUMER_SECRET"]
        try:
            stringEncrypter = StringEncrypter.defaultInstance()
            consumer_secret = stringEncrypter.decrypt(consumer_secret)
        except:
            return False

        self.tapi = ToopherAPI(consumer_key, consumer_secret)
        print "Toopher initialized successfully"

        return True
Esempio n. 3
0
class PersonAuthentication(PersonAuthenticationType):
    def __init__(self, currentTimeMillis):
        self.currentTimeMillis = currentTimeMillis

    def init(self, configurationAttributes):
        print "Toopher. Initialization"
        toopher_creds_file = configurationAttributes.get(
            "toopher_creds_file").getValue2()

        # Load credentials from file
        f = open(toopher_creds_file, 'r')
        try:
            creds = json.loads(f.read())
        except:
            return False
        finally:
            f.close()

        consumer_key = creds["CONSUMER_KEY"]
        consumer_secret = creds["CONSUMER_SECRET"]
        try:
            stringEncrypter = StringEncrypter.defaultInstance()
            consumer_secret = stringEncrypter.decrypt(consumer_secret)
        except:
            return False

        self.tapi = ToopherAPI(consumer_key, consumer_secret)
        print "Toopher. Initialized successfully"

        return True

    def destroy(self, configurationAttributes):
        print "Toopher. Destroy"
        print "Toopher. Destroyed successfully"
        return True

    def getApiVersion(self):
        return 1

    def isValidAuthenticationMethod(self, usageType, configurationAttributes):
        return True

    def getAlternativeAuthenticationMethod(self, usageType,
                                           configurationAttributes):
        return None

    def authenticate(self, configurationAttributes, requestParameters, step):
        context = Contexts.getEventContext()
        userService = UserService.instance()

        toopher_user_timeout = int(
            configurationAttributes.get("toopher_user_timeout").getValue2())

        credentials = Identity.instance().getCredentials()
        user_name = credentials.getUsername()

        if (step == 1):
            print "Toopher. Authenticate for step 1"

            user_password = credentials.getPassword()
            logged_in = False
            if (StringHelper.isNotEmptyString(user_name)
                    and StringHelper.isNotEmptyString(user_password)):
                userService = UserService.instance()
                logged_in = userService.authenticate(user_name, user_password)

            if (not logged_in):
                return False

            # Find user by uid
            userService = UserService.instance()
            find_user_by_uid = userService.getUser(user_name)
            if (find_user_by_uid == None):
                print "Toopher. Authenticate for step 1. Failed to find user"
                return False

            # Check if the user paired account to phone
            user_external_uid_attr = userService.getCustomAttribute(
                find_user_by_uid, "oxExternalUid")
            if ((user_external_uid_attr == None)
                    or (user_external_uid_attr.getValues() == None)):
                print "Toopher. Authenticate for step 1. There is no external UIDs for user: "******"Toopher. Authenticate for step 1. There is no Topher UID for user: "******"toopher_user_uid", topher_user_uid)

            return True
        elif (step == 2):
            print "Toopher. Authenticate for step 2"

            passed_step1 = self.isPassedDefaultAuthentication
            if (not passed_step1):
                return False

            sessionAttributes = context.get("sessionAttributes")
            if (sessionAttributes == None
                ) or not sessionAttributes.containsKey("toopher_user_uid"):
                print "Toopher. Authenticate for step 2. toopher_user_uid is empty"

                # Pair with phone
                pairing_phrase_array = requestParameters.get("pairing_phrase")
                if ArrayHelper.isEmpty(pairing_phrase_array):
                    print "Toopher. Authenticate for step 2. pairing_phrase is empty"
                    return False

                pairing_phrase = pairing_phrase_array[0]
                try:
                    pairing_status = self.tapi.pair(pairing_phrase, user_name)
                    toopher_user_uid = pairing_status.id
                except RequestError, err:
                    print "Toopher. Authenticate for step 2. Failed pair with phone: ", err
                    return False

                pairing_result = self.checkPairingStatus(
                    toopher_user_uid, toopher_user_timeout)

                if (not pairing_result):
                    print "Toopher. Authenticate for step 2. The pairing has not been authorized by the phone yet"
                    return False

                print "Toopher. Authenticate for step 2. Storing toopher_user_uid in user entry", toopher_user_uid

                # Store toopher_user_uid in user entry
                find_user_by_uid = userService.addUserAttribute(
                    user_name, "oxExternalUid", "toopher:" + toopher_user_uid)
                if (find_user_by_uid == None):
                    print "Toopher. Authenticate for step 2. Failed to update current user"
                    return False

                context.set("toopher_user_uid", toopher_user_uid)
            else:
                toopher_user_uid = sessionAttributes.get("toopher_user_uid")

                # Check pairing stastus
                print "Toopher. Authenticate for step 2. toopher_user_uid: ", toopher_user_uid
                pairing_result = self.checkPairingStatus(toopher_user_uid, 0)
                if (not pairing_result):
                    print "Toopher. Authenticate for step 2. The pairing has not been authorized by the phone yet"
                    return False

            return True
        elif (step == 3):
            print "Toopher. Authenticate for step 3"

            passed_step1 = self.isPassedDefaultAuthentication
            if (not passed_step1):
                return False

            sessionAttributes = context.get("sessionAttributes")
            if (sessionAttributes == None
                ) or not sessionAttributes.containsKey("toopher_user_uid"):
                print "Toopher. Authenticate for step 3. toopher_user_uid is empty"
                return False

            toopher_user_uid = sessionAttributes.get("toopher_user_uid")
            passed_step1 = StringHelper.isNotEmptyString(toopher_user_uid)
            if (not passed_step1):
                return False

            toopher_terminal_name = configurationAttributes.get(
                "toopher_terminal_name").getValue2()

            try:
                request_status = self.tapi.authenticate(
                    toopher_user_uid, toopher_terminal_name)
                request_id = request_status.id
            except RequestError, err:
                print "Toopher. Authenticate for step 3. Failed to send authentication request to phone: ", err
                return False
class ExternalAuthenticator(ExternalAuthenticatorType):

    def __init__(self, currentTimeMillis):
        self.currentTimeMillis = currentTimeMillis

    def init(self, configurationAttributes):
        print "Toopher initialization"
        toopher_creds_file = configurationAttributes.get("toopher_creds_file").getValue2()

        # Load credentials from file
        f = open(toopher_creds_file, 'r')
        try:
            creds = json.loads(f.read())
        except:
            return False
        finally:
            f.close()

        consumer_key = creds["CONSUMER_KEY"]
        consumer_secret = creds["CONSUMER_SECRET"]
        try:
            stringEncrypter = StringEncrypter.defaultInstance()
            consumer_secret = stringEncrypter.decrypt(consumer_secret)
        except:
            return False

        self.tapi = ToopherAPI(consumer_key, consumer_secret)
        print "Toopher initialized successfully"

        return True

    def isValidAuthenticationMethod(self, usageType, configurationAttributes):
        return True

    def getAlternativeAuthenticationMethod(self, usageType, configurationAttributes):
        return None

    def authenticate(self, configurationAttributes, requestParameters, step):
        context = Contexts.getEventContext()
        userService = UserService.instance()

        stringEncrypter = StringEncrypter.defaultInstance()

        toopher_user_timeout = int(configurationAttributes.get("toopher_user_timeout").getValue2())

        credentials = Identity.instance().getCredentials()
        user_name = credentials.getUsername()

        if (step == 1):
            print "Toopher authenticate for step 1"

            user_password = credentials.getPassword()
            logged_in = False
            if (StringHelper.isNotEmptyString(user_name) and StringHelper.isNotEmptyString(user_password)):
                userService = UserService.instance()
                logged_in = userService.authenticate(user_name, user_password)

            if (not logged_in):
                return False

            # Find user by uid
            userService = UserService.instance()
            find_user_by_uid = userService.getUser(user_name)
            if (find_user_by_uid == None):
                print "Toopher authenticate for step 1. Failed to find user"
                return False

            # Check if the user paired account to phone
            user_external_uid_attr = userService.getCustomAttribute(find_user_by_uid, "oxExternalUid")
            if ((user_external_uid_attr == None) or (user_external_uid_attr.getValues() == None)):
                print "Toopher authenticate for step 1. There is no external UIDs for user: "******"Toopher authenticate for step 1. There is no Topher UID for user: "******"toopher_user_uid", stringEncrypter.encrypt(topher_user_uid))

            return True
        elif (step == 2):
            print "Toopher authenticate for step 2"

            passed_step1 = self.isPassedDefaultAuthentication
            if (not passed_step1):
                return False

            toopher_user_uid_array = requestParameters.get("toopher_user_uid")
            
            if (ArrayHelper.isEmpty(toopher_user_uid_array) or StringHelper.isEmptyString(toopher_user_uid_array[0])):
                print "Toopher authenticate for step 2. toopher_user_uid is empty"

                # Pair with phone
                pairing_phrase_array = requestParameters.get("pairing_phrase")
                if ArrayHelper.isEmpty(pairing_phrase_array):
                    print "Toopher authenticate for step 2. pairing_phrase is empty"
                    return False
                
                pairing_phrase = pairing_phrase_array[0]
                try:
                    pairing_status = self.tapi.pair(pairing_phrase, user_name);
                    toopher_user_uid = pairing_status.id;
                except RequestError, err:
                    print "Toopher authenticate for step 2. Failed pair with phone: ", err
                    return False
                
                pairing_result = self.checkPairingStatus(toopher_user_uid, toopher_user_timeout) 

                if (not pairing_result):
                    print "Toopher authenticate for step 2. The pairing has not been authorized by the phone yet"
                    return False
                    
                print "Toopher authenticate for step 2. Storing toopher_user_uid in user entry", toopher_user_uid

                # Store toopher_user_uid in user entry
                find_user_by_uid = userService.addUserAttribute(user_name, "oxExternalUid", "toopher:" + toopher_user_uid)
                if (find_user_by_uid == None):
                    print "Toopher authenticate for step 2. Failed to update current user"
                    return False

                context.set("toopher_user_uid", stringEncrypter.encrypt(toopher_user_uid))
            else:
                toopher_user_uid = stringEncrypter.decrypt(toopher_user_uid_array[0])

                # Check pairing stastus
                print "Toopher authenticate for step 2. toopher_user_uid: ", toopher_user_uid
                pairing_result = self.checkPairingStatus(toopher_user_uid, 0) 
                if (not pairing_result):
                    print "Toopher authenticate for step 2. The pairing has not been authorized by the phone yet"
                    return False

            return True
        elif (step == 3):
            print "Toopher authenticate for step 3"

            passed_step1 = self.isPassedDefaultAuthentication
            if (not passed_step1):
                return False

            toopher_user_uid_array = requestParameters.get("toopher_user_uid")
            if ArrayHelper.isEmpty(toopher_user_uid_array):
                print "Toopher authenticate for step 3. toopher_user_uid is empty"
                return False

            toopher_terminal_name = configurationAttributes.get("toopher_terminal_name").getValue2()

            toopher_user_uid = stringEncrypter.decrypt(toopher_user_uid_array[0])

            try:
                request_status = self.tapi.authenticate(toopher_user_uid, toopher_terminal_name);
                request_id = request_status.id;
            except RequestError, err:
                print "Toopher authenticate for step 3. Failed to send authentication request to phone: ", err
                return False