Skip to content

Commit

Permalink
AnsattPorten controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoSekulic committed Nov 19, 2024
1 parent cdae4a6 commit ce5f840
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
47 changes: 47 additions & 0 deletions backend/src/Designer/Controllers/AnsattPortenController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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(IAuthenticationService authService) : ControllerBase
{
[Authorize(AnsattPortenConstants.AnsattportenAuthorizationPolicy)]
[HttpGet("login")]
public async Task<IActionResult> Login([FromQuery] 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 authService.AuthenticateAsync(HttpContext,
AnsattPortenConstants.AnsattportenAuthenticationScheme);

var authStatus = new AuthStatus
{
IsLoggedIn = authenticateResult.Succeeded
};

return Ok(authStatus);
}


}
9 changes: 3 additions & 6 deletions backend/src/Designer/Controllers/AppScopesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


namespace Altinn.Studio.Designer.Controllers;
// TODO split the endppoint

[FeatureGate(StudioFeatureFlags.AnsattPorten)]
[Route("designer/api/{org}/{app:regex(^(?!datamodels$)[[a-z]][[a-z0-9-]]{{1,28}}[[a-z0-9]]$)}/app-scopes")]

Expand All @@ -27,7 +27,7 @@ public async Task<IActionResult> GetScopesFromMaskinPorten(string org, string ap
{
var scopes = await maskinPortenHttpClient.GetAvailableScopes(cancellationToken);

var reponse = new AppScopesResponse()
var response = new AppScopesResponse()
{
Scopes = scopes.Select(x => new MaskinPortenScopeDto()
{
Expand All @@ -36,10 +36,9 @@ public async Task<IActionResult> GetScopesFromMaskinPorten(string org, string ap
}).ToHashSet()
};

return Ok(reponse);
return Ok(response);
}


[Authorize]
[HttpPut]
public async Task UpsertAppScopes(string org, string app, [FromBody] AppScopesUpsertRequest appScopesUpsertRequest,
Expand All @@ -55,7 +54,6 @@ public async Task UpsertAppScopes(string org, string app, [FromBody] AppScopesUp
await appScopesService.UpsertScopesAsync(AltinnRepoEditingContext.FromOrgRepoDeveloper(org, app, developer), scopes, cancellationToken);
}


[Authorize]
[HttpGet]
public async Task<IActionResult> GetAppScopes(string org, string app, CancellationToken cancellationToken)
Expand All @@ -73,5 +71,4 @@ public async Task<IActionResult> GetAppScopes(string org, string app, Cancellati

return Ok(reponse);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ private static IServiceCollection AddAnsattPortenAuthentication(this IServiceCol
options.Events.OnRedirectToIdentityProvider = context =>
{

if (!context.Request.Path.StartsWithSegments("/designer/api") ||
!context.Request.Path.Value!.Contains("/maskinporten"))
if (!context.Request.Path.StartsWithSegments("/designer/api/ansattporten/login"))
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
context.HandleResponse();
Expand Down
6 changes: 6 additions & 0 deletions backend/src/Designer/Models/Dto/AuthStatus.cs
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; }
}

0 comments on commit ce5f840

Please sign in to comment.