-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(backend): Initial account creation endpoints
- Loading branch information
1 parent
e48e28c
commit 3f4ffbd
Showing
9 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
from typing import NewType | ||
from typing import NewType, TypeAlias | ||
from odmantic import AIOEngine | ||
|
||
WalletAddress = NewType("WalletAddress", str) | ||
""" | ||
A type for vault wallet addresses. | ||
""" | ||
|
||
Engine: TypeAlias = AIOEngine | ||
""" | ||
A database engine instance. | ||
""" | ||
|
||
HashString = NewType("HashString", str) | ||
""" | ||
HashString | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
User Authentication service for the Nautilus Vault backend. | ||
""" |
3 changes: 3 additions & 0 deletions
3
backend-services/src/user_auth_service/operations/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
The collection of operations provided by the user authenticatioin service. | ||
""" |
25 changes: 25 additions & 0 deletions
25
backend-services/src/user_auth_service/operations/authenticate_user.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from common.types import Engine | ||
from create_new_user import argon2_context | ||
from fastapi import HTTPException | ||
from user_auth_service.schema.actions import AuthenticateUser, AuthenticateUserResult, AuthenticateUserSuccess, AuthenticateUserFailure | ||
from user_auth_service.schema.entites import UserDetailsStorable | ||
|
||
def verify_password(password_attempt: str, hashed_password: str): | ||
return argon2_context.verify(password_attempt, hashed_password) | ||
|
||
async def authenticate_user(engine: Engine, params: AuthenticateUser) -> AuthenticateUserResult: | ||
""" | ||
Look through DB to see if any user matches to the above. | ||
If user exists, verify password. | ||
""" | ||
existing_user = await engine.find_one(UserDetailsStorable, UserDetailsStorable.email_address == AuthenticateUser.email_address) | ||
if existing_user is None: | ||
return AuthenticateUserFailure( | ||
Failed = 'Username does not exist.' | ||
) | ||
|
||
if not verify_password(AuthenticateUser.password,existing_user.hashed_password): | ||
return AuthenticateUserFailure( | ||
Failed = 'Invalid Password' | ||
) | ||
return existing_user |
36 changes: 36 additions & 0 deletions
36
backend-services/src/user_auth_service/operations/create_new_user.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from user_auth_service.schema.actions import CreateNewUser, CreateNewUserResult, CreateNewUserSuccess, CreateNewUserFailure | ||
from user_auth_service.schema.entites import UserDetailsStorable, UserDisplay | ||
from common.types import Engine | ||
from passlib.context import CryptContext | ||
|
||
argon2_context = CryptContext(schemes=['argon2'], depricated='auto') | ||
|
||
def password_hash(password: str): | ||
return argon2_context.hash(password) | ||
|
||
|
||
async def create_new_user(engine: Engine, params: CreateNewUser) -> CreateNewUserResult: | ||
""" | ||
User Creation. | ||
""" | ||
hash_password = password_hash(params.password) | ||
|
||
new_user = UserDetailsStorable( | ||
email_address = params.email_address, | ||
full_name = params.full_name, | ||
phone_number = params.phone_number, | ||
password_hash_string = hash_password | ||
) | ||
try: | ||
await engine.save(new_user) | ||
user_display = UserDisplay( | ||
user_id = UserDetailsStorable.id, | ||
email_address = params.email_address, | ||
full_name = params.full_name, | ||
phone_number = params.phone_number | ||
) | ||
return CreateNewUserSuccess(created=user_display) | ||
except: | ||
return CreateNewUserFailure( | ||
Failed = 'Unable to save user credentials.' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
""" | ||
Internals and abstractions for the User Authentication service. | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from user_auth_service.schema.entites import UserDisplay | ||
from pydantic import BaseModel | ||
|
||
from common.types import WalletAddress | ||
from datetime import datetime | ||
from typing import TypeAlias | ||
|
||
|
||
class CreateNewUser(BaseModel): | ||
""" | ||
User creation parameters. | ||
""" | ||
|
||
full_name: str | ||
phone_number: str | ||
email_address: str | ||
password: str | ||
|
||
class CreateNewUserSuccess(BaseModel): | ||
""" | ||
Return email address, full name, and phone number. | ||
""" | ||
|
||
Created: UserDisplay | ||
|
||
class CreateNewUserFailure(BaseModel): | ||
""" | ||
Return Failure if user's credentials is not created. | ||
""" | ||
Failed: str | ||
|
||
CreateNewUserResult: TypeAlias = CreateNewUserSuccess | CreateNewUserFailure | ||
|
||
class AuthenticateUser(BaseModel): | ||
""" | ||
Authentic user parameters. | ||
""" | ||
email_address: str | ||
password: str | ||
|
||
class AuthenticateUserSuccess(BaseModel): | ||
""" | ||
Successfully authenticated user. | ||
""" | ||
|
||
Opened: str | ||
|
||
class AuthenticateUserFailure(BaseModel): | ||
""" | ||
Failed to authenticate user. | ||
""" | ||
|
||
Failed: str | ||
|
||
class AuthenticateUserResult: TypeAlias = AuthenticateUserSuccess | AuthenticateUserFailure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import TypeAlias | ||
from common.types import HashString | ||
|
||
from odmantic import Model | ||
from pydantic import BaseModel | ||
|
||
from common.types import HashString | ||
|
||
class UserDetailsStorable(Model): | ||
""" | ||
Storing new ueser's credentials. | ||
""" | ||
|
||
email_address: str | ||
full_name: str; | ||
phone_number: str | ||
password_hash_string: HashString; | ||
|
||
class Config: | ||
collection = 'user' | ||
|
||
class UserDisplay(BaseModel): | ||
""" | ||
Return User credentials when user is created or opened. | ||
""" | ||
|
||
user_id: str | ||
email_address: str | ||
owner_name: str | ||
phone_number: str | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters