Exemple #1
0
def bewit(ctx, url, ttl):
    '''
    Create a bewit for the given URL.
    '''
    url = url.rstrip('/')
    if url == ctx['meta_post']['server']['urls']['posts_feed'] or url == ctx['meta_post']['entity']:
        click.echo("Use bewits only for specific posts!")
        return False
    if '?' in url:
        click.echo("Can't make bewits for queries!")
        return False
    click.echo("Bewit URL: ")
    click.echo(url + '?bewit=' + hawk_get_bewit(url, {'credentials': ctx['credentials'], 'ttl_sec': ttl * 60}))
Exemple #2
0
def before_insert_posts(documents):
    meta_post = current_app.config.get('META_POST')
    posts_endpoint = meta_post['server']['urls']['posts_feed']
    app_type = str(url_for('server_info.types_item', name='app', _external=True))
    credentials_type = str(url_for('server_info.types_item', name='credentials', _external=True))
    
    for document in documents:
        # Create version information, but save app version data if present
        current_time = time.time()
        time_seconds = int(current_time)
        time_millisec = int(current_time * 1000)
        time_microsec = int(current_time * 1000000)
        app_version = get_app_version(document)
        digest = create_version_digest(document)
        document['version'] = create_version_document(digest, time_millisec, app_version)
        
        # Create an ID for the document
        if document['type'] == credentials_type:
            # Since credentials posts are created automatically by the server,
            # we need to specify their IDs in microseconds
            document['_id'] = str(NewBase60(time_microsec))
        else:
            document['_id'] = str(NewBase60(time_seconds))
        
        # Additional processing for certain post types
        if document['type'] == app_type:
            # For app posts, we must create an additional credentials post
            # and make sure they link to each other
            hawk_key = str(random_string(64))
            hawk_algorithm = 'sha256'
            credentials_post = {
                'entity': str(meta_post['entity']),
                'type': credentials_type,
                'content': {
                    'hawk_key': hawk_key,
                    'hawk_algorithm': hawk_algorithm
                },
                'links': [
                    {
                        'post': str(document['_id']),
                        'url': str(posts_endpoint + "/" + document['_id']),
                        'type': app_type
                    }
                ]
            }
            response, _, _, status = post_internal('posts', credentials_post)
            if not 'links' in document:
                document['links'] = []
            cred_id = str(response['_id'])
            cred_url = str(posts_endpoint + "/" + cred_id)
            document['links'].append({
                    'post': cred_id,
                    'url': cred_url,
                    'type': credentials_type
                })
            # Create a signal to change the POST response envelope
            g.app_POST = True
            credentials = {
                'id': cred_id,
                'key': hawk_key,
                'algorithm': hawk_algorithm
            }
            g.cred_bewit_url = cred_url + '?bewit=' + hawk_get_bewit(cred_url, {'credentials': credentials, 'ttl_sec': BEWIT_TTL})