Exemple #1
0
        /// <inhertitdoc />
        public async Task <AuthenticationResult> AuthenticateAsync(IAuthenticationHandler authenticationHandler, CancellationToken cancellationToken = default)
        {
            if (authenticationHandler == null)
            {
                throw new ArgumentNullException(nameof(authenticationHandler));
            }

            cancellationToken.ThrowIfCancellationRequested();

            ITransport transport = _context.Transport ?? throw new InvalidOperationException("Cannot access transport for authentication.");

            // Read challenge
            ReadOnlyMemory <byte> challengeBytes = await transport.Stream.ReadAllAsync(16, cancellationToken).ConfigureAwait(false);

            // Request password input
            PasswordAuthenticationInput input = await authenticationHandler
                                                .ProvideAuthenticationInputAsync(_context.Connection, this, new PasswordAuthenticationInputRequest()).ConfigureAwait(false);

            ReadOnlyMemory <byte> passwordBytes = Encoding.UTF8.GetBytes(input.Password);

            // Calculate response
            ReadOnlyMemory <byte> response = CreateChallengeResponse(challengeBytes, passwordBytes);

            // Send response
            Debug.Assert(response.Length == 16, "response.Length == 16");
            await transport.Stream.WriteAsync(response, cancellationToken).ConfigureAwait(false);

            return(new AuthenticationResult());
        }