The Free Software Media System - Server Backend & API
Allow apikey to authenticate as admin
crobibero39924f99927a
diff --git a/Emby.Server.Implementations/HttpServer/Security/AuthService.cs b/Emby.Server.Implementations/HttpServer/Security/AuthService.csindex 68d981ad1f..50c5b5b79e 100644--- a/Emby.Server.Implementations/HttpServer/Security/AuthService.cs+++ b/Emby.Server.Implementations/HttpServer/Security/AuthService.cs@@ -19,12 +19,7 @@ namespace Emby.Server.Implementations.HttpServer.Securitypublic AuthorizationInfo Authenticate(HttpRequest request){var auth = _authorizationContext.GetAuthorizationInfo(request);- if (auth?.User == null)- {- return null;- }-- if (auth.User.HasPermission(PermissionKind.IsDisabled))+ if (auth.User?.HasPermission(PermissionKind.IsDisabled) ?? false){throw new SecurityException("User account has been disabled.");}diff --git a/Emby.Server.Implementations/HttpServer/Security/AuthorizationContext.cs b/Emby.Server.Implementations/HttpServer/Security/AuthorizationContext.csindex 4b407dd9d2..c7666452c2 100644--- a/Emby.Server.Implementations/HttpServer/Security/AuthorizationContext.cs+++ b/Emby.Server.Implementations/HttpServer/Security/AuthorizationContext.cs@@ -111,82 +111,84 @@ namespace Emby.Server.Implementations.HttpServer.SecurityToken = token};- AuthenticationInfo originalAuthenticationInfo = null;- if (!string.IsNullOrWhiteSpace(token))+ if (string.IsNullOrWhiteSpace(token)){- var result = _authRepo.Get(new AuthenticationInfoQuery- {- AccessToken = token- });+ // Request doesn't contain a token.+ throw new SecurityException("Unauthorized.");+ }- originalAuthenticationInfo = result.Items.Count > 0 ? result.Items[0] : null;+ var result = _authRepo.Get(new AuthenticationInfoQuery+ {+ AccessToken = token+ });- if (originalAuthenticationInfo != null)- {- var updateToken = false;+ var originalAuthenticationInfo = result.Items.Count > 0 ? result.Items[0] : null;- // TODO: Remove these checks for IsNullOrWhiteSpace- if (string.IsNullOrWhiteSpace(authInfo.Client))- {- authInfo.Client = originalAuthenticationInfo.AppName;- }+ if (originalAuthenticationInfo != null)+ {+ var updateToken = false;- if (string.IsNullOrWhiteSpace(authInfo.DeviceId))- {- authInfo.DeviceId = originalAuthenticationInfo.DeviceId;- }+ // TODO: Remove these checks for IsNullOrWhiteSpace+ if (string.IsNullOrWhiteSpace(authInfo.Client))+ {+ authInfo.Client = originalAuthenticationInfo.AppName;+ }- // Temporary. TODO - allow clients to specify that the token has been shared with a casting device- var allowTokenInfoUpdate = authInfo.Client == null || authInfo.Client.IndexOf("chromecast", StringComparison.OrdinalIgnoreCase) == -1;+ if (string.IsNullOrWhiteSpace(authInfo.DeviceId))+ {+ authInfo.DeviceId = originalAuthenticationInfo.DeviceId;+ }- if (string.IsNullOrWhiteSpace(authInfo.Device))- {- authInfo.Device = originalAuthenticationInfo.DeviceName;- }- else if (!string.Equals(authInfo.Device, originalAuthenticationInfo.DeviceName, StringComparison.OrdinalIgnoreCase))- {- if (allowTokenInfoUpdate)- {- updateToken = true;- originalAuthenticationInfo.DeviceName = authInfo.Device;- }- }+ // Temporary. TODO - allow clients to specify that the token has been shared with a casting device+ var allowTokenInfoUpdate = authInfo.Client == null || authInfo.Client.IndexOf("chromecast", StringComparison.OrdinalIgnoreCase) == -1;- if (string.IsNullOrWhiteSpace(authInfo.Version))- {- authInfo.Version = originalAuthenticationInfo.AppVersion;- }- else if (!string.Equals(authInfo.Version, originalAuthenticationInfo.AppVersion, StringComparison.OrdinalIgnoreCase))+ if (string.IsNullOrWhiteSpace(authInfo.Device))+ {+ authInfo.Device = originalAuthenticationInfo.DeviceName;+ }+ else if (!string.Equals(authInfo.Device, originalAuthenticationInfo.DeviceName, StringComparison.OrdinalIgnoreCase))+ {+ if (allowTokenInfoUpdate){- if (allowTokenInfoUpdate)- {- updateToken = true;- originalAuthenticationInfo.AppVersion = authInfo.Version;- }+ updateToken = true;+ originalAuthenticationInfo.DeviceName = authInfo.Device;}+ }- if ((DateTime.UtcNow - originalAuthenticationInfo.DateLastActivity).TotalMinutes > 3)+ if (string.IsNullOrWhiteSpace(authInfo.Version))+ {+ authInfo.Version = originalAuthenticationInfo.AppVersion;+ }+ else if (!string.Equals(authInfo.Version, originalAuthenticationInfo.AppVersion, StringComparison.OrdinalIgnoreCase))+ {+ if (allowTokenInfoUpdate){- originalAuthenticationInfo.DateLastActivity = DateTime.UtcNow;updateToken = true;+ originalAuthenticationInfo.AppVersion = authInfo.Version;}+ }- if (!originalAuthenticationInfo.UserId.Equals(Guid.Empty))- {- authInfo.User = _userManager.GetUserById(originalAuthenticationInfo.UserId);+ if ((DateTime.UtcNow - originalAuthenticationInfo.DateLastActivity).TotalMinutes > 3)+ {+ originalAuthenticationInfo.DateLastActivity = DateTime.UtcNow;+ updateToken = true;+ }- if (authInfo.User != null && !string.Equals(authInfo.User.Username, originalAuthenticationInfo.UserName, StringComparison.OrdinalIgnoreCase))- {- originalAuthenticationInfo.UserName = authInfo.User.Username;- updateToken = true;- }- }+ if (!originalAuthenticationInfo.UserId.Equals(Guid.Empty))+ {+ authInfo.User = _userManager.GetUserById(originalAuthenticationInfo.UserId);- if (updateToken)+ if (authInfo.User != null && !string.Equals(authInfo.User.Username, originalAuthenticationInfo.UserName, StringComparison.OrdinalIgnoreCase)){- _authRepo.Update(originalAuthenticationInfo);+ originalAuthenticationInfo.UserName = authInfo.User.Username;+ updateToken = true;}}++ if (updateToken)+ {+ _authRepo.Update(originalAuthenticationInfo);+ }}return (authInfo, originalAuthenticationInfo);diff --git a/Jellyfin.Api/Auth/BaseAuthorizationHandler.cs b/Jellyfin.Api/Auth/BaseAuthorizationHandler.csindex d732b6bc6a..c4567d058c 100644--- a/Jellyfin.Api/Auth/BaseAuthorizationHandler.cs+++ b/Jellyfin.Api/Auth/BaseAuthorizationHandler.cs@@ -1,4 +1,5 @@-using System.Security.Claims;+using System;+using System.Security.Claims;using Jellyfin.Api.Helpers;using Jellyfin.Data.Enums;using MediaBrowser.Common.Extensions;@@ -57,6 +58,12 @@ namespace Jellyfin.Api.Authreturn false;}+ // UserId of Guid.Empty means token is an apikey.+ if (userId.Equals(Guid.Empty))+ {+ return true;+ }+// Ensure userId links to a valid user.var user = _userManager.GetUserById(userId.Value);if (user == null)diff --git a/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs b/Jellyfin.Api/Auth/CustomAuthenticationHandler.csindex 733c6959eb..ec5d172a21 100644--- a/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs+++ b/Jellyfin.Api/Auth/CustomAuthenticationHandler.cs@@ -1,3 +1,4 @@+using System;using System.Globalization;using System.Security.Authentication;using System.Security.Claims;@@ -43,18 +44,17 @@ namespace Jellyfin.Api.Authtry{var authorizationInfo = _authService.Authenticate(Request);- if (authorizationInfo == null)+ var role = UserRoles.User;+ // UserId of Guid.Empty means token is an apikey.+ if (authorizationInfo.UserId.Equals(Guid.Empty) || authorizationInfo.User.HasPermission(PermissionKind.IsAdministrator)){- return Task.FromResult(AuthenticateResult.NoResult());- // TODO return when legacy API is removed.- // Don't spam the log with "Invalid User"- // return Task.FromResult(AuthenticateResult.Fail("Invalid user"));+ role = UserRoles.Administrator;}var claims = new[]{- new Claim(ClaimTypes.Name, authorizationInfo.User.Username),- new Claim(ClaimTypes.Role, authorizationInfo.User.HasPermission(PermissionKind.IsAdministrator) ? UserRoles.Administrator : UserRoles.User),+ new Claim(ClaimTypes.Name, authorizationInfo.User?.Username ?? string.Empty),+ new Claim(ClaimTypes.Role, role),new Claim(InternalClaimTypes.UserId, authorizationInfo.UserId.ToString("N", CultureInfo.InvariantCulture)),new Claim(InternalClaimTypes.DeviceId, authorizationInfo.DeviceId),new Claim(InternalClaimTypes.Device, authorizationInfo.Device),