From 5cce6cfdae5ae41b7c711f343d1044fc71ccf545 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 22 Jun 2020 17:01:29 +0200 Subject: [PATCH] Change stuff --- royalnet/backpack/stars/__init__.py | 16 +++--- ...royalnet.py => api_auth_login_royalnet.py} | 6 +-- ...{api_token_create.py => api_auth_token.py} | 19 +++++-- royalnet/backpack/stars/api_token_info.py | 13 ----- royalnet/backpack/stars/api_user_create.py | 50 +++++++++++++++++++ ...api_token_passwd.py => api_user_passwd.py} | 14 ++++-- royalnet/backpack/stars/docs.py | 7 +-- royalnet/constellation/api/apierrors.py | 18 +++---- royalnet/constellation/api/apistar.py | 30 +++++++---- 9 files changed, 119 insertions(+), 54 deletions(-) rename royalnet/backpack/stars/{api_login_royalnet.py => api_auth_login_royalnet.py} (93%) rename royalnet/backpack/stars/{api_token_create.py => api_auth_token.py} (69%) delete mode 100644 royalnet/backpack/stars/api_token_info.py create mode 100644 royalnet/backpack/stars/api_user_create.py rename royalnet/backpack/stars/{api_token_passwd.py => api_user_passwd.py} (80%) diff --git a/royalnet/backpack/stars/__init__.py b/royalnet/backpack/stars/__init__.py index fc911ada..e41a2038 100644 --- a/royalnet/backpack/stars/__init__.py +++ b/royalnet/backpack/stars/__init__.py @@ -1,24 +1,24 @@ # Imports go here! from .api_royalnet_version import ApiRoyalnetVersionStar -from .api_login_royalnet import ApiLoginRoyalnetStar -from .api_token_info import ApiTokenInfoStar -from .api_token_passwd import ApiTokenPasswdStar -from .api_token_create import ApiTokenCreateStar +from .api_auth_login_royalnet import ApiAuthLoginRoyalnetStar +from .api_user_passwd import ApiUserPasswd +from .api_auth_token import ApiAuthTokenStar from .api_user_get import ApiUserGetStar from .api_user_list import ApiUserListStar from .api_user_find import ApiUserFindStar +from .api_user_create import ApiUserCreateStar from .docs import DocsStar # Enter the PageStars of your Pack here! available_page_stars = [ ApiRoyalnetVersionStar, - ApiLoginRoyalnetStar, - ApiTokenInfoStar, - ApiTokenPasswdStar, - ApiTokenCreateStar, + ApiAuthLoginRoyalnetStar, + ApiUserPasswd, + ApiAuthTokenStar, ApiUserGetStar, ApiUserListStar, ApiUserFindStar, + ApiUserCreateStar, DocsStar, ] diff --git a/royalnet/backpack/stars/api_login_royalnet.py b/royalnet/backpack/stars/api_auth_login_royalnet.py similarity index 93% rename from royalnet/backpack/stars/api_login_royalnet.py rename to royalnet/backpack/stars/api_auth_login_royalnet.py index d8b71c18..2bd4086e 100644 --- a/royalnet/backpack/stars/api_login_royalnet.py +++ b/royalnet/backpack/stars/api_auth_login_royalnet.py @@ -6,8 +6,8 @@ from ..tables.users import User from ..tables.tokens import Token -class ApiLoginRoyalnetStar(ApiStar): - path = "/api/login/royalnet/v1" +class ApiAuthLoginRoyalnetStar(ApiStar): + path = "/api/auth/login/royalnet/v1" methods = ["POST"] @@ -18,7 +18,7 @@ class ApiLoginRoyalnetStar(ApiStar): } } - tags = ["login"] + tags = ["auth"] async def post(self, data: ApiData) -> ru.JSON: """Login with a Royalnet account. diff --git a/royalnet/backpack/stars/api_token_create.py b/royalnet/backpack/stars/api_auth_token.py similarity index 69% rename from royalnet/backpack/stars/api_token_create.py rename to royalnet/backpack/stars/api_auth_token.py index 66d0b172..f1453bfc 100644 --- a/royalnet/backpack/stars/api_token_create.py +++ b/royalnet/backpack/stars/api_auth_token.py @@ -5,18 +5,29 @@ from royalnet.constellation.api import * from ..tables.tokens import Token -class ApiTokenCreateStar(ApiStar): - path = "/api/token/create/v1" +class ApiAuthTokenStar(ApiStar): + path = "/api/auth/token/v1" - methods = ["POST"] + methods = ["GET", "POST"] parameters = { + "get": {}, "post": { "duration": "The duration in seconds of the new token." } } - tags = ["login"] + auth = { + "get": True, + "post": True, + } + + tags = ["auth"] + + async def get(self, data: ApiData) -> ru.JSON: + """Get information about the current login token.""" + token = await data.token() + return token.json() async def post(self, data: ApiData) -> ru.JSON: """Create a new login token for the authenticated user. diff --git a/royalnet/backpack/stars/api_token_info.py b/royalnet/backpack/stars/api_token_info.py deleted file mode 100644 index 4b753234..00000000 --- a/royalnet/backpack/stars/api_token_info.py +++ /dev/null @@ -1,13 +0,0 @@ -import royalnet.utils as ru -from royalnet.constellation.api import * - - -class ApiTokenInfoStar(ApiStar): - path = "/api/token/info/v1" - - tags = ["login"] - - async def get(self, data: ApiData) -> ru.JSON: - """Get information about the current login token.""" - token = await data.token() - return token.json() diff --git a/royalnet/backpack/stars/api_user_create.py b/royalnet/backpack/stars/api_user_create.py new file mode 100644 index 00000000..ca7fd35d --- /dev/null +++ b/royalnet/backpack/stars/api_user_create.py @@ -0,0 +1,50 @@ +from typing import * +import datetime +import royalnet.constellation.api as rca +import royalnet.utils as ru +import royalnet.backpack.tables as rbt +from ..tables import * + + +class ApiUserCreateStar(rca.ApiStar): + path = "/api/user/create/v1" + + methods = ["POST"] + + parameters = { + "post": { + "username": "The name of the user you are creating.", + "password": "The password of the user you are creating.", + "email": "(Optional) The email of the user you are creating.", + "avatar_url": "(Optional) An URL pointing to the avatar of the user you are creating.", + } + } + + tags = ["user"] + + async def post(self, data: rca.ApiData) -> ru.JSON: + """Create a new Royalnet account.""" + UserT = self.alchemy.get(User) + + username = data.str("username") + password = data.str("password") + email = data.str("email", optional=True) + avatar_url = data.str("avatar_url", optional=True) + + # Check if the username is used by an user or an alias + user = await User.find(self.alchemy, data.session, username) + if user is not None: + raise rca.InvalidParameterError("An user with that username or alias already exists.") + + # Create the user + user = UserT( + username=username, + email=email, + avatar_url=avatar_url + ) + user.set_password(password) + user.add_alias(self.alchemy, username) + data.session.add(user) + await data.session_commit() + + return user.json() diff --git a/royalnet/backpack/stars/api_token_passwd.py b/royalnet/backpack/stars/api_user_passwd.py similarity index 80% rename from royalnet/backpack/stars/api_token_passwd.py rename to royalnet/backpack/stars/api_user_passwd.py index a2195ecb..bef929ad 100644 --- a/royalnet/backpack/stars/api_token_passwd.py +++ b/royalnet/backpack/stars/api_user_passwd.py @@ -6,12 +6,12 @@ from sqlalchemy import and_ from ..tables.tokens import Token -class ApiTokenPasswdStar(ApiStar): - path = "/api/token/passwd/v1" +class ApiUserPasswd(ApiStar): + path = "/api/user/passwd/v1" methods = ["PUT"] - tags = ["token"] + tags = ["user"] parameters = { "put": { @@ -19,10 +19,16 @@ class ApiTokenPasswdStar(ApiStar): } } + auth = { + "put": True + } + requires_auth = True async def put(self, data: ApiData) -> ru.JSON: - """Change the password of the currently logged in user.""" + """Change the password of the currently logged in user. + + This method also revokes all the issued tokens for the user.""" TokenT = self.alchemy.get(Token) token = await data.token() user = token.user diff --git a/royalnet/backpack/stars/docs.py b/royalnet/backpack/stars/docs.py index 03a929a9..76511c5c 100644 --- a/royalnet/backpack/stars/docs.py +++ b/royalnet/backpack/stars/docs.py @@ -42,11 +42,12 @@ class DocsStar(PageStar): Royalnet Docs - - - + + + +
{spec}