-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
user and oauth normalized + required crud
- Loading branch information
1 parent
666b047
commit d6971d0
Showing
2 changed files
with
43 additions
and
23 deletions.
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 |
---|---|---|
|
@@ -42,10 +42,15 @@ async def _create_user_from_email(self, email: str, password: str) -> User: | |
await self._add_item(user, unique_fields=["email"]) | ||
return user | ||
|
||
async def _create_user_from_auth_key(self, auth_key: str, email: str) -> User: | ||
user = await self._create_user_from_email(email) | ||
key = OAuthKey.create(auth_key, user.id) | ||
await self._add_item(key, unique_fields=["user_token"]) | ||
async def _create_user_from_oauth(self, email: str, provider: str, token: str) -> User: | ||
user = User.create(email=email, password=None) | ||
if provider == "github": | ||
user.github_id = token | ||
elif provider == "google": | ||
user.google_id = token | ||
await self._add_item(user, unique_fields=["email"]) | ||
oauth_key = OAuthKey.create(user_id=user.id, provider=provider, token=token) | ||
await self._add_item(oauth_key, unique_fields=["user_token"]) | ||
return user | ||
|
||
@overload | ||
|
@@ -71,7 +76,7 @@ async def get_user_from_github_token(self, token: str, email: str) -> User: | |
user = await self._get_user_from_auth_key(auth_key) | ||
if user is not None: | ||
return user | ||
return await self._create_user_from_auth_key(auth_key, email) | ||
return await self._create_user_from_oauth(email, "github", auth_key) | ||
|
||
async def delete_github_token(self, github_id: str) -> None: | ||
await self._delete_item(await self._get_oauth_key(github_auth_key(github_id), throw_if_missing=True)) | ||
|
@@ -81,7 +86,7 @@ async def get_user_from_google_token(self, token: str, email: str) -> User | Non | |
user = await self._get_user_from_auth_key(auth_key) | ||
if user is not None: | ||
return user | ||
return await self._create_user_from_auth_key(auth_key, email) | ||
return await self._create_user_from_oauth(email, "google", auth_key) | ||
|
||
async def delete_google_token(self, google_id: str) -> None: | ||
await self._delete_item(await self._get_oauth_key(google_auth_key(google_id), throw_if_missing=True)) | ||
|
@@ -126,7 +131,7 @@ async def delete_api_key(self, token: APIKey | str) -> None: | |
|
||
async def test_adhoc() -> None: | ||
async with UserCrud() as crud: | ||
await crud._create_user_from_email(email="[email protected]") | ||
await crud._create_user_from_email(email="[email protected]", password="examplepas$w0rd") | ||
|
||
|
||
if __name__ == "__main__": | ||
|
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