Skip to content

Implement the core prototype #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e4b2445
Convert the airnominal to the core app
ArtyomVancyan Jun 19, 2023
9f5a84c
Remove unused and unnecessary stuff
ArtyomVancyan Jun 21, 2023
f8b84e8
Fix 'state' usage and validation
ArtyomVancyan Jun 22, 2023
c063340
Combine `process_login` and `verify_and_process`
ArtyomVancyan Jun 22, 2023
127966d
Rearrange the files and create some necessary ones
ArtyomVancyan Jun 22, 2023
1a02068
Refactor env vars and cookie setting
ArtyomVancyan Jun 22, 2023
d8bbbc7
Define the desired list of features
ArtyomVancyan Jun 23, 2023
cf05444
Change the route prefixes
ArtyomVancyan Jun 23, 2023
cae41be
Shift the package source in the project tree (move to `src`)
ArtyomVancyan Jun 23, 2023
3f356eb
Configure the setup metadata and build-system
ArtyomVancyan Jun 23, 2023
b8d2c50
Automate the wheel building to avoid editable mode bugs
ArtyomVancyan Jun 23, 2023
d0e208e
Create the core middleware and integrate
ArtyomVancyan Jun 24, 2023
40284e8
Move exceptions to a separate module
ArtyomVancyan Jun 24, 2023
f0c5434
Add a few notes about usage expectations
ArtyomVancyan Jun 24, 2023
e3a7450
Remove usage of the `OAUTH2_REDIRECT_URL` env var
ArtyomVancyan Jun 25, 2023
0f94a13
Move the `OAuth2Client` to a separate module
ArtyomVancyan Jun 25, 2023
ca20dac
Implement `Auth` and `User` response types
ArtyomVancyan Jun 25, 2023
d329903
Make `OAuth2Core` replace the `GitHubOAuth2`
ArtyomVancyan Jun 26, 2023
5c785b5
Move JWT manipulation methods to the `Auth` class
ArtyomVancyan Jun 26, 2023
0d602a7
Provide configs from client code
ArtyomVancyan Jun 26, 2023
5f3d33a
Replace the deprecated usage with new style
ArtyomVancyan Jun 26, 2023
7930bc2
Sanitize code snippets
ArtyomVancyan Jun 26, 2023
a4dedfa
Change the JWT expiration time
ArtyomVancyan Jun 26, 2023
5b8cae6
Implement a custom strategy for using the `user_data` method
ArtyomVancyan Jun 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
OAUTH2_CLIENT_ID=eccd08d6736b7999a32a
OAUTH2_CLIENT_SECRET=642999c1c5f2b3df8b877afdc78252ef5b594d31
OAUTH2_CALLBACK_URL=http://127.0.0.1:8000/oauth2/token

JWT_SECRET=secret
JWT_ALGORITHM=HS256
JWT_EXPIRES=900
22 changes: 16 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
# fastapi-oauth2

Easy to setup social authentication mechanism with support for several auth providers.
Easy to setup OAuth2 social authentication mechanism with support for several auth providers.

## Examples
## Demo

- [airnominal](./examples/airnominal) - [fastapi-sso](https://github.com/tomasvotava/fastapi-sso) based implementation
- [dogeapi](./examples/DogeAPI) - [fastapi-allauth](https://github.com/K-villain/fastapi-allauth) based implementation
This sample application is made to demonstrate the use of the [**fastapi-oauth2**](./fastapi_oauth2) package.

Both can be run using the following command:
## Running the application

```bash
uvicorn main:app --reload
```

## TODO

- Segregate the prototype of the `fastapi-oauth2` core.
- Make the [**fastapi-oauth2**](./fastapi_oauth2) depend
on (overuse) the [**social-core**](https://github.com/python-social-auth/social-core)

## Features

- Integrate with any existing FastAPI project (no dependencies of the project should stop the work of
the `fastapi-oauth2`)
* Implementation must allow to provide a context for configurations (also, see how it is done in another projects)
- Use multiple OAuth2 providers at the same time
* There need to be provided a way to configure the OAuth2 for multiple providers
- Token -> user data, user data -> token easy conversion
- Customize OAuth2 routes
8 changes: 8 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

# last version of `build` supporting Python 3.6
pip install build==0.9.0

# build the wheel and install it
WHEEL_NAME=$(python -m build | grep -Po "fastapi_oauth2-.*\.whl" | tail -n 1)
pip install dist/$WHEEL_NAME
File renamed without changes.
39 changes: 39 additions & 0 deletions demo/dependencies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from typing import Optional

from fastapi import HTTPException
from fastapi.openapi.models import OAuthFlows as OAuthFlowsModel
from fastapi.security import OAuth2
from fastapi.security.utils import get_authorization_scheme_param
from starlette.requests import Request
from starlette.status import HTTP_403_FORBIDDEN


class OAuth2PasswordBearerCookie(OAuth2):
def __init__(
self,
tokenUrl: str,
scheme_name: str = None,
scopes: dict = None,
auto_error: bool = True,
):
flows = OAuthFlowsModel(password={"tokenUrl": tokenUrl, "scopes": scopes or {}})
super().__init__(flows=flows, scheme_name=scheme_name, auto_error=auto_error)

async def __call__(self, request: Request) -> Optional[str]:
scheme, param = get_authorization_scheme_param(request.headers.get("Authorization"))
authorization = scheme.lower() == "bearer"
if not authorization:
scheme, param = get_authorization_scheme_param(request.cookies.get("Authorization"))
authorization = scheme.lower() == "bearer"

if not authorization:
if self.auto_error:
raise HTTPException(
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
)
else:
return None
return param


oauth2_scheme = OAuth2PasswordBearerCookie(tokenUrl="/token")
17 changes: 17 additions & 0 deletions demo/router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from fastapi import APIRouter
from fastapi import Depends
from starlette.requests import Request

from .dependencies import oauth2_scheme

router = APIRouter()


@router.get("/user")
def user(request: Request, _: str = Depends(oauth2_scheme)):
return request.user


@router.post("/token")
def token(request: Request):
return request.cookies.get("Authorization")
130 changes: 0 additions & 130 deletions examples/DogeAPI/.gitignore

This file was deleted.

113 changes: 0 additions & 113 deletions examples/DogeAPI/api/blog.py

This file was deleted.

Loading