コード例 #1
0
ファイル: ComponentClient.cs プロジェクト: adz21c/Periturf
        public async Task <TokenResponse> RequestDeviceTokenAsync(DeviceTokenRequest request, CancellationToken cancellationToken = default)
        {
            var discovery = await _discoveryCache.GetAsync().ConfigureAwait(false);

            request.Address = discovery.DeviceAuthorizationEndpoint;
            return(await _httpClient.RequestDeviceTokenAsync(request, cancellationToken).ConfigureAwait(false));
        }
コード例 #2
0
        public virtual async Task <ApiResult> PutTokenAsync(string identifier, DeviceTokenRequest request)
        {
            if (!Connectivity.IsConnected)
            {
                return(HandledNotConnected());
            }

            using (var client = new ApiHttpClient())
            {
                var requestMessage = new TokenHttpRequestMessage(request)
                {
                    Method     = HttpMethod.Put,
                    RequestUri = new Uri(client.BaseAddress, string.Concat(ApiRoute, "/identifier/", identifier, "/token")),
                };

                try
                {
                    var response = await client.SendAsync(requestMessage).ConfigureAwait(false);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(await HandleErrorAsync(response).ConfigureAwait(false));
                    }

                    return(ApiResult.Success(response.StatusCode));
                }
                catch (WebException)
                {
                    return(HandledWebException());
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Registers a device token with extended properties with the Urban Airship site, this can be used for new device
        /// tokens and for existing tokens. If a token has become inactive reregistering it will make it active again.
        /// </summary>
        /// <returns>Response from Urban Airship</returns>
        public BaseResponse RegisterDeviceToken(DeviceToken deviceToken)
        {
            if (string.IsNullOrEmpty(deviceToken.Token))
            {
                throw new ArgumentException("A device Tokens Token field is Required", "deviceToken");
            }

            var request = new DeviceTokenRequest(deviceToken, _cfg);

            return(SendRequest(request));
        }
コード例 #4
0
    /// <summary>
    /// Sends a token request using the urn:ietf:params:oauth:grant-type:device_code grant type.
    /// </summary>
    /// <param name="deviceCode">The device code.</param>
    /// <param name="parameters">Extra parameters.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <returns></returns>
    public Task <TokenResponse> RequestDeviceTokenAsync(string deviceCode, Parameters parameters = null, CancellationToken cancellationToken = default)
    {
        var request = new DeviceTokenRequest
        {
            DeviceCode = deviceCode
        };

        ApplyRequestParameters(request, parameters);

        return(_client().RequestDeviceTokenAsync(request, cancellationToken));
    }
コード例 #5
0
        /// <summary>
        /// Registers a device token with extended properties with the Urban Airship site, this can be used for new device
        /// tokens and for existing tokens. If a token has become inactive reregistering it will make it active again.
        /// </summary>
        /// <returns>Response from Urban Airship</returns>
        public BaseResponse RegisterDeviceToken(DeviceToken deviceToken)
        {
            if (string.IsNullOrEmpty(deviceToken.Token))
            {
                throw new ArgumentException("A device Tokens Token field is Required", "deviceToken");
            }

            var deviceRequest = new DeviceTokenRequest(deviceToken);
            var requestTask   = deviceRequest.ExecuteAsync();

            return(requestTask.Result);
        }
コード例 #6
0
        public async Task <IActionResult> UploadFcmToken([FromBody] DeviceTokenRequest request)
        {
            int userId = GetUserId();

            var device = _mapper.Map <Device>(request);

            device.UserId = userId;
            var result = await _devicesManager.StoreDeviceInfoForUser(device);

            if (!result.Success)
            {
                return(BadRequest(result.Message));
            }

            return(Ok(result));
        }
コード例 #7
0
        private static async Task <TokenResponse> RequestTokenAsync(DeviceAuthorizationResponse authorizeResponse)
        {
            var disco = await _cache.GetAsync();

            if (disco.IsError)
            {
                throw new InvalidOperationException(disco.Error);
            }

            using (var httpClient = new HttpClient())
            {
                while (true)
                {
                    using (var deviceTokenRequest = new DeviceTokenRequest())
                    {
                        deviceTokenRequest.Address    = disco.TokenEndpoint;
                        deviceTokenRequest.ClientId   = "device";
                        deviceTokenRequest.DeviceCode = authorizeResponse.DeviceCode;

                        var response = await httpClient.RequestDeviceTokenAsync(deviceTokenRequest);

                        if (response.IsError)
                        {
                            if (response.Error == OidcConstants.TokenErrors.AuthorizationPending || response.Error == OidcConstants.TokenErrors.SlowDown)
                            {
                                WriteLine($"{response.Error}...waiting.");
                                Thread.Sleep(authorizeResponse.Interval * 1000);
                            }
                            else
                            {
                                throw new InvalidOperationException(response.Error);
                            }
                        }
                        else
                        {
                            return(response);
                        }
                    }
                }
            }
        }
コード例 #8
0
 public Task PutDeviceTokenAsync(string identifier, DeviceTokenRequest request)
 {
     return(SendAsync <DeviceTokenRequest, object>(
                HttpMethod.Put, $"/devices/identifier/{identifier}/token", request, true, false));
 }
コード例 #9
0
    /// <summary>
    /// Sends a token request using the urn:ietf:params:oauth:grant-type:device_code grant type.
    /// </summary>
    /// <param name="client">The client.</param>
    /// <param name="request">The request.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    /// <returns></returns>
    public static async Task <TokenResponse> RequestDeviceTokenAsync(this HttpMessageInvoker client, DeviceTokenRequest request, CancellationToken cancellationToken = default)
    {
        var clone = request.Clone();

        clone.Parameters.AddRequired(OidcConstants.TokenRequest.GrantType, OidcConstants.GrantTypes.DeviceCode);
        clone.Parameters.AddRequired(OidcConstants.TokenRequest.DeviceCode, request.DeviceCode);

        return(await client.RequestTokenAsync(clone, cancellationToken).ConfigureAwait());
    }