Added device client option

This commit is contained in:
Profitroll 2023-01-20 16:02:46 +01:00
parent 673c986ff9
commit daa3b0ca73
4 changed files with 9 additions and 7 deletions

View File

@ -30,6 +30,7 @@ async def devices_get_by_name(name: str, apikey: APIKey = Depends(get_api_key)):
device = col_devices.find_one({"user": user_by_key(apikey), "name": name}) device = col_devices.find_one({"user": user_by_key(apikey), "name": name})
if device is not None: if device is not None:
del device["_id"] del device["_id"]
del device["user"]
return UJSONResponse(device) return UJSONResponse(device)
else: else:
raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find device with that name.") raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find device with that name.")
@ -46,7 +47,7 @@ async def devices_delete_by_name(name: str, apikey: APIKey = Depends(get_api_key
raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find device with that name.") raise HTTPException(HTTP_404_NOT_FOUND, detail="Could not find device with that name.")
@app.post("/devices", response_class=UJSONResponse, description="Create new device") @app.post("/devices", response_class=UJSONResponse, description="Create new device")
async def devices_post(name: str, os: str, apikey: APIKey = Depends(get_api_key)): async def devices_post(name: str, os: str, client: str, apikey: APIKey = Depends(get_api_key)):
user = user_by_key(apikey) user = user_by_key(apikey)
@ -62,7 +63,7 @@ async def devices_post(name: str, os: str, apikey: APIKey = Depends(get_api_key)
if col_devices.find_one({"user": user, "name": name}) is not None: if col_devices.find_one({"user": user, "name": name}) is not None:
raise HTTPException(HTTP_409_CONFLICT, detail="Device with this name already exists.") raise HTTPException(HTTP_409_CONFLICT, detail="Device with this name already exists.")
col_devices.insert_one({"user": user, "name": name, "os": os, "last_save": 0}) col_devices.insert_one({"user": user, "name": name, "os": os, "client": client, "last_save": 0})
return Response(status_code=HTTP_204_NO_CONTENT) return Response(status_code=HTTP_204_NO_CONTENT)

View File

@ -4,4 +4,5 @@ class Device(BaseModel):
user: str user: str
name: str name: str
os: str os: str
client: str
last_save: int last_save: int

View File

@ -13,13 +13,11 @@ app = FastAPI(title="Stardew Sync", docs_url=None, redoc_url=None, version="0.1"
api_key_query = APIKeyQuery(name="apikey", auto_error=False) api_key_query = APIKeyQuery(name="apikey", auto_error=False)
api_key_header = APIKeyHeader(name="apikey", auto_error=False) api_key_header = APIKeyHeader(name="apikey", auto_error=False)
api_key_cookie = APIKeyCookie(name="apikey", auto_error=False)
async def get_api_key( async def get_api_key(
api_key_query: str = Security(api_key_query), api_key_query: str = Security(api_key_query),
api_key_header: str = Security(api_key_header), api_key_header: str = Security(api_key_header),
api_key_cookie: str = Security(api_key_cookie),
) -> str: ) -> str:
def is_valid(key): def is_valid(key):
@ -29,10 +27,8 @@ async def get_api_key(
return api_key_query return api_key_query
elif is_valid(api_key_header): elif is_valid(api_key_header):
return api_key_header return api_key_header
elif is_valid(api_key_cookie):
return api_key_cookie
else: else:
if (col_expired.find_one({"hash": passEncode(api_key_query)}) is not None) or (col_expired.find_one({"hash": passEncode(api_key_header)}) is not None) or (col_expired.find_one({"hash": passEncode(api_key_cookie)}) is not None): if (col_expired.find_one({"hash": passEncode(api_key_query)}) is not None) or (col_expired.find_one({"hash": passEncode(api_key_header)}) is not None):
raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail=configGet("key_expired", "messages")) raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail=configGet("key_expired", "messages"))
else: else:
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail=configGet("key_invalid", "messages")) raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail=configGet("key_invalid", "messages"))

View File

@ -4,6 +4,7 @@
"user", "user",
"name", "name",
"os", "os",
"client",
"last_save" "last_save"
], ],
"properties": { "properties": {
@ -16,6 +17,9 @@
"os": { "os": {
"bsonType": "string" "bsonType": "string"
}, },
"client": {
"bsonType": "string"
},
"last_save": { "last_save": {
"bsonType": ["int", "double"] "bsonType": ["int", "double"]
} }