-
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.
Add SSO authentication and unit test infra
- Loading branch information
1 parent
cb92ec3
commit 5d1d31f
Showing
9 changed files
with
259 additions
and
5 deletions.
There are no files selected for viewing
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,47 @@ | ||
using Descope.Internal; | ||
using Descope.Internal.Auth; | ||
using Xunit; | ||
|
||
namespace Descope.Test.Unit | ||
{ | ||
public class SsoTests | ||
{ | ||
|
||
[Fact] | ||
public async Task SSO_Start() | ||
{ | ||
var client = new MockHttpClient(); | ||
ISsoAuth sso = new Sso(client); | ||
client.PostResponse = new { url = "url" }; | ||
client.PostAssert = (url, body, queryParams) => | ||
{ | ||
Assert.Equal(Routes.SsoStart, url); | ||
Assert.Equal("tenant", queryParams!["tenant"]); | ||
Assert.Equal("redirectUrl", queryParams!["redirectUrl"]); | ||
Assert.Equal("prompt", queryParams!["prompt"]); | ||
Assert.Contains("\"stepup\":true", Utils.Serialize(body!)); | ||
return null; | ||
}; | ||
var response = await sso.Start("tenant", redirectUrl: "redirectUrl", prompt: "prompt", loginOptions: new LoginOptions { StepUp = true }); | ||
Assert.Equal("url", response); | ||
Assert.Equal(1, client.PostCount); | ||
} | ||
|
||
[Fact] | ||
public async Task SSO_Exchange() | ||
{ | ||
var client = new MockHttpClient(); | ||
ISsoAuth sso = new Sso(client); | ||
client.PostResponse = new AuthenticationResponse("", "", "", "", 0, 0, new UserResponse(new List<string>(), "", ""), false); | ||
client.PostAssert = (url, body, queryParams) => | ||
{ | ||
Assert.Equal(Routes.SsoExchange, url); | ||
Assert.Null(queryParams); | ||
Assert.Contains("\"code\":\"code\"", Utils.Serialize(body!)); | ||
return null; | ||
}; | ||
var response = await sso.Exchange("code"); | ||
Assert.Equal(1, client.PostCount); | ||
} | ||
} | ||
} |
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,88 @@ | ||
using Descope.Internal; | ||
using JsonSerializer = System.Text.Json.JsonSerializer; | ||
|
||
namespace Descope.Test.Unit | ||
{ | ||
internal class Utils | ||
{ | ||
public static string Serialize(object o) | ||
{ | ||
return JsonSerializer.Serialize(o); | ||
} | ||
|
||
public static T Convert<T>(object? o) | ||
{ | ||
var s = JsonSerializer.Serialize(o ?? "{}"); | ||
var d = JsonSerializer.Deserialize<T>(s); | ||
return d ?? throw new Exception("Conversion error"); | ||
} | ||
} | ||
|
||
internal class MockHttpClient : IHttpClient | ||
{ | ||
|
||
// Delete | ||
public bool DeleteFailure { get; set; } | ||
public Exception? DeleteError { get; set; } | ||
public int DeleteCount { get; set; } | ||
public Func<string, Dictionary<string, string?>?, object?>? DeleteAssert { get; set; } | ||
public object? DeleteResponse { get; set; } | ||
|
||
// Get | ||
public bool GetFailure { get; set; } | ||
public Exception? GetError { get; set; } | ||
public int GetCount { get; set; } | ||
public Func<string, Dictionary<string, string?>?, object?>? GetAssert { get; set; } | ||
public object? GetResponse { get; set; } | ||
|
||
// Post | ||
public bool PostFailure { get; set; } | ||
public Exception? PostError { get; set; } | ||
public int PostCount { get; set; } | ||
public Func<string, object?, Dictionary<string, string?>?, object?>? PostAssert { get; set; } | ||
public object? PostResponse { get; set; } | ||
|
||
// IHttpClient Properties | ||
public DescopeConfig DescopeConfig { get; set; } | ||
|
||
public MockHttpClient() | ||
{ | ||
DescopeConfig = new DescopeConfig(projectId: "test"); | ||
} | ||
|
||
// IHttpClient Implementation | ||
|
||
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously | ||
|
||
public async Task<TResponse> Delete<TResponse>(string resource, string pswd, Dictionary<string, string?>? queryParams = null) | ||
{ | ||
DeleteCount++; | ||
DeleteAssert?.Invoke(resource, queryParams); | ||
if (DeleteError != null) throw DeleteError; | ||
if (DeleteFailure) throw new Exception(); | ||
return Utils.Convert<TResponse>(DeleteResponse); | ||
} | ||
|
||
public async Task<TResponse> Get<TResponse>(string resource, string? pswd = null, Dictionary<string, string?>? queryParams = null) | ||
{ | ||
GetCount++; | ||
GetAssert?.Invoke(resource, queryParams); | ||
if (GetError != null) throw GetError; | ||
if (GetFailure) throw new Exception(); | ||
return Utils.Convert<TResponse>(GetResponse); | ||
} | ||
|
||
|
||
public async Task<TResponse> Post<TResponse>(string resource, string? pswd = null, object? body = null, Dictionary<string, string?>? queryParams = null) | ||
{ | ||
PostCount++; | ||
PostAssert?.Invoke(resource, body, queryParams); | ||
if (PostError != null) throw PostError; | ||
if (PostFailure) throw new Exception(); | ||
return Utils.Convert<TResponse>(PostResponse); | ||
} | ||
|
||
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously | ||
|
||
} | ||
} |
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,41 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Descope.Internal.Auth | ||
{ | ||
public class Sso : ISsoAuth | ||
{ | ||
private readonly IHttpClient _httpClient; | ||
|
||
public Sso(IHttpClient httpClient) | ||
{ | ||
_httpClient = httpClient; | ||
} | ||
|
||
public async Task<string> Start(string tenant, string? redirectUrl, string? prompt, LoginOptions? loginOptions) | ||
{ | ||
Utils.EnforceRequiredArgs(("tenant", tenant)); | ||
var body = new { loginOptions }; | ||
var queryParams = new Dictionary<string, string?> { { "tenant", tenant }, { "redirectUrl", redirectUrl }, { "prompt", prompt } }; | ||
var response = await _httpClient.Post<UrlResponse>(Routes.SsoStart, body: body, queryParams: queryParams); | ||
return response.Url; | ||
} | ||
|
||
public async Task<AuthenticationResponse> Exchange(string code) | ||
{ | ||
Utils.EnforceRequiredArgs(("code", code)); | ||
var body = new { code }; | ||
return await _httpClient.Post<AuthenticationResponse>(Routes.SsoExchange, body: body); | ||
} | ||
} | ||
|
||
internal class UrlResponse | ||
{ | ||
[JsonPropertyName("url")] | ||
public string Url { get; set; } | ||
|
||
public UrlResponse(string url) | ||
{ | ||
Url = url; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
namespace Descope.Internal.Management | ||
namespace Descope.Internal | ||
{ | ||
internal class Utils | ||
{ | ||
|
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