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

How to Handle AccessToken Authentication in Copilot #756

Open
lianghsun opened this issue Feb 21, 2024 · 5 comments
Open

How to Handle AccessToken Authentication in Copilot #756

lianghsun opened this issue Feb 21, 2024 · 5 comments
Labels
auth Pertaining to authentication.

Comments

@lianghsun
Copy link

When using Copilot, if I want to utilize an accessToken for authentication, how can I receive this token on the Python side? I've attempted using @cl.password_auth_callback, but it seems that's not the correct approach. Are there any examples I can refer to?

@willydouhard
Copy link
Collaborator

willydouhard commented Feb 22, 2024

So with the copilot, authentication works a bit differently.

In the regular chainlit, the user goes through an authentication flow that triggers some callbacks such as @cl.password_auth_callback.

For the copilot, it does not make sense to put the user through an auth process since it is probably up to the host website to authenticate the user. That is why it is up to the host website to generate the accessToken and pass it directly to chainlit, by passing the callback auth mechanism.

@lianghsun
Copy link
Author

Thanks for replying @willydouhard ! But how do I get accessToken in Chainlit side?

@alirizasaral
Copy link

Thanks for replying @willydouhard ! But how do I get accessToken in Chainlit side?

+1

@lehic
Copy link

lehic commented Mar 1, 2024

@lianghsun The access token should be managed on your end. You can generate an access token for each user.

Here is an example:

an import jwt
from datetime import datetime, timedelta

CHAINLIT_AUTH_SECRET = "YOUR_CHAINLIT_AUTH_SECRET"

def create_jwt(identifier: str, metadata: dict) -> str:
    to_encode = {
      "identifier": identifier,
      "metadata": metadata,
      "exp": datetime.utcnow() + timedelta(minutes=60 * 24 * 15),  # 15 days
      }

    encoded_jwt = jwt.encode(to_encode, CHAINLIT_AUTH_SECRET, algorithm="HS256")
    return encoded_jwt

user_access_token = create_jwt("user-1", {"name": "John Doe"})

After that, when you load the Copilot from your page, you can attach the access_token that is linked to your user on the mountChainlitWidget.
Like this:

        <script src="http://localhost:8000/copilot/index.js"></script>
        <script>
            window.mountChainlitWidget({
                chainlitServer: 'http://localhost:8000',
                accessToken: '<user_access_token>'
            });
        </script>

Replace <user_access_token> with the user_access_token variable.

From your Chainlit app, you can access user information like this:
user = cl.user_session.get("user")

@ajosegun
Copy link

ajosegun commented Jul 1, 2024

Hi,

I am facing a similar error!!

Did you get it working?

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

No branches or pull requests

6 participants