Repository navigation
perf(gateway): verify each request's account token once #261
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
| using OpenClaw.Core.Models; | ||
| using OpenClaw.Gateway; | ||
| using OpenClaw.Gateway.Bootstrap; | ||
| using OpenClaw.Gateway.Endpoints; | ||
| using Xunit; | ||
|
|
||
| namespace OpenClaw.Tests; | ||
|
|
||
| public sealed class EndpointHelpersAuthenticationTests : IDisposable | ||
| { | ||
| private readonly string _storagePath = Path.Combine(Path.GetTempPath(), "openclaw-endpoint-auth-tests", Guid.NewGuid().ToString("N")); | ||
|
|
||
| public void Dispose() | ||
| { | ||
| try { Directory.Delete(_storagePath, recursive: true); } | ||
| catch { } | ||
|
Telli marked this conversation as resolved.
Telli marked this conversation as resolved.
|
||
| } | ||
|
|
||
| [Fact] | ||
| public void AccountToken_WhenCheckedTwiceInOneRequest_ShouldBeVerifiedOnce() | ||
| { | ||
| var (startup, services, accounts, accountId, token) = CreateOperatorToken(); | ||
| var ctx = CreateRequest(services, token.Token); | ||
|
|
||
| Assert.True(EndpointHelpers.IsAuthorizedRequest(ctx, startup.Config, startup.IsNonLoopbackBind)); | ||
|
|
||
| // Revoking after the first check makes a second verification observable: a re-run of the slow | ||
| // token check would now fail, while a reused result for this request still succeeds. | ||
| Assert.True(accounts.RevokeToken(accountId, token.TokenInfo!.Id)); | ||
| var auth = EndpointHelpers.AuthorizeOperatorRequest(ctx, startup, services.GetRequiredService<BrowserSessionAuthService>(), requireCsrf: false); | ||
|
|
||
| Assert.True(auth.IsAuthorized); | ||
| Assert.Equal(accountId, auth.AccountId); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AccountToken_WhenRevokedBeforeNextRequest_ShouldBeRejected() | ||
| { | ||
| var (startup, services, accounts, accountId, token) = CreateOperatorToken(); | ||
| Assert.True(EndpointHelpers.IsAuthorizedRequest(CreateRequest(services, token.Token), startup.Config, startup.IsNonLoopbackBind)); | ||
|
|
||
| Assert.True(accounts.RevokeToken(accountId, token.TokenInfo!.Id)); | ||
|
|
||
| Assert.False(EndpointHelpers.IsAuthorizedRequest(CreateRequest(services, token.Token), startup.Config, startup.IsNonLoopbackBind)); | ||
| } | ||
|
|
||
| private (GatewayStartupContext Startup, ServiceProvider Services, OperatorAccountService Accounts, string AccountId, OperatorAccountTokenCreateResponse Token) CreateOperatorToken() | ||
| { | ||
| var config = new GatewayConfig { AuthToken = "bootstrap-token" }; | ||
| var startup = new GatewayStartupContext | ||
| { | ||
| Config = config, | ||
| RuntimeState = RuntimeModeResolver.Resolve(config.Runtime, dynamicCodeSupported: true), | ||
| IsNonLoopbackBind = true | ||
| }; | ||
| var accounts = new OperatorAccountService(_storagePath, NullLogger<OperatorAccountService>.Instance); | ||
| var account = accounts.Create(new OperatorAccountCreateRequest | ||
| { | ||
| Username = "memo-operator", | ||
| Password = "P@ssw0rd123!", | ||
| Role = OperatorRoleNames.Operator | ||
| }); | ||
| var token = accounts.CreateToken(account.Id, new OperatorAccountTokenCreateRequest { Label = "memo" })!; | ||
|
|
||
| var services = new ServiceCollection(); | ||
| services.AddSingleton(new BrowserSessionAuthService(config)); | ||
| services.AddSingleton(accounts); | ||
| services.AddSingleton(new OrganizationPolicyService(_storagePath, NullLogger<OrganizationPolicyService>.Instance)); | ||
| return (startup, services.BuildServiceProvider(), accounts, account.Id, token); | ||
| } | ||
|
|
||
| private static DefaultHttpContext CreateRequest(IServiceProvider services, string token) | ||
| { | ||
| var ctx = new DefaultHttpContext { RequestServices = services }; | ||
| ctx.Request.Headers.Authorization = $"Bearer {token}"; | ||
| return ctx; | ||
| } | ||
| } | ||
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.