Example #1
0
        private async Task <AuthenticationResultEx> SendHttpMessageAsync(IRequestParameters requestParameters)
        {
            client = new AdalHttpClient(this.Authenticator.TokenUri, this.CallState)
            {
                Client = { BodyParameters = requestParameters }
            };
            TokenResponse tokenResponse = await client.GetResponseAsync <TokenResponse>().ConfigureAwait(false);

            return(tokenResponse.GetResult());
        }
Example #2
0
        private async Task <AuthenticationResultEx> SendHttpMessageAsync(IRequestParameters requestParameters)
        {
            var client = new AdalHttpClient(this.Authenticator.TokenUri, this.CallState)
            {
                Client = { BodyParameters = requestParameters }
            };
            TokenResponse tokenResponse = await client.GetResponseAsync <TokenResponse>(ClientMetricsEndpointType.Token);

            return(tokenResponse.GetResult());
        }
        internal static async Task <UserRealmDiscoveryResponse> CreateByDiscoveryAsync(string userRealmUri, string userName, CallState callState)
        {
            string userRealmEndpoint = userRealmUri;

            userRealmEndpoint += (userName + "?api-version=1.0");

            PlatformPlugin.Logger.Information(callState, string.Format(CultureInfo.CurrentCulture, " Sending user realm discovery request to '{0}'", userRealmEndpoint));

            var client = new AdalHttpClient(userRealmEndpoint, callState)
            {
                Client = { Accept = "application/json" }
            };

            return(await client.GetResponseAsync <UserRealmDiscoveryResponse>(ClientMetricsEndpointType.UserRealmDiscovery));
        }
Example #4
0
        internal async Task <DeviceCodeResult> RunHandlerAsync()
        {
            await this.authenticator.UpdateFromTemplateAsync(this.callState);

            this.ValidateAuthorityType();
            AdalHttpClient     client   = new AdalHttpClient(CreateDeviceCodeRequestUriString(), this.callState);
            DeviceCodeResponse response = await client.GetResponseAsync <DeviceCodeResponse>();

            if (!string.IsNullOrEmpty(response.Error))
            {
                throw new AdalException(response.Error, response.ErrorDescription);
            }

            return(response.GetResult(clientKey.ClientId, resource));
        }
Example #5
0
        internal static async Task <UserRealmDiscoveryResponse> CreateByDiscoveryAsync(string userRealmUri, string userName, CallState callState)
        {
            string userRealmEndpoint = userRealmUri;

            userRealmEndpoint += (userName + "?api-version=1.0");

            callState.Logger.Information(callState, "Sending request to userrealm endpoint.");

            var client = new AdalHttpClient(userRealmEndpoint, callState)
            {
                Client = { Accept = "application/json" }
            };

            return(await client.GetResponseAsync <UserRealmDiscoveryResponse>().ConfigureAwait(false));
        }
        // No return value. Modifies InstanceCache directly.
        private static async Task DiscoverAsync(Uri authority, bool validateAuthority, CallState callState)
        {
            string instanceDiscoveryEndpoint = string.Format(
                CultureInfo.InvariantCulture,
                "https://{0}/common/discovery/instance?api-version=1.1&authorization_endpoint={1}",
                WhitelistedAuthorities.Contains(authority.Host) ? authority.Host : DefaultTrustedAuthority,
                FormatAuthorizeEndpoint(authority.Host, GetTenant(authority)));
            var client = new AdalHttpClient(instanceDiscoveryEndpoint, callState);
            InstanceDiscoveryResponse discoveryResponse = null;

            try
            {
                discoveryResponse = await client.GetResponseAsync <InstanceDiscoveryResponse>().ConfigureAwait(false);

                if (validateAuthority && discoveryResponse.TenantDiscoveryEndpoint == null)
                {
                    // hard stop here
                    throw new AdalException(AdalError.AuthorityNotInValidList);
                }
            }
            catch (AdalServiceException ex)
            {
                // The pre-existing implementation (https://github.com/AzureAD/azure-activedirectory-library-for-dotnet/pull/796/files#diff-e4febd8f40f03e71bcae0f990f9690eaL99)
                // has been coded in this way: it catches the AdalServiceException and then translate it into 2 validation-relevant exceptions.
                // So the following implementation absorbs these specific exceptions when the validateAuthority flag is false.
                // All other unexpected exceptions will still bubble up, as always.
                if (validateAuthority)
                {
                    // hard stop here
                    throw new AdalException(
                              (ex.ErrorCode == "invalid_instance")
                            ? AdalError.AuthorityNotInValidList
                            : AdalError.AuthorityValidationFailed, ex);
                }
            }

            foreach (var entry in discoveryResponse?.Metadata ?? Enumerable.Empty <InstanceDiscoveryMetadataEntry>())
            {
                foreach (var aliasedAuthority in entry?.Aliases ?? Enumerable.Empty <string>())
                {
                    InstanceCache.TryAdd(aliasedAuthority, entry);
                }
            }

            AddMetadataEntry(authority.Host);
        }
        public async Task VerifyAnotherHostByInstanceDiscoveryAsync(string host, string tenant, CallState callState)
        {
            string instanceDiscoveryEndpoint = this.InstanceDiscoveryEndpoint;

            instanceDiscoveryEndpoint += ("?api-version=1.0&authorization_endpoint=" + AuthorizeEndpointTemplate);
            instanceDiscoveryEndpoint  = instanceDiscoveryEndpoint.Replace("{host}", host);
            instanceDiscoveryEndpoint  = instanceDiscoveryEndpoint.Replace("{tenant}", tenant);

            try
            {
                var client = new AdalHttpClient(instanceDiscoveryEndpoint, callState);
                InstanceDiscoveryResponse discoveryResponse = await client.GetResponseAsync <InstanceDiscoveryResponse>().ConfigureAwait(false);

                if (discoveryResponse.TenantDiscoveryEndpoint == null)
                {
                    throw new AdalException(AdalError.AuthorityNotInValidList);
                }
            }
            catch (AdalServiceException ex)
            {
                throw new AdalException((ex.ErrorCode == "invalid_instance") ? AdalError.AuthorityNotInValidList : AdalError.AuthorityValidationFailed, ex);
            }
        }