mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-07-15 09:42:56 -07:00
Added the ability to impersonate a user when using the API Key. This allows people to use the API and request as a certain user. #2363
This commit is contained in:
parent
c14f603705
commit
c6a362bf2b
1 changed files with 25 additions and 3 deletions
|
@ -94,9 +94,31 @@ namespace Ombi
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var identity = new GenericIdentity("API");
|
// Check if we have a UserName header if so we can impersonate that user
|
||||||
var principal = new GenericPrincipal(identity, new[] { "Admin", "ApiUser" });
|
if (context.Request.Headers.Keys.Contains("UserName", StringComparer.InvariantCultureIgnoreCase))
|
||||||
context.User = principal;
|
{
|
||||||
|
var username = context.Request.Headers["UserName"].FirstOrDefault();
|
||||||
|
var um = context.RequestServices.GetService<OmbiUserManager>();
|
||||||
|
var user = await um.Users.FirstOrDefaultAsync(x =>
|
||||||
|
x.UserName.Equals(username, StringComparison.InvariantCultureIgnoreCase));
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
|
||||||
|
await context.Response.WriteAsync("Invalid User");
|
||||||
|
await next.Invoke(context);
|
||||||
|
}
|
||||||
|
var roles = await um.GetRolesAsync(user);
|
||||||
|
var identity = new GenericIdentity(user.UserName);
|
||||||
|
var principal = new GenericPrincipal(identity, roles.ToArray());
|
||||||
|
context.User = principal;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var identity = new GenericIdentity("API");
|
||||||
|
var principal = new GenericPrincipal(identity, new[] { "Admin", "ApiUser" });
|
||||||
|
context.User = principal;
|
||||||
|
}
|
||||||
|
|
||||||
await next.Invoke(context);
|
await next.Invoke(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue