add user types and get_user_by_username

pull/12/head
def 2022-07-23 11:39:52 +02:00
parent ce20adb564
commit f8c3f01bc4
4 changed files with 87 additions and 32 deletions

View File

@ -5,11 +5,12 @@
import strawberry
from selfprivacy_api.graphql import IsAuthenticated
from selfprivacy_api.graphql.common_types.user import User, UserMutationReturn
from selfprivacy_api.graphql.common_types.user import User, UserMutationReturn, UserType
from selfprivacy_api.utils import (
WriteUserData,
ReadUserData,
get_user_by_username,
validate_ssh_public_key,
)
@ -19,46 +20,47 @@ class SshMutationsInput:
"""Input type for ssh mutation"""
username: str
sshKey: str
ssh_key: str
@strawberry.type
class UserMutations:
class SshMutations:
"""Mutations ssh"""
@strawberry.mutation(permission_classes=[IsAuthenticated])
def create_ssh(self, input: SshMutationsInput) -> UserMutationReturn:
"""Create a new ssh"""
if not validate_ssh_public_key(input.ssh_key):
return UserMutationReturn(
success=False,
message="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
code=400,
user=get_user_by_username(input.username),
)
with WriteUserData() as data:
if not validate_ssh_public_key(input.sshKey):
return UserMutationReturn(
success=False,
message="Invalid key type. Only ssh-ed25519 and ssh-rsa are supported",
code=400,
user=User(input.username, data["users"][input.username]["sshKeys"]),
)
if input.username == data["username"]:
if "sshKeys" not in data:
data["sshKeys"] = []
# Return 409 if key already in array
for key in data["sshKeys"]:
if key == input.sshKey:
if key == input.ssh_key:
return UserMutationReturn(
success=False,
message="Key already exists",
code=409,
user=User(data["username"], data["sshKeys"]),
user=get_user_by_username(input.username),
)
data["sshKeys"].append(input.sshKey)
data["sshKeys"].append(input.ssh_key)
return UserMutationReturn(
success=True,
message="New SSH key successfully written",
code=201,
user=User(data["username"], data["sshKeys"]),
user=get_user_by_username(input.username),
)
if "users" not in data:
@ -69,22 +71,22 @@ class UserMutations:
user["sshKeys"] = []
# Return 409 if key already in array
for key in user["sshKeys"]:
if key == input.sshKey:
if key == input.ssh_key:
return UserMutationReturn(
success=False,
message="Key already exists",
code=409,
user=User(user["username"], user["sshKeys"]),
user=get_user_by_username(input.username),
)
user["sshKeys"].append(input.sshKey)
user["sshKeys"].append(input.ssh_key)
return UserMutationReturn(
success=True,
message="New SSH key successfully written",
code=201,
user=User(user["username"], user["sshKeys"]),
user=get_user_by_username(input.username),
)
return UserMutationReturn(
@ -106,40 +108,40 @@ class UserMutations:
data["ssh"]["rootKeys"] = []
# Return 404 if key not in array
for key in data["ssh"]["rootKeys"]:
if key == input.sshKey:
if key == input.ssh_key:
data["ssh"]["rootKeys"].remove(key)
return UserMutationReturn(
success=True,
message="SSH key deleted",
code=200,
user=User("root", data["ssh"]["rootKeys"]),
user=get_user_by_username(input.username),
)
return UserMutationReturn(
success=False,
message="Key not found",
code=404,
user=User("root", data["ssh"]["rootKeys"]),
user=get_user_by_username(input.username),
)
if input.username == data["username"]:
if "sshKeys" not in data:
data["sshKeys"] = []
# Return 404 if key not in array
for key in data["sshKeys"]:
if key == input.sshKey:
if key == input.ssh_key:
data["sshKeys"].remove(key)
return UserMutationReturn(
success=True,
message="SSH key deleted",
code=200,
user=User(data["username"], data["sshKeys"]),
user=get_user_by_username(input.username),
)
return UserMutationReturn(
success=False,
message="Key not found",
code=404,
user=User(data["username"], data["sshKeys"]),
user=get_user_by_username(input.username),
)
if "users" not in data:
data["users"] = []
@ -149,20 +151,20 @@ class UserMutations:
user["sshKeys"] = []
# Return 404 if key not in array
for key in user["sshKeys"]:
if key == input.sshKey:
if key == input.ssh_key:
user["sshKeys"].remove(key)
return UserMutationReturn(
success=True,
message="SSH key deleted",
code=200,
user=User(input.username, user["sshKeys"]),
user=get_user_by_username(input.username),
)
return UserMutationReturn(
success=False,
message="Key not found",
code=404,
user=User(input.username, user["sshKeys"]),
user=get_user_by_username(input.username),
)
return UserMutationReturn(

View File

@ -10,7 +10,12 @@ from selfprivacy_api.graphql.common_types.user import User, UserMutationReturn
from selfprivacy_api.graphql.mutations.mutation_interface import (
MutationReturnInterface,
)
from selfprivacy_api.utils import WriteUserData, ReadUserData, is_username_forbidden
from selfprivacy_api.utils import (
WriteUserData,
ReadUserData,
get_user_by_username,
is_username_forbidden,
)
from selfprivacy_api.utils import hash_password
@ -92,7 +97,7 @@ class UserMutations:
success=True,
message="User was successfully created!",
code=201,
user=User(user.username),
user=get_user_by_username(user.username),
)
@strawberry.mutation(permission_classes=[IsAuthenticated])
@ -153,5 +158,5 @@ class UserMutations:
success=True,
message="User was successfully updated",
code=200,
user=User(user.username, ssh_keys),
user=get_user_by_username(user.username),
)

View File

@ -1,6 +1,6 @@
"""GraphQL API for SelfPrivacy."""
# pylint: disable=too-few-public-methods
import typing
import strawberry
from selfprivacy_api.graphql import IsAuthenticated
from selfprivacy_api.graphql.mutations.api_mutations import ApiMutations
@ -9,6 +9,9 @@ from selfprivacy_api.graphql.mutations.system_mutations import SystemMutations
from selfprivacy_api.graphql.queries.api_queries import Api
from selfprivacy_api.graphql.queries.system import System
from selfprivacy_api.resources.users import Users
from selfprivacy_api.graphql.mutations.users_mutations import UserMutations
@strawberry.type
class Query:
@ -24,9 +27,18 @@ class Query:
"""API access status"""
return Api()
@strawberry.field
def users(self) -> Users:
"""Users queries"""
return Users()
@strawberry.type
class Mutation(ApiMutations, SystemMutations):
class Mutation(
ApiMutations,
SystemMutations,
UserMutations,
):
"""Root schema for mutations"""
pass

View File

@ -7,6 +7,8 @@ import os
import subprocess
import portalocker
from selfprivacy_api.graphql.common_types.user import User, UserType
USERDATA_FILE = "/etc/nixos/userdata/userdata.json"
TOKENS_FILE = "/etc/nixos/userdata/tokens.json"
@ -173,3 +175,37 @@ def hash_password(password):
hashed_password = hashed_password.decode("ascii")
hashed_password = hashed_password.rstrip()
return hashed_password
def get_user_by_username(username):
with ReadUserData() as data:
if username == "root":
if data["ssh"]["rootKeys"] not in data:
data["ssh"]["rootKeys"] = []
return User(
user_type=UserType.ROOT,
username="root",
ssh_keys=data["ssh"]["rootKeys"],
)
elif username == data["username"]:
if "sshKeys" not in data:
data["sshKeys"] = []
return User(
user_type=UserType.PRIMARY,
username=username,
ssh_keys=data["sshKeys"],
)
else:
for user in data["users"]:
if user["username"] == username:
if "sshKeys" not in user:
user["sshKeys"] = []
return User(
user_type=UserType.NORMAL,
username=username,
ssh_keys=user["sshKeys"],
)