﻿# The example shows how to add a credit card record 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_credit_card(self, box_id, name, description=None, tags=None, note=None, 
#   number=None, expiration=None, cvv=None, pin=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 credit card 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
    # number
    # expiration (date)
    # cvv
    # pin
    # secret_note
    added = sab.add_credit_card(safe_box['id'], 'ccred1', description='description3', tags='cards', 
                                note='note1\nnote2', number='7452 3265 8752 6985', 
                                expiration='01/11/2032', cvv='135', pin='1111', 
                                sec_note='secret1\nsecret2')

    # if the credit card record was added, add_credit_card 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

