# FastAPI EasyEasyAuth #
## What is this? ##
This library quickly and easily creates authentication using JWT and Cookies. You can also use easyauth to identify an active user
### Using ###
First, we need to import the EasyAuth and Jwt classes from easyauth
from fastapi_easyauth import EasyAuth, Jwt, ALGORITHM
After that, we create instances of classes
```python
jwt = Jwt(
secret = "SECRET", # The secret key for generating tokens and decoding them. Keep the token secret
algorithm = ALGORITHM.HS256 # The encryption algorithm
model = MyModel # Your Pydantic model. When decoding the token, the result will be converted to this model
)
auth = EasyAuth(
cookie_name = "user" # The Name Of The Cookie File
jwt = jwt
expires = exp.EXPIRES_30_DAYS # Cookie lifetime
)
```
Great, everything is ready. Now we can create tokens and decode them. Also check if the user is active.
```python
from fastapi import FastAPI, Request, Response, Depends
from fastapi_easyauth import EasyAuth, Jwt, ALGORITHM, exp
from pydantic import BaseModel
class User(BaseModel):
name: str
password: str
app = FastAPI()
jwt = Jwt(
secret = "SECRET",
algorithm = ALGORITHM.HS256
)
auth = EasyAuth(
cookie_name = "user"
jwt = jwt
expires = exp.EXPIRES_30_DAYS
)
@app.post('/log')
def log(user: User, response: Response):
token = jwt.create_token(user)
auth.save_token_in_cookie(response, token)
return {'status': 200}
@app.get('/active')
def active(user: User = Depends(auth.active_user))
return user
```
----------
# What was added and changed in version 0.1.0 #
+ Now, using only an instance of the EasyAuth class, you can create a token and immediately save it in a cookie.
```python
@app.post('/login')
def login(user: User, response: Response):
token = auth.create_token(subject = user, response = response)
return token
@app.get('/getToken')
def get_token(request: Request):
data = auth.decode_token(request)
return data
```
+ The name of the Auth class has also been changed to EasyAuth.
+ All functions have been documented
+ Now, when initializing the Jwt class, you can specify the model. Each time you decode the token, the result will be returned in the form of the model that you specified
```python
class User(BaseModel):
id: int
username: str
jwt = Jwt(
secret = "SECRET",
model = User
)
def get_user(token: str) -> User:
# if you did not specify the model during initialization, the result will be returned as a dictionary
user = jwt.decode_token(token)
return user
def get_user_in_model(token: str) -> User:
# If you are not going to add models when initializing the Jwt class, then you can use the decode_taken_in_model function.
# It accepts a token and a model in which
user = jwt.decode_token_in_model(token, User)
return user
```
----------
### There are also two simple functions in this library so far. The first is password hashing. The second is an error about an unauthorized user. ###
```python
def hash_password(password: str) -> str:
"""
hash_password: hashes the password
Args:
password (str): User password
Returns:
str: hashed password
"""
return hashlib.sha256(password.encode()).hexdigest()
def not_authorized() -> HTTPException:
"""
not_authorized: a function that returns an error when an unauthorized user
"""
return HTTPException(
status_code=401,
detail='Unauthorized'
)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/Trejgus/fastapi-easyauth",
"name": "fastapi-easyauth",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "fastapi fastapi-jwt fastapi-easyauth fastapi-auth",
"author": "duckduck",
"author_email": "dimondtp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/08/5b/8c0df839e4c44082c93a00b68ca1a6783c4aeedfb1cbf577d4f7f65315d5/fastapi-easyauth-0.1.0.tar.gz",
"platform": null,
"description": "# FastAPI EasyEasyAuth #\n\n## What is this? ##\nThis library quickly and easily creates authentication using JWT and Cookies. You can also use easyauth to identify an active user\n\n\n\n\n### Using ###\n\nFirst, we need to import the EasyAuth and Jwt classes from easyauth\n\n from fastapi_easyauth import EasyAuth, Jwt, ALGORITHM\n\nAfter that, we create instances of classes\n```python\njwt = Jwt(\n secret = \"SECRET\", # The secret key for generating tokens and decoding them. Keep the token secret\n algorithm = ALGORITHM.HS256 # The encryption algorithm\n model = MyModel # Your Pydantic model. When decoding the token, the result will be converted to this model\n)\n\nauth = EasyAuth(\n cookie_name = \"user\" # The Name Of The Cookie File\n jwt = jwt\n expires = exp.EXPIRES_30_DAYS # Cookie lifetime\n )\n```\n\nGreat, everything is ready. Now we can create tokens and decode them. Also check if the user is active. \n```python\nfrom fastapi import FastAPI, Request, Response, Depends\nfrom fastapi_easyauth import EasyAuth, Jwt, ALGORITHM, exp\n\nfrom pydantic import BaseModel\n\nclass User(BaseModel):\n name: str\n password: str\n\napp = FastAPI()\n\njwt = Jwt(\n secret = \"SECRET\",\n algorithm = ALGORITHM.HS256\n)\n\nauth = EasyAuth(\n cookie_name = \"user\"\n jwt = jwt\n expires = exp.EXPIRES_30_DAYS\n)\n\n@app.post('/log')\ndef log(user: User, response: Response):\n token = jwt.create_token(user)\n auth.save_token_in_cookie(response, token)\n return {'status': 200}\n\n\n@app.get('/active')\ndef active(user: User = Depends(auth.active_user))\n return user\n```\n----------\n\n# What was added and changed in version 0.1.0 #\n\n+ Now, using only an instance of the EasyAuth class, you can create a token and immediately save it in a cookie.\n\n```python\n@app.post('/login')\ndef login(user: User, response: Response):\n token = auth.create_token(subject = user, response = response)\n return token\n\n@app.get('/getToken')\ndef get_token(request: Request):\n data = auth.decode_token(request)\n return data\n```\n\n+ The name of the Auth class has also been changed to EasyAuth.\n+ All functions have been documented\n+ Now, when initializing the Jwt class, you can specify the model. Each time you decode the token, the result will be returned in the form of the model that you specified\n\n```python\nclass User(BaseModel):\n id: int\n username: str\n\njwt = Jwt(\n secret = \"SECRET\",\n model = User\n)\n\n\ndef get_user(token: str) -> User:\n # if you did not specify the model during initialization, the result will be returned as a dictionary\n \n user = jwt.decode_token(token)\n return user\n\n\ndef get_user_in_model(token: str) -> User:\n # If you are not going to add models when initializing the Jwt class, then you can use the decode_taken_in_model function.\n # It accepts a token and a model in which\n \n user = jwt.decode_token_in_model(token, User)\n return user\n```\n\n----------\n### There are also two simple functions in this library so far. The first is password hashing. The second is an error about an unauthorized user. ###\n\n```python\ndef hash_password(password: str) -> str:\n \"\"\"\n hash_password: hashes the password\n\n Args:\n password (str): User password\n\n Returns:\n str: hashed password\n \"\"\"\n\n return hashlib.sha256(password.encode()).hexdigest()\n\n\ndef not_authorized() -> HTTPException:\n \"\"\"\n not_authorized: a function that returns an error when an unauthorized user\n \"\"\"\n\n return HTTPException(\n status_code=401,\n detail='Unauthorized'\n )\n```\n\n\n\n\n\n\n\n\n\n",
"bugtrack_url": null,
"license": null,
"summary": "A library for quickly creating authentication using JWT and Cookies",
"version": "0.1.0",
"project_urls": {
"GitHub": "https://github.com/Trejgus/fastapi-easyauth",
"Homepage": "https://github.com/Trejgus/fastapi-easyauth"
},
"split_keywords": [
"fastapi",
"fastapi-jwt",
"fastapi-easyauth",
"fastapi-auth"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "0b8a1374d8438eeb4600bb9c515f8ee4ca91d8ea19c22d2b4089c5dfe9551dca",
"md5": "dd4b88931cf5a3fc11aa3bb2776e7d01",
"sha256": "f9fe74d992f85b67f6fc54801ef4f9f68da390df26915fd927a29ae965a0ac8d"
},
"downloads": -1,
"filename": "fastapi_easyauth-0.1.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "dd4b88931cf5a3fc11aa3bb2776e7d01",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 6456,
"upload_time": "2024-04-20T18:25:04",
"upload_time_iso_8601": "2024-04-20T18:25:04.306725Z",
"url": "https://files.pythonhosted.org/packages/0b/8a/1374d8438eeb4600bb9c515f8ee4ca91d8ea19c22d2b4089c5dfe9551dca/fastapi_easyauth-0.1.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "085b8c0df839e4c44082c93a00b68ca1a6783c4aeedfb1cbf577d4f7f65315d5",
"md5": "df1e55580ff5f1e596ce33f9b594f81c",
"sha256": "e4d6e79d52fb485a4eb3b37129566108c8f3dca6de5d7717bba22c9e1d8f2ad2"
},
"downloads": -1,
"filename": "fastapi-easyauth-0.1.0.tar.gz",
"has_sig": false,
"md5_digest": "df1e55580ff5f1e596ce33f9b594f81c",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 5411,
"upload_time": "2024-04-20T18:25:05",
"upload_time_iso_8601": "2024-04-20T18:25:05.747527Z",
"url": "https://files.pythonhosted.org/packages/08/5b/8c0df839e4c44082c93a00b68ca1a6783c4aeedfb1cbf577d4f7f65315d5/fastapi-easyauth-0.1.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-20 18:25:05",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Trejgus",
"github_project": "fastapi-easyauth",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "fastapi-easyauth"
}