Implement disabling of local oauth2 controller

This commit is contained in:
2025-04-15 13:08:28 +02:00
parent 7defc9a6a9
commit 65ea5985d3

View File

@@ -43,6 +43,9 @@ public class OAuth2Controller : Controller
[FromQuery(Name = "view")] string view = "login" [FromQuery(Name = "view")] string view = "login"
) )
{ {
if (!Configuration.Authentication.EnableLocalOAuth2)
throw new HttpApiException("Local OAuth2 has been disabled", 403);
if (Configuration.Authentication.OAuth2.ClientId != clientId || if (Configuration.Authentication.OAuth2.ClientId != clientId ||
redirectUri != ExpectedRedirectUri || redirectUri != ExpectedRedirectUri ||
responseType != "code") responseType != "code")
@@ -88,6 +91,9 @@ public class OAuth2Controller : Controller
[FromQuery(Name = "view")] string view = "login" [FromQuery(Name = "view")] string view = "login"
) )
{ {
if (!Configuration.Authentication.EnableLocalOAuth2)
throw new HttpApiException("Local OAuth2 has been disabled", 403);
if (Configuration.Authentication.OAuth2.ClientId != clientId || if (Configuration.Authentication.OAuth2.ClientId != clientId ||
redirectUri != ExpectedRedirectUri || redirectUri != ExpectedRedirectUri ||
responseType != "code") responseType != "code")
@@ -161,6 +167,9 @@ public class OAuth2Controller : Controller
[FromForm(Name = "client_id")] string clientId [FromForm(Name = "client_id")] string clientId
) )
{ {
if (!Configuration.Authentication.EnableLocalOAuth2)
throw new HttpApiException("Local OAuth2 has been disabled", 403);
// Check header // Check header
if (!Request.Headers.ContainsKey("Authorization")) if (!Request.Headers.ContainsKey("Authorization"))
throw new HttpApiException("You are missing the Authorization header", 400); throw new HttpApiException("You are missing the Authorization header", 400);
@@ -227,7 +236,7 @@ public class OAuth2Controller : Controller
}; };
} }
private async Task<string> GenerateCode(User user) private Task<string> GenerateCode(User user)
{ {
var securityTokenDescriptor = new SecurityTokenDescriptor() var securityTokenDescriptor = new SecurityTokenDescriptor()
{ {
@@ -252,7 +261,9 @@ public class OAuth2Controller : Controller
var jwtSecurityTokenHandler = new JwtSecurityTokenHandler(); var jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
var securityToken = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor); var securityToken = jwtSecurityTokenHandler.CreateToken(securityTokenDescriptor);
return jwtSecurityTokenHandler.WriteToken(securityToken); return Task.FromResult(
jwtSecurityTokenHandler.WriteToken(securityToken)
);
} }
private async Task<User> Register(string username, string email, string password) private async Task<User> Register(string username, string email, string password)
@@ -270,9 +281,7 @@ public class OAuth2Controller : Controller
Password = HashHelper.Hash(password) Password = HashHelper.Hash(password)
}; };
var finalUser = await UserRepository.Add(user); return await UserRepository.Add(user);
return finalUser;
} }
private async Task<User> Login(string email, string password) private async Task<User> Login(string email, string password)