Exemplo n.º 1
0
 public static string ToJson(this object obj)
 {
     return(JsonHelper.Serialize(obj));
 }
Exemplo n.º 2
0
        /// <summary>
        /// Makes a token request. This will make a token request now, even if the library already
        /// has a valid token. It would typically be used to issue tokens for use by other clients.
        /// </summary>
        /// <param name="tokenParams">The <see cref="TokenRequest"/> data used for the token</param>
        /// <param name="options">Extra <see cref="AuthOptions"/> used for creating a token </param>
        /// <returns>A valid ably token</returns>
        /// <exception cref="AblyException"></exception>
        public virtual async Task <TokenDetails> RequestTokenAsync(TokenParams tokenParams = null, AuthOptions options = null)
        {
            var mergedOptions = options != null?options.Merge(Options) : Options;

            string keyId = "", keyValue = "";

            if (mergedOptions.Key.IsNotEmpty())
            {
                var key = mergedOptions.ParseKey();
                keyId    = key.KeyName;
                keyValue = key.KeySecret;
            }

            var @params = MergeTokenParamsWithDefaults(tokenParams);

            if (mergedOptions.QueryTime.GetValueOrDefault(false))
            {
                @params.Timestamp = await _rest.TimeAsync();
            }

            EnsureSecureConnection();

            var request = _rest.CreatePostRequest($"/keys/{keyId}/requestToken");

            request.SkipAuthentication = true;
            TokenRequest postData = null;

            if (mergedOptions.AuthCallback != null)
            {
                var callbackResult = await mergedOptions.AuthCallback(@params);

                if (callbackResult == null)
                {
                    throw new AblyException("AuthCallback returned null");
                }

                if (callbackResult is TokenDetails)
                {
                    return(callbackResult as TokenDetails);
                }

                if (callbackResult is TokenRequest || callbackResult is string)
                {
                    postData = GetTokenRequest(callbackResult);

                    request.Url = $"/keys/{postData.KeyName}/requestToken";
                }

                else
                {
                    throw new AblyException($"AuthCallback returned an unsupported type ({callbackResult.GetType()}. Expected either TokenDetails or TokenRequest");
                }
            }
            else if (mergedOptions.AuthUrl.IsNotEmpty())
            {
                var response = await CallAuthUrl(mergedOptions, @params);

                if (response.Type == ResponseType.Text) //Return token string
                {
                    return(new TokenDetails(response.TextResponse, Now));
                }

                var signedData = response.TextResponse;
                var jData      = JObject.Parse(signedData);

                if (TokenDetails.IsToken(jData))
                {
                    return(JsonHelper.DeserializeObject <TokenDetails>(jData));
                }

                postData = JsonHelper.Deserialize <TokenRequest>(signedData);

                request.Url = $"/keys/{postData.KeyName}/requestToken";
            }
            else
            {
                if (keyId.IsEmpty() || keyValue.IsEmpty())
                {
                    throw new AblyException("TokenAuth is on but there is no way to generate one");
                }

                postData = new TokenRequest(Now).Populate(@params, keyId, keyValue);
            }

            request.PostData = postData;

            TokenDetails result = await _rest.ExecuteRequest <TokenDetails>(request);

            if (result == null)
            {
                throw new AblyException(new ErrorInfo("Invalid token response returned", 500));
            }

            return(result);
        }