﻿# The example shows how to obtain station password for today

# SABClient methods used:
#
# login(self, username, password, domain='system', access_code=None)
# logout(self)
# get_station_password(self, station_name, date, username, configuration, platform, time_zone)

import sys
import json
from datetime import date
import sabclient


# If the logged-on user requests a second factor, SABClient calls the second factor callback 
#   to retrieve the six-digit code
def get_second_factor():
  print('** SECOND FACTOR AUTHENTICATION **')
  print()
  s = input("Enter 6-digit code from your authenticator application: ") 
  return s

################################## main ######################################################

# first we need to create SecureAnyBox (SAB) client for given SAB server address (including base path);
# the callback applies when the second factor is required to sign in
sab = sabclient.SABClient('http://127.0.0.1:8843/secureanybox/', callback=get_second_factor)

# Then we need to authenticate the user.
# Don't use admin account, create account with minimum access rights, only to the required safe box
result = sab.login('serviceuser', 'password', access_code='123456', domain='test')

if result:
# Call sabclient method get_station_password with station name parameter
#   Other parameters of the method take default values for most common cases
    pswd = sab.get_station_password('PC1')

    print('username = ', pswd['userName'])
    print('password = ', pswd['password'])

# when not needed anymore, you can logout the client, which will destroy session cookies and the client can no
# longer be used to work with SAB server
sab.logout
