Skip to content

Commit

Permalink
Adds support for CYOK end-points (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
kailash-b authored Nov 7, 2024
2 parents 1f547ec + 2167a12 commit e769e45
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Auth0.ManagementApi/Clients/IKeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,11 @@ public interface IKeysClient
/// <param name="request"><see cref="WrappingKeyCreateRequest"/></param>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
Task<WrappingKey> CreatePublicWrappingKeyAsync(WrappingKeyCreateRequest request, CancellationToken cancellationToken = default);

/// <summary>
/// Perform rekeying operation on the key hierarchy.
/// </summary>
/// <param name="cancellationToken">The cancellation token to cancel operation.</param>
Task RekeyAsync(CancellationToken cancellationToken = default);
}
}
13 changes: 12 additions & 1 deletion src/Auth0.ManagementApi/Clients/KeysClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,24 @@ public Task<WrappingKey> CreatePublicWrappingKeyAsync(

if (string.IsNullOrEmpty(request.Kid))
throw new ArgumentNullException(nameof(request.Kid));

return Connection.SendAsync<WrappingKey>(
HttpMethod.Post,
BuildUri($"keys/encryption/{EncodePath(request.Kid)}/wrapping-key"),
body: null,
headers: DefaultHeaders,
cancellationToken: cancellationToken);
}

/// <inheritdoc cref="IKeysClient.RekeyAsync"/>
public Task RekeyAsync(CancellationToken cancellationToken = default)
{
return Connection.SendAsync<object>(
HttpMethod.Post,
BuildUri("keys/encryption/rekey"),
body: null,
headers: DefaultHeaders,
cancellationToken: cancellationToken);
}
}
}
36 changes: 36 additions & 0 deletions tests/Auth0.ManagementApi.IntegrationTests/KeysTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,41 @@ public async void Test_import_encrypted_keys()
importedKeys.Type.Should().Be(EncryptionKeyType.CustomerProvidedRootKey);
importedKeys.ParentKid.Should().Be("a20128c5-9bf5-4209-8c43-b6dfcee60e9b");
}

[Fact]
public async void Test_rekey_encrypted_keys()
{
// Get the existing Tenant Master Key
var existingTenantMasterKey = await GetExistingTenantMasterKey();

await fixture.ApiClient.Keys.RekeyAsync();

var newTenantMasterKey = await GetExistingTenantMasterKey();

// After rekey operation, a new Tenant Master Key should be generated
existingTenantMasterKey.Should().NotBeEquivalentTo(newTenantMasterKey);

var existingTenantMasterKeyAfterRekey =
await fixture.ApiClient.Keys.GetEncryptionKeyAsync(
new EncryptionKeyGetRequest()
{
Kid = existingTenantMasterKey.Kid
}
);

// Confirming that the old master key is destroyed
existingTenantMasterKeyAfterRekey.State.Should().Be(EncryptionKeyState.Destroyed);
}

private async Task<EncryptionKey> GetExistingTenantMasterKey()
{
var existingEncryptionKeys = await fixture.ApiClient.Keys.GetAllEncryptionKeysAsync(new PaginationInfo());

// Get the existing Tenant Master Key
var existingTenantMasterKey =
existingEncryptionKeys.FirstOrDefault(x => x.State == EncryptionKeyState.Active &&
x.Type == EncryptionKeyType.TenantMasterKey);
return existingTenantMasterKey;
}
}
}

0 comments on commit e769e45

Please sign in to comment.