Public Access
Let a team be joined only by somebody who is already here
An invitation decided access from an assertion about an address. Everything else
in this model decides it from something a person did — an admin naming an
account, a key holder wrapping a vault key to a key they verified — and this was
the one place a token's email claim was the thing that let somebody in.
It was guarded as tightly as that can be guarded: the claim was refused outright
on an unverified or absent `email_verified`, with no setting to relax it. But the
guard and the risk were the same shape. The whole defence was one boolean sent by
a system the deployment does not control.
So `POST /teams/{id}/members` is the only way in, and an address with no account
is refused with `no-such-account` — which is now the end of the road rather than
the signal to invite. Both clients say the remedy: that person signs in here
once, which is what creates the account, and then they can be added. The desktop
leaves the address in the box, because a message telling you to come back later
is one you act on later.
Gone with it: the `team_invitation` table, the claim hook in the sign-in path,
and `Oidc:EmailVerifiedClaim`, which that hook was the only reader of. Nothing in
the server now reads the email claim to decide anything.
Pending invitations are dropped rather than converted. Converting one would mean
creating a membership because an address matched, which is the property being
removed — and an invitation to an address that did have an account here had
already been claimed by the hourly sweep, so what is left is offers to people who
never arrived.
Two tests carry the property rather than the feature: the endpoint inventory
asserts the three routes are absent, and the API suite adds an address that has
no account, watches the refusal, then signs that address in and checks it joined
nothing. Without the second half, a server that merely renamed the deferred path
would pass.
This commit is contained in:
@@ -468,108 +468,6 @@ public sealed class SchemaConstraintTests(PostgresFixture fixture)
|
||||
UpdatedAtUtc = Now,
|
||||
};
|
||||
|
||||
// ---- Team invitations ----
|
||||
|
||||
/// <remarks>
|
||||
/// One live invitation per address per team. Without the index two admins acting a minute apart
|
||||
/// would each leave a row, and the claim at sign-in would apply both — quietly overwriting whichever
|
||||
/// role was decided second with whichever was written first.
|
||||
/// </remarks>
|
||||
[Fact]
|
||||
public async Task Invitation_IsUniquePerTeamAndAddress()
|
||||
{
|
||||
await using var context = fixture.CreateContext();
|
||||
var user = await SeedUserAsync(context);
|
||||
var team = SeedTeam(context, user.Id);
|
||||
var email = $"invite{Guid.CreateVersion7():N}@example.com";
|
||||
|
||||
context.TeamInvitations.Add(NewInvitation(team.Id, email, user.Id));
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
context.TeamInvitations.Add(NewInvitation(team.Id, email, user.Id));
|
||||
|
||||
var exception = await Should.ThrowAsync<DbUpdateException>(() => context.SaveChangesAsync());
|
||||
|
||||
exception.InnerException.ShouldBeOfType<PostgresException>()
|
||||
.SqlState.ShouldBe(PostgresErrorCodes.UniqueViolation);
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// The citext proof, and it is load-bearing rather than tidy: the address in an invitation is typed
|
||||
/// by a person and the one on the token is chosen by the identity provider, so a column that
|
||||
/// compared them case-sensitively would let <c>Alice@</c> and <c>alice@</c> be two invitations and
|
||||
/// would make the claim at sign-in miss the one that was actually sent.
|
||||
/// </remarks>
|
||||
[Fact]
|
||||
public async Task Invitation_IsCaseInsensitivelyUniquePerTeam()
|
||||
{
|
||||
await using var context = fixture.CreateContext();
|
||||
var user = await SeedUserAsync(context);
|
||||
var team = SeedTeam(context, user.Id);
|
||||
var email = $"Invite{Guid.CreateVersion7():N}@Example.com";
|
||||
|
||||
context.TeamInvitations.Add(NewInvitation(team.Id, email.ToUpperInvariant(), user.Id));
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
context.TeamInvitations.Add(NewInvitation(team.Id, email.ToLowerInvariant(), user.Id));
|
||||
|
||||
var exception = await Should.ThrowAsync<DbUpdateException>(() => context.SaveChangesAsync());
|
||||
|
||||
exception.InnerException.ShouldBeOfType<PostgresException>()
|
||||
.SqlState.ShouldBe(PostgresErrorCodes.UniqueViolation);
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// The other half of the filter. Withdrawing an invitation and issuing a fresh one — at a different
|
||||
/// role, say — has to be possible, so the uniqueness is among live rows rather than all of them, and
|
||||
/// the withdrawn row stays for the history.
|
||||
/// </remarks>
|
||||
[Fact]
|
||||
public async Task Invitation_MayBeReissuedAfterItIsRevoked()
|
||||
{
|
||||
await using var context = fixture.CreateContext();
|
||||
var user = await SeedUserAsync(context);
|
||||
var team = SeedTeam(context, user.Id);
|
||||
var email = $"invite{Guid.CreateVersion7():N}@example.com";
|
||||
|
||||
var first = NewInvitation(team.Id, email, user.Id);
|
||||
context.TeamInvitations.Add(first);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
first.RevokedAtUtc = Now;
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
context.TeamInvitations.Add(NewInvitation(team.Id, email, user.Id));
|
||||
|
||||
await Should.NotThrowAsync(() => context.SaveChangesAsync());
|
||||
}
|
||||
|
||||
/// <remarks>
|
||||
/// An accepted invitation frees the slot too, which is what lets somebody removed from a team be
|
||||
/// invited back. The claim marks the old row accepted rather than deleting it, so without this the
|
||||
/// second invitation would collide with a row that has already done its job.
|
||||
/// </remarks>
|
||||
[Fact]
|
||||
public async Task Invitation_MayBeReissuedAfterItIsAccepted()
|
||||
{
|
||||
await using var context = fixture.CreateContext();
|
||||
var user = await SeedUserAsync(context);
|
||||
var team = SeedTeam(context, user.Id);
|
||||
var email = $"invite{Guid.CreateVersion7():N}@example.com";
|
||||
|
||||
var first = NewInvitation(team.Id, email, user.Id);
|
||||
context.TeamInvitations.Add(first);
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
first.AcceptedAtUtc = Now;
|
||||
first.AcceptedByUserId = user.Id;
|
||||
await context.SaveChangesAsync();
|
||||
|
||||
context.TeamInvitations.Add(NewInvitation(team.Id, email, user.Id));
|
||||
|
||||
await Should.NotThrowAsync(() => context.SaveChangesAsync());
|
||||
}
|
||||
|
||||
private static async Task<UserAccount> SeedUserAsync(DodoDbContext context)
|
||||
{
|
||||
var user = NewUser("https://idp.example", Guid.CreateVersion7().ToString());
|
||||
@@ -670,15 +568,4 @@ public sealed class SchemaConstraintTests(PostgresFixture fixture)
|
||||
ActorUserId = Guid.CreateVersion7(),
|
||||
OccurredAtUtc = Now,
|
||||
};
|
||||
|
||||
private static TeamInvitation NewInvitation(Guid teamId, string email, Guid invitedBy) => new()
|
||||
{
|
||||
Id = Guid.CreateVersion7(),
|
||||
TeamId = teamId,
|
||||
Email = email,
|
||||
Role = TeamRole.Member,
|
||||
InvitedByUserId = invitedBy,
|
||||
CreatedAtUtc = Now,
|
||||
ExpiresAtUtc = Now.AddDays(14),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user