Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
add endpoints authorize, callback
Browse files Browse the repository at this point in the history
  • Loading branch information
brulitsan committed Sep 28, 2023
1 parent 9b2f116 commit d950a87
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
4 changes: 1 addition & 3 deletions sapphire/users/api/service.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import fastapi
from facet import ServiceMixin

from sapphire.common.api.service import APIService
from sapphire.users.__version__ import __version__
from sapphire.users.database.service import UsersDatabaseService
from sapphire.users.oauth2.habr import OAuth2HabrBackend
from sapphire.users.settings import UsersSettings

from .router import router


class UsersAPIService(APIService):
def __init__(
self,
database: UsersDatabaseService,
habr_oauth2: OAuth2HabrBackend,
habr_oauth2: OAuth2HabrBackend,
version: str = "0.0.0",
port: int = 8000,
):
Expand Down
11 changes: 8 additions & 3 deletions sapphire/users/api/v1beta/oauth2/habr.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@

@router.get("/authorize", response_class=RedirectResponse)
async def authorize(request: fastapi.Request):
habr_oauth2 = request.app.habr_oauth2
redirect_url = yarl.URL(str(request.url)).parent / "callback"
authorisation_url = habr_oauth2.get_authorization_url(redirect_url=str(redirect_url))
return RedirectResponse(url=authorisation_url)

# Get OAuth2 backend from request.app.habr_oauth2

@router.get("/callback")
async def callback():
pass
async def callback(request: fastapi.Request):
habr_oauth2 = request.app.habr_oauth2
token = await habr_oauth2.get_token()
user_info = await habr_oauth2.get_user_info(token=token)
return user_info
3 changes: 1 addition & 2 deletions sapphire/users/oauth2/habr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import aiohttp
import pydantic

from sapphire.users.settings import UsersSettings

from .base import OAuth2BaseBackend


Expand Down Expand Up @@ -33,6 +31,7 @@ async def get_user_info(self, token: str) -> HabrUser:
is_email_confirmed=(data["is_email_confirmed"] == "1"),
)


def get_oauth2_backend(settings: UsersSettings) -> OAuth2HabrBackend:
return OAuth2HabrBackend(
client_id=settings.habr_oauth2_client_id,
Expand Down
2 changes: 0 additions & 2 deletions sapphire/users/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

class UsersSettings(BaseSettings):
port: conint(ge=1, le=65535) = 8000

db_dsn: AnyUrl = AnyUrl("sqlite+aiosqlite:///users.sqlite3")

habr_oauth2_client_id: str
habr_oauth2_client_secret: str

Expand Down

0 comments on commit d950a87

Please sign in to comment.