Пример #1
0
def post_api_key(
        api_key: rest_models.BaseApiKey,
        dao: Dao = Depends(get_dao),
        auth: authorization.Rules = Depends(get_rules),
):

    auth.assert_create_api_key_roles(api_key.roles)

    user_id = auth.assert_user()

    key = generate_random_key(32)
    dao.create_api_key(user_id, api_key, key)

    user_role_keys, custom_role_keys = dao.get_api_keys_with_members(
        user_id, key)

    if len(user_role_keys) > 0:
        key = user_role_keys[0]
        return rest_models.ApiKey(
            key=key.key,
            description=key.description,
            time_created=key.time_created,
            expire_at=key.expire_at,
            roles=None,
        )

    else:
        key = custom_role_keys[0][0]
        package_member = custom_role_keys[0][1]
        channel_member = custom_role_keys[0][2]
        roles = []
        if package_member:
            roles.append(
                CPRole(
                    channel=package_member.channel_name,
                    package=package_member.package_name,
                    role=package_member.role,
                ))

        if channel_member:
            roles.append(
                CPRole(
                    channel=channel_member.channel_name,
                    package=None,
                    role=channel_member.role,
                ))

        return rest_models.ApiKey(
            key=key.key,
            description=key.description,
            time_created=key.time_created,
            expire_at=key.expire_at,
            roles=roles,
        )
Пример #2
0
def get_api_keys(dao: Dao = Depends(get_dao),
                 auth: authorization.Rules = Depends(get_rules)):
    """Get API keys for current user"""

    user_id = auth.assert_user()
    user_role_keys, custom_role_keys = dao.get_api_keys_with_members(user_id)

    api_keys = []

    for key in user_role_keys:
        api_keys.append(
            rest_models.ApiKey(
                key=key.key,
                description=key.description,
                time_created=key.time_created,
                expire_at=key.expire_at,
                roles=None,
            ))

    from itertools import groupby

    grouped_by_key = groupby(custom_role_keys, key=lambda k: k[0])

    for group_key, group_items in grouped_by_key:
        roles = []
        for _, package_member, channel_member in group_items:
            if package_member:
                roles.append(
                    CPRole(
                        channel=package_member.channel_name,
                        package=package_member.package_name,
                        role=package_member.role,
                    ))
            if channel_member:
                roles.append(
                    CPRole(
                        channel=channel_member.channel_name,
                        package=None,
                        role=channel_member.role,
                    ))
        api_keys.append(
            rest_models.ApiKey(
                key=group_key.key,
                description=group_key.description,
                time_created=group_key.time_created,
                expire_at=group_key.expire_at,
                roles=roles,
            ))

    return api_keys