sciber-io / yubikey-locker

Sciber Yubikey Locker
MIT License
1 stars 0 forks source link

Refactor multi-platform imports #41

Closed andreaspersson-sciber closed 4 months ago

andreaspersson-sciber commented 4 months ago

All OS-specific imports are currently done in the main sciber_yclocker.py module regardless of platform. This means that OS-specific imports have to be overloaded with an EmptyModule shim.

This can be avoided by breaking out OS-specific functionality to their own files.

For example, in sciber_yclocker.py:

if platform.system() == "Windows":
    from lib_win import lock_system, log_message

elif platform.system() == "Linux":
    from lib_lx import lock_system, log_message

elif platform.system() == "Darwin":
    from lib_mac import lock_system, log_message

...

    def lock(self):
            if self.get_removal_option() != RemovalOption.NOTHING:
                lock_system(self.get_removal_option())
...

    def logger(self, message):
        log_message(message)

New file lib_mac.py:

import syslog
from lib import RemovalOption
import os
import CDLL

def log_message(msg):
    syslog.syslog(syslog.LOG_INFO, msg)

def lock_system(removal_option):
    loginPF = CDLL(
        "/System/Library/PrivateFrameworks/login.framework/Versions/Current/login"
    )
    loginPF.SACLockScreenImmediate()

This removes the need for overloading modules with EmptyModule, and makes cross-platform development implementable via separate modules.