aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
diff options
context:
space:
mode:
authorcvium <clausvium@gmail.com>2022-10-21 11:55:32 +0200
committercvium <clausvium@gmail.com>2022-10-21 11:55:32 +0200
commitb836fe96857de9e39d9b565b1f57a151a82e401d (patch)
treec49622deedb94fa93ca95aea3199953ce0f4e629 /Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
parent509c6ec24ca35b2e16561808792cd581c5f9d8fc (diff)
remove JellyfinDbProvider and add second level caching
Diffstat (limited to 'Jellyfin.Server.Implementations/Security/AuthorizationContext.cs')
-rw-r--r--Jellyfin.Server.Implementations/Security/AuthorizationContext.cs142
1 files changed, 73 insertions, 69 deletions
diff --git a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
index 9f813f532..4d1a1b3cf 100644
--- a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
+++ b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs
@@ -4,6 +4,7 @@ using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
+using EFCoreSecondLevelCacheInterceptor;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
@@ -15,12 +16,12 @@ namespace Jellyfin.Server.Implementations.Security
{
public class AuthorizationContext : IAuthorizationContext
{
- private readonly JellyfinDbProvider _jellyfinDbProvider;
+ private readonly IDbContextFactory<JellyfinDb> _jellyfinDbProvider;
private readonly IUserManager _userManager;
private readonly IServerApplicationHost _serverApplicationHost;
public AuthorizationContext(
- JellyfinDbProvider jellyfinDb,
+ IDbContextFactory<JellyfinDb> jellyfinDb,
IUserManager userManager,
IServerApplicationHost serverApplicationHost)
{
@@ -121,96 +122,99 @@ namespace Jellyfin.Server.Implementations.Security
#pragma warning restore CA1508
authInfo.HasToken = true;
- await using var dbContext = _jellyfinDbProvider.CreateContext();
- var device = await dbContext.Devices.FirstOrDefaultAsync(d => d.AccessToken == token).ConfigureAwait(false);
-
- if (device != null)
+ var dbContext = await _jellyfinDbProvider.CreateDbContextAsync().ConfigureAwait(false);
+ await using (dbContext.ConfigureAwait(false))
{
- authInfo.IsAuthenticated = true;
- var updateToken = false;
-
- // TODO: Remove these checks for IsNullOrWhiteSpace
- if (string.IsNullOrWhiteSpace(authInfo.Client))
- {
- authInfo.Client = device.AppName;
- }
+ var device = await dbContext.Devices.FirstOrDefaultAsync(d => d.AccessToken == token).ConfigureAwait(false);
- if (string.IsNullOrWhiteSpace(authInfo.DeviceId))
+ if (device != null)
{
- authInfo.DeviceId = device.DeviceId;
- }
-
- // Temporary. TODO - allow clients to specify that the token has been shared with a casting device
- var allowTokenInfoUpdate = !authInfo.Client.Contains("chromecast", StringComparison.OrdinalIgnoreCase);
+ authInfo.IsAuthenticated = true;
+ var updateToken = false;
- if (string.IsNullOrWhiteSpace(authInfo.Device))
- {
- authInfo.Device = device.DeviceName;
- }
- else if (!string.Equals(authInfo.Device, device.DeviceName, StringComparison.OrdinalIgnoreCase))
- {
- if (allowTokenInfoUpdate)
+ // TODO: Remove these checks for IsNullOrWhiteSpace
+ if (string.IsNullOrWhiteSpace(authInfo.Client))
{
- updateToken = true;
- device.DeviceName = authInfo.Device;
+ authInfo.Client = device.AppName;
}
- }
- if (string.IsNullOrWhiteSpace(authInfo.Version))
- {
- authInfo.Version = device.AppVersion;
- }
- else if (!string.Equals(authInfo.Version, device.AppVersion, StringComparison.OrdinalIgnoreCase))
- {
- if (allowTokenInfoUpdate)
+ if (string.IsNullOrWhiteSpace(authInfo.DeviceId))
{
- updateToken = true;
- device.AppVersion = authInfo.Version;
+ authInfo.DeviceId = device.DeviceId;
}
- }
- if ((DateTime.UtcNow - device.DateLastActivity).TotalMinutes > 3)
- {
- device.DateLastActivity = DateTime.UtcNow;
- updateToken = true;
- }
+ // Temporary. TODO - allow clients to specify that the token has been shared with a casting device
+ var allowTokenInfoUpdate = !authInfo.Client.Contains("chromecast", StringComparison.OrdinalIgnoreCase);
- authInfo.User = _userManager.GetUserById(device.UserId);
+ if (string.IsNullOrWhiteSpace(authInfo.Device))
+ {
+ authInfo.Device = device.DeviceName;
+ }
+ else if (!string.Equals(authInfo.Device, device.DeviceName, StringComparison.OrdinalIgnoreCase))
+ {
+ if (allowTokenInfoUpdate)
+ {
+ updateToken = true;
+ device.DeviceName = authInfo.Device;
+ }
+ }
- if (updateToken)
- {
- dbContext.Devices.Update(device);
- await dbContext.SaveChangesAsync().ConfigureAwait(false);
- }
- }
- else
- {
- var key = await dbContext.ApiKeys.FirstOrDefaultAsync(apiKey => apiKey.AccessToken == token).ConfigureAwait(false);
- if (key != null)
- {
- authInfo.IsAuthenticated = true;
- authInfo.Client = key.Name;
- authInfo.Token = key.AccessToken;
- if (string.IsNullOrWhiteSpace(authInfo.DeviceId))
+ if (string.IsNullOrWhiteSpace(authInfo.Version))
{
- authInfo.DeviceId = _serverApplicationHost.SystemId;
+ authInfo.Version = device.AppVersion;
+ }
+ else if (!string.Equals(authInfo.Version, device.AppVersion, StringComparison.OrdinalIgnoreCase))
+ {
+ if (allowTokenInfoUpdate)
+ {
+ updateToken = true;
+ device.AppVersion = authInfo.Version;
+ }
}
- if (string.IsNullOrWhiteSpace(authInfo.Device))
+ if ((DateTime.UtcNow - device.DateLastActivity).TotalMinutes > 3)
{
- authInfo.Device = _serverApplicationHost.Name;
+ device.DateLastActivity = DateTime.UtcNow;
+ updateToken = true;
}
- if (string.IsNullOrWhiteSpace(authInfo.Version))
+ authInfo.User = _userManager.GetUserById(device.UserId);
+
+ if (updateToken)
{
- authInfo.Version = _serverApplicationHost.ApplicationVersionString;
+ dbContext.Devices.Update(device);
+ await dbContext.SaveChangesAsync().ConfigureAwait(false);
}
+ }
+ else
+ {
+ var key = await dbContext.ApiKeys.FirstOrDefaultAsync(apiKey => apiKey.AccessToken == token).ConfigureAwait(false);
+ if (key != null)
+ {
+ authInfo.IsAuthenticated = true;
+ authInfo.Client = key.Name;
+ authInfo.Token = key.AccessToken;
+ if (string.IsNullOrWhiteSpace(authInfo.DeviceId))
+ {
+ authInfo.DeviceId = _serverApplicationHost.SystemId;
+ }
+
+ if (string.IsNullOrWhiteSpace(authInfo.Device))
+ {
+ authInfo.Device = _serverApplicationHost.Name;
+ }
+
+ if (string.IsNullOrWhiteSpace(authInfo.Version))
+ {
+ authInfo.Version = _serverApplicationHost.ApplicationVersionString;
+ }
- authInfo.IsApiKey = true;
+ authInfo.IsApiKey = true;
+ }
}
- }
- return authInfo;
+ return authInfo;
+ }
}
/// <summary>