Repaired register by discord #70

This commit is contained in:
Sven Heidemann 2022-10-23 12:44:51 +02:00
parent 602b1f3f98
commit ffe7b5e109
14 changed files with 21561 additions and 21484 deletions

View File

@ -12,6 +12,12 @@
"FileLogLevel": "TRACE"
},
"BotLoggingSettings": {
"Api": {
"Path": "logs/",
"Filename": "api.log",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
},
"Command": {
"Path": "logs/",
"Filename": "commands.log",
@ -21,7 +27,7 @@
"Database": {
"Path": "logs/",
"Filename": "database.log",
"ConsoleLogLevel": "DEBUG",
"ConsoleLogLevel": "TRACE",
"FileLogLevel": "TRACE"
},
"Message": {

View File

@ -50,7 +50,7 @@ class AuthServiceABC(ABC):
async def add_auth_user_by_oauth_async(self, dto: OAuthDTO): pass
@abstractmethod
async def add_auth_user_by_discord_async(self, user_dto: AuthUserDTO) -> OAuthDTO: pass
async def add_auth_user_by_discord_async(self, user_dto: AuthUserDTO, dc_id: int) -> OAuthDTO: pass
@abstractmethod
async def update_user_async(self, update_user_dto: UpdateAuthUserDTO): pass

View File

@ -1 +1,28 @@
{}
{
"Api": {
"Port": 5000,
"Host": "0.0.0.0",
"RedirectToHTTPS": false
},
"Authentication": {
"SecretKey": "RjNiNUxEeisjSnZ6Zz1XIUBnc2EleHNG",
"Issuer": "http://localhost:5000",
"Audience": "http://localhost:4200",
"TokenExpireTime": 1,
"RefreshTokenExpireTime": 7
},
"DiscordAuthentication": {
"ClientSecret": "V3FTb3JYVFBiVktEeHZxdWJDWW4xcnBCbXRwdmpwcy0=",
"_RedirectURL": "http://localhost:5000/api/auth/discord/register",
"RedirectURL": "http://localhost:4200/auth/register",
"Scope": [
"identify",
"email"
],
"TokenURL": "https://discordapp.com/api/oauth2/token",
"AuthURL": "https://discordapp.com/api/oauth2/authorize"
},
"Frontend": {
"URL": "http://localhost:4200/"
}
}

View File

@ -78,9 +78,8 @@ class AuthDiscordController:
response['email'],
str(uuid.uuid4()),
None,
AuthRoleEnum.normal,
response['id']
))
AuthRoleEnum.normal
), response['id'])
return jsonify(result.to_dict())
@Route.post(f'{BasePath}/register')

View File

@ -8,8 +8,9 @@ from typing import Optional
import jwt
from cpl_core.database.context import DatabaseContextABC
from cpl_core.environment import ApplicationEnvironmentABC
from cpl_core.mailing import EMailClientABC, EMail
from cpl_core.mailing import EMail
from cpl_core.utils import CredentialManager
from cpl_discord.service import DiscordBotServiceABC
from cpl_query.extension import List
from cpl_translation import TranslatePipe
from flask import request
@ -31,9 +32,12 @@ from bot_api.model.token_dto import TokenDTO
from bot_api.model.update_auth_user_dto import UpdateAuthUserDTO
from bot_api.transformer.auth_user_transformer import AuthUserTransformer as AUT
from bot_data.abc.auth_user_repository_abc import AuthUserRepositoryABC
from bot_data.abc.server_repository_abc import ServerRepositoryABC
from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.model.auth_role_enum import AuthRoleEnum
from bot_data.model.auth_user import AuthUser
from bot_data.model.auth_user_users_relation import AuthUserUsersRelation
from bot_data.model.user import User
_email_regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
@ -44,9 +48,11 @@ class AuthService(AuthServiceABC):
self,
env: ApplicationEnvironmentABC,
logger: ApiLogger,
bot: DiscordBotServiceABC,
db: DatabaseContextABC,
auth_users: AuthUserRepositoryABC,
users: UserRepositoryABC,
db: DatabaseContextABC,
servers: ServerRepositoryABC,
mailer: MailThread,
t: TranslatePipe,
auth_settings: AuthenticationSettings,
@ -57,9 +63,11 @@ class AuthService(AuthServiceABC):
self._environment = env
self._logger = logger
self._bot = bot
self._db = db
self._auth_users = auth_users
self._users = users
self._db = db
self._servers = servers
self._mailer = mailer
self._t = t
self._auth_settings = auth_settings
@ -134,7 +142,7 @@ class AuthService(AuthServiceABC):
def _create_and_save_refresh_token(self, user: AuthUser) -> str:
token = str(uuid.uuid4())
user.refresh_token = token
user.refresh_token_expire_time = datetime.now(tz=timezone.utc) + timedelta(days=self._auth_settings.refresh_token_expire_time)
user.refresh_token_expire_time = datetime.now() + timedelta(days=self._auth_settings.refresh_token_expire_time)
self._auth_users.update_auth_user(user)
self._db.save_changes()
return token
@ -244,20 +252,21 @@ class AuthService(AuthServiceABC):
self._auth_users.update_auth_user(db_user)
self._db.save_changes()
async def add_auth_user_by_discord_async(self, user_dto: AuthUserDTO) -> OAuthDTO:
db_user = self._auth_users.find_auth_user_by_email(user_dto.email)
async def add_auth_user_by_discord_async(self, user_dto: AuthUserDTO, dc_id: int) -> OAuthDTO:
db_auth_user = self._auth_users.find_auth_user_by_email(user_dto.email)
# user exists
if db_user is not None and db_user.user_id is not None:
if db_auth_user is not None and db_auth_user.users.count() > 0:
# raise ServiceException(ServiceErrorCode.InvalidUser, 'User already exists')
self._logger.debug(__name__, f'Discord user already exists')
return OAuthDTO(AUT.to_dto(db_user), None)
return OAuthDTO(AUT.to_dto(db_auth_user), None)
# user exists but discord user id not set
elif db_user is not None and db_user.user_id is None:
elif db_auth_user is not None and db_auth_user.users.count() == 0:
self._logger.debug(__name__, f'Auth user exists but not linked with discord')
user = self._users.get_users_by_discord_id(user_dto.user_id).single()
db_user.user_id = user.user_id
db_user.oauth_id = None
# users = self._users.get_users_by_discord_id(user_dto.user_id)
# add auth_user to user refs
db_auth_user.oauth_id = None
else:
# user does not exists
@ -269,12 +278,24 @@ class AuthService(AuthServiceABC):
user_dto.user_id = None
await self.add_auth_user_async(user_dto)
db_user = self._auth_users.get_auth_user_by_email(user_dto.email)
db_user.oauth_id = uuid.uuid4()
db_auth_user = self._auth_users.get_auth_user_by_email(user_dto.email)
db_auth_user.oauth_id = uuid.uuid4()
self._auth_users.update_auth_user(db_user)
for g in self._bot.guilds:
member = g.get_member(int(dc_id))
if member is None:
continue
server = self._servers.get_server_by_discord_id(g.id)
users = self._users.get_users_by_discord_id(dc_id)
for user in users:
if user.server.server_id != server.server_id:
continue
self._auth_users.add_auth_user_user_rel(AuthUserUsersRelation(db_auth_user, user))
self._auth_users.update_auth_user(db_auth_user)
self._db.save_changes()
return OAuthDTO(AUT.to_dto(db_user), db_user.oauth_id)
return OAuthDTO(AUT.to_dto(db_auth_user), db_auth_user.oauth_id)
async def update_user_async(self, update_user_dto: UpdateAuthUserDTO):
if update_user_dto is None:

View File

@ -20,7 +20,7 @@ class AuthUserTransformer(TransformerABC):
None,
None,
None,
datetime.now(tz=timezone.utc),
datetime.now(),
AuthRoleEnum.normal if dto.auth_role is None else AuthRoleEnum(dto.auth_role),
dto.user_id,
auth_user_id=0 if dto.id is None else dto.id

View File

@ -6,6 +6,7 @@ from cpl_query.extension import List
from bot_api.filter.auth_user_select_criteria import AuthUserSelectCriteria
from bot_data.filtered_result import FilteredResult
from bot_data.model.auth_user import AuthUser
from bot_data.model.auth_user_users_relation import AuthUserUsersRelation
class AuthUserRepositoryABC(ABC):
@ -39,3 +40,12 @@ class AuthUserRepositoryABC(ABC):
@abstractmethod
def delete_auth_user(self, user: AuthUser): pass
@abstractmethod
def add_auth_user_user_rel(self, rel: AuthUserUsersRelation): pass
@abstractmethod
def update_auth_user_user_rel(self, rel: AuthUserUsersRelation): pass
@abstractmethod
def delete_auth_user_user_rel(self, rel: AuthUserUsersRelation): pass

View File

@ -30,8 +30,8 @@ class ApiMigration(MigrationABC):
`OAuthId` VARCHAR(255) DEFAULT NULL,
`RefreshTokenExpiryTime` DATETIME(6) NOT NULL,
`AuthRole` INT NOT NULL DEFAULT '0',
`CreatedOn` DATETIME(6) NOT NULL,
`LastModifiedOn` DATETIME(6) NOT NULL,
`CreatedAt` DATETIME(6) NOT NULL,
`LastModifiedAt` DATETIME(6) NOT NULL,
PRIMARY KEY(`Id`)
);
""")
@ -43,8 +43,8 @@ class ApiMigration(MigrationABC):
`Id` BIGINT NOT NULL AUTO_INCREMENT,
`AuthUserId` BIGINT DEFAULT NULL,
`UserId` BIGINT DEFAULT NULL,
`CreatedOn` DATETIME(6) NOT NULL,
`LastModifiedOn` DATETIME(6) NOT NULL,
`CreatedAt` DATETIME(6) NOT NULL,
`LastModifiedAt` DATETIME(6) NOT NULL,
PRIMARY KEY(`Id`),
FOREIGN KEY (`AuthUserId`) REFERENCES `AuthUsers`(`Id`),
FOREIGN KEY (`UserId`) REFERENCES `Users`(`UserId`)

View File

@ -207,8 +207,8 @@ class AuthUser(TableABC):
`OAuthId`,
`RefreshTokenExpiryTime`,
`AuthRole`,
`CreatedOn`,
`LastModifiedOn`
`CreatedAt`,
`LastModifiedAt`
) VALUES (
{self._auth_user_id},
'{self._first_name}',
@ -220,7 +220,7 @@ class AuthUser(TableABC):
'{"NULL" if self._confirmation_id is None else self._confirmation_id}',
'{"NULL" if self._forgot_password_id is None else self._forgot_password_id}',
'{"NULL" if self._oauth_id is None else self._oauth_id}',
'{self._refresh_token_expire_time}',
'{self._refresh_token_expire_time.isoformat()}',
{self._auth_role_id.value},
'{self._created_at}',
'{self._modified_at}'
@ -240,9 +240,9 @@ class AuthUser(TableABC):
`ConfirmationId` = '{"NULL" if self._confirmation_id is None else self._confirmation_id}',
`ForgotPasswordId` = '{"NULL" if self._forgot_password_id is None else self._forgot_password_id}',
`OAuthId` = '{"NULL" if self._oauth_id is None else self._oauth_id}',
`RefreshTokenExpiryTime` = '{self._refresh_token_expire_time}',
`RefreshTokenExpiryTime` = '{self._refresh_token_expire_time.isoformat()}',
`AuthRole` = {self._auth_role_id.value},
`LastModifiedOn` = '{self._modified_at}'
`LastModifiedAt` = '{self._modified_at}'
WHERE `AuthUsers`.`Id` = {self._auth_user_id};
""")

View File

@ -1,11 +1,9 @@
from datetime import datetime
from typing import Optional
from cpl_core.database import TableABC
from bot_data.model.auth_user import AuthUser
from bot_data.model.user import User
from bot_data.model.server import Server
class AuthUserUsersRelation(TableABC):

View File

@ -10,6 +10,7 @@ from bot_data.abc.user_repository_abc import UserRepositoryABC
from bot_data.filtered_result import FilteredResult
from bot_data.model.auth_role_enum import AuthRoleEnum
from bot_data.model.auth_user import AuthUser
from bot_data.model.auth_user_users_relation import AuthUserUsersRelation
from bot_data.model.user import User
@ -145,3 +146,15 @@ class AuthUserRepositoryService(AuthUserRepositoryABC):
def delete_auth_user(self, user: AuthUser):
self._logger.trace(__name__, f'Send SQL command: {user.delete_string}')
self._context.cursor.execute(user.delete_string)
def add_auth_user_user_rel(self, rel: AuthUserUsersRelation):
self._logger.trace(__name__, f'Send SQL command: {rel.insert_string}')
self._context.cursor.execute(rel.insert_string)
def update_auth_user_user_rel(self, rel: AuthUserUsersRelation):
self._logger.trace(__name__, f'Send SQL command: {rel.udpate_string}')
self._context.cursor.execute(rel.udpate_string)
def delete_auth_user_user_rel(self, rel: AuthUserUsersRelation):
self._logger.trace(__name__, f'Send SQL command: {rel.delete_string}')
self._context.cursor.execute(rel.delete_string)

View File

@ -101,5 +101,8 @@
}
}
}
},
"cli": {
"analytics": false
}
}

View File

@ -1,12 +1,12 @@
{
"name": "kdb-web",
"version": "0.3.0",
"version": "0.3.dev70",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "kdb-web",
"version": "0.3.0",
"version": "0.3.dev70",
"dependencies": {
"@angular/animations": "^14.0.0",
"@angular/common": "^14.0.0",