Example #1
0
        /// <summary>
        /// Retrieve a token from the STS service for the specified account identifier asynchronously.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns><see cref="Response{AccessToken}"/>.</returns>
        public virtual async Task <Response <AccessToken> > GetTokenAsync(CancellationToken cancellationToken = default)
        {
            MixedRealityTokenRequestOptions headerOptions = MixedRealityTokenRequestOptions.GenerateNew();

            using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(MixedRealityStsClient)}.{nameof(GetToken)}");
            scope.AddAttribute(nameof(headerOptions.ClientRequestId), headerOptions.ClientRequestId);
            scope.Start();

            try
            {
                ResponseWithHeaders <StsTokenResponseMessage, MixedRealityStsGetTokenHeaders> response = await _restClient.GetTokenAsync(AccountId, headerOptions, cancellationToken).ConfigureAwait(false);

                return(ResponseWithHeaders.FromValue(response.Value.ToAccessToken(), response.Headers, response.GetRawResponse()));
            }
            catch (Exception ex)
            {
                scope.Failed(ex);
                throw;
            }
        }
Example #2
0
        internal HttpMessage CreateGetTokenRequest(Guid accountId, MixedRealityTokenRequestOptions tokenRequestOptions)
        {
            var message = _pipeline.CreateMessage();
            var request = message.Request;

            request.Method = RequestMethod.Get;
            var uri = new RawRequestUriBuilder();

            uri.Reset(_endpoint);
            uri.AppendPath("/Accounts/", false);
            uri.AppendPath(accountId, true);
            uri.AppendPath("/token", false);
            uri.AppendQuery("api-version", _apiVersion, true);
            request.Uri = uri;
            if (tokenRequestOptions?.ClientRequestId != null)
            {
                request.Headers.Add("X-MRC-CV", tokenRequestOptions.ClientRequestId);
            }
            request.Headers.Add("Accept", "application/json");
            return(message);
        }
        public async Task <ResponseWithHeaders <StsTokenResponseMessage, MixedRealityStsGetTokenHeaders> > GetTokenAsync(Guid accountId, MixedRealityTokenRequestOptions tokenRequestOptions = null, CancellationToken cancellationToken = default)
        {
            using var message = CreateGetTokenRequest(accountId, tokenRequestOptions);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            var headers = new MixedRealityStsGetTokenHeaders(message.Response);

            switch (message.Response.Status)
            {
            case 200:
            {
                StsTokenResponseMessage value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = StsTokenResponseMessage.DeserializeStsTokenResponseMessage(document.RootElement);
                return(ResponseWithHeaders.FromValue(value, headers, message.Response));
            }