Files
dtourolle 7be07d16a2
🏗️ Build Plugin / build (push) Has been cancelled
🧪 Test Plugin / test (push) Has been cancelled
Implement Watched Together shared viewing accounts
Replaces the plugin template with a working plugin that lets several
users share one viewing account while keeping their individual watched
lists accurate.

Three pieces:

- Auto-creating groups. Logging in as "alice+bob" with any named
  member's own password provisions the shared account and signs you in.
  Verified against 10.11.5: AuthenticateUser offers unmatched usernames
  to every enabled provider and re-queries afterwards, which is the hook
  this relies on. Gated on a real member password so knowing two
  usernames is not enough to create an account.

- Multi-password authentication. IRequiresResolvedUser hands us the
  resolved shared account; each member's live stored hash is checked via
  ICryptoProvider.Verify. Deliberately avoids re-entering
  UserManager.AuthenticateUser, which would trip every member's
  failed-attempt counter whenever a different member's password matched.

- One-way played-state sync. Shared account to members only, filtered to
  PlaybackFinished/TogglePlayed/Import so playback progress ticks are
  ignored. No loop guard needed: member writes carry a non-shared id.

Membership is stored as user IDs rather than re-parsed from the username,
so shared accounts can be renamed freely. The +/name collision resolves
itself because Jellyfin only consults the plugin when no local user
matches the typed name.

Targets Jellyfin 10.11.x / net9.0. Adds Gitea CI (test, build, release),
a builder image, and 34 tests covering the auth and sync rules.
2026-07-29 00:00:13 +02:00

54 lines
1.8 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;
}
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);
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();
}