﻿# The example shows how to add a secret account to a safe box in SecureAnyBox
#   in this example, helper method find_safe_box_by_name is used to get safe box id 
#  for a given safe box name ('test box')

# Note: usually you can use a fixed safe box id and don't need to lookup safe box id every time you add new records

# SABClient methods used:
#
# login(self, username, password, domain='system', access_code=None)
# logout(self)
# find_safe_box_by_name(self, box_name, parent_box_id=None)
# add_secret_account(self, box_id, name, description=None, tags=None, note=None, 
#   login_site=None, password=None, sec_server_address=None, 
#   sec_login_name=None, sec_note=None)

import sys
import json
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:
    # to add any record, we need to know an id of parent safe box we can get a safe box id from URL in SecureAnyBox
    # web interface (e.g in https://172.22.14.2/sab/static/safe-search.page?s#$groupbrowse$/group/893 the 893 is a
    # safe box id)

    # or we can find the safe box by name (internally SABClient will list safe boxes and find one with name matching
    # safe_box_name or return None if no such safe box was found)
    safe_box_name = 'test box'
    safe_box = sab.find_safe_box_by_name(safe_box_name);

    if safe_box is None:
        print('safe box ' + safe_box_name + ' was not found.')
        exit(1)  # quit with non-zero exit code so that calling script can test if this script failed or not

    # With safe box id we can add a secret account. Note that the second argument - secret account name 
    #   must be unique in given safe
    # box. Other fields are optional. You can use following fields:
    # description
    # tags ... an array of tags (strings)
    # note
    # login_site
    # password
    # sec_server_address
    # sec_login_name
    # sec_note
    added = sab.add_secret_account(safe_box['id'], 'sec-account2', description='description2', 
                                   note='note1\nnote2', login_site='is.tdp.cz', password='tdp', 
                                   sec_server_address='www.doe.com', sec_login_name='joedoe', 
                                   sec_note='sec-note1\nsec-note2')

    # if the secret account was added, add_secret_account returns True
    if added:
        print('OK done')

# 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
