vistassh-py/ext_session.py
2024-03-02 00:45:21 -05:00

41 lines
1.6 KiB
Python

#!/usr/bin/env python3
from typing import Optional
import re
import configparser
import autoproc
import XWBSSOi
async def task_smartcard(proc, config: Optional[configparser.ConfigParser]=None):
"""Smartcard authentication"""
async with proc.sendline, autoproc.expect_async(proc) as expect:
if await expect.endswith('\r\nACCESS CODE: ', timeout_settle=60):
try:
certificate = config['auth']['certificate']
except (TypeError, KeyError):
choice = XWBSSOi.get_vista_certificate()
certificate = XWBSSOi.get_certificate_thumbprint(choice).hex() if choice else None
doc = await XWBSSOi.get_sso_token_async(certificate=certificate)
if doc:
proc.sendline(re.sub(r'[\r\n]', '', doc))
if await expect.earliest(re.compile(r'^\s+You last signed on\s+', flags=re.MULTILINE), timeout_settle=5):
if certificate:
config.set('auth', 'certificate', certificate)
proc.create_task(task_keepalive(proc, True), name='@task:keepalive')
return True
async def task_keepalive(proc, suppress=False):
"""Keepalive"""
with autoproc.expect(proc) as expect:
if suppress:
while True:
if m := await expect.endswith('\r\nDo you want to halt and continue with this option later? YES// ', '\r\nDo you really want to halt? YES// ', timeout_settle=60):
await proc.sendline.withlock('NO')
if m.index == 1:
print('Suppressed; type \'^\' or \'^Halt\' to actually halt.')
else:
while True:
if await expect.endswith('\r\nDo you want to halt and continue with this option later? YES// ', timeout_settle=60):
await proc.sendline.withlock('NO')