Repository navigation
fix(private-api): device routes only act on the signed-in account #102
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
Merged
Merged
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| using EcencyApi.Handlers; | ||
| using Xunit; | ||
|
|
||
| namespace EcencyApi.Tests; | ||
|
|
||
| /// <summary> | ||
| /// /private-api/register-device and /private-api/detail-device act on a push | ||
| /// registration, which belongs to the account the code was issued for. | ||
| /// </summary> | ||
| public class DeviceAuthorizationTests | ||
| { | ||
| [Theory] | ||
| [InlineData("good-karma")] | ||
| // Hive names are lowercase, but the comparison must not hinge on that. | ||
| [InlineData("Good-Karma")] | ||
| [InlineData("GOOD-KARMA")] | ||
| public void TheCodesOwnAccountIsAccepted(string requested) | ||
| { | ||
| Assert.True(PrivateApi.IsOwnDeviceRequest("good-karma", requested)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("someone-else")] | ||
| [InlineData("good-karm")] | ||
| [InlineData("good-karma2")] | ||
| [InlineData(" good-karma")] | ||
| [InlineData("good_karma")] | ||
| public void AnyOtherAccountIsRefused(string requested) | ||
| { | ||
| Assert.False(PrivateApi.IsOwnDeviceRequest("good-karma", requested)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| public void AMissingUsernameIsRefused(string? requested) | ||
| { | ||
| Assert.False(PrivateApi.IsOwnDeviceRequest("good-karma", requested)); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| public void WithoutAValidatedCodeNothingIsAccepted(string? validated) | ||
| { | ||
| Assert.False(PrivateApi.IsOwnDeviceRequest(validated, "good-karma")); | ||
| Assert.False(PrivateApi.IsOwnDeviceRequest(validated, validated)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The device handlers themselves: a code for one account cannot register or read a | ||
| /// device row under another, and nothing reaches upstream when it tries. | ||
| /// </summary> | ||
| [Collection("device-auth")] | ||
| public class DeviceHandlerTests : IDisposable | ||
| { | ||
| private readonly CurationDeskTestSupport.Recorder _upstream = new(); | ||
|
|
||
| public DeviceHandlerTests() | ||
| { | ||
| // `code` "as:alice" validates as alice; anything else is invalid. | ||
| PrivateApi.DeviceValidateCode = body => | ||
| { | ||
| var code = body["code"]?.GetValue<string>(); | ||
| return Task.FromResult(code != null && code.StartsWith("as:", StringComparison.Ordinal) ? code[3..] : null); | ||
| }; | ||
| PrivateApi.DeviceUpstream = (endpoint, method, payload) => | ||
| _upstream.Handle(endpoint, method, Array.Empty<KeyValuePair<string, string>>(), payload); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| PrivateApi.DeviceValidateCode = PrivateApi.ValidateCode; | ||
| PrivateApi.DeviceUpstream = (endpoint, method, payload) => | ||
| EcencyApi.Infrastructure.ApiClient.ApiRequest(endpoint, method, null, payload); | ||
| } | ||
|
|
||
| private static string Register(string code, string? username) => | ||
| username == null | ||
| ? $$"""{"code":"{{code}}","token":"t1","system":"fcm-ios","allows_notify":1,"notify_types":[1]}""" | ||
| : $$"""{"code":"{{code}}","username":"{{username}}","token":"t1","system":"fcm-ios","allows_notify":1,"notify_types":[1]}"""; | ||
|
|
||
| [Theory] | ||
| [InlineData("alice")] | ||
| [InlineData("Alice")] | ||
| public async Task RegisteringTheCodesOwnAccountIsForwarded(string username) | ||
| { | ||
| var ctx = CurationDeskTestSupport.Post("/private-api/register-device", Register("as:alice", username)); | ||
| await PrivateApi.RegisterDevice(ctx); | ||
|
|
||
| Assert.Equal(200, ctx.Response.StatusCode); | ||
| var call = Assert.Single(_upstream.Calls); | ||
| Assert.Equal("rgstrmbldvc/", call.Endpoint); | ||
| Assert.Equal(HttpMethod.Post, call.Method); | ||
| Assert.Equal(username, call.Payload?["username"]?.GetValue<string>()); | ||
| Assert.Equal("t1", call.Payload?["token"]?.GetValue<string>()); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("bob")] | ||
| [InlineData(null)] | ||
| public async Task RegisteringAnotherAccountIsRefused(string? username) | ||
| { | ||
| var ctx = CurationDeskTestSupport.Post("/private-api/register-device", Register("as:alice", username)); | ||
| await PrivateApi.RegisterDevice(ctx); | ||
|
|
||
| Assert.Equal(403, ctx.Response.StatusCode); | ||
| Assert.Empty(_upstream.Calls); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task RegisteringWithoutAValidCodeIsUnauthorized() | ||
| { | ||
| var ctx = CurationDeskTestSupport.Post("/private-api/register-device", Register("bogus", "alice")); | ||
| await PrivateApi.RegisterDevice(ctx); | ||
|
|
||
| Assert.Equal(401, ctx.Response.StatusCode); | ||
| Assert.Empty(_upstream.Calls); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadingTheCodesOwnDeviceIsForwarded() | ||
| { | ||
| var ctx = CurationDeskTestSupport.Post( | ||
| "/private-api/detail-device", """{"code":"as:alice","username":"alice","token":"t1"}"""); | ||
| await PrivateApi.DetailDevice(ctx); | ||
|
|
||
| Assert.Equal(200, ctx.Response.StatusCode); | ||
| var call = Assert.Single(_upstream.Calls); | ||
| Assert.Equal("mbldvcdtl/alice/t1", call.Endpoint); | ||
| Assert.Equal(HttpMethod.Get, call.Method); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReadingAnotherAccountsDeviceIsRefused() | ||
| { | ||
| var ctx = CurationDeskTestSupport.Post( | ||
| "/private-api/detail-device", """{"code":"as:alice","username":"bob","token":"t1"}"""); | ||
| await PrivateApi.DetailDevice(ctx); | ||
|
|
||
| Assert.Equal(403, ctx.Response.StatusCode); | ||
| Assert.Empty(_upstream.Calls); | ||
| } | ||
| } | ||
|
|
||
| [CollectionDefinition("device-auth", DisableParallelization = true)] | ||
| public class DeviceAuthCollection { } |
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.