Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit b1ab6ca
Author: Allan Ritchie <allan.ritchie@gmail.com>
Date:   Tue Aug 6 00:08:28 2024 -0400

    Minor
  • Loading branch information
aritchie committed Aug 6, 2024
1 parent e0e343c commit 4e3e4dc
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 17 deletions.
1 change: 1 addition & 0 deletions ProjectTemplates/ShinyApp/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
global using ReactiveUI.Fody.Helpers;
#endif
#if shinyframework || prism
global using Prism.AppModel;
global using Prism.Navigation;
global using Prism.Services;
#endif
Expand Down
2 changes: 2 additions & 0 deletions ProjectTemplates/ShinyAspNet/Documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Created by [Allan Ritchie](https://github.com/aritchie)
* [GitHub](https://github.com/shinyorg/mediator)
* [Documentation](https://shinylib.net/client/mediator)

## ASP.NET Health Checks

* [Documentation](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/health-checks?view=aspnetcore-8.0)

<!--#if (orleans)-->
## Microsoft Orleans
Expand Down
2 changes: 1 addition & 1 deletion ProjectTemplates/ShinyAspNet/Handlers/Auth/Contracts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace ShinyAspNet.Handlers.Auth;
public record SignOutRequest(
string RefreshToken,
string? PushToken
) : IRequest;
) : Shiny.Mediator.IRequest;


public record SignInRequest(string Scheme) : IRequest<SignInResponse> { }
Expand Down
4 changes: 2 additions & 2 deletions ProjectTemplates/ShinyAspNet/Handlers/Auth/RefreshHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public async Task<RefreshResponse> Handle(RefreshRequest request, CancellationTo
var refreshToken = await data
.RefreshTokens
.Include(x => x.User)
.FirstOrDefaultAsync(x => x.Id == request.Token);
.FirstOrDefaultAsync(x => x.Id == request.Token, cancellationToken);

var tokens = await jwtService.CreateJwt(refreshToken.User);
var tokens = await jwtService.CreateJwt(refreshToken.User, cancellationToken);

return RefreshResponse.Successful(tokens.Jwt, tokens.RefreshToken);
}
Expand Down
8 changes: 4 additions & 4 deletions ProjectTemplates/ShinyAspNet/Handlers/Auth/SignInHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<SignInResponse> Handle(SignInRequest request, CancellationToke
if (user1 == null)
return SignInResponse.Fail;

var uritest = await this.CreateTokenToApp(user1!, false);
var uritest = await this.CreateTokenToApp(user1!, false, cancellationToken);
return SignInResponse.Sucessful(uritest);
}
#endif
Expand All @@ -33,9 +33,9 @@ public async Task<SignInResponse> Handle(SignInRequest request, CancellationToke
return SignInResponse.Fail;

var newUser = false;
var claims = auth.Principal.Identities.FirstOrDefault()?.Claims;
var claims = auth.Principal.Identities.FirstOrDefault()?.Claims.ToList();
var email = claims?.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value;
var user = await data.Users.FirstOrDefaultAsync(x => x.Email == email);
var user = await data.Users.FirstOrDefaultAsync(x => x.Email == email, cancellationToken);

if (user == null)
{
Expand All @@ -51,7 +51,7 @@ public async Task<SignInResponse> Handle(SignInRequest request, CancellationToke
user.LastName = claims!.First(x => x.Type == ClaimTypes.Surname).Value;
await data.SaveChangesAsync();

var uri = await this.CreateTokenToApp(user, newUser);
var uri = await this.CreateTokenToApp(user, newUser, cancellationToken);
return SignInResponse.Sucessful(uri);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ await data
x.UserId == userId &&
x.Id == request.RefreshToken
)
.ExecuteDeleteAsync();
.ExecuteDeleteAsync(cancellationToken);
}
}
8 changes: 8 additions & 0 deletions ProjectTemplates/ShinyAspNet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
});
});

builder.Services
.AddHealthChecks()
.AddDbContextCheck<AppDbContext>();

builder.Services.AddScoped<JwtService>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddHttpContextAccessor();
Expand Down Expand Up @@ -243,4 +247,8 @@
});
#endif

app
.MapHealthChecks("/health")
.RequireHost("*:5001");

app.Run();
19 changes: 11 additions & 8 deletions ProjectTemplates/ShinyAspNet/Services/JwtService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ public JwtService(IConfiguration cfg, AppDbContext data)
}


public async Task<(string Jwt, string RefreshToken)> CreateJwt(User user)
public async Task<(string Jwt, string RefreshToken)> CreateJwt(User user, CancellationToken cancellationToken)
{
var jwtString = this.CreateJwtString(
TimeSpan.FromMinutes(this.tokenExpiryMins),
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())
);
var rtoken = await this.CreateRefreshToken(user);
var rtoken = await this.CreateRefreshToken(user, cancellationToken);
return (jwtString, rtoken);
}


public async Task<bool> ValidateRefreshToken(string token)
public async Task<bool> ValidateRefreshToken(string token, CancellationToken cancellationToken)
{
try
{
Expand All @@ -60,7 +60,10 @@ public async Task<bool> ValidateRefreshToken(string token)
var storeToken = await this.data
.RefreshTokens
.Include(x => x.User)
.FirstOrDefaultAsync(x => x.Id == token);
.FirstOrDefaultAsync(
x => x.Id == token,
cancellationToken
);

if (storeToken == null)
return false;
Expand Down Expand Up @@ -97,7 +100,7 @@ string CreateJwtString(TimeSpan untilExpiry, params Claim[] claims)
}


async Task<string> CreateRefreshToken(User user)
async Task<string> CreateRefreshToken(User user, CancellationToken cancellationToken)
{
var jwtString = this.CreateJwtString(
TimeSpan.FromHours(this.refreshExpiryHours),
Expand All @@ -110,11 +113,11 @@ async Task<string> CreateRefreshToken(User user)
DateCreated = DateTimeOffset.UtcNow,
UserId = user.Id
});
await data.SaveChangesAsync();
await data.SaveChangesAsync(cancellationToken);
return jwtString;
}


SymmetricSecurityKey GetSigningKey() => new SymmetricSecurityKey(Encoding.UTF8.GetBytes(this.signingKey));
SigningCredentials GetJwtKey() => new SigningCredentials(this.GetSigningKey(), SecurityAlgorithms.HmacSha256);
SymmetricSecurityKey GetSigningKey() => new(Encoding.UTF8.GetBytes(this.signingKey));
SigningCredentials GetJwtKey() => new(this.GetSigningKey(), SecurityAlgorithms.HmacSha256);
}
3 changes: 3 additions & 0 deletions ProjectTemplates/ShinyAspNet/ShinyAspNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="8.0.7" />
<!--#if (efmssql)-->
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(MicrosoftVersion)" />
<!--#endif-->
Expand Down Expand Up @@ -62,6 +63,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<!--#endif-->

<PackageReference Include="Riok.Mapperly" Version="3.6.0" />

<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.9.0" />
Expand Down
2 changes: 1 addition & 1 deletion Template.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<Description>Shiny.NET Templates - One stop shop to setup almost everything you can imagine within your .NET MAUI application</Description>
<PackageType>Template</PackageType>
<PackageVersion>2.52.0</PackageVersion>
<PackageVersion>2.53.0</PackageVersion>
<PackageId>Shiny.Templates</PackageId>
<Title>Shiny Templates</Title>
<Authors>Allan Ritchie</Authors>
Expand Down

0 comments on commit 4e3e4dc

Please sign in to comment.