Custom TokenVerifier #1325
Replies: 7 comments 1 reply
-
|
as far i understand, you have to use the Middleware for any custom validations. Although, I am facing a different challenge there #1362 |
Beta Was this translation helpful? Give feedback.
-
|
@jaimeescano They work only within tool, prompt, resources functions, also in Middleware |
Beta Was this translation helpful? Give feedback.
-
|
Hi @kskarthik thanks for your answer. Yeah, I checked about the "only works with tool prompts and resources" ... the problem on my side is that the "authentification" happends before any middleware is executed .. so I can't add a logic to "extract" any header and put it in the state ... so I can use it in my auth logic. I just noticed that two days ago .. server/auth has changed quite a bit ... so will take a look. Just noticed they have a folder with "providers", so I might be able to implement my own provider and might have access to the "conn" variable (which tracing other oauth providers they do get). Appreciated you feedback BTW. |
Beta Was this translation helpful? Give feedback.
-
|
@jaimeescano and @kskarthik You create your own wrapper and decorate your tools with it (my wrapper below... it's mid-refactor). Then, in the wrapper, you can use get_http_headers (or get_access_token) and handle what you need. Basically, you have logic for each "auth provider" and you attach it to ctx. Voila, all tools that are wrapped can access that via ctx. The beauty is the wrapper operates per request... so it will have the unique header and the unique ctx for just that particular auth-provider's request. WrapperUsage |
Beta Was this translation helpful? Give feedback.
-
|
The alternative is you use FastMCP's StaticTokenVerifier. Now, I understand it is stored in plaintext so it depends on your setup. But you can set different tokens in the server side (make them long API keys) for each auth-provider. Then, access is similar to the above... but them in a wrapper or in the tools themselves Token Setup in ServerUsage in Tool or Wrapper |
Beta Was this translation helpful? Give feedback.
-
|
@Norcim133 My use case is that the user needs to provide an API key & a custom variable for context, via the headers. So, basically I need to validate 2 headers & throw err if not supplied. API_TOKEN & MY_CUSTOM_HEADER |
Beta Was this translation helpful? Give feedback.
-
|
@kskarthik I have this one working in my system and it would give you the same thing with headers. Mine is adding to context but you want yours to block. I also hav this elsewhere which could go in Middleware too... I run this as a way to debug my middlewares are working. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi there,
Not sure if someone has faced this challenge. Currently I'm trying to implement a logic to authenticate users from different "token" sources. So to identify the source of the token, the user must provide a request header "X-API-Auth-Provider" ... which then I'm trying to capture and then implement different logic based on the value.
I have tried using the following code .. but it looks like the
But headers are empty ... not sure if it's a bug or is there a different way to access the current "HTTP connection" from a token verifier?
Beta Was this translation helpful? Give feedback.
All reactions