Skip to content

Commit 99311fb

Browse files
author
Binon
committed
Merge branch 'Automatic_version_update_dependabot' of https://github.com/TechnologyEnhancedLearning/LearningHub.Nhs.UserApi into Automatic_version_update_dependabot
2 parents 43de1f0 + 0b6c5c3 commit 99311fb

37 files changed

+1278
-651
lines changed

.github/workflows/continuous-integration-workflow.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ jobs:
1818
dotnet-version: 8.0.x
1919

2020
- name: Add Azure artifact
21-
run: dotnet nuget add source 'https://pkgs.dev.azure.com/e-LfH/_packaging/LearningHubFeed/nuget/v3/index.json' --name 'LearningHubFeed' --username 'kevin.whittaker' --password ${{ secrets.AZURE_DEVOPS_PAT }} --store-password-in-clear-text
21+
run: |
22+
dotnet nuget remove source LearningHubFeed || true
23+
dotnet nuget add source 'https://pkgs.dev.azure.com/e-LfH/_packaging/LearningHubFeed/nuget/v3/index.json' --name 'LearningHubFeed' --username 'kevin.whittaker' --password ${{ secrets.AZURE_DEVOPS_PAT }} --store-password-in-clear-text
2224
2325
- name: Use NuGet 5.8
2426
uses: nuget/setup-nuget@v1
Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,27 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<TargetFramework>net8.0</TargetFramework>
54
<GenerateDocumentationFile>true</GenerateDocumentationFile>
65
<IsPackable>false</IsPackable>
7-
<Platforms>x64</Platforms>
6+
<Platforms>x64</Platforms>
87
</PropertyGroup>
9-
10-
118
<ItemGroup>
12-
<PackageReference Include="elfhHub.Nhs.Models" Version="3.0.8" />
13-
<PackageReference Include="LearningHub.Nhs.Models" Version="3.0.33" />
14-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
15-
<PackageReference Include="Microsoft.TestPlatform.TestHost" Version="17.12.0" />
16-
<PackageReference Include="Moq" Version="4.20.72" />
17-
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
9+
<PackageReference Include="elfhHub.Nhs.Models" />
10+
<PackageReference Include="LearningHub.Nhs.Models" />
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
12+
<PackageReference Include="Microsoft.TestPlatform.TestHost" />
13+
<PackageReference Include="Moq" />
14+
<PackageReference Include="StyleCop.Analyzers">
1815
<PrivateAssets>all</PrivateAssets>
1916
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2017
</PackageReference>
21-
<PackageReference Include="xunit" Version="2.9.3" />
22-
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
18+
<PackageReference Include="xunit" />
19+
<PackageReference Include="xunit.runner.visualstudio">
2320
<PrivateAssets>all</PrivateAssets>
2421
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
2522
</PackageReference>
2623
</ItemGroup>
27-
2824
<ItemGroup>
2925
<ProjectReference Include="..\LearningHub.Nhs.Auth\LearningHub.Nhs.Auth.csproj" />
3026
</ItemGroup>
31-
32-
</Project>
27+
</Project>

Auth/LearningHub.Nhs.Auth/Configuration/ServiceMappings.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,22 @@ public static void AddServiceMappings(this IServiceCollection services, IConfigu
4040
ServerCertificateCustomValidationCallback =
4141
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
4242
});
43+
44+
services.AddHttpClient<IMoodleHttpClient, MoodleHttpClient>()
45+
.ConfigurePrimaryHttpMessageHandler(
46+
() => new HttpClientHandler
47+
{
48+
ServerCertificateCustomValidationCallback =
49+
HttpClientHandler.DangerousAcceptAnyServerCertificateValidator,
50+
});
4351
}
4452
else
4553
{
4654
services.AddHttpClient<IUserApiHttpClient, UserApiHttpClient>();
55+
services.AddHttpClient<IMoodleHttpClient, MoodleHttpClient>();
4756
}
4857

58+
services.AddScoped<IMoodleApiService, MoodleApiService>();
4959
services.AddDistributedMemoryCache();
5060
services.AddScoped<IExternalSystemService, ExternalSystemService>();
5161
services.AddTransient<IRegistrationService, RegistrationService>();

Auth/LearningHub.Nhs.Auth/Configuration/WebSettings.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,15 @@ public class WebSettings
5656
/// Gets or sets the SupportFeedbackForm.
5757
/// </summary>
5858
public string SupportFeedbackForm { get; set; }
59-
}
59+
60+
/// <summary>
61+
/// Gets or sets a value indicating whether IsPasswordUpdate.
62+
/// </summary>
63+
public bool IsPasswordUpdate { get; set; }
64+
65+
/// <summary>
66+
/// Gets or sets a value indicating whether gets or sets a value to Enable Moodle.
67+
/// </summary>
68+
public bool EnableMoodle { get; set; }
69+
}
6070
}

Auth/LearningHub.Nhs.Auth/Controllers/AccountController.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,11 @@
2020
using LearningHub.Nhs.Auth.Models.Account;
2121
using LearningHub.Nhs.Caching;
2222
using LearningHub.Nhs.Models.Common;
23-
using LearningHub.Nhs.Models.Entities.Reporting;
2423
using Microsoft.AspNetCore.Authentication;
2524
using Microsoft.AspNetCore.Authorization;
26-
using Microsoft.AspNetCore.Http;
2725
using Microsoft.AspNetCore.Mvc;
28-
using Microsoft.Extensions.Configuration;
2926
using Microsoft.Extensions.Logging;
3027
using Microsoft.Extensions.Options;
31-
using NHSUKViewComponents.Web.ViewModels;
3228

3329
/// <summary>
3430
/// Account Controller operations.
@@ -72,7 +68,7 @@ public AccountController(
7268
this.authConfig = authConfig?.Value;
7369
this.webSettings = webSettings;
7470
this.logger = logger;
75-
}
71+
}
7672

7773
/// <summary>
7874
/// Shows the Login page.
@@ -214,9 +210,9 @@ await this.UserService.AddLogonToUserHistory(
214210
this.ModelState.AddModelError(string.Empty, loginResult.ErrorMessage);
215211
}
216212

217-
showFormWithError:
213+
showFormWithError:
218214

219-
// something went wrong, show form with error
215+
// something went wrong, show form with error
220216
var vm = await this.BuildLoginViewModelAsync(model);
221217
if ((vm.ClientId == "learninghubwebclient") || (vm.ClientId == "learninghubadmin"))
222218
{
@@ -268,6 +264,9 @@ public async Task<IActionResult> Logout(LogoutInputModel model)
268264
// delete local authentication cookie
269265
await this.HttpContext.SignOutAsync();
270266

267+
// Delete the authentication cookie to ensure it is invalidated
268+
this.HttpContext.Response.Cookies.Delete(".AspNetCore.Identity.Application");
269+
271270
// raise the logout event
272271
await this.Events.RaiseAsync(new UserLogoutSuccessEvent(this.User.GetSubjectId(), this.User.GetDisplayName()));
273272

@@ -296,7 +295,15 @@ public async Task<IActionResult> Logout(LogoutInputModel model)
296295
return this.SignOut(new AuthenticationProperties { RedirectUri = url }, vm.ExternalAuthenticationScheme);
297296
}
298297

299-
return this.View("LoggedOut", vm);
298+
if (this.webSettings.IsPasswordUpdate)
299+
{
300+
var redirectUri = $"{this.webSettings.LearningHubWebClient}Home/ChangePasswordAcknowledgement";
301+
return this.Redirect(redirectUri);
302+
}
303+
else
304+
{
305+
return this.View("LoggedOut", vm);
306+
}
300307
}
301308

302309
/// <summary>

Auth/LearningHub.Nhs.Auth/Controllers/HomeController.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,27 @@ public async Task<IActionResult> Error()
8080
return this.View("Error");
8181
}
8282

83+
/// <summary>
84+
/// IsPasswordUpdateMethod.
85+
/// </summary>
86+
/// <param name="isLogout">The Logout.</param>
87+
/// <returns>The <see cref="ActionResult"/>.</returns>
88+
[HttpGet]
89+
public IActionResult SetIsPasswordUpdate(bool isLogout)
90+
{
91+
if (isLogout)
92+
{
93+
this.webSettings.IsPasswordUpdate = false;
94+
}
95+
else
96+
{
97+
this.webSettings.IsPasswordUpdate = true;
98+
}
99+
100+
var redirectUri = $"{this.webSettings.LearningHubWebClient}Home/UserLogout";
101+
return this.Redirect(redirectUri);
102+
}
103+
83104
/// <summary>
84105
/// Shows the HealthCheck response.
85106
/// </summary>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
namespace LearningHub.Nhs.Auth.Helpers
2+
{
3+
using System;
4+
using System.Collections.Concurrent;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Authentication;
8+
using Microsoft.AspNetCore.Authentication.Cookies;
9+
10+
/// <summary>
11+
/// Defines the <see cref="InMemoryTicketStore" />.
12+
/// </summary>
13+
public class InMemoryTicketStore : ITicketStore
14+
{
15+
private readonly ConcurrentDictionary<string, AuthenticationTicket> cache;
16+
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="InMemoryTicketStore"/> class.
19+
/// The InMemoryTicketStore.
20+
/// </summary>
21+
/// <param name="cache">the cache.</param>
22+
public InMemoryTicketStore(ConcurrentDictionary<string, AuthenticationTicket> cache)
23+
{
24+
this.cache = cache;
25+
}
26+
27+
/// <summary>
28+
/// The StoreAsync.
29+
/// </summary>
30+
/// <param name="ticket">The ticket.</param>
31+
/// <returns>The key.</returns>
32+
public async Task<string> StoreAsync(AuthenticationTicket ticket)
33+
{
34+
var ticketUserId = ticket.Principal.Claims.Where(c => c.Type == "sub")
35+
.FirstOrDefault()
36+
.Value;
37+
var matchingAuthTicket = this.cache.Values.FirstOrDefault(
38+
t => t.Principal.Claims.FirstOrDefault(
39+
c => c.Type == "sub"
40+
&& c.Value == ticketUserId) != null);
41+
if (matchingAuthTicket != null)
42+
{
43+
var cacheKey = this.cache.Where(
44+
entry => entry.Value == matchingAuthTicket)
45+
.Select(entry => entry.Key)
46+
.FirstOrDefault();
47+
this.cache.TryRemove(
48+
cacheKey,
49+
out _);
50+
}
51+
52+
var key = Guid
53+
.NewGuid()
54+
.ToString();
55+
await this.RenewAsync(
56+
key,
57+
ticket);
58+
return key;
59+
}
60+
61+
/// <summary>
62+
/// The RenewAsync.
63+
/// </summary>
64+
/// <param name="key">The key.</param>
65+
/// <param name="ticket">The ticket.</param>
66+
/// <returns>The Task.</returns>
67+
public Task RenewAsync(
68+
string key,
69+
AuthenticationTicket ticket)
70+
{
71+
this.cache.AddOrUpdate(
72+
key,
73+
ticket,
74+
(_, _) => ticket);
75+
return Task.CompletedTask;
76+
}
77+
78+
/// <summary>
79+
/// The RetrieveAsync.
80+
/// </summary>
81+
/// <param name="key">The Key.</param>
82+
/// <returns>The Task.</returns>
83+
public Task<AuthenticationTicket> RetrieveAsync(string key)
84+
{
85+
this.cache.TryGetValue(
86+
key,
87+
out var ticket);
88+
return Task.FromResult(ticket);
89+
}
90+
91+
/// <summary>
92+
/// The RemoveAsync.
93+
/// </summary>
94+
/// <param name="key">The key.</param>
95+
/// <returns>The Task.</returns>
96+
public Task RemoveAsync(string key)
97+
{
98+
this.cache.TryRemove(
99+
key,
100+
out _);
101+
return Task.CompletedTask;
102+
}
103+
}
104+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace LearningHub.Nhs.Auth.Interfaces
2+
{
3+
using System.Threading.Tasks;
4+
5+
/// <summary>
6+
/// IMoodleApiService.
7+
/// </summary>
8+
public interface IMoodleApiService
9+
{
10+
/// <summary>
11+
/// GetResourcesAsync.
12+
/// </summary>
13+
/// <param name="currentUserId">The current User Id.</param>
14+
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
15+
Task<int> GetMoodleUserIdByUsernameAsync(int currentUserId);
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace LearningHub.Nhs.Auth.Interfaces
2+
{
3+
using System.Net.Http;
4+
using System.Threading.Tasks;
5+
6+
/// <summary>
7+
/// The Moodle Http Client interface.
8+
/// </summary>
9+
public interface IMoodleHttpClient
10+
{
11+
/// <summary>
12+
/// The get cient async.
13+
/// </summary>
14+
/// <returns>The <see cref="Task"/>.</returns>
15+
Task<HttpClient> GetClient();
16+
17+
/// <summary>
18+
/// GetDefaultParameters.
19+
/// </summary>
20+
/// <returns>defaultParameters.</returns>
21+
string GetDefaultParameters();
22+
}
23+
}

0 commit comments

Comments
 (0)