Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After setting ALLOWED_USERS, unable to make successful requests. #39

Open
ssfun opened this issue Jul 2, 2024 · 4 comments
Open

After setting ALLOWED_USERS, unable to make successful requests. #39

ssfun opened this issue Jul 2, 2024 · 4 comments

Comments

@ssfun
Copy link

ssfun commented Jul 2, 2024

2024-07-02 07:47:31,070 MainThread middleware.py  :69 INFO    : Checking auth for ZhX~
2024-07-02 07:47:31,070 MainThread middleware.py  :31 INFO    : Getting user info by token: ZhX~
2024-07-02 07:47:31,117 MainThread middleware.py  :40 ERROR   : Failed to get user info: 403
2024-07-02 07:47:31,117 MainThread middleware.py  :71 INFO    : User email: None for token ZhX~
2024-07-02 07:47:31,117 MainThread middleware.py  :73 WARNING : User None is not allowed
INFO:     10.16.12.234:31711 - "GET /api/v1/ai/models HTTP/1.1" 403 Forbidden
INFO:     10.16.12.234:3661 - "GET /api/v1/ai/models HTTP/1.1" 401 Unauthorized
INFO:     10.16.46.197:10602 - "GET /api/v1/ai/models HTTP/1.1" 401 Unauthorized
INFO:     10.16.19.59:30557 - "GET /api/v1/ai/models HTTP/1.1" 401 Unauthorized
INFO:     10.16.19.59:30557 - "GET /api/v1/ai/models HTTP/1.1" 401 Unauthorized
INFO:     10.16.39.2:59174 - "GET / HTTP/1.1" 401 Unauthorized
INFO:     10.16.12.234:2681 - "GET / HTTP/1.1" 401 Unauthorized

The ALLOWED_USERS variable value is the same as the logged-in email address.

@yufeikang
Copy link
Owner

Could you please provide the debug logs? With the current information, it's challenging to pinpoint the issue. From the logs, it seems like I didn't receive the email address from the /api/me endpoint.

User email: None for token ZhX
You might want to use tools like Proxyman to capture the packets and confirm if the email address is being retrieved. Thanks! 😊

@ssfun
Copy link
Author

ssfun commented Jul 5, 2024

2024-07-05 07:03:01,715 MainThread middleware.py  :31 INFO    : Getting user info by token: ZhX~
2024-07-05 07:03:01,748 MainThread middleware.py  :40 ERROR   : Failed to get user info: 403
2024-07-05 07:03:01,749 MainThread middleware.py  :71 INFO    : User email: None for token ZhX~
2024-07-05 07:03:01,749 MainThread middleware.py  :73 WARNING : User None is not allowed
INFO:     10.16.46.197:42690 - "GET /api/v1/me HTTP/1.1" 403 Forbidden

Failed to retrieve email.

Using another project, fake-raycast-backend, which is similarly configured with ALLOWED_USERS, can correctly retrieve the data.

@ssfun
Copy link
Author

ssfun commented Jul 5, 2024

Before updating Anthropic support, the project was functioning normally.

@ssfun
Copy link
Author

ssfun commented Jul 21, 2024

Modify the code in app/middleware.py:

async def get_user_info_by_token(headers: Headers):
    async with lock:
        token = headers.get("Authorization", "").split(" ")[1]
        if token in USER_SESSION:
            return USER_SESSION[token]
        logger.info(f"Getting user info by token: {token}")
        headers = dict(headers)
        headers["accept-encoding"] = "identity"  # disable compression
        # delete content-length
        headers.pop("content-length", None)
        httpx_client = httpx.AsyncClient()
        response = await httpx_client.get(
            f"https://backend.raycast.com/api/v1/me",
            headers=headers,
        )
        if response.status_code != 200:
            logger.error(f"Failed to get user info: {response.status_code}")
            return None
        data = response.json()
        return data["email"]

After modifying the code as follows, it can be executed normally

async def get_user_info_by_token(headers: Headers):
    async with lock:
        token = headers.get("Authorization", "").split(" ")[1]
        if token in USER_SESSION:
            return USER_SESSION[token]
        logger.info(f"Getting user info by token: {token}")
        headers = dict(headers)
        headers["accept-encoding"] = "identity"  # disable compression
        # delete content-length
        headers.pop("content-length", None)
        httpx_client = httpx.AsyncClient()
        response = await httpx_client.get(
            f"https://backend.raycast.com/api/v1/me",
            headers={     
                "Authorization": f"Bearer {token}",     
                "Accept": "application/json",
                },
        )
        if response.status_code != 200:
            logger.error(f"Failed to get user info: {response.status_code}")
            return None
        data = response.json()
        return data["email"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants