IndominusByte / fastapi-jwt-auth

FastAPI extension that provides JWT Auth support (secure, easy to use, and lightweight)
http://indominusbyte.github.io/fastapi-jwt-auth/
MIT License
660 stars 153 forks source link

Function get_raw_jwt() can pass parameter encoded_token #23

Closed himalacharya closed 4 years ago

himalacharya commented 4 years ago

I am not clear on this update. On documentation

@app.get('/claims')
def user(Authorize: AuthJWT = Depends()):
    Authorize.jwt_required()

    foo_claims = Authorize.get_raw_jwt()['foo']
    return {"foo": foo_claims}

How to use new update on this get_raw_jwt() in above example?

IndominusByte commented 4 years ago

is optional is very helpful if you develop WebSocket which is a token on the query string or path parameter. get_raw_jwt() automatically filled if your request in header or cookie but not for WebSocket

@app.websocket('/ws')
async def websocket(websocket: WebSocket, token: str = Query(...), Authorize: AuthJWT = Depends()):
    await websocket.accept()
    try:
        Authorize.jwt_required("websocket",token=token)
        await websocket.send_text("Successfully Login!")
        decoded_token = Authorize.get_raw_jwt(token) # here you can pass encoded_token from query string
        await websocket.send_text(f"Here your decoded token: {decoded_token}")
    except AuthJWTException as err:
        await websocket.send_text(err.message)
        await websocket.close()