Repository navigation
feat: 新增 AUTH_RECOVERY 控制认证失效后的续签材料读取 #3
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,14 @@ package app | |
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/Mag1cFall/AIStudio2API/internal/aistudio" | ||
| "github.com/Mag1cFall/AIStudio2API/internal/camoufoxnative" | ||
| ) | ||
|
|
||
| // TestAuthRefreshWithoutExtension 验证缺少续签扩展的账户从当前 Chrome 导入同一邮箱并写回扩展,重新登录保留扩展 | ||
|
|
@@ -73,3 +76,103 @@ func TestAuthRefreshWithoutExtension(t *testing.T) { | |
| t.Fatalf("重新登录丢失续签扩展: %+v", kept) | ||
| } | ||
| } | ||
|
|
||
| // newAuthRecoveryFixture 创建有一个账户、已注入账户租约上下文的续签测试环境 | ||
| func newAuthRecoveryFixture(t *testing.T) (*aistudio.Account, *aistudio.AccountLease, *aistudio.AccountPool, context.Context) { | ||
| t.Helper() | ||
| directory := t.TempDir() | ||
| storage := filepath.Join(directory, "storage-state.json") | ||
| state := `{"cookies":[{"name":"SAPISID","value":"old","domain":".google.com","path":"/","secure":true,"sameSite":"Lax"}],"origins":[]}` | ||
| if err := os.WriteFile(storage, []byte(state), 0o600); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| loaded, err := aistudio.LoadStorageState(storage) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| account := &aistudio.Account{ | ||
| ID: "[email protected]", Directory: directory, StoragePath: storage, | ||
| RuntimePath: filepath.Join(directory, "runtime-state.json"), StorageState: loaded, | ||
| Config: aistudio.DefaultAccountConfig("[email protected]"), | ||
| } | ||
| pool := aistudio.NewAccountPool([]*aistudio.Account{account}, 1) | ||
| lease, err := pool.AcquireAccount(t.Context(), account.ID) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| t.Cleanup(func() { lease.Release() }) | ||
| return account, lease, pool, aistudio.ContextWithAccountLease(t.Context(), lease) | ||
| } | ||
|
|
||
| // TestAuthRecoveryDisabledMarksRequired 验证关闭续签时认证失败不读取续签材料,账户仍按官网确认的结果标记为需要重新登录 | ||
| func TestAuthRecoveryDisabledMarksRequired(t *testing.T) { | ||
| account, lease, pool, ctx := newAuthRecoveryFixture(t) | ||
| used := false | ||
| refresher := &authRuntimeRefresher{ | ||
| pool: pool, requests: newRequestRegistry(t.Context()), authRecovery: false, | ||
| refresh: func(context.Context, aistudio.AuthSource, aistudio.ChromeOAuthMaterial, string) ([]aistudio.StateCookie, *aistudio.BoundSession, error) { | ||
| used = true | ||
| return nil, nil, nil | ||
| }, | ||
| importCurrent: func(context.Context, aistudio.AuthSource, *aistudio.ChromeOAuthMaterial, string) (aistudio.StorageState, error) { | ||
| used = true | ||
| return aistudio.StorageState{}, nil | ||
| }, | ||
| chromeHas: func(email string) bool { return email == account.ID }, | ||
| reset: func(string) error { return nil }, | ||
| } | ||
| if !refresher.Available(ctx) { | ||
| t.Fatal("账户在 Chrome 中登录但被判定为不可续签") | ||
| } | ||
| cause := fmt.Errorf("%w: test", camoufoxnative.ErrAuthenticationRequired) | ||
| err := refresher.Recover(ctx, cause, lease.AuthGeneration()) | ||
| if err == nil || !errors.Is(err, camoufoxnative.ErrAuthenticationRequired) { | ||
| t.Fatalf("Recover err = %v", err) | ||
| } | ||
| if used { | ||
| t.Fatal("关闭续签后仍读取了续签材料") | ||
| } | ||
| if account.State != aistudio.AccountAuthRequired { | ||
| t.Fatalf("account.State = %v", account.State) | ||
| } | ||
| } | ||
|
|
||
| // TestAuthRecoveryEnabledRenews 验证默认开启续签时认证失败按账户保存的材料原地续签 | ||
| func TestAuthRecoveryEnabledRenews(t *testing.T) { | ||
| account, lease, pool, ctx := newAuthRecoveryFixture(t) | ||
| var cookies []aistudio.StateCookie | ||
| for _, name := range []string{"SAPISID", "__Secure-1PAPISID", "__Secure-3PAPISID"} { | ||
| cookies = append(cookies, aistudio.StateCookie{Name: name, Value: "renewed", Domain: ".google.com", Path: "/", Expires: -1, Secure: true, SameSite: "Lax"}) | ||
| } | ||
| imported := false | ||
| refresher := &authRuntimeRefresher{ | ||
| pool: pool, requests: newRequestRegistry(t.Context()), authRecovery: true, | ||
| refresh: func(context.Context, aistudio.AuthSource, aistudio.ChromeOAuthMaterial, string) ([]aistudio.StateCookie, *aistudio.BoundSession, error) { | ||
| return nil, nil, fmt.Errorf("没有 OAuth 材料时不应调用 refresh") | ||
| }, | ||
| importCurrent: func(_ context.Context, source aistudio.AuthSource, _ *aistudio.ChromeOAuthMaterial, _ string) (aistudio.StorageState, error) { | ||
| imported = true | ||
| updated := aistudio.StorageState{Cookies: cookies} | ||
| updated.SetAuthExtension(aistudio.AuthExtension{ | ||
| Source: source, OAuth: &aistudio.ChromeOAuthMaterial{GaiaID: "gaia", RefreshToken: "token"}, | ||
| }) | ||
| return updated, nil | ||
| }, | ||
| chromeHas: func(email string) bool { return email == account.ID }, | ||
| reset: func(string) error { return nil }, | ||
| } | ||
| cause := fmt.Errorf("%w: test", camoufoxnative.ErrAuthenticationRequired) | ||
| if err := refresher.Recover(ctx, cause, lease.AuthGeneration()); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !imported { | ||
| t.Fatal("开启续签时认证失败没有读取续签材料") | ||
| } | ||
| stored, err := aistudio.LoadStorageState(account.StoragePath) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if len(stored.Cookies) == 0 || stored.Cookies[0].Value != "renewed" { | ||
| t.Fatalf("续签结果 cookies = %+v", stored.Cookies) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.