Skip to content

Adds support for auth in generating TypeSpec. Closes #1148 - #1154

Merged
garrytrinder merged 1 commit into
dotnet:mainfrom
waldekmastykarz:tsp-auth
Apr 29, 2025
Merged

garrytrinder merged 1 commit into
dotnet:mainfrom
waldekmastykarz:tsp-auth

Conversation

@waldekmastykarz

@waldekmastykarz waldekmastykarz commented Apr 29, 2025 •

Copy link
Copy Markdown
Collaborator

Adds support for auth in generating TypeSpec. Closes #1148

Also:

  • fixes bug in using default config for Ollama and its OpenAI-compatible APIs
  • fixed bug in caching LLM responses

To test support for auth, you can use the following requests (access tokens are dummy so no security issues):

# anonymous
curl -ikx http://127.0.0.1:8000 https://jsonplaceholder.typicode.com/posts/1

# api-key header
curl -ikx http://127.0.0.1:8000 -H "api-key:123" https://jsonplaceholder.typicode.com/posts/1

# api-key header + a regular header
curl -ikx http://127.0.0.1:8000 -H "api-key:123" -H "x-custom: value" https://jsonplaceholder.typicode.com/posts/1

# api-key query
curl -ikx http://127.0.0.1:8000 https://jsonplaceholder.typicode.com/posts/1?code=123

# bearer
curl -ikx http://127.0.0.1:8000 -H "authorization: Bearer 123" https://jsonplaceholder.typicode.com/posts/1

# oauth2
curl -ikx http://127.0.0.1:8000 -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IkRldiBQcm94eSIsInN1YiI6IkRldiBQcm94eSIsImp0aSI6IjYzYTYwYWIwIiwic2NwIjoiU2l0ZXMuUmVhZC5BbGwiLCJuYmYiOjE3NDU4NTE2NTMsImV4cCI6MTc0NTg1MTY1MywiaWF0IjoxNzQ1ODUxNjUzLCJpc3MiOiJodHRwczovL2NvbnRvc28uY29tIn0.jbw3iseLLgNX5bO8c_VuDTmALKB_D5soSXj0gN7dFZY" https://jsonplaceholder.typicode.com/posts/1

# entra
## Delegated
curl -ikx http://127.0.0.1:8000 -H "authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6Imk2bEdrM0ZaenhSY1ViMkMzbkVRN3N5SEpsWSJ9.eyJhdWQiOiI2ZTc0MTcyYi1iZTU2LTQ4NDMtOWZmNC1lNjZhMzliYjEyZTMiLCJpc3MiOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vNzJmOTg4YmYtODZmMS00MWFmLTkxYWItMmQ3Y2QwMTFkYjQ3L3YyLjAiLCJpYXQiOjE1MzcyMzEwNDgsIm5iZiI6MTUzNzIzMTA0OCwiZXhwIjoxNTM3MjM0OTQ4LCJhaW8iOiJBWFFBaS84SUFBQUF0QWFaTG8zQ2hNaWY2S09udHRSQjdlQnE0L0RjY1F6amNKR3hQWXkvQzNqRGFOR3hYZDZ3TklJVkdSZ2hOUm53SjFsT2NBbk5aY2p2a295ckZ4Q3R0djMzMTQwUmlvT0ZKNGJDQ0dWdW9DYWcxdU9UVDIyMjIyZ0h3TFBZUS91Zjc5UVgrMEtJaWpkcm1wNjlSY3R6bVE9PSIsImF6cCI6IjZlNzQxNzJiLWJlNTYtNDg0My05ZmY0LWU2NmEzOWJiMTJlMyIsImF6cGFjciI6IjAiLCJuYW1lIjoiQWJlIExpbmNvbG4iLCJvaWQiOiI2OTAyMjJiZS1mZjFhLTRkNTYtYWJkMS03ZTRmN2QzOGU0NzQiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJhYmVsaUBtaWNyb3NvZnQuY29tIiwicmgiOiJJIiwic2NwIjoiYWNjZXNzX2FzX3VzZXIiLCJzdWIiOiJIS1pwZmFIeVdhZGVPb3VZbGl0anJJLUtmZlRtMjIyWDVyclYzeERxZktRIiwidGlkIjoiNzJmOTg4YmYtODZmMS00MWFmLTkxYWItMmQ3Y2QwMTFkYjQ3IiwidXRpIjoiZnFpQnFYTFBqMGVRYTgyUy1JWUZBQSIsInZlciI6IjIuMCJ9.pj4N-w_3Us9DrBLfpCt" https://jsonplaceholder.typicode.com/posts/1

## Application
curl -ikx http://127.0.0.1:8000 -H "authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1bmlxdWVfbmFtZSI6IkRldiBQcm94eSIsInN1YiI6IkRldiBQcm94eSIsImp0aSI6IjlhY2M4MGU2Iiwicm9sZXMiOiJTaXRlcy5SZWFkLkFsbCIsIm5iZiI6MTc0NTg0NzU3NCwiZXhwIjoxNzQ1ODQ3NTc0LCJpYXQiOjE3NDU4NDc1NzQsImlzcyI6Imh0dHBzOi8vbG9naW4ubWljcm9zb2Z0b25saW5lLmNvbS83MmY5ODhiZi04NmYxLTQxYWYtOTFhYi0yZDdjZDAxMWRiNDcvdjIuMCJ9.TK9s-qhE0xf0M9dJy_JzC0JtySIDHaASVfZloSDhrtg" https://jsonplaceholder.typicode.com/posts/1

@waldekmastykarz
waldekmastykarz requested a review from a team as a code owner April 29, 2025 11:08
@waldekmastykarz waldekmastykarz added the pr-bugfix Fixes a bug label Apr 29, 2025
@garrytrinder
garrytrinder requested a review from Copilot April 29, 2025 11:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds authentication support for TypeSpec generation while also fixing bugs in default config usage for Ollama, caching of LLM responses, and enhancing error logging. Key changes include:

  • Trimming and properly assigning the trailing slash in LanguageModel URLs.
  • Introducing auth support in TypeSpec by adding OAuth2Auth in the namespace and updating Operation serialization.
  • Refactoring cache retrieval methods and adding error logging for both OpenAI and Ollama clients, as well as updating header matching in HTTP-related code.

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.

Show a summary per file
File Description
dev-proxy/CommandHandlers/ProxyCommandHandler.cs Improved URL trimming for LanguageModel URLs.
dev-proxy-plugins/TypeSpec/Http.cs Added auth support and updated header name conversion for parameters.
dev-proxy-plugins/TypeSpec/Auth.cs Introduced new auth classes, including OAuth2Auth with alias writing.
dev-proxy-plugins/StringExtensions.cs Added useful string extension methods.
dev-proxy-plugins/Http.cs Updated header list for authentication matching.
dev-proxy-abstractions/LanguageModel/OpenAILanguageModelClient.cs Refactored cache method and improved error logging for OpenAI client.
dev-proxy-abstractions/LanguageModel/OllamaLanguageModelClient.cs Refactored cache method and improved error logging for Ollama client.
Comments suppressed due to low confidence (1)

dev-proxy-plugins/Http.cs:237

  • The removal of the 'api-key' header from the allowed headers may cause authentication issues since test requests use 'api-key'. Consider re-adding 'api-key' or updating tests and related documentation to reflect this change.
"apikey",

@garrytrinder
garrytrinder merged commit 55bf52b into dotnet:main Apr 29, 2025
@waldekmastykarz
waldekmastykarz deleted the tsp-auth branch April 29, 2025 11:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Fixes a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request - TypeSpec generation - include Authentication

3 participants