Ejemplo n.º 1
0
        private async Task AddAuthenticationInfoFromConnect(ServerInfo server,
                                                            ConnectionMode connectionMode,
                                                            ServerCredentials credentials,
                                                            CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(credentials.ConnectUserId))
            {
                throw new ArgumentException("server");
            }

            if (string.IsNullOrWhiteSpace(server.ExchangeToken))
            {
                throw new ArgumentException("server");
            }

            _logger.Debug("Adding authentication info from Connect");

            var url = server.GetAddress(connectionMode);

            url += "/emby/Connect/Exchange?format=json&ConnectUserId=" + credentials.ConnectUserId;

            var headers = new HttpHeaders();

            headers.SetAccessToken(server.ExchangeToken);
            headers["X-Emby-Authorization"] = "MediaBrowser Client=\"" + ApplicationName + "\", Device=\"" + Device.DeviceName + "\", DeviceId=\"" + Device.DeviceId + "\", Version=\"" + ApplicationVersion + "\"";

            try
            {
                using (var stream = await _httpClient.SendAsync(new HttpRequest
                {
                    CancellationToken = cancellationToken,
                    Method = "GET",
                    RequestHeaders = headers,
                    Url = url
                }).ConfigureAwait(false))
                {
                    var auth = JsonSerializer.DeserializeFromStream <ConnectAuthenticationExchangeResult>(stream);

                    server.UserId      = auth.LocalUserId;
                    server.AccessToken = auth.AccessToken;
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception ex)
            {
                // Already logged at a lower level

                server.UserId      = null;
                server.AccessToken = null;
            }
        }
Ejemplo n.º 2
0
        private async Task ValidateAuthentication(ServerInfo server, ConnectionOptions options, CancellationToken cancellationToken)
        {
            _logger.LogDebug("Validating saved authentication");

            var url = server.Address;

            var headers = new HttpHeaders();

            headers.SetAccessToken(server.AccessToken);

            var request = new HttpRequest
            {
                CancellationToken = cancellationToken,
                Method            = "GET",
                RequestHeaders    = headers,
                Url = new Uri(url, "/emby/system/info?format=json")
            };

            try
            {
                using (var stream = await _httpClient.SendAsync(request).ConfigureAwait(false))
                {
                    var systemInfo = JsonSerializer.DeserializeFromStream <SystemInfo>(stream);

                    server.ImportInfo(systemInfo);
                }

                if (server.UserId != Guid.Empty)
                {
                    request.Url = new Uri(url, "/mediabrowser/users/" + server.UserId + "?format=json");

                    using (var stream = await _httpClient.SendAsync(request).ConfigureAwait(false))
                    {
                        var localUser = JsonSerializer.DeserializeFromStream <UserDto>(stream);

                        OnLocalUserSignIn(options, localUser);
                    }
                }
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception)
            {
                // Already logged at a lower level

                server.UserId      = Guid.Empty;
                server.AccessToken = null;
            }
        }
        public async Task <PinStatusResult> GetPinStatus(PinCreationResult pin)
        {
            var dict = new QueryStringDictionary();

            dict.Add("deviceId", pin.DeviceId);
            dict.Add("pin", pin.Pin);

            var url = GetConnectUrl("pin") + "?" + dict.GetQueryString();

            var request = new HttpRequest
            {
                Method = "GET",
                Url    = url
            };

            AddAppInfo(request, _appName, _appVersion);

            using (var stream = await _httpClient.SendAsync(request).ConfigureAwait(false))
            {
                return(JsonSerializer.DeserializeFromStream <PinStatusResult>(stream));
            }
        }
Ejemplo n.º 4
0
 protected async Task <Stream> SendAsync(HttpRequest request)
 {
     return(await HttpClient.SendAsync(request).ConfigureAwait(false));
 }