﻿# The example shows how to add a file record to a safe box in SecureAnyBox including file
#   in this example, helper method find_safe_box_by_name is used to get safe box id 
# variable target_box contains name of the safe box where the record to be saved
#
# used SABClient methods:
#
# login(self, username, password, domain='system', access_code=None)
# logout(self)
# find_safe_box_by_name(self, box_name, parent_box_id=None)
# add_file(self, box_id, name, description=None, tags=None, note=None, 
#   file_name=None, sec_note=None, file_path=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 file record. Note that the second argument - record 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
    # file_name
    # sec_note
    # file_path (path to file)
    added = sab.add_file(safe_box['id'], 'file4', description='description4', tags='files', 
                         note='note1\nnote2', file_name='document.doc', sec_note='sec-note1\nsec-note2', 
                         file_path='.\\document.doc')

    # if the file was added, add_file 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

                           	
