2
0

PinLoginService.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Globalization;
  4. using MediaBrowser.Common.Extensions;
  5. using MediaBrowser.Controller.Net;
  6. using MediaBrowser.Model.Connect;
  7. using ServiceStack;
  8. namespace MediaBrowser.Api
  9. {
  10. [Route("/Auth/Pin", "POST", Summary = "Creates a pin request")]
  11. public class CreatePinRequest : IReturn<PinCreationResult>
  12. {
  13. [ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  14. public string DeviceId { get; set; }
  15. }
  16. [Route("/Auth/Pin", "GET", Summary = "Gets pin status")]
  17. public class GetPinStatusRequest : IReturn<PinStatusResult>
  18. {
  19. [ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  20. public string DeviceId { get; set; }
  21. [ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  22. public string Pin { get; set; }
  23. }
  24. [Route("/Auth/Pin/Exchange", "POST", Summary = "Exchanges a pin")]
  25. public class ExchangePinRequest : IReturn<PinExchangeResult>
  26. {
  27. [ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  28. public string DeviceId { get; set; }
  29. [ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  30. public string Pin { get; set; }
  31. }
  32. [Route("/Auth/Pin/Validate", "POST", Summary = "Validates a pin")]
  33. [Authenticated]
  34. public class ValidatePinRequest : IReturnVoid
  35. {
  36. [ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  37. public string Pin { get; set; }
  38. }
  39. public class PinLoginService : BaseApiService
  40. {
  41. private readonly ConcurrentDictionary<string, MyPinStatus> _activeRequests = new ConcurrentDictionary<string, MyPinStatus>(StringComparer.OrdinalIgnoreCase);
  42. public object Post(CreatePinRequest request)
  43. {
  44. var pin = GetNewPin();
  45. var value = new MyPinStatus
  46. {
  47. CreationTimeUtc = DateTime.UtcNow,
  48. IsConfirmed = false,
  49. IsExpired = false,
  50. Pin = pin,
  51. DeviceId = request.DeviceId
  52. };
  53. _activeRequests.AddOrUpdate(pin, value, (k, v) => value);
  54. return ToOptimizedResult(new PinCreationResult
  55. {
  56. DeviceId = request.DeviceId,
  57. IsConfirmed = false,
  58. IsExpired = false,
  59. Pin = pin
  60. });
  61. }
  62. public object Get(GetPinStatusRequest request)
  63. {
  64. MyPinStatus status;
  65. if (!_activeRequests.TryGetValue(request.Pin, out status))
  66. {
  67. throw new ResourceNotFoundException();
  68. }
  69. EnsureValid(request.DeviceId, status);
  70. return ToOptimizedResult(new PinStatusResult
  71. {
  72. Pin = status.Pin,
  73. IsConfirmed = status.IsConfirmed,
  74. IsExpired = status.IsExpired
  75. });
  76. }
  77. public object Post(ExchangePinRequest request)
  78. {
  79. MyPinStatus status;
  80. if (!_activeRequests.TryGetValue(request.Pin, out status))
  81. {
  82. throw new ResourceNotFoundException();
  83. }
  84. EnsureValid(request.DeviceId, status);
  85. if (!status.IsConfirmed)
  86. {
  87. throw new ResourceNotFoundException();
  88. }
  89. return ToOptimizedResult(new PinExchangeResult
  90. {
  91. // TODO: Add access token
  92. UserId = status.UserId
  93. });
  94. }
  95. public void Post(ValidatePinRequest request)
  96. {
  97. MyPinStatus status;
  98. if (!_activeRequests.TryGetValue(request.Pin, out status))
  99. {
  100. throw new ResourceNotFoundException();
  101. }
  102. EnsureValid(status);
  103. status.IsConfirmed = true;
  104. status.UserId = AuthorizationContext.GetAuthorizationInfo(Request).UserId;
  105. }
  106. private void EnsureValid(string requestedDeviceId, MyPinStatus status)
  107. {
  108. if (!string.Equals(requestedDeviceId, status.DeviceId, StringComparison.OrdinalIgnoreCase))
  109. {
  110. throw new ResourceNotFoundException();
  111. }
  112. EnsureValid(status);
  113. }
  114. private void EnsureValid(MyPinStatus status)
  115. {
  116. if ((DateTime.UtcNow - status.CreationTimeUtc).TotalMinutes > 10)
  117. {
  118. status.IsExpired = true;
  119. }
  120. if (status.IsExpired)
  121. {
  122. throw new ResourceNotFoundException();
  123. }
  124. }
  125. private string GetNewPin()
  126. {
  127. var pin = GetNewPinInternal();
  128. while (IsPinActive(pin))
  129. {
  130. pin = GetNewPinInternal();
  131. }
  132. return pin;
  133. }
  134. private string GetNewPinInternal()
  135. {
  136. var length = 5;
  137. var pin = string.Empty;
  138. while (pin.Length < length)
  139. {
  140. var digit = new Random().Next(0, 9);
  141. pin += digit.ToString(CultureInfo.InvariantCulture);
  142. }
  143. return pin;
  144. }
  145. private bool IsPinActive(string pin)
  146. {
  147. MyPinStatus status;
  148. if (!_activeRequests.TryGetValue(pin, out status))
  149. {
  150. return true;
  151. }
  152. if (status.IsExpired)
  153. {
  154. return true;
  155. }
  156. return false;
  157. }
  158. public class MyPinStatus : PinStatusResult
  159. {
  160. public DateTime CreationTimeUtc { get; set; }
  161. public string DeviceId { get; set; }
  162. public string UserId { get; set; }
  163. }
  164. }
  165. }