Skip to content

Commit

Permalink
feat: use own api clients (#16)
Browse files Browse the repository at this point in the history
* generate kratos client

* kratos client

* add hydra client

* remove clients

* add kratos client

* add hydra client

* reference api clients

* fix cancellationToken

* add model prefix

* migrate projects for new api clients
  • Loading branch information
josxha authored Apr 17, 2024
1 parent 83a27bc commit 7c66265
Show file tree
Hide file tree
Showing 245 changed files with 34,244 additions and 11,385 deletions.
10 changes: 5 additions & 5 deletions KratosSelfService/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task<IActionResult> Login(
KratosLoginFlow flow;
try
{
flow = await api.Frontend.GetLoginFlowAsync(flowId.ToString(), Request.Headers.Cookie, cancellationToken);
flow = await api.Frontend.GetLoginFlowAsync(flowId.ToString(), Request.Headers.Cookie, cancellationToken:cancellationToken);
}
catch (ApiException exception)
{
Expand All @@ -48,7 +48,7 @@ public async Task<IActionResult> Login(
if (flow.Ui.Messages?.Any(text => text.Id == 4000010) ?? false)
// the login requires that the user verifies their email address before logging in
// we will create a new verification flow and redirect the user to the verification page
return await RedirectToVerificationFlow(flow, cancellationToken);
return await RedirectToVerificationFlow(flow, cancellationToken:cancellationToken);

// Render the data using a view:
var initRegistrationQuery = new Dictionary<string, string?>
Expand All @@ -68,7 +68,7 @@ public async Task<IActionResult> Login(

string? logoutUrl = null;
if (flow.RequestedAal == KratosAuthenticatorAssuranceLevel.Aal2 || flow.Refresh)
logoutUrl = await GetLogoutUrl(flow, cancellationToken);
logoutUrl = await GetLogoutUrl(flow, cancellationToken:cancellationToken);

var model = new LoginModel(flow, initRecoveryUrl, initRegistrationUrl, logoutUrl);
return View("Login", model);
Expand All @@ -81,7 +81,7 @@ public async Task<IActionResult> Login(
// to give the user the option to sign out!
try
{
var logoutFlow = await api.Frontend.CreateBrowserLogoutFlowAsync(Request.Headers.Cookie, flow.ReturnTo, cancellationToken);
var logoutFlow = await api.Frontend.CreateBrowserLogoutFlowAsync(Request.Headers.Cookie, flow.ReturnTo, cancellationToken:cancellationToken);
return logoutFlow.LogoutUrl;
}
catch (Exception exception)
Expand Down Expand Up @@ -111,7 +111,7 @@ private async Task<IActionResult> RedirectToVerificationFlow(KratosLoginFlow flo
try
{
var response = await api.Frontend
.CreateBrowserVerificationFlowWithHttpInfoAsync(flow.ReturnTo, cancellationToken);
.CreateBrowserVerificationFlowWithHttpInfoAsync(flow.ReturnTo, cancellationToken:cancellationToken);
var verificationFlow = response.Data;
// we need the csrf cookie from the verification flow
Response.Headers.Append(HeaderNames.SetCookie, response.Headers[HeaderNames.SetCookie].ToString());
Expand Down
4 changes: 2 additions & 2 deletions KratosSelfService/Controllers/LogoutController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<IActionResult> LogoutGet(
// end hydra session
try
{
hydraResponse = await api.HydraOAuth2.AcceptOAuth2LogoutRequestAsync(logoutChallenge, cancellationToken);
hydraResponse = await api.HydraOAuth2.AcceptOAuth2LogoutRequestAsync(logoutChallenge, cancellationToken:cancellationToken);
}
catch (Ory.Hydra.Client.Client.ApiException exception)
{
Expand All @@ -32,7 +32,7 @@ public async Task<IActionResult> LogoutGet(
try
{
var flow = await api.Frontend.CreateBrowserLogoutFlowAsync(Request.Headers.Cookie,
hydraResponse?.RedirectTo, cancellationToken);
hydraResponse?.RedirectTo, cancellationToken:cancellationToken);
return Redirect(flow.LogoutUrl);
}
catch (Ory.Kratos.Client.Client.ApiException exception)
Expand Down
8 changes: 4 additions & 4 deletions KratosSelfService/Controllers/OAuth2Controller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task<IActionResult> ConsentGet([FromQuery(Name = "consent_challenge

// This section processes consent requests and either shows the consent UI or accepts
// the consent request right away if the user has given consent to this app before.
var challengeRequest = await oAuth2Api.GetOAuth2ConsentRequestAsync(challenge, cancellationToken);
var challengeRequest = await oAuth2Api.GetOAuth2ConsentRequestAsync(challenge, cancellationToken:cancellationToken);
// If a user has granted this application the requested scope, hydra will tell us to not show the UI.
if (!CanSkipConsent(challengeRequest))
{
Expand Down Expand Up @@ -152,9 +152,9 @@ private async Task<HydraOAuth2RedirectTo> AcceptRequest(string challenge, List<s

private bool CanSkipConsent(HydraOAuth2ConsentRequest challenge)
{
if (challenge.Skip || challenge._Client.SkipConsent) return true;
if (challenge.Skip || challenge.VarClient.SkipConsent) return true;

if (challenge._Client.ClientId == null || env.HydraTrustedClientIds == null) return false;
return env.HydraTrustedClientIds.Split(",").Contains(challenge._Client.ClientId);
if (challenge.VarClient.ClientId == null || env.HydraTrustedClientIds == null) return false;
return env.HydraTrustedClientIds.Split(",").Contains(challenge.VarClient.ClientId);
}
}
2 changes: 1 addition & 1 deletion KratosSelfService/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public async Task<IActionResult> Profile(CancellationToken cancellationToken)
{
var session = (KratosSession)HttpContext.Items[typeof(KratosSession)]!;
var schema = await schemaService.FetchSchema(session.Identity.SchemaId,
session.Identity.SchemaUrl, cancellationToken);
session.Identity.SchemaUrl, cancellationToken:cancellationToken);
return View("Profile", new ProfileModel(session, IdentitySchemaService.GetTraits(schema)));
}
}
5 changes: 3 additions & 2 deletions KratosSelfService/Controllers/RecoveryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RecoveryController(ILogger<RecoveryController> logger, ApiService a
[AllowAnonymous]
public async Task<IActionResult> Recovery(
[FromQuery(Name = "flow")] Guid? flowId,
[FromQuery(Name = "return_to")] string? returnTo,
[FromQuery(Name = "return_to")] string? returnTo,
CancellationToken cancellationToken)
{
if (flowId == null)
Expand All @@ -28,7 +28,8 @@ public async Task<IActionResult> Recovery(
KratosRecoveryFlow flow;
try
{
flow = await api.Frontend.GetRecoveryFlowAsync(flowId.ToString(), Request.Headers.Cookie, cancellationToken);
flow = await api.Frontend.GetRecoveryFlowAsync(flowId.ToString(), Request.Headers.Cookie,
cancellationToken: cancellationToken);
}
catch (ApiException exception)
{
Expand Down
2 changes: 1 addition & 1 deletion KratosSelfService/Controllers/RegistrationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<IActionResult> Registration(
KratosRegistrationFlow flow;
try
{
flow = await api.Frontend.GetRegistrationFlowAsync(flowId.ToString(), Request.Headers.Cookie, cancellationToken);
flow = await api.Frontend.GetRegistrationFlowAsync(flowId.ToString(), Request.Headers.Cookie, cancellationToken:cancellationToken);
}
catch (ApiException exception)
{
Expand Down
2 changes: 1 addition & 1 deletion KratosSelfService/Controllers/VerificationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public async Task<IActionResult> Verification(
KratosVerificationFlow flow;
try
{
flow = await api.Frontend.GetVerificationFlowAsync(flowId.ToString(), Request.Headers.Cookie, cancellationToken);
flow = await api.Frontend.GetVerificationFlowAsync(flowId.ToString(), Request.Headers.Cookie, cancellationToken:cancellationToken);
}
catch (ApiException exception)
{
Expand Down
2 changes: 1 addition & 1 deletion KratosSelfService/Controllers/WellknownController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class WellknownController(ApiService api) : Controller
[AllowAnonymous]
public async Task<IActionResult> Webauthn(CancellationToken cancellationToken)
{
var script = await api.Frontend.GetWebAuthnJavaScriptAsync(cancellationToken);
var script = await api.Frontend.GetWebAuthnJavaScriptAsync(cancellationToken:cancellationToken);
//Response.ContentType = "text/javascript";
return new ObjectResult(script);
}
Expand Down
7 changes: 5 additions & 2 deletions KratosSelfService/KratosSelfService.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json.Schema" Version="3.0.15" />
<PackageReference Include="Ory.Hydra.Client" Version="2.2.0-rc.3"/>
<PackageReference Include="UAParser" Version="3.1.47"/>
</ItemGroup>

Expand Down Expand Up @@ -130,7 +129,11 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Ory.Kratos.Client\Ory.Kratos.Client.csproj" />
<ProjectReference Include="..\Ory.Hydra.Client\Ory.Hydra.Client.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Ory.Kratos.Client\Ory.Kratos.Client.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion KratosSelfService/Services/IdentitySchemaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public async Task<JSchema> FetchSchema(string schemaId, string schemaUri, Cancel
// check if schema object is cached
if (_schemaCache.TryGetValue(schemaId, out var schema))
return schema;
var response = await _httpClient.GetStringAsync(schemaUri, cancellationToken);
var response = await _httpClient.GetStringAsync(schemaUri, cancellationToken:cancellationToken);
// request and cache new schema object
_schemaCache[schemaId] = JSchema.Parse(response);
return _schemaCache[schemaId];
Expand Down
4 changes: 3 additions & 1 deletion KratosSelfService/ViewComponents/KratosUiTextMessages.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewComponents;
using Ory.Kratos.Client.Model;

Expand Down
4 changes: 2 additions & 2 deletions KratosSelfService/Views/OAuth2/Consent.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
@{
ViewData["Title"] = CustomTranslator.Get("Grant access");
Layout = "_CardLayout";
var clientName = Model.request._Client.ClientName;
if (string.IsNullOrWhiteSpace(clientName)) clientName = Model.request._Client.ClientId;
var clientName = Model.request.VarClient.ClientName;
if (string.IsNullOrWhiteSpace(clientName)) clientName = Model.request.VarClient.ClientId;
}

<form class="section" action="consent" method="post">
Expand Down
Loading

0 comments on commit 7c66265

Please sign in to comment.