Skip to content

Commit bead4f1

Browse files
authored
validate and email on sso privisioning (#6734)
1 parent 3c44430 commit bead4f1

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

src/Core/Auth/UserFeatures/Registration/Implementations/RegisterUserCommand.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public async Task<IdentityResult> RegisterUser(User user)
9999

100100
public async Task<IdentityResult> RegisterSSOAutoProvisionedUserAsync(User user, Organization organization)
101101
{
102+
// Validate that the email domain is not blocked by another organization's policy
103+
await ValidateEmailDomainNotBlockedAsync(user.Email, organization.Id);
104+
102105
var result = await _userService.CreateUserAsync(user);
103106
if (result == IdentityResult.Success)
104107
{

test/Core.Test/Auth/UserFeatures/Registration/RegisterUserCommandTests.cs

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,4 +1382,90 @@ await sutProvider.GetDependency<IMailService>()
13821382
.Received(1)
13831383
.SendOrganizationUserWelcomeEmailAsync(user, organization.DisplayName());
13841384
}
1385+
1386+
[Theory, BitAutoData]
1387+
public async Task RegisterSSOAutoProvisionedUserAsync_WithBlockedDomain_ThrowsException(
1388+
User user,
1389+
Organization organization,
1390+
SutProvider<RegisterUserCommand> sutProvider)
1391+
{
1392+
// Arrange
1393+
user.Email = "user@blocked-domain.com";
1394+
1395+
sutProvider.GetDependency<IFeatureService>()
1396+
.IsEnabled(FeatureFlagKeys.BlockClaimedDomainAccountCreation)
1397+
.Returns(true);
1398+
1399+
sutProvider.GetDependency<IOrganizationDomainRepository>()
1400+
.HasVerifiedDomainWithBlockClaimedDomainPolicyAsync("blocked-domain.com", organization.Id)
1401+
.Returns(true);
1402+
1403+
// Act & Assert
1404+
var exception = await Assert.ThrowsAsync<BadRequestException>(() =>
1405+
sutProvider.Sut.RegisterSSOAutoProvisionedUserAsync(user, organization));
1406+
Assert.Equal("This email address is claimed by an organization using Bitwarden.", exception.Message);
1407+
}
1408+
1409+
[Theory, BitAutoData]
1410+
public async Task RegisterSSOAutoProvisionedUserAsync_WithOwnClaimedDomain_Succeeds(
1411+
User user,
1412+
Organization organization,
1413+
SutProvider<RegisterUserCommand> sutProvider)
1414+
{
1415+
// Arrange
1416+
user.Email = "user@company-domain.com";
1417+
1418+
sutProvider.GetDependency<IFeatureService>()
1419+
.IsEnabled(FeatureFlagKeys.BlockClaimedDomainAccountCreation)
1420+
.Returns(true);
1421+
1422+
// Domain is claimed by THIS organization, so it should be allowed
1423+
sutProvider.GetDependency<IOrganizationDomainRepository>()
1424+
.HasVerifiedDomainWithBlockClaimedDomainPolicyAsync("company-domain.com", organization.Id)
1425+
.Returns(false); // Not blocked because organization.Id is excluded
1426+
1427+
sutProvider.GetDependency<IUserService>()
1428+
.CreateUserAsync(user)
1429+
.Returns(IdentityResult.Success);
1430+
1431+
// Act
1432+
var result = await sutProvider.Sut.RegisterSSOAutoProvisionedUserAsync(user, organization);
1433+
1434+
// Assert
1435+
Assert.True(result.Succeeded);
1436+
await sutProvider.GetDependency<IUserService>()
1437+
.Received(1)
1438+
.CreateUserAsync(user);
1439+
}
1440+
1441+
[Theory, BitAutoData]
1442+
public async Task RegisterSSOAutoProvisionedUserAsync_WithNonClaimedDomain_Succeeds(
1443+
User user,
1444+
Organization organization,
1445+
SutProvider<RegisterUserCommand> sutProvider)
1446+
{
1447+
// Arrange
1448+
user.Email = "user@unclaimed-domain.com";
1449+
1450+
sutProvider.GetDependency<IFeatureService>()
1451+
.IsEnabled(FeatureFlagKeys.BlockClaimedDomainAccountCreation)
1452+
.Returns(true);
1453+
1454+
sutProvider.GetDependency<IOrganizationDomainRepository>()
1455+
.HasVerifiedDomainWithBlockClaimedDomainPolicyAsync("unclaimed-domain.com", organization.Id)
1456+
.Returns(false); // Domain is not claimed by any org
1457+
1458+
sutProvider.GetDependency<IUserService>()
1459+
.CreateUserAsync(user)
1460+
.Returns(IdentityResult.Success);
1461+
1462+
// Act
1463+
var result = await sutProvider.Sut.RegisterSSOAutoProvisionedUserAsync(user, organization);
1464+
1465+
// Assert
1466+
Assert.True(result.Succeeded);
1467+
await sutProvider.GetDependency<IUserService>()
1468+
.Received(1)
1469+
.CreateUserAsync(user);
1470+
}
13851471
}

0 commit comments

Comments
 (0)