Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support for .net8 #2

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>

<Title>EAVFW.Extensions.EasyAuth.MicrosoftEntraId</Title>
<Authors>Poul Kjeldager</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,20 @@
_clientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory));
}

public async Task OnAuthenticate(HttpContext httpcontext, string handleId, string redirectUrl)

Check warning on line 47 in src/EAVFW.Extensions.EasyAuth.MicrosoftEntraId/MicrosoftEntraEasyAuthProvider.cs

View workflow job for this annotation

GitHub Actions / Building

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 47 in src/EAVFW.Extensions.EasyAuth.MicrosoftEntraId/MicrosoftEntraEasyAuthProvider.cs

View workflow job for this annotation

GitHub Actions / Building

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 47 in src/EAVFW.Extensions.EasyAuth.MicrosoftEntraId/MicrosoftEntraEasyAuthProvider.cs

View workflow job for this annotation

GitHub Actions / Building

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 47 in src/EAVFW.Extensions.EasyAuth.MicrosoftEntraId/MicrosoftEntraEasyAuthProvider.cs

View workflow job for this annotation

GitHub Actions / Building

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var email = httpcontext.Request.Query["email"].FirstOrDefault();
var redirectUri = httpcontext.Request.Query["redirectUri"].FirstOrDefault();

var ru = new RequestUrl(_options.Value.AuthorizationUrl);
var callbackUri = $"{httpcontext.Request.Scheme}://{httpcontext.Request.Host}{httpcontext.Request.Path}/callback";

var ru = new RequestUrl(_options.Value.GetMicrosoftAuthorizationUrl(httpcontext));
var authUri = ru.CreateAuthorizeUrl(
clientId: _options.Value.ClientId,
redirectUri: _options.Value.RedirectUrl,
redirectUri: callbackUri,
responseType: ResponseTypes.Code,
responseMode: ResponseModes.FormPost,
scope: _options.Value.Scope,
loginHint: String.IsNullOrEmpty(email) || email == "undefined" ? null : email,
state: handleId + "&" + redirectUri);
httpcontext.Response.Redirect(authUri);
}
Expand All @@ -66,25 +68,22 @@
var state = m.State.Split(new char[] { '&' }, 2);
var handleId = state[0];
var redirectUri = state[1];
var callbackUri = $"{httpcontext.Request.Scheme}://{httpcontext.Request.Host}{httpcontext.Request.Path}";

var http = _clientFactory.CreateClient();
var response = await http.RequestAuthorizationCodeTokenAsync(new AuthorizationCodeTokenRequest
{
Address = _options.Value.TokenEndpoint,
Address = _options.Value.GetMicrosoftTokenEndpoint(httpcontext),
ClientId = _options.Value.ClientId,
ClientSecret = _options.Value.ClientSecret,
Code = m.Code,
RedirectUri = _options.Value.RedirectUrl,
RedirectUri = callbackUri,
});

var handler = new JwtSecurityTokenHandler();
var jwtSecurityToken = handler.ReadJwtToken(response.IdentityToken);
var jti = jwtSecurityToken.Claims.First(claim => claim.Type == "email").Value;

ClaimsPrincipal identity = await _options.Value.ValidateUserAsync(httpcontext, handleId, response);

if (identity == null)
{
httpcontext.Response.Redirect("error=access_denied&error_subcode=user_not_found");
httpcontext.Response.Redirect($"{httpcontext.Request.Scheme}://{httpcontext.Request.Host}callback?error=access_denied&error_subcode=user_not_found");
//return;
}
return await Task.FromResult((new ClaimsPrincipal(identity), redirectUri, handleId));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ namespace EAVFW.Extensions.EasyAuth.MicrosoftEntraId
{
public static class MicrosoftEntraIdEasyAuthExtensions
{
public static AuthenticatedEAVFrameworkBuilder AddMicrosoftEntraIdEasyAuth(this AuthenticatedEAVFrameworkBuilder builder, Func<HttpContext, string, TokenResponse, Task<ClaimsPrincipal>> validateUserAsync)
public static AuthenticatedEAVFrameworkBuilder AddMicrosoftEntraIdEasyAuth(this AuthenticatedEAVFrameworkBuilder builder, Func<HttpContext, string, TokenResponse, Task<ClaimsPrincipal>> validateUserAsync, Func<HttpContext, string> getMicrosoftAuthorizationUrl, Func<HttpContext, string> getMicrosoftTokenEndpoint)
{
builder.AddAuthenticationProvider<MicrosoftEntraEasyAuthProvider, MicrosoftEntraIdEasyAuthOptions,IConfiguration>((options, config) =>
{
config.GetSection("EAVEasyAuth:MicrosoftEntraId").Bind(options);
options.ValidateUserAsync = validateUserAsync;
options.GetMicrosoftAuthorizationUrl = getMicrosoftAuthorizationUrl;
options.GetMicrosoftTokenEndpoint = getMicrosoftTokenEndpoint;

});
return builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
using IdentityModel.Client;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;

namespace EAVFW.Extensions.EasyAuth.MicrosoftEntraId
{
public class MicrosoftEntraIdEasyAuthOptions
{
public string AuthorizationUrl { get; set; }
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string TenantId { get; set; }
public string GroupId { get; set; }
public string Scope { get; set; }
public string TokenEndpoint { get; set; }
public string RedirectUrl { get; set; }


public Func<HttpContext, string> GetMicrosoftAuthorizationUrl { get; set; }
public Func<HttpContext, string> GetMicrosoftTokenEndpoint { get; set; }
public Func<HttpContext, string, TokenResponse, Task<ClaimsPrincipal>> ValidateUserAsync { get; set; }
}
}
Loading