Files
dtourolle 5c8430f207
🏗️ Build Plugin / build (push) Successful in 38s
🧪 Test Plugin / test (push) Successful in 34s
Treat member order as insignificant when resolving a group
"jane+john" and "john+jane" name the same group, but they did not behave
that way. Jellyfin only routes a login here when no account matches the
typed name, so logging in with the reversed spelling of an existing group
found nothing and quietly created a second shared account for the same
two people - each with its own watched state.

Group identity is now order-independent:

- Member names are sorted alphabetically when building an account name,
  so a given set of members always produces the same name.
- Before creating anything, the login path looks for an existing group
  whose members are exactly the named set, compared as a set rather than
  a sequence, and logs into that account if it finds one.
- Stored member lists are kept in the same canonical order on create and
  update, so a group's stored order does not depend on the order an
  admin happened to select members in.

Passing no name through to provisioning lets it generate the canonical
name, rather than preserving whatever order was typed.

Members are also now checked in the order they were typed, stopping at
the first match, so whoever puts their own name first is verified first.
Verification is a deliberately slow hash comparison, so the ordering is
worth having; it is only a preference, and any member's password still
unlocks the group.
2026-07-31 09:35:14 +02:00

61 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using MediaBrowser.Model.Cryptography;
namespace Jellyfin.Plugin.WatchedTogether.Tests;
/// <summary>
/// A crypto provider that accepts exactly the (stored hash, submitted password) pairs it is given.
/// </summary>
/// <remarks>
/// Hand-written rather than mocked: <see cref="ICryptoProvider.Verify"/> takes a
/// <c>ReadOnlySpan&lt;char&gt;</c>, and a ref struct cannot be used as a generic type argument to
/// Moq's <c>It.IsAny&lt;T&gt;</c>.
/// </remarks>
public sealed class StubCryptoProvider : ICryptoProvider
{
private readonly IReadOnlyList<(string Hash, string Password)> _validPairs;
public StubCryptoProvider(IReadOnlyList<(string Hash, string Password)> validPairs)
{
_validPairs = validPairs;
}
/// <summary>
/// Gets the salt of each credential this provider was asked to verify, in call order. Lets a
/// test assert which member's password was checked first.
/// </summary>
public List<string> VerifiedSalts { get; } = new();
public string DefaultHashMethod => "PBKDF2-SHA512";
public bool Verify(PasswordHash hash, ReadOnlySpan<char> password)
{
var candidate = password.ToString();
// Identify the stored credential by its salt rather than by re-formatting the whole hash,
// which need not round-trip through Parse/ToString byte for byte.
var salt = Convert.ToHexString(hash.Salt);
VerifiedSalts.Add(salt);
foreach (var (validHash, validPassword) in _validPairs)
{
var expectedSalt = validHash.Split('$')[3];
if (string.Equals(salt, expectedSalt, StringComparison.OrdinalIgnoreCase)
&& candidate == validPassword)
{
return true;
}
}
return false;
}
public PasswordHash CreatePasswordHash(ReadOnlySpan<char> password)
=> throw new NotSupportedException();
public byte[] GenerateSalt() => throw new NotSupportedException();
public byte[] GenerateSalt(int length) => throw new NotSupportedException();
}