Skip to content

Commit

Permalink
Fix: made swagger ui's authrize button work.
Browse files Browse the repository at this point in the history
	Swagger config setting: app = FastAPI(swagger_ui_parameters={"persistAuthorization": True})
	Ref: swagger-api/swagger-ui#8683 (comment)
  • Loading branch information
ktaka-ccmp committed Mar 3, 2024
1 parent 7629b98 commit ea77502
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 6 deletions.
2 changes: 1 addition & 1 deletion admin/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
router = APIRouter()
templates = Jinja2Templates(directory='templates')

cookie_scheme = APIKeyCookie(name="session_id", description="Session Cookie Auth")
cookie_scheme = APIKeyCookie(name="session_id", description="Admin session_id is created by create_session.sh")

def get_session_by_session_id(session_id: str, cs: Session):
try:
Expand Down
17 changes: 14 additions & 3 deletions data/create_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,19 @@ done
echo "Customer:"
echo "select * from customer" | sqlite3 $DB | tail

echo "Sessions:"
echo "select * from sessions" | sqlite3 data/cache.db

echo "Users:"
echo "select * from user" | sqlite3 $DB

pwgen(){
basenc --base64url < /dev/urandom | head -c 64 ; echo
}

email="admin01@example.com"
ssid=$(pwgen)

DB=data/cache.db

echo "insert or replace into sessions (id, session_id,user_id,email) values (1, '$ssid', 1, '$email')" | sqlite3 $DB
echo "Sessions:"
echo "select * from sessions" | sqlite3 $DB

10 changes: 9 additions & 1 deletion data/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class User(DataStoreBase):
class Sessions(CacheStoreBase):
__tablename__ = 'sessions'
id = Column('id', Integer, primary_key = True, autoincrement = True)
session_id = Column('session_id', String(64))
session_id = Column('session_id', String(254))
user_id = Column('user_id', Integer)
email = Column('email', String(254))

Expand All @@ -67,6 +67,14 @@ class UserBase(BaseModel):
class Config:
from_attributes = True

class SessionBase(BaseModel):
id: int
session_id: str
user_id: int
email: EmailStr
class Config:
from_attributes = True

DataStoreBase.metadata.create_all(bind=DataStore)
CacheStoreBase.metadata.create_all(bind=CacheStore)

Expand Down
33 changes: 33 additions & 0 deletions data/renew_admin_session.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/bin/bash

pwgen(){
basenc --base64url < /dev/urandom | head -c 64 ; echo
}

email="admin01@example.com"
ssid=$(pwgen)

DB=data/cache.db

delete(){
echo "delete from sessions where email = 'admin01@example.com'" | sqlite3 $DB
}

delete_all(){
echo "delete from sessions" | sqlite3 $DB
}

insert_or_replace(){
echo "insert or replace into sessions (id, session_id,user_id,email)
values (1, '$ssid', 1, '$email')" | sqlite3 $DB
}

check(){
echo "Sessions:"
echo "select * from sessions" | sqlite3 $DB
echo "session_id:" $ssid
}

insert_or_replace
check

2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from htmx import htmx, htmx_secret, spa
from images import image

app = FastAPI()
app = FastAPI(swagger_ui_parameters={"persistAuthorization": True})

app.include_router(
spa.router,
Expand Down

0 comments on commit ea77502

Please sign in to comment.