Fixed oauth2 configuration loading

This commit is contained in:
2025-02-28 11:03:54 +01:00
parent b1092985ff
commit 45ccb6fc4c
2 changed files with 25 additions and 12 deletions

View File

@@ -24,6 +24,9 @@ public class AuthController : Controller
private readonly ILogger<AuthController> Logger; private readonly ILogger<AuthController> Logger;
private readonly DatabaseRepository<User> UserRepository; private readonly DatabaseRepository<User> UserRepository;
private readonly string RedirectUri;
private readonly string EndpointUri;
public AuthController( public AuthController(
AppConfiguration configuration, AppConfiguration configuration,
ILogger<AuthController> logger, ILogger<AuthController> logger,
@@ -33,6 +36,14 @@ public class AuthController : Controller
Configuration = configuration; Configuration = configuration;
Logger = logger; Logger = logger;
UserRepository = userRepository; UserRepository = userRepository;
RedirectUri = string.IsNullOrEmpty(Configuration.Authentication.OAuth2.AuthorizationRedirect)
? Configuration.PublicUrl
: Configuration.Authentication.OAuth2.AuthorizationRedirect;
EndpointUri = string.IsNullOrEmpty(Configuration.Authentication.OAuth2.AuthorizationEndpoint)
? Configuration.PublicUrl + "/oauth2/authorize"
: Configuration.Authentication.OAuth2.AuthorizationEndpoint;
} }
[AllowAnonymous] [AllowAnonymous]
@@ -42,8 +53,8 @@ public class AuthController : Controller
var response = new LoginStartResponse() var response = new LoginStartResponse()
{ {
ClientId = Configuration.Authentication.OAuth2.ClientId, ClientId = Configuration.Authentication.OAuth2.ClientId,
RedirectUri = Configuration.Authentication.OAuth2.AuthorizationRedirect ?? Configuration.PublicUrl, RedirectUri = RedirectUri,
Endpoint = Configuration.Authentication.OAuth2.AuthorizationEndpoint ?? Configuration.PublicUrl + "/oauth2/authorize" Endpoint = EndpointUri
}; };
return Task.FromResult(response); return Task.FromResult(response);
@@ -71,7 +82,7 @@ public class AuthController : Controller
[ [
new KeyValuePair<string, string>("grant_type", "authorization_code"), new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("code", request.Code), new KeyValuePair<string, string>("code", request.Code),
new KeyValuePair<string, string>("redirect_uri", Configuration.Authentication.OAuth2.AuthorizationRedirect ?? Configuration.PublicUrl), new KeyValuePair<string, string>("redirect_uri", RedirectUri),
new KeyValuePair<string, string>("client_id", Configuration.Authentication.OAuth2.ClientId) new KeyValuePair<string, string>("client_id", Configuration.Authentication.OAuth2.ClientId)
] ]
)); ));

View File

@@ -21,11 +21,17 @@ public class OAuth2Controller : Controller
{ {
private readonly AppConfiguration Configuration; private readonly AppConfiguration Configuration;
private readonly DatabaseRepository<User> UserRepository; private readonly DatabaseRepository<User> UserRepository;
private readonly string ExpectedRedirectUri;
public OAuth2Controller(AppConfiguration configuration, DatabaseRepository<User> userRepository) public OAuth2Controller(AppConfiguration configuration, DatabaseRepository<User> userRepository)
{ {
Configuration = configuration; Configuration = configuration;
UserRepository = userRepository; UserRepository = userRepository;
ExpectedRedirectUri = string.IsNullOrEmpty(Configuration.Authentication.OAuth2.AuthorizationRedirect)
? Configuration.PublicUrl
: Configuration.Authentication.OAuth2.AuthorizationRedirect;
} }
[AllowAnonymous] [AllowAnonymous]
@@ -37,10 +43,8 @@ public class OAuth2Controller : Controller
[FromQuery(Name = "view")] string view = "login" [FromQuery(Name = "view")] string view = "login"
) )
{ {
var requiredRedirectUri = Configuration.Authentication.OAuth2.AuthorizationRedirect ?? Configuration.PublicUrl;
if (Configuration.Authentication.OAuth2.ClientId != clientId || if (Configuration.Authentication.OAuth2.ClientId != clientId ||
requiredRedirectUri != redirectUri || redirectUri != ExpectedRedirectUri ||
responseType != "code") responseType != "code")
{ {
throw new HttpApiException("Invalid oauth2 request", 400); throw new HttpApiException("Invalid oauth2 request", 400);
@@ -84,10 +88,8 @@ public class OAuth2Controller : Controller
[FromQuery(Name = "view")] string view = "login" [FromQuery(Name = "view")] string view = "login"
) )
{ {
var requiredRedirectUri = Configuration.Authentication.OAuth2.AuthorizationRedirect ?? Configuration.PublicUrl;
if (Configuration.Authentication.OAuth2.ClientId != clientId || if (Configuration.Authentication.OAuth2.ClientId != clientId ||
requiredRedirectUri != redirectUri || redirectUri != ExpectedRedirectUri ||
responseType != "code") responseType != "code")
{ {
throw new HttpApiException("Invalid oauth2 request", 400); throw new HttpApiException("Invalid oauth2 request", 400);
@@ -175,7 +177,7 @@ public class OAuth2Controller : Controller
if(clientId != Configuration.Authentication.OAuth2.ClientId) if(clientId != Configuration.Authentication.OAuth2.ClientId)
throw new HttpApiException("Invalid client id provided", 400); throw new HttpApiException("Invalid client id provided", 400);
if(redirectUri != (Configuration.Authentication.OAuth2.AuthorizationRedirect ?? Configuration.PublicUrl)) if(redirectUri != ExpectedRedirectUri)
throw new HttpApiException("Invalid redirect uri provided", 400); throw new HttpApiException("Invalid redirect uri provided", 400);
var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
@@ -187,7 +189,7 @@ public class OAuth2Controller : Controller
codeData = jwtSecurityTokenHandler.ValidateToken(code, new TokenValidationParameters() codeData = jwtSecurityTokenHandler.ValidateToken(code, new TokenValidationParameters()
{ {
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes( IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(
Configuration.Authentication.Secret Configuration.Authentication.OAuth2.Secret
)), )),
ValidateIssuerSigningKey = true, ValidateIssuerSigningKey = true,
ValidateLifetime = true, ValidateLifetime = true,
@@ -241,7 +243,7 @@ public class OAuth2Controller : Controller
}, },
SigningCredentials = new SigningCredentials( SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey( new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(Configuration.Authentication.Secret) Encoding.UTF8.GetBytes(Configuration.Authentication.OAuth2.Secret)
), ),
SecurityAlgorithms.HmacSha256 SecurityAlgorithms.HmacSha256
) )