-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split app-scopes endpoint into, status, login and app-scopes (#…
…14075) Co-authored-by: William Thorenfeldt <48119543+wrt95@users.noreply.github.com> Co-authored-by: Mirko Sekulic <misha.sekulic@gmail.com>
- Loading branch information
1 parent
52fc27f
commit 7afbaa4
Showing
18 changed files
with
257 additions
and
58 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
backend/src/Designer/Controllers/AnsattPortenController.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,45 @@ | ||
using System.Threading.Tasks; | ||
using Altinn.Studio.Designer.Constants; | ||
using Altinn.Studio.Designer.Models.Dto; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.FeatureManagement.Mvc; | ||
|
||
namespace Altinn.Studio.Designer.Controllers; | ||
|
||
[FeatureGate(StudioFeatureFlags.AnsattPorten)] | ||
[Route("designer/api/[controller]")] | ||
[ApiController] | ||
public class AnsattPortenController : ControllerBase | ||
{ | ||
[Authorize(AnsattPortenConstants.AnsattportenAuthorizationPolicy)] | ||
[HttpGet("login")] | ||
public async Task<IActionResult> Login([FromQuery(Name = "redirect_to")] string redirectTo) | ||
{ | ||
await Task.CompletedTask; | ||
if (!Url.IsLocalUrl(redirectTo)) | ||
{ | ||
return Forbid(); | ||
} | ||
|
||
return LocalRedirect(redirectTo); | ||
} | ||
|
||
[AllowAnonymous] | ||
[HttpGet("auth-status")] | ||
public async Task<IActionResult> AuthStatus() | ||
{ | ||
await Task.CompletedTask; | ||
var authenticateResult = | ||
await HttpContext.AuthenticateAsync(AnsattPortenConstants.AnsattportenAuthenticationScheme); | ||
|
||
var authStatus = new AuthStatus | ||
{ | ||
IsLoggedIn = authenticateResult.Succeeded | ||
}; | ||
|
||
return Ok(authStatus); | ||
} | ||
|
||
} |
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
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,6 @@ | ||
namespace Altinn.Studio.Designer.Models.Dto; | ||
|
||
public class AuthStatus | ||
{ | ||
public bool IsLoggedIn { get; set; } | ||
} |
82 changes: 82 additions & 0 deletions
82
backend/tests/Designer.Tests/Controllers/AnsattPortenController/AuthStatusTests.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,82 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Altinn.Studio.Designer.Models.Dto; | ||
using Designer.Tests.Controllers.AnsattPortenController.Base; | ||
using Designer.Tests.Controllers.ApiTests; | ||
using FluentAssertions; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.AspNetCore.Mvc.Testing.Handlers; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Xunit; | ||
|
||
namespace Designer.Tests.Controllers.AnsattPortenController; | ||
|
||
public class AuthStatusTest : AnsattPortenControllerTestsBase<AuthStatusTest>, IClassFixture<WebApplicationFactory<Program>> | ||
{ | ||
private static string VersionPrefix => "/designer/api/ansattporten/auth-status"; | ||
|
||
// Setup unauthenticated http client | ||
protected override HttpClient GetTestClient() | ||
{ | ||
string configPath = GetConfigPath(); | ||
IConfiguration configuration = new ConfigurationBuilder() | ||
.AddJsonFile(configPath, false, false) | ||
.AddJsonStream(GenerateJsonOverrideConfig()) | ||
.AddEnvironmentVariables() | ||
.Build(); | ||
|
||
return Factory.WithWebHostBuilder(builder => | ||
{ | ||
builder.UseConfiguration(configuration); | ||
builder.ConfigureAppConfiguration((_, conf) => | ||
{ | ||
conf.AddJsonFile(configPath); | ||
conf.AddJsonStream(GenerateJsonOverrideConfig()); | ||
}); | ||
builder.ConfigureTestServices(ConfigureTestServices); | ||
builder.ConfigureServices(ConfigureTestServicesForSpecificTest); | ||
}).CreateDefaultClient(new CookieContainerHandler()); | ||
} | ||
|
||
public AuthStatusTest(WebApplicationFactory<Program> factory) : base(factory) | ||
{ | ||
} | ||
|
||
[Fact] | ||
public async Task AuthStatus_Should_ReturnFalse_IfNotAuthenticated() | ||
{ | ||
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, VersionPrefix); | ||
|
||
using var response = await HttpClient.SendAsync(httpRequestMessage); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
|
||
AuthStatus authStatus = await response.Content.ReadAsAsync<AuthStatus>(); | ||
authStatus.IsLoggedIn.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public async Task AuthStatus_Should_ReturnTrue_IfAuthenticated() | ||
{ | ||
// Setup test authentication | ||
ConfigureTestServicesForSpecificTest = services => | ||
{ | ||
services.AddAuthentication(defaultScheme: TestAuthConstants.TestAuthenticationScheme) | ||
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>( | ||
TestAuthConstants.TestAuthenticationScheme, options => { }); | ||
services.AddTransient<IAuthenticationSchemeProvider, TestSchemeProvider>(); | ||
}; | ||
|
||
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, VersionPrefix); | ||
|
||
using var response = await HttpClient.SendAsync(httpRequestMessage); | ||
response.StatusCode.Should().Be(HttpStatusCode.OK); | ||
|
||
AuthStatus authStatus = await response.Content.ReadAsAsync<AuthStatus>(); | ||
authStatus.IsLoggedIn.Should().BeTrue(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...Designer.Tests/Controllers/AnsattPortenController/Base/AnsattPortenControllerTestsBase.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,24 @@ | ||
using Altinn.Studio.Designer.Constants; | ||
using Designer.Tests.Controllers.ApiTests; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
|
||
namespace Designer.Tests.Controllers.AnsattPortenController.Base; | ||
|
||
public class AnsattPortenControllerTestsBase<TControllerTest> : DesignerEndpointsTestsBase<TControllerTest> where TControllerTest : class | ||
{ | ||
public AnsattPortenControllerTestsBase(WebApplicationFactory<Program> factory) : base(factory) | ||
{ | ||
JsonConfigOverrides.Add( | ||
$$""" | ||
{ | ||
"FeatureManagement": { | ||
"{{StudioFeatureFlags.AnsattPorten}}": true | ||
}, | ||
"AnsattPortenLoginSettings": { | ||
"ClientId": "non-empty-for-testing", | ||
"ClientSecret": "non-empty-for-testing" | ||
} | ||
} | ||
"""); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
backend/tests/Designer.Tests/Controllers/AnsattPortenController/LoginTests.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,37 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Altinn.Studio.Designer.Constants; | ||
using Designer.Tests.Controllers.AnsattPortenController.Base; | ||
using Designer.Tests.Controllers.ApiTests; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Xunit; | ||
|
||
namespace Designer.Tests.Controllers.AnsattPortenController; | ||
|
||
public class LoginTests : AnsattPortenControllerTestsBase<LoginTests>, IClassFixture<WebApplicationFactory<Program>> | ||
{ | ||
private static string VersionPrefix => "/designer/api/ansattporten/login"; | ||
|
||
public LoginTests(WebApplicationFactory<Program> factory) : base(factory) | ||
{ | ||
} | ||
|
||
[Theory] | ||
[InlineData("/test", HttpStatusCode.Redirect)] | ||
[InlineData("/", HttpStatusCode.Redirect)] | ||
[InlineData("https://docs.altinn.studio/", HttpStatusCode.Forbidden)] | ||
public async Task LoginShouldReturn_ExpectedCode(string redirectTo, HttpStatusCode expectedStatusCode) | ||
{ | ||
using var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get | ||
, $"{VersionPrefix}?redirect_to={redirectTo}"); | ||
|
||
using var response = await HttpClient.SendAsync(httpRequestMessage); | ||
Assert.Equal(expectedStatusCode, response.StatusCode); | ||
|
||
if (expectedStatusCode == HttpStatusCode.Redirect) | ||
{ | ||
Assert.Equal(redirectTo, response.Headers.Location?.ToString()); | ||
} | ||
} | ||
} |
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
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
Oops, something went wrong.