Ejemplo n.º 1
0
    def _login(self):
        access_token, refresh_token = read_tokens()

        if access_token is None or refresh_token is None:
            oauth = bx.OAuth2(
                client_id=self.client_id,
                client_secret=self.client_secret,
                store_tokens=store_tokens,
            )
            self.auth_url, csrf_token = oauth.get_authorization_url(
                self.redirect_url)
            webbrowser.open(self.auth_url)
            global _access_code
            while _access_code is None:
                log.debug(_access_code)
                time.sleep(1)

            if _access_code == "denied":
                raise PermissionError("Failed to authenticate to Box.")

            access_token, refresh_token = authenticate(oauth, _access_code)

        oauth = bx.OAuth2(
            client_id=self.client_id,
            client_secret=self.client_secret,
            access_token=access_token,
            refresh_token=refresh_token,
        )
        self._client = bx.Client(oauth)
Ejemplo n.º 2
0
 def _init_client(self):
     oauth = boxsdk.OAuth2(client_id=self.creds["client_id"],
                           client_secret=self.creds["client_secret"],
                           store_tokens=self._store_tokens,
                           access_token=self.creds["user_access_token"],
                           refresh_token=self.creds["user_refresh_token"])
     client = boxsdk.Client(oauth)
     return client
Ejemplo n.º 3
0
def get_box_client():
    oauth = boxsdk.OAuth2(
        client_id='f5qebb99vwnebdrdok3wv7ew4pk84nl3',
        client_secret='cLwt5hj96589T15SG6lAp11fZYBsqyel',
        access_token='rVSyiCPelJx3hPYPquM8Gy9ZRuLvoCHl',
    )
    client = boxsdk.Client(oauth)
    return client
Ejemplo n.º 4
0
    def __init__(self, client_id=None, client_secret=None, access_token=None):
        client_id = check_env('BOX_CLIENT_ID', client_id)
        client_secret = check_env('BOX_CLIENT_SECRET', client_secret)
        access_token = check_env('BOX_ACCESS_TOKEN', access_token)

        oauth = boxsdk.OAuth2(client_id=client_id,
                              client_secret=client_secret,
                              access_token=access_token)
        self.client = boxsdk.Client(oauth)
def get_box_client():
    """
    Sets up a Box Platform API client as a global variable.
    """
    global box_client
    if not box_client:
        box_auth = get_box_auth()
        box_client = boxsdk.Client(box_auth)

    return box_client
Ejemplo n.º 6
0
 def __init__(self, archive, settings):
     self.archive = archive
     self.client_id = settings.get('box.client_id')
     self.client_secret = settings.get('box.client_secret')
     self.oauth = boxsdk.OAuth2(client_id=self.client_id,
                                client_secret=self.client_secret,
                                access_token=archive.access_token,
                                refresh_token=archive.refresh_token,
                                store_tokens=archive.store)
     self.client = boxsdk.Client(self.oauth)
Ejemplo n.º 7
0
def get_users_box_access_token(username):
    """
    Gets a user's Box Platform app user API access token
    """
    box_client = get_box_client()
    box_user_id = get_users_box_app_user_id(username)
    box_user = box_client.user(box_user_id)
    auth = boxsdk.JWTAuth.from_settings_dictionary(box_jwt_keys, user=box_user)
    auth.authenticate_user()
    box_as_user_client = boxsdk.Client(auth)

    return box_as_user_client._oauth.access_token
Ejemplo n.º 8
0
def authenticate(identifier, secret, token): 
    '''identifier: client_id, provided in config.py
    secret: client_secret, provided in config.py
    token: developer token, accessed from box developer console
    '''
    oauth = boxsdk.OAuth2(
        client_id=identifier, 
        client_secret=secret, 
        access_token=token
    )
    client = boxsdk.Client(oauth)
    return client
Ejemplo n.º 9
0
def get_box_client(as_user_email=None):
    """
    Sets up a Box Platform API client as a global variable.
    """
    global box_client
    if not box_client:
        box_auth = get_box_auth()
        box_client = boxsdk.Client(box_auth)

    if as_user_email:
        user = box_client.users(filter_term=as_user_email, limit=1).next()
        box_client = box_client.as_user(user)

    return box_client
Ejemplo n.º 10
0
def cds():

    # Authenticate with Box web services and get web service client
    box_auth = boxsdk.JWTAuth.from_settings_file(
        os.path.join('box_auth', 'kaiser_permanente_california.json'))
    box_client = boxsdk.Client(box_auth)

    # ------------------------------------------------------------------------------------------------------------------
    # This implementation for retrieving files from Box is not suitable for production.
    # In the production application, services sending CDS to Box will write Redis record with newly created Box file id
    #
    # Key = [patient id]-latest-cds-box-file-id
    # Value = [newly created box file id]
    #
    # Example in Redis
    # 443340029-latest-cds-box-file-id:1233321
    #
    # This request handler's route will change to /cds/[patient_id]
    # Patient id will be used to retrieve Box file id from Redis, then the Box Download File API will be called with the
    # associated Box file id.
    # ------------------------------------------------------------------------------------------------------------------

    # Demoware solution to get a file from Box
    # Get Box folder
    box_folder = box_client.folder(folder_id='0').get()

    # Determine last CDS uploaded to Box folder
    latest_entry = None
    latest_entry_created_at = None
    for entry in box_folder.item_collection['entries']:
        current_created_at = datetime.datetime.strptime(
            entry.get().created_at, '%Y-%m-%dT%H:%M:%S%z')
        if not latest_entry_created_at or current_created_at > latest_entry_created_at:
            latest_entry = entry
            latest_entry_created_at = current_created_at

    # Write CDS to file system
    with open(os.path.join(os.getcwd(), 'tmp', latest_entry.name), 'wb') as fh:
        latest_entry.download_to(fh)

    # Build response with CDS included
    response = flask.send_from_directory('tmp', filename=latest_entry.name)

    # Disable browser caching
    response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
    response.headers['Pragma'] = 'no-cache'
    response.headers['Expires'] = '0'

    # Respond to client request
    return response
Ejemplo n.º 11
0
def connectJWTAuth():

    auth = boxsdk.JWTAuth(client_id=BoxClientID,
                          client_secret=BoxClientSecret,
                          enterprise_id=BoxEnterpriseID,
                          jwt_key_id=BoxPublicKeyID,
                          rsa_private_key_file_sys_path=BoxRSAFile,
                          rsa_private_key_passphrase=BoxPassPhrase,
                          store_tokens=store_tokens)

    # https://github.com/box/box-python-sdk#authorization
    # access_token = auth.authenticate_instance()

    client = boxsdk.Client(auth)
    return client
Ejemplo n.º 12
0
def connectJWTAuth():

    brk2 = BoxRSAKey.replace('\\n', '\n')

    auth = boxsdk.JWTAuth(
        client_id=BoxClientID,
        client_secret=BoxClientSecret,
        enterprise_id=BoxEnterpriseID,
        jwt_key_id=BoxPublicKeyID,
        # NB: BoxRSAKey needs to be byte string vs. unicode
        rsa_private_key_data=brk2.encode(),  # BoxRSAKey.encode(),
        rsa_private_key_passphrase=BoxPassPhrase,
        store_tokens=store_tokens)

    # https://github.com/box/box-python-sdk#authorization
    # access_token = auth.authenticate_instance()

    client = boxsdk.Client(auth)
    return client
Ejemplo n.º 13
0
 def __init__(self, stache_secret):
     # Authenticate the API client using the JWT authentication method.
     private_key_stream = BytesIO(
         stache_secret["boxAppSettings"]["appAuth"]["privateKey"].encode())
     jwt_options = {
         "client_id":
         stache_secret["boxAppSettings"]["clientID"],
         "client_secret":
         stache_secret["boxAppSettings"]["clientSecret"],
         "enterprise_id":
         stache_secret["enterpriseID"],
         "jwt_key_id":
         stache_secret["boxAppSettings"]["appAuth"]["publicKeyID"],
         "rsa_private_key_passphrase":
         stache_secret["boxAppSettings"]["appAuth"]["passphrase"].encode(),
         "rsa_private_key_data":
         private_key_stream
     }
     auth = boxsdk.JWTAuth(**jwt_options)
     auth.authenticate_instance()
     self.client = boxsdk.Client(auth)
Ejemplo n.º 14
0
def start_session():
    """Initialize a Box session using Box API.  """
    global access_token, refresh_token, server_thread, httpd
    # Try to retrieve tokens from keychain
    #   If error during OAuth or no tokens, re-authenticate
    try:
        access_token, refresh_token = retrieve_tokens()
        oauth = boxsdk.OAuth2(client_id=BOX_CLIENT_ID,
                              access_token=access_token,
                              refresh_token=refresh_token,
                              client_secret=BOX_CLIENT_SECRET,
                              store_tokens=store_tokens)
        if None in (access_token, refresh_token):
            raise boxsdk.exception.BoxOAuthException('No Tokens')
    except boxsdk.exception.BoxOAuthException:
        # Authenticate with OAuth
        print("\nAuthenticating...".ljust(71), end="", flush=True)
        oauth = boxsdk.OAuth2(client_id=BOX_CLIENT_ID,
                              client_secret=BOX_CLIENT_SECRET,
                              store_tokens=store_tokens)

        # Start HTTP server to get auth token from Box.com redirect
        server_thread = threading.Thread(target=run_server)
        server_thread.start()
        auth_url, csrf = oauth.get_authorization_url('http://127.0.0.1:7777')

        # Open browser and wait for user authentification
        webbrowser.open(auth_url)
        while not auth_code:
            pass

        # Shutdown HTTP Server
        httpd.shutdown()
        server_thread.join()
        access_token, refresh_token = oauth.authenticate(auth_code)
        httpd.shutdown()
        print("Complete!")

    # Return authenticated client
    return boxsdk.Client(oauth)
Ejemplo n.º 15
0
def main():
    configure_logging()
    # Load the Box Platform app's JWT keys into memory as a dicitons
    with open(BOX_AUTH_FILE_PATH) as fh:
        box_auth_dict = json.load(fh)

    # Get Box access tokens with the JWT keys from Box's authentication service
    auth = boxsdk.JWTAuth.from_settings_dictionary(box_auth_dict)
    auth.authenticate_instance()

    # Set up a Box HTTP client
    box_client = boxsdk.Client(auth)

    # Set up a rate limiter
    rate_limiter = RateLimiter()

    while True:

        # Call the rate limiter as content manager
        with rate_limiter:

            # Call Box
            folder = box_client.folder("0")
            log.info(f"got Box folder {folder}")
Ejemplo n.º 16
0
def get_refresh_token_from_file():
    data = get_token()
    return data['refresh_token']

def get_access_token():
    secret_token(get_refresh_token_from_file())
    return get_access_token_from_file()

oauth = boxsdk.OAuth2(
    client_id=box_client_id,
    client_secret=box_client_secret,
    access_token=get_access_token(),
)

client = boxsdk.Client(oauth)
# root_folder = client.folder(folder_id='0').get_items(limit=100, offset=0)
list_to_create = []

def log(message):
    with open('log.txt', 'a') as f:
        f.write(message + '\n')

def sync_create_file(dropbox_path, box_folder_id):
    file_name = dropbox_path[1:].split('/')[-1]
    sys.stdout.write('Downloading from Dropbox -')
    sys.stdout.flush()
    with open('temp-'+file_name, 'wb') as temp_f:
        metadata, res = dbx.files_download(dropbox_path)
        temp_f.write(res.content)
    sys.stdout.write(' Uploading to Box -')
Ejemplo n.º 17
0
def configure_box_client(config: dict) -> boxsdk.Client:
    box_auth = configure_box_auth(config)
    client = boxsdk.Client(box_auth)

    return client
Ejemplo n.º 18
0
    def __init__(self, args, flags=[], client='microsoft'):
        parser = argparse.ArgumentParser()
        parser.add_argument(
            '-c',
            '--config',
            help='Config file location',
            default='/etc/odm.yaml',
        )
        parser.add_argument(
            '-v',
            '--verbose',
            help='Enable verbose output',
            action='count',
            default=0,
        )
        for arg in args:
            parser.add_argument(arg)

        for flag in flags:
            parser.add_argument(flag, action='store_true')

        self.args = parser.parse_args()

        with open(self.args.config, 'r') as configfile:
            self.config = yaml.safe_load(configfile)

        self.config['args'] = self.args

        # Configure root logger
        logger = logging.getLogger()
        handler = logging.StreamHandler(sys.stderr)
        handler.setFormatter(
            logging.Formatter('%(asctime)s %(name)s: %(message)s',
                              '%Y-%m-%dT%H:%M:%S'))
        if self.args.verbose == 0:
            logger.setLevel(logging.WARNING)
        elif self.args.verbose == 1:
            logger.setLevel(logging.INFO)
        else:
            logger.setLevel(logging.DEBUG)

        if self.args.verbose < 3:
            handler.addFilter(lambda x: x.name.startswith('odm'))

        logger.addHandler(handler)

        self.logger = logging.getLogger(__name__)

        self.logger.debug('Using config file %s', self.args.config)

        if client == 'google':
            self.client = googledriveclient.GoogleDriveClient(self.config)
        elif client == 'microsoft':
            self.client = onedriveclient.OneDriveClient(self.config)
        elif client == 'box':
            auth = boxsdk.JWTAuth(
                client_id=self.config['box']['clientID'],
                client_secret=self.config['box']['clientSecret'],
                enterprise_id=self.config['box']['enterpriseID'],
                jwt_key_id=self.config['box']['appAuth']['publicKeyID'],
                rsa_private_key_data=self.config['box']['appAuth']
                ['privateKey'],
                rsa_private_key_passphrase=self.config['box']['appAuth']
                ['passphrase'],
            )
            session = boxsdk.session.session.AuthorizedSession(
                auth,
                default_headers={
                    'Box-Notifications': 'off',
                },
            )
            self.client = boxsdk.Client(auth, session)
Ejemplo n.º 19
0
#Box API configurations
with open('box_config.json', 'r') as f:
    boxapi = json.load(f)
box_auth = boxsdk.JWTAuth(
    client_id=boxapi["boxAppSettings"]["clientID"],
    client_secret=boxapi["boxAppSettings"]["clientSecret"],
    enterprise_id=boxapi["enterpriseID"],
    jwt_key_id=boxapi["boxAppSettings"]["appAuth"]["publicKeyID"],
    rsa_private_key_data=boxapi["boxAppSettings"]["appAuth"]["privateKey"],
    rsa_private_key_passphrase=boxapi["boxAppSettings"]["appAuth"]
    ["passphrase"],
)

box_access_token = box_auth.authenticate_instance()
box_client = boxsdk.Client(box_auth)


#Roman numeral utility
def toRoman(data):
    romans = ["I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
    romin = int(data) - 1
    if romin >= 0 and romin < len(romans):
        romreg = romans[romin]
    else:
        romreg = data
    return romreg


@app.route('/debug-sentry')
def trigger_error():
Ejemplo n.º 20
0
def configure_box_client(config: dict) -> boxsdk.Client:
    box_auth = configure_jwt_auth(config)
    box_client = boxsdk.Client(box_auth)
    log.debug(f"set up Box client {box_client}")

    return box_client
Ejemplo n.º 21
0
def box_client_from_config(config: dict) -> boxsdk.Client:
    box_auth = box_auth_from_config(config)
    client = boxsdk.Client(box_auth)

    return client
Ejemplo n.º 22
0
def poll_cds():

    # Create database connection to OpenEMR MySQL backend
    db_connection = pymysql.connect(host='localhost',
                                    user='******',
                                    password='******',
                                    db='openemr',
                                    cursorclass=pymysql.cursors.DictCursor)

    # Create Redis connection for cache
    cache_connection = redis.Redis(host='localhost', port=6379, db=0)

    # Get database cursor
    with db_connection.cursor() as cursor:
        try:

            # Query OpenEMR for one patient record
            cursor.execute("SELECT * FROM openemr.patient_data;")
            db_record = cursor.fetchone()

            if db_record:

                # ------------------------------------------------------------------------------------------------------
                # This is where the clinical subject matter experts logic to determine care recommendations is
                # implemented. For the demo, the CDS logic is simply
                #
                # Create CDS if never done before or patient age changes
                # If we create CDS
                #     Make no recommendations if the patient is under or 26 years old
                #     Make one set of recommendations if the patient is over 26 years old
                #
                # ------------------------------------------------------------------------------------------------------

                # Get patient age
                db_patient_dob = db_record['DOB']
                today = datetime.date.today()
                db_patient_age = today.year - db_patient_dob.year - (
                    (today.month, today.day) <
                    (db_patient_dob.month, db_patient_dob.day))

                # Get patient age
                patient_id = db_record['id']

                # Retrieve patient age from cache - cache records represented as {patient id: patient age}
                cached_patient_age = cache_connection.get(patient_id)

                # Generate CDS if first encounter of patient of patient age changed since last poll
                if not cached_patient_age or int(
                        cached_patient_age) != db_patient_age:
                    # Update cache with new patient id age mapping
                    cache_connection.set(patient_id, db_patient_age)

                    # Render CDS recommendations as HTML
                    provide_recommendation = True if db_patient_age > 26 else False
                    # ------------------------------------------------------------------------------------------------------

                    template_env_loader = jinja2.FileSystemLoader(
                        os.path.join(os.getcwd(), 'static'))
                    template_env = jinja2.Environment(
                        loader=template_env_loader)
                    cds_template = template_env.get_template(
                        'cds_template.html')
                    rendered_cds = cds_template.render(
                        provide_recommendation=provide_recommendation,
                        age=db_patient_age,
                        fname=db_record['fname'],
                        lname=db_record['lname'],
                        sex=db_record['sex'],
                        patient_id=patient_id)
                    tmp_cds_path = os.path.join(
                        os.getcwd(),
                        f'tmp/cds_{patient_id}_{db_patient_age}_{int(time.time())}.pdf'
                    )

                    # Convert rendered HTML to PDF
                    pdfkit.from_string(rendered_cds, tmp_cds_path)

                    # Authenticate with Box web services and get web service client
                    box_auth = boxsdk.JWTAuth.from_settings_file(
                        os.path.join('box_auth',
                                     'kaiser_permanente_california.json'))
                    box_client = boxsdk.Client(box_auth)

                    # Upload CDS to Box
                    box_client.folder('0').upload(tmp_cds_path)

                    # Remove CDS from file system
                    os.remove(tmp_cds_path)
        finally:

            # Close database cursor
            cursor.close()
#
import watchtower
# print(desktop_uploader.get_file_created("test/iguana.jpg"))

# Global
SECONDS_DELAY = 10.0
last_reference = {}
try:
    with open('persist.json') as f:
        last_reference = json.load(f)
except:
    pass
lock = threading.Lock()
t = None
auth = boxsdk.JWTAuth.from_settings_file('box_config.json')
client = boxsdk.Client(auth)
with open('config.json') as f:
    config = json.load(f)
unprocessed_dir = config['unprocessed_dir']
error_dir = config['error_dir']
done_dir = config['done_dir']
postgres = config['postgres']
cloudwatch = config['cloudwatch']

# Fail on startup if something's wrong
print("Checking the connections...")
# None of the dirs should be the same as another
assert (len([unprocessed_dir, error_dir,
             done_dir]) == len(set([unprocessed_dir, error_dir, done_dir])))
# Check Box connection
client.user().get()
Ejemplo n.º 24
0
            file_name = os.path.join(folder_name, item.name)
            with open(file_name, 'bw') as file:
                item.download_to(file)
                logging.info('Downloaded {}.'.format(file))
        else:
            new_folder_name = os.path.join(folder_name, item.name)
            os.makedirs(new_folder_name)
            try:
                new_folder_content = item.get_items()
                download_items(new_folder_content, folder_name=new_folder_name)
            except:
                print('Error on {}'.format(item))


if __name__ == '__main__':

    # Authenticate
    sdk = box.JWTAuth.from_settings_file(
        '/home/tbiancal/.ssh/hubmap-downloader_box.json')
    client = box.Client(sdk)

    # Get root folder
    root_folder = client.folder(folder_id='0')
    root_items = root_folder.get_items()
    items = [item for item in root_items]
    folder = items[0]
    assert folder.name == 'HuBMAP_Satija_Kharchenko_CCF'
    items = folder.get_items()

    download_items(items)