Esempio n. 1
0
def get_api_user(
        security: IdEncodingHelper = depends(IdEncodingHelper),
        user_manager: UserManager = depends(UserManager),
        key: Optional[str] = Query(None),
        x_api_key: Optional[str] = Header(None),
        run_as:
    Optional[EncodedDatabaseIdField] = Header(
        default=None,
        title='Run as User',
        description=
        ('The user ID that will be used to effectively make this API call. '
         'Only admins and designated users can make API calls on behalf of other users.'
         ))) -> Optional[User]:
    api_key = key or x_api_key
    if not api_key:
        return None
    user = user_manager.by_api_key(api_key=api_key)
    if run_as:
        if user_manager.user_can_do_run_as(user):
            try:
                decoded_run_as_id = security.decode_id(run_as)
            except Exception:
                raise UserInvalidRunAsException
            return user_manager.by_id(decoded_run_as_id)
        else:
            raise UserCannotRunAsException
    return user
Esempio n. 2
0
def decode_dataset_user(trans, dataset_hash, user_hash):
    # decode dataset id as usual
    # decode user id using the dataset create time as the key
    dataset_id = trans.security.decode_id(dataset_hash)
    dataset = trans.sa_session.query(
        trans.app.model.HistoryDatasetAssociation).get(dataset_id)
    assert dataset, "Bad Dataset id provided to decode_dataset_user"
    if user_hash in [None, 'None']:
        user = None
    else:
        security = IdEncodingHelper(id_secret=dataset.create_time)
        user_id = security.decode_id(user_hash)
        user = trans.sa_session.query(trans.app.model.User).get(user_id)
        assert user, "A Bad user id was passed to decode_dataset_user"
    return dataset, user
Esempio n. 3
0
def get_api_user(security: IdEncodingHelper = depends(IdEncodingHelper),
                 user_manager: UserManager = depends(UserManager),
                 key: Optional[str] = Query(None),
                 x_api_key: Optional[str] = Header(None),
                 run_as: Optional[EncodedDatabaseIdField] = Header(
                     None, title='Run as User',
                     description='Admins and ')) -> Optional[User]:
    api_key = key or x_api_key
    if not api_key:
        return None
    user = user_manager.by_api_key(api_key=api_key)
    if run_as:
        if user_manager.user_can_do_run_as(user):
            try:
                decoded_run_as_id = security.decode_id(run_as)
            except Exception:
                raise UserInvalidRunAsException
            return user_manager.by_id(decoded_run_as_id)
        else:
            raise UserCannotRunAsException
    return user
Esempio n. 4
0
def decode_with_security(security: IdEncodingHelper, id: Any):
    return security.decode_id(str(id))