Exemple #1
0
        public async Task <IAdalResult> AcquireTokenAsync(string authorityHostUrl, string resource, string clientId, Uri redirectUri, string extraQueryParameters)
        {
            object output = null;

            try
            {
                output = await _adal.AcquireTokenAsync(authorityHostUrl, resource, clientId, redirectUri, extraQueryParameters);
            }
            catch (Exception adalException)
            {
                output = adalException;
            }

            var input = new CapturedAdalInput
            {
                ClientId             = clientId,
                ExtraQueryParameters = extraQueryParameters,
                RedirectUrl          = redirectUri?.ToString(),
                Resource             = resource,
            };

            Capture(authorityHostUrl, input, output);

            switch (output)
            {
            case Exception exception:
                throw exception;

            case IAdalResult result:
                return(result);
            }

            throw new InvalidOperationException($"Unexpected result from `{nameof(AcquireTokenAsync)}`.");
        }
Exemple #2
0
        internal bool GetCapturedData(ICapturedDataFilter filter, out CapturedAdalData capturedData)
        {
            if (filter is null)
            {
                throw new ArgumentNullException(nameof(filter));
            }

            var operations = new List <CapturedAdalOperation>(_captures.Count);

            foreach (var capture in _captures)
            {
                var operation = new CapturedAdalOperation
                {
                    AuthorityUrl = filter.ApplyFilter(capture.AuthorityUrl),
                    Error        = new CapturedAdalException
                    {
                        Message = filter.ApplyFilter(capture.Error.Message),
                    },
                    Input = new CapturedAdalInput
                    {
                        ClientId             = filter.ApplyFilter(capture.Input.ClientId),
                        ExtraQueryParameters = filter.ApplyFilter(capture.Input.ExtraQueryParameters),
                        RedirectUrl          = filter.ApplyFilter(capture.Input.RedirectUrl),
                        Resource             = filter.ApplyFilter(capture.Input.Resource),
                    },
                    Result = new CapturedAdalResult
                    {
                        AccessToken = capture.Result.AccessToken is null ? null : CapturedAdalAccessToken,
                        Authority   = filter.ApplyFilter(capture.Result.Authority),
                        TenantId    = capture.Result.TenantId,
                        TokenType   = capture.Result.TokenType,
                    },
                };

                operations.Add(operation);
            }

            capturedData = new CapturedAdalData
            {
                Operations = operations,
            };

            return(true);
        }
Exemple #3
0
        private void Capture(string authorityUrl, CapturedAdalInput input, object output)
        {
            if (authorityUrl is null)
            {
                throw new ArgumentNullException(nameof(authorityUrl));
            }

            _context.Trace.WriteLine($"{nameof(CaptureAdal)}: \"{nameof(authorityUrl)}\".");

            lock (_syncpoint)
            {
                var capture = new CapturedAdalOperation
                {
                    AuthorityUrl = authorityUrl,
                    Input        = input,
                };

                if (output is Exception error)
                {
                    capture.Error = new CapturedAdalException
                    {
                        Message = error.Message,
                    };
                }
                else if (output is IAdalResult result)
                {
                    capture.Result = new CapturedAdalResult
                    {
                        AccessToken = capture.Result.AccessToken,
                        Authority   = result.Authority,
                        TenantId    = result.TenantId,
                        TokenType   = result.AccessTokenType,
                    };
                }

                _captures.Add(capture);
            }
        }