-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99e38f8
commit 9667b1f
Showing
7 changed files
with
566 additions
and
1 deletion.
There are no files selected for viewing
146 changes: 146 additions & 0 deletions
146
Descope.Test/IntegrationTests/Management/SsoApplicationTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
using Xunit; | ||
|
||
namespace Descope.Test.Integration | ||
{ | ||
public class SsoApplicationTests | ||
{ | ||
private readonly DescopeClient _descopeClient = IntegrationTestSetup.InitDescopeClient(); | ||
|
||
[Fact] | ||
public async Task SsoApplication_SamlCreateUpdateAndDelete() | ||
{ | ||
string? id = null; | ||
try | ||
{ | ||
var name = "name"; | ||
var url = "https://sometestidp.com"; | ||
// Create | ||
var options = new SamlApplicationOptions(name, url); | ||
id = await _descopeClient.Management.SsoApplication.CreateSAMLApplication(options); | ||
|
||
// Load | ||
var loadedApp = await _descopeClient.Management.SsoApplication.Load(id); | ||
Assert.Equal(name, loadedApp.Name); | ||
Assert.Equal(url, loadedApp.SamlSettings!.LoginPageUrl); | ||
|
||
// Update | ||
options.Id = id; | ||
name = "updated name"; | ||
url = "https://someothertestidp.com"; | ||
await _descopeClient.Management.SsoApplication.UpdateSAMLApplication(options); | ||
|
||
// Load All | ||
var apps = await _descopeClient.Management.SsoApplication.LoadAll(); | ||
loadedApp = apps.Find(a => a.Id == id); | ||
Assert.Equal(name, loadedApp!.Name); | ||
Assert.Equal(url, loadedApp.SamlSettings!.LoginPageUrl); | ||
|
||
// Delete | ||
await _descopeClient.Management.SsoApplication.Delete(id); | ||
id = null; | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(id)) | ||
{ | ||
try { await _descopeClient.Management.SsoApplication.Delete(id); } | ||
catch { } | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Sso_SamlByMetadata() | ||
{ | ||
string? tenantId = null; | ||
string? roleName = null; | ||
try | ||
{ | ||
// Create a tenant | ||
tenantId = await _descopeClient.Management.Tenant.Create(new TenantOptions(Guid.NewGuid().ToString())); | ||
roleName = Guid.NewGuid().ToString()[..20]; | ||
await _descopeClient.Management.Role.Create(roleName, tenantId: tenantId); | ||
|
||
// update sso settings | ||
var settings = new SsoSamlSettingsByMetadata("https://sometestidpmd.com") | ||
{ | ||
RoleMappings = new List<RoleMapping> { new RoleMapping(new List<string> { "group1", "group2" }, roleName) } | ||
}; | ||
await _descopeClient.Management.Sso.ConfigureSamlSettingsByMetadata(tenantId, settings, "https://myredirect.com", new List<string> { "domain1.com" }); | ||
|
||
var loadedSetting = await _descopeClient.Management.Sso.LoadSettings(tenantId); | ||
|
||
// Make sure the settings match | ||
Assert.Equal(settings.IdpMetadataUrl, loadedSetting.Saml.IdpMetadataUrl); | ||
Assert.NotEmpty(loadedSetting.Saml.GroupsMapping?.First()?.Role?.Id ?? ""); | ||
Assert.Equal("group1", loadedSetting.Saml.GroupsMapping?.First()?.Groups?[0]); | ||
Assert.Equal("group2", loadedSetting.Saml.GroupsMapping?.First()?.Groups?[1]); | ||
Assert.Equal("https://myredirect.com", loadedSetting.Saml?.RedirectUrl); | ||
Assert.Equal("domain1.com", loadedSetting.Tenant.Domains.First()); | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(tenantId)) | ||
{ | ||
try { await _descopeClient.Management.Tenant.Delete(tenantId); } | ||
catch { } | ||
} | ||
if (!string.IsNullOrEmpty(roleName)) | ||
{ | ||
try { await _descopeClient.Management.Role.Delete(roleName); } | ||
catch { } | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Sso_Oidc() | ||
{ | ||
string? tenantId = null; | ||
string? roleName = null; | ||
try | ||
{ | ||
// Create a tenant | ||
tenantId = await _descopeClient.Management.Tenant.Create(new TenantOptions(Guid.NewGuid().ToString())); | ||
roleName = Guid.NewGuid().ToString()[..20]; | ||
await _descopeClient.Management.Role.Create(roleName, tenantId: tenantId); | ||
|
||
// Update sso settings | ||
var settings = new SsoOidcSettings | ||
{ | ||
Name = "Name", | ||
ClientId = "ClientId", | ||
ClientSecret = "ClientSecret", | ||
AuthUrl = "https://mytestauth.com", | ||
TokenUrl = "https://mytestauth.com", | ||
JwksUrl = "https://mytestauth.com", | ||
AttributeMapping = new OidcAttributeMapping { } | ||
}; | ||
await _descopeClient.Management.Sso.ConfigureOidcSettings(tenantId, settings, new List<string> { "domain1.com" }); | ||
|
||
var loadedSetting = await _descopeClient.Management.Sso.LoadSettings(tenantId); | ||
|
||
// Make sure the settings match | ||
Assert.Equal(settings.Name, loadedSetting.Oidc.Name); | ||
Assert.Equal(settings.ClientId, loadedSetting.Oidc.ClientId); | ||
Assert.Equal(settings.AuthUrl, loadedSetting.Oidc.AuthUrl); | ||
Assert.Equal(settings.TokenUrl, loadedSetting.Oidc.TokenUrl); | ||
Assert.Equal(settings.JwksUrl, loadedSetting.Oidc.JwksUrl); | ||
Assert.Equal("domain1.com", loadedSetting.Tenant.Domains.First()); | ||
} | ||
finally | ||
{ | ||
if (!string.IsNullOrEmpty(tenantId)) | ||
{ | ||
try { await _descopeClient.Management.Tenant.Delete(tenantId); } | ||
catch { } | ||
} | ||
if (!string.IsNullOrEmpty(roleName)) | ||
{ | ||
try { await _descopeClient.Management.Role.Delete(roleName); } | ||
catch { } | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
using System.Text.Json.Serialization; | ||
|
||
namespace Descope.Internal.Management | ||
{ | ||
internal class SsoApplication : ISsoApplication | ||
{ | ||
private readonly IHttpClient _httpClient; | ||
private readonly string _managementKey; | ||
|
||
internal SsoApplication(IHttpClient httpClient, string managementKey) | ||
{ | ||
_httpClient = httpClient; | ||
_managementKey = managementKey; | ||
} | ||
|
||
public async Task<string> CreateOidcApplication(OidcApplicationOptions options) | ||
{ | ||
var resp = await _httpClient.Post<SsoApplicationCreateResponse>(Routes.SsoApplicationOidcCreate, _managementKey, body: options); | ||
return resp.Id; | ||
} | ||
|
||
public async Task<string> CreateSAMLApplication(SamlApplicationOptions options) | ||
{ | ||
var resp = await _httpClient.Post<SsoApplicationCreateResponse>(Routes.SsoApplicationSamlCreate, _managementKey, body: options); | ||
return resp.Id; | ||
} | ||
|
||
public async Task UpdateOIDCApplication(OidcApplicationOptions options) | ||
{ | ||
await _httpClient.Post<object>(Routes.SsoApplicationOidcUpdate, _managementKey, body: options); | ||
} | ||
|
||
public async Task UpdateSAMLApplication(SamlApplicationOptions options) | ||
{ | ||
await _httpClient.Post<object>(Routes.SsoApplicationSamlUpdate, _managementKey, body: options); | ||
} | ||
|
||
public async Task Delete(string id) | ||
{ | ||
var body = new { id }; | ||
await _httpClient.Post<object>(Routes.SsoApplicationDelete, _managementKey, body: body); | ||
} | ||
|
||
public async Task<SsoApplicationResponse> Load(string id) | ||
{ | ||
return await _httpClient.Get<SsoApplicationResponse>(Routes.SsoApplicationLoad, _managementKey, queryParams: new Dictionary<string, string?> { { "id", id } }); | ||
} | ||
|
||
public async Task<List<SsoApplicationResponse>> LoadAll() | ||
{ | ||
return await _httpClient.Get<List<SsoApplicationResponse>>(Routes.SsoApplicationLoad, _managementKey); | ||
} | ||
|
||
} | ||
|
||
internal class SsoApplicationCreateResponse | ||
{ | ||
[JsonPropertyName("id")] | ||
public string Id { get; set; } | ||
|
||
public SsoApplicationCreateResponse(string id) | ||
{ | ||
Id = id; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.