From 0ed2fed529102eaf4b644c65aaa28ef9edcdf89b Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Fri, 16 Oct 2015 10:29:02 -0400 Subject: [PATCH 1/2] Restore appstore registration end point --- MediaBrowser.Api/PluginService.cs | 35 +++++++++++ .../Security/PluginSecurityManager.cs | 61 +++++++++++++++++++ .../Security/RegRecord.cs | 1 + .../Security/ISecurityManager.cs | 17 ++++++ 4 files changed, 114 insertions(+) diff --git a/MediaBrowser.Api/PluginService.cs b/MediaBrowser.Api/PluginService.cs index eb49914eb1..c1d29681c6 100644 --- a/MediaBrowser.Api/PluginService.cs +++ b/MediaBrowser.Api/PluginService.cs @@ -118,6 +118,30 @@ namespace MediaBrowser.Api public string Name { get; set; } } + [Route("/Appstore/Register", "POST", Summary = "Registers an appstore sale")] + [Authenticated] + public class RegisterAppstoreSale + { + [ApiMember(Name = "Store", Description = "Store Name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string Store { get; set; } + [ApiMember(Name = "Application", Description = "Application id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string Application { get; set; } + [ApiMember(Name = "Product", Description = "Product id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string Product { get; set; } + [ApiMember(Name = "Type", Description = "Type of product (Product or Subscription)", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string Type { get; set; } + [ApiMember(Name = "StoreId", Description = "Store User Id (if needed)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")] + public string StoreId { get; set; } + [ApiMember(Name = "StoreToken", Description = "Unique ID for this purchase in the store", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string StoreToken { get; set; } + [ApiMember(Name = "Feature", Description = "Emby Feature Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string Feature { get; set; } + [ApiMember(Name = "Email", Description = "Email address for purchase", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string Email { get; set; } + [ApiMember(Name = "Amount", Description = "String representation of price (can have currency sign)", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string Amount { get; set; } + } + /// /// Class PluginsService /// @@ -265,6 +289,17 @@ namespace MediaBrowser.Api return ToOptimizedSerializedResultUsingCache(result); } + /// + /// Post app store sale + /// + /// + /// + public async Task Post(RegisterAppstoreSale request) + { + var success = await _securityManager.RegisterAppStoreSale(request.Store, request.Application, request.Product, request.Feature, request.Type, request.StoreId, request.StoreToken, request.Email, request.Amount); + if (!success) throw new ApplicationException("Error registering store sale"); + } + /// /// Posts the specified request. /// diff --git a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs index c2725e9bf6..861d638f2c 100644 --- a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs +++ b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs @@ -18,6 +18,7 @@ namespace MediaBrowser.Common.Implementations.Security public class PluginSecurityManager : ISecurityManager { private const string MBValidateUrl = MbAdmin.HttpsUrl + "service/registration/validate"; + private const string AppstoreRegUrl = /*MbAdmin.HttpsUrl*/ "http://mb3admin.com/test/admin/" + "service/appstore/register"; /// /// The _is MB supporter @@ -185,6 +186,66 @@ namespace MediaBrowser.Common.Implementations.Security } } + /// + /// Register an app store sale with our back-end. It will validate the transaction with the store + /// and then register the proper feature and then fill in the supporter key on success. + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// true if successful + public async Task RegisterAppStoreSale(string store, string application, string product, string feature, + string type, string storeId, string storeToken, string email, string amt) + { + var data = new Dictionary() + { + {"store", store}, + {"application", application}, + {"product", product}, + {"feature", feature}, + {"type", type}, + {"storeId", storeId}, + {"token", storeToken}, + {"email", email}, + {"amt", amt} + }; + + var options = new HttpRequestOptions() + { + Url = AppstoreRegUrl, + CancellationToken = CancellationToken.None + }; + options.RequestHeaders.Add("X-Emby-Token", /*_appHost.SystemId*/ "08606E86D043"); + + try + { + using (var json = await _httpClient.Post(options, data).ConfigureAwait(false)) + { + var reg = _jsonSerializer.DeserializeFromStream(json); + if (!String.IsNullOrEmpty(reg.key)) + { + SupporterKey = reg.key; + } + + return true; + } + + } + catch (Exception e) + { + _logger.ErrorException("Error registering appstore purchase {0}", e, _jsonSerializer.SerializeToString(data)); + //TODO - really need to write this to a file so we can re-try it automatically + return false; + } + + } + private async Task GetRegistrationStatusInternal(string feature, string mb2Equivalent = null, string version = null) diff --git a/MediaBrowser.Common.Implementations/Security/RegRecord.cs b/MediaBrowser.Common.Implementations/Security/RegRecord.cs index f4e4337bfa..ece70b7726 100644 --- a/MediaBrowser.Common.Implementations/Security/RegRecord.cs +++ b/MediaBrowser.Common.Implementations/Security/RegRecord.cs @@ -7,5 +7,6 @@ namespace MediaBrowser.Common.Implementations.Security public string featId { get; set; } public bool registered { get; set; } public DateTime expDate { get; set; } + public string key { get; set; } } } \ No newline at end of file diff --git a/MediaBrowser.Common/Security/ISecurityManager.cs b/MediaBrowser.Common/Security/ISecurityManager.cs index 9354543532..5d15808da5 100644 --- a/MediaBrowser.Common/Security/ISecurityManager.cs +++ b/MediaBrowser.Common/Security/ISecurityManager.cs @@ -1,3 +1,4 @@ +using System; using MediaBrowser.Model.Entities; using System.Threading.Tasks; @@ -45,5 +46,21 @@ namespace MediaBrowser.Common.Security /// /// Task<SupporterInfo>. Task GetSupporterInfo(); + + /// + /// Register and app store sale with our back-end + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// true if successful + Task RegisterAppStoreSale(string store, string application, string product, string feature, + string type, string storeId, string storeToken, string email, string amt); } } \ No newline at end of file From cd42bce822453e4860430989de5b75f379c11e6b Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Fri, 16 Oct 2015 10:54:53 -0400 Subject: [PATCH 2/2] Move exception to SecurityManager --- MediaBrowser.Api/PluginService.cs | 3 +-- .../Security/PluginSecurityManager.cs | 7 ++----- MediaBrowser.Common/Security/ISecurityManager.cs | 3 +-- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/MediaBrowser.Api/PluginService.cs b/MediaBrowser.Api/PluginService.cs index c1d29681c6..dc3d103eb5 100644 --- a/MediaBrowser.Api/PluginService.cs +++ b/MediaBrowser.Api/PluginService.cs @@ -296,8 +296,7 @@ namespace MediaBrowser.Api /// public async Task Post(RegisterAppstoreSale request) { - var success = await _securityManager.RegisterAppStoreSale(request.Store, request.Application, request.Product, request.Feature, request.Type, request.StoreId, request.StoreToken, request.Email, request.Amount); - if (!success) throw new ApplicationException("Error registering store sale"); + await _securityManager.RegisterAppStoreSale(request.Store, request.Application, request.Product, request.Feature, request.Type, request.StoreId, request.StoreToken, request.Email, request.Amount); } /// diff --git a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs index 861d638f2c..facaaeff9b 100644 --- a/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs +++ b/MediaBrowser.Common.Implementations/Security/PluginSecurityManager.cs @@ -199,8 +199,7 @@ namespace MediaBrowser.Common.Implementations.Security /// /// /// - /// true if successful - public async Task RegisterAppStoreSale(string store, string application, string product, string feature, + public async Task RegisterAppStoreSale(string store, string application, string product, string feature, string type, string storeId, string storeToken, string email, string amt) { var data = new Dictionary() @@ -232,8 +231,6 @@ namespace MediaBrowser.Common.Implementations.Security { SupporterKey = reg.key; } - - return true; } } @@ -241,7 +238,7 @@ namespace MediaBrowser.Common.Implementations.Security { _logger.ErrorException("Error registering appstore purchase {0}", e, _jsonSerializer.SerializeToString(data)); //TODO - really need to write this to a file so we can re-try it automatically - return false; + throw new ApplicationException("Error registering store sale"); } } diff --git a/MediaBrowser.Common/Security/ISecurityManager.cs b/MediaBrowser.Common/Security/ISecurityManager.cs index 5d15808da5..1b2e22a78c 100644 --- a/MediaBrowser.Common/Security/ISecurityManager.cs +++ b/MediaBrowser.Common/Security/ISecurityManager.cs @@ -59,8 +59,7 @@ namespace MediaBrowser.Common.Security /// /// /// - /// true if successful - Task RegisterAppStoreSale(string store, string application, string product, string feature, + Task RegisterAppStoreSale(string store, string application, string product, string feature, string type, string storeId, string storeToken, string email, string amt); } } \ No newline at end of file