PinLoginService.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. public string DeviceId { get; set; }
  14. }
  15. [Route("/Auth/Pin", "GET", Summary = "Gets pin status")]
  16. public class GetPinStatusRequest : IReturn<PinStatusResult>
  17. {
  18. public string DeviceId { get; set; }
  19. public string Pin { get; set; }
  20. }
  21. [Route("/Auth/Pin/Exchange", "POST", Summary = "Exchanges a pin")]
  22. public class ExchangePinRequest : IReturn<PinExchangeResult>
  23. {
  24. public string DeviceId { get; set; }
  25. public string Pin { get; set; }
  26. }
  27. [Route("/Auth/Pin/Validate", "POST", Summary = "Validates a pin")]
  28. [Authenticated]
  29. public class ValidatePinRequest : IReturnVoid
  30. {
  31. public string Pin { get; set; }
  32. }
  33. public class PinLoginService : BaseApiService
  34. {
  35. private readonly ConcurrentDictionary<string, MyPinStatus> _activeRequests = new ConcurrentDictionary<string, MyPinStatus>(StringComparer.OrdinalIgnoreCase);
  36. public object Post(CreatePinRequest request)
  37. {
  38. var pin = GetNewPin(5);
  39. var key = GetKey(request.DeviceId, pin);
  40. var value = new MyPinStatus
  41. {
  42. CreationTimeUtc = DateTime.UtcNow,
  43. IsConfirmed = false,
  44. IsExpired = false,
  45. Pin = pin
  46. };
  47. _activeRequests.AddOrUpdate(key, value, (k, v) => value);
  48. return ToOptimizedResult(new PinCreationResult
  49. {
  50. DeviceId = request.DeviceId,
  51. IsConfirmed = false,
  52. IsExpired = false,
  53. Pin = pin
  54. });
  55. }
  56. public object Get(GetPinStatusRequest request)
  57. {
  58. MyPinStatus status;
  59. if (!_activeRequests.TryGetValue(GetKey(request.DeviceId, request.Pin), out status))
  60. {
  61. throw new ResourceNotFoundException();
  62. }
  63. CheckExpired(status);
  64. if (status.IsExpired)
  65. {
  66. throw new ResourceNotFoundException();
  67. }
  68. return ToOptimizedResult(new PinStatusResult
  69. {
  70. Pin = status.Pin,
  71. IsConfirmed = status.IsConfirmed,
  72. IsExpired = status.IsExpired
  73. });
  74. }
  75. public object Post(ExchangePinRequest request)
  76. {
  77. MyPinStatus status;
  78. if (!_activeRequests.TryGetValue(GetKey(request.DeviceId, request.Pin), out status))
  79. {
  80. throw new ResourceNotFoundException();
  81. }
  82. CheckExpired(status);
  83. if (status.IsExpired)
  84. {
  85. throw new ResourceNotFoundException();
  86. }
  87. return ToOptimizedResult(new PinExchangeResult
  88. {
  89. });
  90. }
  91. public void Post(ValidatePinRequest request)
  92. {
  93. }
  94. private void CheckExpired(MyPinStatus status)
  95. {
  96. if ((DateTime.UtcNow - status.CreationTimeUtc).TotalMinutes > 10)
  97. {
  98. status.IsExpired = true;
  99. }
  100. }
  101. private string GetNewPin(int length)
  102. {
  103. var pin = string.Empty;
  104. while (pin.Length < length)
  105. {
  106. var digit = new Random().Next(0, 9);
  107. pin += digit.ToString(CultureInfo.InvariantCulture);
  108. }
  109. return pin;
  110. }
  111. private string GetKey(string deviceId, string pin)
  112. {
  113. return deviceId + pin;
  114. }
  115. public class MyPinStatus : PinStatusResult
  116. {
  117. public DateTime CreationTimeUtc { get; set; }
  118. }
  119. }
  120. }