feat(auth): add post-auth hook mechanism#1527
feat(auth): add post-auth hook mechanism#1527HEUDavid wants to merge 9 commits intorouter-for-me:devfrom
Conversation
Summary of ChangesHello @HEUDavid, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a flexible post-authentication hook system, allowing for dynamic modification of authentication records based on incoming HTTP request details. This enhancement facilitates advanced scenarios such as tagging credentials with specific attributes like user IDs or usage plans, which can then be leveraged for routing or policy enforcement within the system. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a post-authentication hook mechanism, which is a great feature for extensibility. The implementation is mostly solid, but I've found a couple of areas for improvement.
My main concern is the use of context.Background() in the token request handlers, which detaches the asynchronous operations from the request's lifecycle. I've suggested using c.Request.Context() to ensure proper cancellation propagation.
Additionally, I've provided suggestions to improve the PopulateAuthContext function to handle multi-value headers/parameters correctly and to use a typed context key, which is a Go best practice.
|
@luispater 你好,已经对 AI 的建议都做出回应,请 CR 代码。 |
能否给我详细解释一下这个PR的应用场景是什么?我理解不了它的应用场景。 |
我用代码来说明:
WithPostAuthHook(func(ctx context.Context, auth *coreauth.Auth) error {
// Extract RequestInfo from context
if info := coreauth.GetRequestInfo(ctx); info != nil {
log.Printf("PostAuthHook: Injected Metadata for %s", auth.ID)
log.Printf("Request Headers: %+v", info.Headers)
log.Printf("Request Query: %+v", info.Query)
auth.Metadata["AAA"] = "AAA"
auth.Metadata["BBB"] = "BBB"
if key := info.Headers["X-Management-Key"]; key != "" {
if auth.Metadata == nil {
auth.Metadata = make(map[string]any)
}
auth.Metadata["X-Management-Key"] = key
}
}
return nil
}).
Build()
auth-file 中会额外记录Metadata, 当前代码在各类型的 provider 中是 hardcode 操作 email 等那些信息,没有拓展能力。没有充分利用您定义的Metadata字段 // Attributes stores provider specific metadata needed by executors (immutable configuration).
Attributes map[string]string `json:"attributes,omitempty"`
// Metadata stores runtime mutable provider state (e.g. tokens, cookies).
Metadata map[string]any `json:"metadata,omitempty"`{
"AAA": "AAA",
"BBB": "BBB",
"X-Management-Key": "CCC",
"access_token": "",
"email": "1770736309056",
"expired": "2026-02-11T05:11:49+08:00",
"last_refresh": "2026-02-10T23:11:49+08:00",
"refresh_token": "",
"resource_url": "portal.qwen.ai",
"type": "qwen"
}本 PR 提供一个接口来操作 auth-file, 可以灵活拓展 auth-file 属性,甚至操作 auth-file 文件名等。可以更容易地把启动 oauth 流程的用户属性与其 auth-file 直接绑定起来,类似 SAAS 服务中单实例多租户场景。 |
16163a8 to
e0c82b7
Compare
e0c82b7 to
65debb8
Compare
|
@luispater 你好,还有其他问题吗?或者还需要我做其他调整吗? |
Summary
This PR introduces a PostAuthHook mechanism that allows developers to intercept and modify authentication records before they are saved to disk. This is particularly useful for injecting metadata derived from the HTTP request (such as headers or query parameters) into the auth record, which can then be used for routing or policy enforcement.
Changes
sdk/cliproxy/auth.WithPostAuthHookoption toServerOptionto allow registering a hook at startup.Handlerandauth_files.goto execute the registered hook when creating or updating auth files, passingRequestInfoextracted from the Gin context.Use Case
This feature enables scenarios where auth credentials need to be tagged with specific attributes (e.g., user ID, Client, origin, usage plan) present in the registration request headers or query parameters.