diff --git a/fabapi/api.py b/fabapi/api.py deleted file mode 100644 index 82dd6f3..0000000 --- a/fabapi/api.py +++ /dev/null @@ -1,7 +0,0 @@ -import os -import capnp - -path = os.path.dirname(os.path.abspath(__file__)) - -connection_capnp = capnp.load(path + '/schema/connection.capnp') -authenticationsystem = capnp.load(path + '/schema/authenticationsystem.capnp') \ No newline at end of file diff --git a/fabapi/connect.py b/fabapi/connect.py index 9c01dbe..f5c9374 100644 --- a/fabapi/connect.py +++ b/fabapi/connect.py @@ -2,9 +2,9 @@ import asyncio import socket import ssl -from fabapi import api -connection_capnp = api.connection_capnp -authenticationsystem_capnp = api.authenticationsystem +import capnp +from .schema import connection_capnp +from .schema import authenticationsystem_capnp async def myreader(client, reader): while True: diff --git a/fabapi/session.py b/fabapi/session.py new file mode 100644 index 0000000..90d631b --- /dev/null +++ b/fabapi/session.py @@ -0,0 +1,11 @@ +class Session: + """ + A connected API session + + Implements housekeeping functionality around conneciton keep-alive and re-establishment. + """ + + session = None + + def __init__(self, session): + self.session = session \ No newline at end of file diff --git a/fabapi/user_system.py b/fabapi/user_system.py index e4a0271..c485534 100644 --- a/fabapi/user_system.py +++ b/fabapi/user_system.py @@ -1,12 +1,27 @@ +import capnp +from .schema import usersystem_capnp +from .schema import role_capnp - -class UserSystem: +async def add_user(usersystem, username, password, roles=None): """ - Higher-level API for managing users + Add a new user with a given username and password. + + If provided a given list of roles is granted to the newly created user. + Returns the new user """ - api_usersystem = None - - def __init__(self, usersystem): - self.api_usersystem = usersystem + new_user_fallible = await usersystem.manage.addUserFallible(username, password).a_wait() + if new_user_fallible.which._str() == "successful": + new_user = new_user_fallible.successful + if roles: + for r in roles: + new_user.admin.addRole(role_capnp.Role(name=r)) + return new_user + else: + return new_user_fallible.failed.error +async def del_user(usersystem, username): + user_optional = await usersystem.search.getUserByName(username).a_wait() + if user_optional.which._str() == "just": + user = user_optional.just + await usersystem.manage.removeUser(user).a_wait() diff --git a/test.py b/test.py index dd53237..5048ac2 100644 --- a/test.py +++ b/test.py @@ -1,6 +1,6 @@ import asyncio import fabapi - +import fabapi.user_system async def main(): session = await fabapi.connect("localhost", 59661, "Testuser", "secret") @@ -8,6 +8,18 @@ async def main(): ma = await info.info.getMachineURN("urn:fabaccess:resource:Another").a_wait() print(ma) + # Add an user with the given roles + roles = [ + "somerole", "testrole" + ] + user = await fabapi.user_system.add_user(session.userSystem, "ANewUser", "ANewSecret", roles=roles) + + # As you can see, Roles were attached + await user.info.listRoles().a_wait() + + # Delete the same user again + await fabapi.user_system.del_user(session.userSystem, "ANewUser") + if __name__ == "__main__": loop = asyncio.get_event_loop() loop.run_until_complete(main())