Skip to content

Commit 5ec6947

Browse files
committed
My Awesome FastAPI Project
0 parents  commit 5ec6947

20 files changed

+640
-0
lines changed

__pycache__/main.cpython-38.pyc

2.83 KB
Binary file not shown.

app/__pycache__/model.cpython-38.pyc

2.26 KB
Binary file not shown.
1.34 KB
Binary file not shown.
957 Bytes
Binary file not shown.

app/auth/auth_bearer.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#The goal of this file is to check whether the reques tis authorized or not [ verification of the proteced route]
2+
from fastapi import Request, HTTPException
3+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
4+
5+
from .auth_handler import decodeJWT
6+
7+
8+
class JWTBearer(HTTPBearer):
9+
def __init__(self, auto_error: bool = True):
10+
super(JWTBearer, self).__init__(auto_error=auto_error)
11+
12+
async def __call__(self, request: Request):
13+
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
14+
if credentials:
15+
if not credentials.scheme == "Bearer":
16+
raise HTTPException(status_code=403, detail="Invalid authentication scheme.")
17+
if not self.verify_jwt(credentials.credentials):
18+
raise HTTPException(status_code=403, detail="Invalid token or expired token.")
19+
return credentials.credentials
20+
else:
21+
raise HTTPException(status_code=403, detail="Invalid authorization code.")
22+
23+
def verify_jwt(self, jwtoken: str) -> bool:
24+
isTokenValid: bool = False
25+
26+
try:
27+
payload = decodeJWT(jwtoken)
28+
except:
29+
payload = None
30+
if payload:
31+
isTokenValid = True
32+
return isTokenValid

app/auth/auth_handler.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This file is responsible for signing , encoding , decoding and returning JWTS
2+
import time
3+
from typing import Dict
4+
5+
import jwt
6+
from decouple import config
7+
8+
9+
JWT_SECRET = config("secret")
10+
JWT_ALGORITHM = config("algorithm")
11+
12+
13+
def token_response(token: str):
14+
return {
15+
"access_token": token
16+
}
17+
18+
# function used for signing the JWT string
19+
def signJWT(user_id: str) -> Dict[str, str]:
20+
payload = {
21+
"user_id": user_id,
22+
"expires": time.time() + 600
23+
}
24+
token = jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGORITHM)
25+
26+
return token_response(token)
27+
28+
29+
def decodeJWT(token: str) -> dict:
30+
try:
31+
decoded_token = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
32+
return decoded_token if decoded_token["expires"] >= time.time() else None
33+
except:
34+
return {}

app/model.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
from pydantic import BaseModel, Field, EmailStr
2+
3+
class PostSchema(BaseModel):
4+
id: int = Field(default=None)
5+
title: str = Field(...)
6+
content: str = Field(...)
7+
8+
class Config:
9+
schema_extra = {
10+
"example": {
11+
"title": "Securing FastAPI applications with JWT.",
12+
"content": "In this tutorial, you'll learn how to secure your application by enabling authentication using JWT. We'll be using PyJWT to sign, encode and decode JWT tokens...."
13+
}
14+
}
15+
16+
17+
class CommentSchema(BaseModel):
18+
id: int = Field(default=None)
19+
post_id: int
20+
content: str
21+
22+
class Config:
23+
schema_extra = {
24+
"example": {
25+
"post_id": 1,
26+
"content": "This is a great post!"
27+
}
28+
}
29+
30+
31+
class UserSchema(BaseModel):
32+
FirstName: str = Field(...)
33+
LastName: str = Field(...)
34+
Email: EmailStr = Field(...)
35+
UserName: str = Field(...)
36+
Password: str = Field(...)
37+
38+
class Config:
39+
schema_extra = {
40+
"example": {
41+
"FirstName": "Md.Mizanur Rahaman2 ",
42+
"LastName": "(Sobuz)",
43+
"Email": "sobuz80@gmail.com",
44+
"UserName": "chowdhury",
45+
"Password": "12345678"
46+
}
47+
}
48+
49+
class UserLoginSchema(BaseModel):
50+
Email: EmailStr = Field(...)
51+
Password: str = Field(...)
52+
53+
class Config:
54+
schema_extra = {
55+
"example": {
56+
"Email": "sobuz80@gmail.com",
57+
"Password": "12345678"
58+
}
59+
}

install/Document.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
##My Awesome FastAPI Project
2+
This repository contains a Python API project using the FastAPI framework. The API provides endpoints for performing CRUD (Create, Read, Update, Delete) operations.
3+
4+
5+
6+
##Install the dependencies:
7+
pip install -r requirements.txt
8+
uvicorn main:app --reload
9+
10+
11+
#Env Setep
12+
-python -m venv env
13+
-env\scripts\activate
14+
15+
1.Step :Auth
16+
2.Step :All users
17+
3.Step :All Blog Posts (accesstoken To Send BlogPost)
18+
4.Step :Comments

0 commit comments

Comments
 (0)