Skip to content

Commit 1f3248a

Browse files
Implement the core prototype (GH-2)
2 parents 5a40010 + 5b8cae6 commit 1f3248a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+541
-1945
lines changed

.env

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
OAUTH2_CLIENT_ID=eccd08d6736b7999a32a
2+
OAUTH2_CLIENT_SECRET=642999c1c5f2b3df8b877afdc78252ef5b594d31
3+
OAUTH2_CALLBACK_URL=http://127.0.0.1:8000/oauth2/token
4+
5+
JWT_SECRET=secret
6+
JWT_ALGORITHM=HS256
7+
JWT_EXPIRES=900

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
# fastapi-oauth2
22

3-
Easy to setup social authentication mechanism with support for several auth providers.
3+
Easy to setup OAuth2 social authentication mechanism with support for several auth providers.
44

5-
## Examples
5+
## Demo
66

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

10-
Both can be run using the following command:
9+
## Running the application
1110

1211
```bash
1312
uvicorn main:app --reload
1413
```
1514

1615
## TODO
1716

18-
- Segregate the prototype of the `fastapi-oauth2` core.
17+
- Make the [**fastapi-oauth2**](./fastapi_oauth2) depend
18+
on (overuse) the [**social-core**](https://github.com/python-social-auth/social-core)
19+
20+
## Features
21+
22+
- Integrate with any existing FastAPI project (no dependencies of the project should stop the work of
23+
the `fastapi-oauth2`)
24+
* Implementation must allow to provide a context for configurations (also, see how it is done in another projects)
25+
- Use multiple OAuth2 providers at the same time
26+
* There need to be provided a way to configure the OAuth2 for multiple providers
27+
- Token -> user data, user data -> token easy conversion
28+
- Customize OAuth2 routes

build.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
# last version of `build` supporting Python 3.6
4+
pip install build==0.9.0
5+
6+
# build the wheel and install it
7+
WHEEL_NAME=$(python -m build | grep -Po "fastapi_oauth2-.*\.whl" | tail -n 1)
8+
pip install dist/$WHEEL_NAME
File renamed without changes.

demo/dependencies.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import Optional
2+
3+
from fastapi import HTTPException
4+
from fastapi.openapi.models import OAuthFlows as OAuthFlowsModel
5+
from fastapi.security import OAuth2
6+
from fastapi.security.utils import get_authorization_scheme_param
7+
from starlette.requests import Request
8+
from starlette.status import HTTP_403_FORBIDDEN
9+
10+
11+
class OAuth2PasswordBearerCookie(OAuth2):
12+
def __init__(
13+
self,
14+
tokenUrl: str,
15+
scheme_name: str = None,
16+
scopes: dict = None,
17+
auto_error: bool = True,
18+
):
19+
flows = OAuthFlowsModel(password={"tokenUrl": tokenUrl, "scopes": scopes or {}})
20+
super().__init__(flows=flows, scheme_name=scheme_name, auto_error=auto_error)
21+
22+
async def __call__(self, request: Request) -> Optional[str]:
23+
scheme, param = get_authorization_scheme_param(request.headers.get("Authorization"))
24+
authorization = scheme.lower() == "bearer"
25+
if not authorization:
26+
scheme, param = get_authorization_scheme_param(request.cookies.get("Authorization"))
27+
authorization = scheme.lower() == "bearer"
28+
29+
if not authorization:
30+
if self.auto_error:
31+
raise HTTPException(
32+
status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
33+
)
34+
else:
35+
return None
36+
return param
37+
38+
39+
oauth2_scheme = OAuth2PasswordBearerCookie(tokenUrl="/token")

demo/router.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from fastapi import APIRouter
2+
from fastapi import Depends
3+
from starlette.requests import Request
4+
5+
from .dependencies import oauth2_scheme
6+
7+
router = APIRouter()
8+
9+
10+
@router.get("/user")
11+
def user(request: Request, _: str = Depends(oauth2_scheme)):
12+
return request.user
13+
14+
15+
@router.post("/token")
16+
def token(request: Request):
17+
return request.cookies.get("Authorization")

examples/DogeAPI/.gitignore

Lines changed: 0 additions & 130 deletions
This file was deleted.

examples/DogeAPI/api/blog.py

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)