aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/PinLoginService.cs
blob: a4957651fb7925345a9c9c5cbe61ea4cc7a8dd0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Threading.Tasks;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Session;
using MediaBrowser.Model.Connect;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Session;
using ServiceStack;

namespace MediaBrowser.Api
{
    [Route("/Auth/Pin", "POST", Summary = "Creates a pin request")]
    public class CreatePinRequest : IReturn<PinCreationResult>
    {
        [ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string DeviceId { get; set; }
        [ApiMember(Name = "AppName", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string AppName { get; set; }
    }

    [Route("/Auth/Pin", "GET", Summary = "Gets pin status")]
    public class GetPinStatusRequest : IReturn<PinStatusResult>
    {
        [ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
        public string DeviceId { get; set; }
        [ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
        public string Pin { get; set; }
    }

    [Route("/Auth/Pin/Exchange", "POST", Summary = "Exchanges a pin")]
    public class ExchangePinRequest : IReturn<PinExchangeResult>
    {
        [ApiMember(Name = "DeviceId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string DeviceId { get; set; }
        [ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string Pin { get; set; }
    }

    [Route("/Auth/Pin/Validate", "POST", Summary = "Validates a pin")]
    [Authenticated]
    public class ValidatePinRequest : IReturn<SessionInfoDto>
    {
        [ApiMember(Name = "Pin", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
        public string Pin { get; set; }
    }

    public class PinLoginService : BaseApiService
    {
        private static readonly ConcurrentDictionary<string, MyPinStatus> _activeRequests = new ConcurrentDictionary<string, MyPinStatus>(StringComparer.OrdinalIgnoreCase);
        private readonly ISessionManager _sessionManager;
        private readonly IUserManager _userManager;

        public PinLoginService(ISessionManager sessionManager, IUserManager userManager)
        {
            _sessionManager = sessionManager;
            _userManager = userManager;
        }

        public object Post(CreatePinRequest request)
        {
            if (string.IsNullOrWhiteSpace(request.DeviceId))
            {
                throw new ArgumentNullException("DeviceId");
            }
            if (string.IsNullOrWhiteSpace(request.AppName))
            {
                throw new ArgumentNullException("AppName");
            }

            var pin = GetNewPin();

            var value = new MyPinStatus
            {
                CreationTimeUtc = DateTime.UtcNow,
                IsConfirmed = false,
                IsExpired = false,
                Pin = pin,
                DeviceId = request.DeviceId,
                AppName = request.AppName
            };

            _activeRequests.AddOrUpdate(pin, value, (k, v) => value);

            return ToOptimizedResult(new PinCreationResult
            {
                DeviceId = request.DeviceId,
                IsConfirmed = false,
                IsExpired = false,
                Pin = pin
            });
        }

        public object Get(GetPinStatusRequest request)
        {
            MyPinStatus status;

            if (!_activeRequests.TryGetValue(request.Pin, out status))
            {
                Logger.Debug("Pin {0} not found.", request.Pin);
                throw new ResourceNotFoundException();
            }

            EnsureValid(request.DeviceId, status);

            return ToOptimizedResult(new PinStatusResult
            {
                Pin = status.Pin,
                IsConfirmed = status.IsConfirmed,
                IsExpired = status.IsExpired
            });
        }

        public async Task<object> Post(ExchangePinRequest request)
        {
            MyPinStatus status;

            if (!_activeRequests.TryGetValue(request.Pin, out status))
            {
                Logger.Debug("Pin {0} not found.", request.Pin);
                throw new ResourceNotFoundException();
            }

            EnsureValid(request.DeviceId, status);

            if (!status.IsConfirmed)
            {
                throw new ResourceNotFoundException();
            }

            var auth = AuthorizationContext.GetAuthorizationInfo(Request);
            var user = _userManager.GetUserById(status.UserId);

            var result = await _sessionManager.CreateNewSession(new AuthenticationRequest
            {
                App = auth.Client,
                AppVersion = auth.Version,
                DeviceId = auth.DeviceId,
                DeviceName = auth.Device,
                RemoteEndPoint = Request.RemoteIp,
                Username = user.Name

            }).ConfigureAwait(false);

            return ToOptimizedResult(result);
        }

        public object Post(ValidatePinRequest request)
        {
            MyPinStatus status;

            if (!_activeRequests.TryGetValue(request.Pin, out status))
            {
                throw new ResourceNotFoundException();
            }

            EnsureValid(status);

            status.IsConfirmed = true;
            status.UserId = AuthorizationContext.GetAuthorizationInfo(Request).UserId;

            return ToOptimizedResult(new ValidatePinResult
            {
                AppName = status.AppName
            });
        }

        private void EnsureValid(string requestedDeviceId, MyPinStatus status)
        {
            if (!string.Equals(requestedDeviceId, status.DeviceId, StringComparison.OrdinalIgnoreCase))
            {
                Logger.Debug("Pin device Id's do not match. requestedDeviceId: {0}, status.DeviceId: {1}", requestedDeviceId, status.DeviceId);
                throw new ResourceNotFoundException();
            }

            EnsureValid(status);
        }

        private void EnsureValid(MyPinStatus status)
        {
            if ((DateTime.UtcNow - status.CreationTimeUtc).TotalMinutes > 10)
            {
                status.IsExpired = true;
            }

            if (status.IsExpired)
            {
                Logger.Debug("Pin {0} is expired", status.Pin);
                throw new ResourceNotFoundException();
            }
        }

        private string GetNewPin()
        {
            var pin = GetNewPinInternal();

            while (IsPinActive(pin))
            {
                pin = GetNewPinInternal();
            }

            return pin;
        }

        private string GetNewPinInternal()
        {
            return new Random().Next(10000, 99999).ToString(CultureInfo.InvariantCulture);
        }

        private bool IsPinActive(string pin)
        {
            MyPinStatus status;

            if (!_activeRequests.TryGetValue(pin, out status))
            {
                return false;
            }

            if (status.IsExpired)
            {
                return false;
            }

            return true;
        }

        public class MyPinStatus : PinStatusResult
        {
            public DateTime CreationTimeUtc { get; set; }
            public string DeviceId { get; set; }
            public string UserId { get; set; }
            public string AppName { get; set; }
        }
    }

    public class ValidatePinResult
    {
        public string AppName { get; set; }
    }
}