# The example shows how to find a record by text pattern in record note in SecureAnyBox and display it
#   in this example, helper method own_search_by_note is used to get found id of objects

# SABClient methods used:
#
# login(self, username, password, domain='system', access_code=None)
# logout(self)
# own_search_by_note(self, pattern, show_browsing_safeboxes)
# get_record(self, record_id):
# get_safebox(self, safebox_id):

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', domain='test')

if result:

    # method SABClient.search_by_note can search stored records (such as Accounts, File, etc.), 
    # by pattern in their note

    pattern = 'Imported'
    result_list = sab.own_search_by_note(pattern, show_browsing_safeboxes=True)

    # result_list contains found records
    # You can get following fields:

    # id ... ID of Record
    # name ... Record name
    # note ... Record note
    # box_id ... parent safebox id
    # box_name ... name of parent safebox
    # group_name ... name of parent group safebox if exists
    # import_sign ... note line with pattern

    print()
    print('id , "record name", "safe box full path", "import signature"')
    print()

    for tuple in result_list:
        id = tuple[0]
        box_id = tuple[1]

        record = sab.get_record(id)
        name = record['name']
        note = record['note']
        lines = note.splitlines()

        safebox = sab.get_safebox(box_id)
        box_name = safebox["name"]
        group_name = safebox["group_name"]

        found = False
        import_sign = ''
        for s in lines:
            # string.find() method searches patterns case sensitive
            if s != '' and s.find(pattern) >= 0:
                import_sign = s
                break

        if group_name != '':
            print(id, ',', '"' + name + '"', ',', '"'+group_name+'/'+box_name+'"', ',', '"'+import_sign+'"')
        else:
            print(id, ',', '"' + name + '"', ',', '"'+box_name+'"', ',', '"'+import_sign+'"')

    print()
    print('id , "record name", "safe box full path", "import signature"')
    print()

    print()
    print('displayed records: ', len(result_list))

# 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

