Skip to content
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

Merge Main into Py3.10 ✨ #91

Merged
merged 9 commits into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"files": [
"README.md"
],
"imageSize": 100,
"commit": false,
"contributorsPerLine": 7,
"projectName": "AuthX",
"projectOwner": "yezz123",
"repoType": "github",
"repoHost": "https://github.com",
"skipCi": true
}
2 changes: 1 addition & 1 deletion .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Test Documentation Build
name: Test Documentation Building


on: [pull_request]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Testing on Docker
name: Docker tests

on:
push:
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Local tests

on: [push, pull_request]

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: "3.9"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install -r requirements.dev.txt
- name: Test Project
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
python -m pytest --cov=AuthX/ --cov-report=html
codecov
- name: lint
run: |
pre-commit run --all-files
2 changes: 1 addition & 1 deletion AuthX/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Ready to use and customizable Authentications and Oauth2 management for FastAPI"""

__version__ = "0.0.8"
__version__ = "0.0.9"

__license__ = "MIT"

Expand Down
1 change: 1 addition & 0 deletions AuthX/database/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def set_client(self, redis: Redis) -> None:
self._redis = redis

async def get(self, key: str) -> str:
# TODO: Check if key exists
return await self._redis.get(key)

async def delete(self, key: str) -> None:
Expand Down
50 changes: 48 additions & 2 deletions AuthX/services/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


class AuthService:

_repo: UsersRepo
_auth_backend: JWTBackend
_debug: bool
Expand Down Expand Up @@ -80,16 +81,47 @@ def setup(
cls._display_name = display_name

def _validate_user_model(self, model, data: dict):
"""
Validate user model.

Args:
model (UserInRegister): UserInRegister model.
data (dict): data.

Raises:
HTTPException: 400 - validation error.

Returns:
UserInRegister: UserInRegister model.
"""
try:
return model(**data)
except ValidationError as e:
msg = e.errors()[0].get("msg")
raise HTTPException(400, detail=get_error_message(msg))

async def _email_exists(self, email: str) -> bool:
"""
Check if email exists.

Args:
email (str): email.

Returns:
bool: True if email exists.
"""
return await self._repo.get_by_email(email) is not None

async def _username_exists(self, username: str) -> bool:
"""
Check if username exists.

Args:
username (str): username.

Returns:
bool: True if username exists.
"""
return await self._repo.get_by_username(username) is not None

def _create_email_client(self) -> EmailClient:
Expand Down Expand Up @@ -154,9 +186,25 @@ async def register(self, data: dict) -> Dict[str, str]:
return self._auth_backend.create_tokens(payload)

async def _is_bruteforce(self, ip: str, login: str) -> bool:
"""
Check if user is bruteforce.

Args:
ip (str): ip.
login (str): login.

Returns:
bool: True if user is bruteforce.
"""
return await self._repo.is_bruteforce(ip, login)

async def _update_last_login(self, id: int) -> None:
"""
Update last login.

Args:
id (int): id.
"""
await self._repo.update(id, {"last_login": datetime.utcnow()})

async def login(self, data: dict, ip: str) -> Dict[str, str]:
Expand Down Expand Up @@ -199,8 +247,6 @@ async def login(self, data: dict, ip: str) -> Dict[str, str]:
payload = UserPayload(**item).dict()
return self._auth_backend.create_tokens(payload)

# async def token(self) -> dict:

async def refresh_access_token(self, refresh_token: str) -> str:
"""POST /token/refresh

Expand Down
33 changes: 33 additions & 0 deletions AuthX/services/password.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@


class PasswordService:
"""
Password Service

Raises:
HTTPException: 400 - validation or timeout.
HTTPException: 404 - token not found.
HTTPException: 406 - password already exists.
HTTPException: 409 - password reset before.
HTTPException: 500 - email not sent.

Returns:
None
"""

_repo: UsersRepo
_auth_backend: JWTBackend
_debug: bool
Expand Down Expand Up @@ -73,6 +87,19 @@ def _create_email_client(self) -> EmailClient:
)

def _validate_user_model(self, model, data):
"""
Validate user model.

Args:
model (UserInSetPassword): UserInSetPassword
data (dict): {password1: "password", password2: "password"}

Raises:
HTTPException: 400 - validation or timeout.

Returns:
UserInSetPassword: UserInSetPassword
"""
try:
return model(**data)
except ValidationError as e:
Expand Down Expand Up @@ -143,6 +170,12 @@ async def forgot_password(self, data: dict, ip: str) -> None:
"""

async def password_status(self) -> dict:
"""
POST /password_status

Returns:
dict: password_status
"""
status = await self._repo.get_password_status(self._user.id)
return {"status": status}

Expand Down
10 changes: 10 additions & 0 deletions AuthX/services/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@


class SearchService:
"""
Private

Raises:
HTTPException: If the user does not exist.

Returns:
The user.
"""

_repo: UsersRepo

@classmethod
Expand Down
12 changes: 12 additions & 0 deletions AuthX/services/social.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@


class SocialService:
"""
Social Service

Raises:
SocialException: social error
SocialException: email exists
SocialException: ban

Returns:
Tuple[str, str]: access token, refresh token
"""

_repo: UsersRepo
_auth_backend: JWTBackend
_base_url: str
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

[![Docker Build](https://github.com/yezz123/AuthX/actions/workflows/docker.yml/badge.svg)](https://github.com/yezz123/AuthX/actions/workflows/docker.yml)
[![Python Package](https://github.com/yezz123/AuthX/actions/workflows/build.yml/badge.svg)](https://github.com/yezz123/AuthX/actions/workflows/build.yml)
[![Local tests](https://github.com/yezz123/AuthX/actions/workflows/test.yml/badge.svg)](https://github.com/yezz123/AuthX/actions/workflows/test.yml)
[![codecov](https://codecov.io/gh/yezz123/AuthX/branch/main/graph/badge.svg?token=3j5znCNzDp)](https://codecov.io/gh/yezz123/AuthX)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b510202495654916843956856fd9a1f6)](https://www.codacy.com?utm_source=github.com&utm_medium=referral&utm_content=yezz123/AuthX&utm_campaign=Badge_Grade)
[![PyPI version](https://badge.fury.io/py/AuthX.svg)](https://badge.fury.io/py/AuthX)
[![Downloads](https://pepy.tech/badge/authx/month)](https://pepy.tech/project/authx)
[![Downloads](https://pepy.tech/badge/authx/week)](https://pepy.tech/project/authx)
Expand Down Expand Up @@ -66,7 +69,7 @@ app.include_router(auth.social_router, prefix="/auth")
app.include_router(auth.password_router, prefix="/api/users")
app.include_router(auth.admin_router, prefix="/api/users")
app.include_router(auth.search_router, prefix="/api/users")
...

```

### Startup 🏁
Expand Down Expand Up @@ -121,7 +124,7 @@ def anonym_test(user: User = Depends(auth.get_user)):
def user_test(user: User = Depends(auth.get_authenticated_user)):
pass

#
# Set Admin User
@router.get("/admin", dependencies=[Depends(auth.admin_required)])
def admin_test():
pass
Expand Down Expand Up @@ -194,6 +197,20 @@ As you see the Package still under development, you can contribute to it, also i
- [telegram](https://t.me/yezz123)
- Where i can see the Project Roadmap? 🤔
- I use to manage AuthX on [Trello](https://trello.com/b/0NNZMP8T), you could check and see all the tasks if you could help me to do it.
- Check Also [Release Notes](https://yezz123.github.io/AuthX/release/).

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->

<!-- markdownlint-restore -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!

## License 📝

Expand Down
2 changes: 1 addition & 1 deletion Setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.0.8
current_version = 0.0.9
commit = True
tag = True

Expand Down
5 changes: 3 additions & 2 deletions docs/configuration/auth/jwt.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ __Notes__: For testing this and create a TestCases always assert a `payload.get`
The Revoke Token is used to revoke the access token, and is used to generate a new access token, this one is based more on the cache backend.

```py
from datetime import datetime
from AuthX.core.jwt import JWTBackend

def logout():
from datetime import datetime
from AuthX.core.jwt import JWTBackend

key = # Key to revoke
epoch = datetime.utcfromtimestamp(0)
Expand Down
6 changes: 4 additions & 2 deletions docs/faq.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Frequently Asked Questions 🍂

![image](header.svg)

## What is the purpose of this project?

- This Project is an Authentication Package helping to Add a Fully registration and authentication or authorization system to your FastAPI project. Is designed to be as customizable and adaptable as possible, so that you can use it as you want.
Expand Down Expand Up @@ -46,7 +48,7 @@ This book covers the following exciting features:

If you feel this book is for you, get your [copy](https://amzn.to/3kTvgjG) today!

## How You Manage Your Projects?
## How Did You Manage AuthX?

Managing the project during my Work was somehow Hard for me, cause as many know I start this project after i finish my ex. Job, and i start working on it even if i joined my new Job as a Backend Developer for [@Obytes](https://obytes.com).

Expand All @@ -64,7 +66,7 @@ There you could buy me a [coffee ☕️](https://www.buymeacoffee.com/tahiri) to

And you can also become a Silver or Gold sponsor for AuthX. 🏅🎉

### Reference
### Basic References

I use The Following Reference to Get The package Done:

Expand Down
File renamed without changes
4 changes: 3 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
<em>Ready to use and customizable Authentications and Oauth2 management for FastAPI ⚡</em>
</p>

[![codecov](https://codecov.io/gh/yezz123/AuthX/branch/main/graph/badge.svg?token=3j5znCNzDp)](https://codecov.io/gh/yezz123/AuthX)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/b510202495654916843956856fd9a1f6)](https://www.codacy.com?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=yezz123/AuthX&amp;utm_campaign=Badge_Grade)
[![PyPI version](https://badge.fury.io/py/AuthX.svg)](https://badge.fury.io/py/AuthX)
[![Downloads](https://pepy.tech/badge/authx/month)](https://pepy.tech/project/authx)
[![Downloads](https://pepy.tech/badge/authx/week)](https://pepy.tech/project/authx)
[![Lang](https://img.shields.io/badge/Language-Python-green?style)](https://github.com/yezz123)
[![Lang](https://img.shields.io/badge/Language-Python-green?style)](https://www.python.org/)
[![framework](https://img.shields.io/badge/Framework-FastAPI-blue?style)](https://fastapi.tiangolo.com/)
[![Star Badge](https://img.shields.io/static/v1?label=%F0%9F%8C%9F&message=If%20Useful&style=style=flatcolor=BC4E99)](https://github.com/yezz123/AuthX)
[![Pypi](https://img.shields.io/pypi/pyversions/AuthX.svg?color=%2334D058)](https://pypi.org/project/AuthX)
Expand Down
Loading