User system things

This commit is contained in:
Nadja Reitzenstein 2022-04-28 20:57:40 +02:00
parent 4b00313372
commit c8ed03caec
5 changed files with 49 additions and 18 deletions

View File

@ -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')

View File

@ -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:

11
fabapi/session.py Normal file
View File

@ -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

View File

@ -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()

14
test.py
View File

@ -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())