jellyfin/MediaBrowser.Api/ConnectService.cs

178 lines
6.9 KiB
C#
Raw Normal View History

2016-02-18 19:15:26 +00:00
using MediaBrowser.Common.Extensions;
2014-10-18 19:02:54 +00:00
using MediaBrowser.Controller.Connect;
using MediaBrowser.Controller.Library;
2014-09-14 15:10:51 +00:00
using MediaBrowser.Controller.Net;
2014-10-13 20:14:53 +00:00
using MediaBrowser.Model.Connect;
using System.Collections.Generic;
2014-10-18 19:02:54 +00:00
using System.Linq;
2014-09-14 17:42:23 +00:00
using System.Threading.Tasks;
2016-09-14 21:34:19 +00:00
using MediaBrowser.Controller.Session;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
2014-09-14 15:10:51 +00:00
namespace MediaBrowser.Api
{
[Route("/Users/{Id}/Connect/Link", "POST", Summary = "Creates a Connect link for a user")]
2014-10-18 19:02:54 +00:00
[Authenticated(Roles = "Admin")]
2014-09-14 23:39:06 +00:00
public class CreateConnectLink : IReturn<UserLinkResult>
2014-09-14 15:10:51 +00:00
{
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Id { get; set; }
[ApiMember(Name = "ConnectUsername", Description = "Connect username", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ConnectUsername { get; set; }
}
[Route("/Users/{Id}/Connect/Link", "DELETE", Summary = "Removes a Connect link for a user")]
2014-10-18 19:02:54 +00:00
[Authenticated(Roles = "Admin")]
2014-09-14 17:12:47 +00:00
public class DeleteConnectLink : IReturnVoid
2014-09-14 15:10:51 +00:00
{
[ApiMember(Name = "Id", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string Id { get; set; }
}
2014-09-14 17:42:23 +00:00
2014-10-13 20:14:53 +00:00
[Route("/Connect/Invite", "POST", Summary = "Creates a Connect link for a user")]
2014-10-18 19:02:54 +00:00
[Authenticated(Roles = "Admin")]
2014-10-13 20:14:53 +00:00
public class CreateConnectInvite : IReturn<UserLinkResult>
{
2014-10-31 04:57:24 +00:00
[ApiMember(Name = "ConnectUsername", Description = "Connect username", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
2014-10-13 20:14:53 +00:00
public string ConnectUsername { get; set; }
2014-10-31 04:57:24 +00:00
[ApiMember(Name = "SendingUserId", Description = "Sending User Id", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
2014-10-13 20:14:53 +00:00
public string SendingUserId { get; set; }
2014-10-31 04:57:24 +00:00
2015-01-20 05:19:13 +00:00
[ApiMember(Name = "EnabledLibraries", Description = "EnabledLibraries", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string EnabledLibraries { get; set; }
2014-10-31 04:57:24 +00:00
[ApiMember(Name = "EnabledChannels", Description = "EnabledChannels", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public string EnabledChannels { get; set; }
2014-10-31 04:57:24 +00:00
[ApiMember(Name = "EnableLiveTv", Description = "EnableLiveTv", IsRequired = true, DataType = "string", ParameterType = "body", Verb = "POST")]
public bool EnableLiveTv { get; set; }
2014-10-13 20:14:53 +00:00
}
[Route("/Connect/Pending", "GET", Summary = "Creates a Connect link for a user")]
2014-10-18 19:02:54 +00:00
[Authenticated(Roles = "Admin")]
2014-10-13 20:14:53 +00:00
public class GetPendingGuests : IReturn<List<ConnectAuthorization>>
{
}
[Route("/Connect/Pending", "DELETE", Summary = "Deletes a Connect link for a user")]
2014-10-18 19:02:54 +00:00
[Authenticated(Roles = "Admin")]
2014-10-13 20:14:53 +00:00
public class DeleteAuthorization : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Authorization Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string Id { get; set; }
}
2014-10-18 19:02:54 +00:00
[Route("/Connect/Exchange", "GET", Summary = "Gets the corresponding local user from a connect user id")]
[Authenticated]
public class GetLocalUser : IReturn<ConnectAuthenticationExchangeResult>
{
[ApiMember(Name = "ConnectUserId", Description = "ConnectUserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ConnectUserId { get; set; }
}
2014-09-14 15:10:51 +00:00
public class ConnectService : BaseApiService
{
private readonly IConnectManager _connectManager;
2016-09-14 21:34:19 +00:00
private readonly ISessionManager _sessionManager;
2016-11-10 14:41:24 +00:00
private readonly IAuthorizationContext _authContext;
2014-09-14 15:10:51 +00:00
2016-11-10 14:41:24 +00:00
public ConnectService(IConnectManager connectManager, ISessionManager sessionManager, IAuthorizationContext authContext)
2014-09-14 15:10:51 +00:00
{
_connectManager = connectManager;
2016-09-14 21:34:19 +00:00
_sessionManager = sessionManager;
2016-11-10 14:41:24 +00:00
_authContext = authContext;
2014-09-14 15:10:51 +00:00
}
2014-09-14 23:39:06 +00:00
public object Post(CreateConnectLink request)
2014-09-14 15:10:51 +00:00
{
2014-09-14 23:39:06 +00:00
return _connectManager.LinkUser(request.Id, request.ConnectUsername);
2014-09-14 15:10:51 +00:00
}
2014-10-13 20:14:53 +00:00
public object Post(CreateConnectInvite request)
{
2015-01-20 05:19:13 +00:00
var enabledLibraries = (request.EnabledLibraries ?? string.Empty)
2014-10-31 04:57:24 +00:00
.Split(',')
.Where(i => !string.IsNullOrWhiteSpace(i))
.ToArray();
var enabledChannels = (request.EnabledChannels ?? string.Empty)
2014-10-31 04:57:24 +00:00
.Split(',')
.Where(i => !string.IsNullOrWhiteSpace(i))
.ToArray();
return _connectManager.InviteUser(new ConnectAuthorizationRequest
{
ConnectUserName = request.ConnectUsername,
SendingUserId = request.SendingUserId,
2015-01-20 05:19:13 +00:00
EnabledLibraries = enabledLibraries,
EnabledChannels = enabledChannels,
2014-10-31 04:57:24 +00:00
EnableLiveTv = request.EnableLiveTv
});
2014-10-13 20:14:53 +00:00
}
2014-09-14 15:10:51 +00:00
public void Delete(DeleteConnectLink request)
{
2014-10-14 04:22:17 +00:00
var task = _connectManager.RemoveConnect(request.Id);
2014-09-14 15:10:51 +00:00
Task.WaitAll(task);
}
2014-10-13 20:14:53 +00:00
public async Task<object> Get(GetPendingGuests request)
{
var result = await _connectManager.GetPendingGuests().ConfigureAwait(false);
return ToOptimizedResult(result);
}
public void Delete(DeleteAuthorization request)
{
var task = _connectManager.CancelAuthorization(request.Id);
Task.WaitAll(task);
}
2014-10-18 19:02:54 +00:00
2014-11-03 03:38:43 +00:00
public async Task<object> Get(GetLocalUser request)
2014-10-18 19:02:54 +00:00
{
2014-11-03 03:38:43 +00:00
var user = await _connectManager.GetLocalUser(request.ConnectUserId).ConfigureAwait(false);
2014-10-18 19:02:54 +00:00
if (user == null)
{
throw new ResourceNotFoundException();
}
2016-11-10 14:41:24 +00:00
var auth = _authContext.GetAuthorizationInfo(Request);
2016-09-14 21:34:19 +00:00
if (string.IsNullOrWhiteSpace(auth.Client))
{
return ToOptimizedResult(new ConnectAuthenticationExchangeResult
{
AccessToken = user.ConnectAccessKey,
LocalUserId = user.Id.ToString("N")
});
}
var session = await _sessionManager.CreateNewSession(new AuthenticationRequest
{
App = auth.Client,
AppVersion = auth.Version,
DeviceId = auth.DeviceId,
DeviceName = auth.Device,
RemoteEndPoint = Request.RemoteIp,
Username = user.Name,
UserId = user.Id.ToString("N")
}).ConfigureAwait(false);
2014-10-18 19:02:54 +00:00
return ToOptimizedResult(new ConnectAuthenticationExchangeResult
{
2016-09-14 21:34:19 +00:00
AccessToken = session.AccessToken,
LocalUserId = session.User.Id
2014-10-18 19:02:54 +00:00
});
}
2014-09-14 15:10:51 +00:00
}
}