public async Task <IOauthGrantAuthenticationResult> PasswordGrantAsync(
            IOwinEnvironment environment,
            IApplication application,
            string login,
            string password,
            CancellationToken cancellationToken)
        {
            var preLoginHandlerContext = new PreLoginContext(environment)
            {
                Login = login
            };

            await _handlers.PreLoginHandler(preLoginHandlerContext, cancellationToken);

            var passwordGrantRequest = OauthRequests.NewPasswordGrantRequest()
                                       .SetLogin(preLoginHandlerContext.Login)
                                       .SetPassword(password);

            if (preLoginHandlerContext.AccountStore != null)
            {
                passwordGrantRequest.SetAccountStore(preLoginHandlerContext.AccountStore);
            }

            var passwordGrantAuthenticator = application.NewPasswordGrantAuthenticator();
            var grantResult = await passwordGrantAuthenticator
                              .AuthenticateAsync(passwordGrantRequest.Build(), cancellationToken);

            return(grantResult);
        }
        public async Task <IOauthGrantAuthenticationResult> PasswordGrantAsync(
            IOwinEnvironment environment,
            IApplication application,
            Func <string, CancellationToken, Task> errorHandler,
            string login,
            string password,
            CancellationToken cancellationToken)
        {
            var preLoginHandlerContext = new PreLoginContext(environment)
            {
                Login = login
            };

            await _handlers.PreLoginHandler(preLoginHandlerContext, cancellationToken);

            if (preLoginHandlerContext.Result != null)
            {
                if (!preLoginHandlerContext.Result.Success)
                {
                    var message = string.IsNullOrEmpty(preLoginHandlerContext.Result.ErrorMessage)
                        ? "An error has occurred. Please try again."
                        : preLoginHandlerContext.Result.ErrorMessage;
                    await errorHandler(message, cancellationToken);

                    return(null);
                }
            }

            var passwordGrantRequest = OauthRequests.NewPasswordGrantRequest()
                                       .SetLogin(preLoginHandlerContext.Login)
                                       .SetPassword(password);

            if (preLoginHandlerContext.AccountStore != null)
            {
                passwordGrantRequest.SetAccountStore(preLoginHandlerContext.AccountStore);
            }

            if (!string.IsNullOrEmpty(preLoginHandlerContext.OrganizationNameKey))
            {
                passwordGrantRequest.SetOrganizationNameKey(preLoginHandlerContext.OrganizationNameKey);
            }

            var passwordGrantAuthenticator = application.NewPasswordGrantAuthenticator();
            var grantResult = await passwordGrantAuthenticator
                              .AuthenticateAsync(passwordGrantRequest.Build(), cancellationToken);

            return(grantResult);
        }
        public async Task <IOauthGrantAuthenticationResult> ClientCredentialsGrantAsync(
            IOwinEnvironment environment,
            IApplication application,
            Func <AbstractError, CancellationToken, Task> errorHandler,
            string id,
            string secret,
            CancellationToken cancellationToken)
        {
            var preLoginHandlerContext = new PreLoginContext(environment)
            {
                Login = id
            };

            await _handlers.PreLoginHandler(preLoginHandlerContext, cancellationToken);

            if (preLoginHandlerContext.Result != null)
            {
                if (!preLoginHandlerContext.Result.Success)
                {
                    var message = string.IsNullOrEmpty(preLoginHandlerContext.Result.ErrorMessage)
                        ? "An error has occurred. Please try again."
                        : preLoginHandlerContext.Result.ErrorMessage;
                    await errorHandler(new BadRequest(message), cancellationToken);

                    return(null);
                }
            }

            var request = new ClientCredentialsGrantRequest
            {
                Id     = id,
                Secret = secret
            };

            if (preLoginHandlerContext.AccountStore != null)
            {
                request.AccountStoreHref = preLoginHandlerContext.AccountStore.Href;
            }

            if (!string.IsNullOrEmpty(preLoginHandlerContext.OrganizationNameKey))
            {
                request.OrganizationNameKey = preLoginHandlerContext.OrganizationNameKey;
            }

            IOauthGrantAuthenticationResult tokenResult;

            try
            {
                tokenResult = await application
                              .ExecuteOauthRequestAsync(request, cancellationToken)
                              .ConfigureAwait(false);
            }
            // Catch error 10019 (API Authentication failed)
            catch (ResourceException rex) when(rex.Code == 10019)
            {
                await errorHandler(new OauthInvalidClient(), cancellationToken);

                return(null);
            }

            return(tokenResult);
        }