def C(request): fp = '../NK_C_API.h' declarations = [] with open(fp, 'r') as f: declarations = f.readlines() a = iter(declarations) for declaration in a: if declaration.startswith('NK_C_API'): declaration = declaration.replace('NK_C_API', '').strip() while not ';' in declaration: declaration += (next(a)).strip() print(declaration) ffi.cdef(declaration, override=True) C = None import os, sys path_build = os.path.join("..", "build") paths = [ os.path.join(path_build, "libnitrokey-log.so"), os.path.join(path_build, "libnitrokey.so") ] for p in paths: print p if os.path.exists(p): C = ffi.dlopen(p) break else: print("File does not exist: " + p) print("Trying another") if not C: print("No library file found") sys.exit(1) C.NK_set_debug(False) nk_login = C.NK_login_auto() if nk_login != 1: print('No devices detected!') assert nk_login != 0 # returns 0 if not connected or wrong model or 1 when connected global device_type firmware_version = C.NK_get_major_firmware_version() model = 'P' if firmware_version in [7, 8] else 'S' device_type = (model, firmware_version) # assert C.NK_first_authenticate(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK # assert C.NK_user_authenticate(DefaultPasswords.USER, DefaultPasswords.USER_TEMP) == DeviceErrorCode.STATUS_OK # C.NK_status() def fin(): print('\nFinishing connection to device') C.NK_logout() print('Finished') request.addfinalizer(fin) C.NK_set_debug(True) return C
def library_read_declarations(): fp = '../NK_C_API.h' declarations = [] with open(fp, 'r') as f: declarations = f.readlines() cnt = 0 a = iter(declarations) for declaration in a: if declaration.strip().startswith('NK_C_API') \ or declaration.strip().startswith('struct'): declaration = declaration.replace('NK_C_API', '').strip() while ');' not in declaration and '};' not in declaration: declaration += (next(a)).strip() + '\n' ffi.cdef(declaration, override=True) cnt += 1 print('Imported {} declarations'.format(cnt))
def get_library(request, allow_offline=False): fp = '../NK_C_API.h' declarations = [] with open(fp, 'r') as f: declarations = f.readlines() cnt = 0 a = iter(declarations) for declaration in a: if declaration.strip().startswith('NK_C_API') \ or declaration.strip().startswith('struct'): declaration = declaration.replace('NK_C_API', '').strip() while ');' not in declaration and '};' not in declaration: declaration += (next(a)).strip()+'\n' ffi.cdef(declaration, override=True) cnt += 1 print('Imported {} declarations'.format(cnt)) C = None import os, sys path_build = os.path.join("..", "build") paths = [ os.environ.get('LIBNK_PATH', None), os.path.join(path_build,"libnitrokey.so"), os.path.join(path_build,"libnitrokey.dylib"), os.path.join(path_build,"libnitrokey.dll"), os.path.join(path_build,"nitrokey.dll"), ] for p in paths: if not p: continue print("Trying " +p) p = os.path.abspath(p) if os.path.exists(p): print("Found: "+p) C = ffi.dlopen(p) break else: print("File does not exist: " + p) if not C: print("No library file found") sys.exit(1) C.NK_set_debug_level(int(os.environ.get('LIBNK_DEBUG', 2))) nk_login = C.NK_login_auto() if nk_login != 1: print('No devices detected!') if not allow_offline: assert nk_login != 0 # returns 0 if not connected or wrong model or 1 when connected global device_type firmware_version = C.NK_get_minor_firmware_version() model = C.NK_get_device_model() model = 'P' if model == 1 else 'S' if model == 2 else 'U' device_type = (model, firmware_version) print('Connected device: {} {}'.format(model, firmware_version)) # assert C.NK_first_authenticate(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK # assert C.NK_user_authenticate(DefaultPasswords.USER, DefaultPasswords.USER_TEMP) == DeviceErrorCode.STATUS_OK # C.NK_status() def fin(): print('\nFinishing connection to device') C.NK_logout() print('Finished') if request: request.addfinalizer(fin) # C.NK_set_debug(True) C.NK_set_debug_level(int(os.environ.get('LIBNK_DEBUG', 3))) return AttrProxy(C, "libnitrokey C")
def get_library(request, allow_offline=False): fp = '../NK_C_API.h' declarations = [] with open(fp, 'r') as f: declarations = f.readlines() cnt = 0 a = iter(declarations) for declaration in a: if declaration.strip().startswith('NK_C_API') \ or declaration.strip().startswith('struct'): declaration = declaration.replace('NK_C_API', '').strip() while ');' not in declaration and '};' not in declaration: declaration += (next(a)).strip()+'\n' ffi.cdef(declaration, override=True) cnt += 1 print('Imported {} declarations'.format(cnt)) C = None import os, sys path_build = os.path.join("..", "build") paths = [ os.environ.get('LIBNK_PATH', None), os.path.join(path_build,"libnitrokey.so"), os.path.join(path_build,"libnitrokey.dylib"), os.path.join(path_build,"libnitrokey.dll"), os.path.join(path_build,"nitrokey.dll"), ] for p in paths: if not p: continue print("Trying " +p) p = os.path.abspath(p) if os.path.exists(p): print("Found: "+p) C = ffi.dlopen(p) break else: print("File does not exist: " + p) if not C: print("No library file found") sys.exit(1) C.NK_set_debug_level(int(os.environ.get('LIBNK_DEBUG', 2))) nk_login = C.NK_login_auto() if nk_login != 1: print('No devices detected!') if not allow_offline: assert nk_login != 0 # returns 0 if not connected or wrong model or 1 when connected global device_type firmware_version = C.NK_get_minor_firmware_version() model = 'P' if firmware_version < 20 else 'S' device_type = (model, firmware_version) print('Connected device: {} {}'.format(model, firmware_version)) # assert C.NK_first_authenticate(DefaultPasswords.ADMIN, DefaultPasswords.ADMIN_TEMP) == DeviceErrorCode.STATUS_OK # assert C.NK_user_authenticate(DefaultPasswords.USER, DefaultPasswords.USER_TEMP) == DeviceErrorCode.STATUS_OK # C.NK_status() def fin(): print('\nFinishing connection to device') C.NK_logout() print('Finished') if request: request.addfinalizer(fin) # C.NK_set_debug(True) C.NK_set_debug_level(int(os.environ.get('LIBNK_DEBUG', 3))) return AttrProxy(C, "libnitrokey C")