public async Task <AuthenticationData> RequestPasswordTokenAsync(string username, string password,
                                                                         CommonConstants.ApiScopes apiScopes)
        {
            ArgumentValidation.ValidateString(nameof(username), username);
            ArgumentValidation.ValidateString(nameof(password), password);

            var disco = await GetDiscoveryResponse();

            var client   = new HttpClient();
            var response = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
            {
                Address = disco.TokenEndpoint,

                ClientId     = Options.ClientId,
                ClientSecret = Options.ClientSecret,

                UserName = username,
                Password = password,

                Scope = CommonConstants.GetScopes(apiScopes,
                                                  CommonConstants.DefaultScopes.UserDefault | CommonConstants.DefaultScopes.OfflineAccess)
            });

            ValidateResponse(response);

            return(new AuthenticationData
            {
                AccessToken = response.AccessToken,
                RefreshToken = response.RefreshToken,
                ExpiresAt = DateTime.Now.AddSeconds(response.ExpiresIn)
            });
        }
        public async Task <AuthenticationData> RequestClientCredentialsTokenAsync(CommonConstants.ApiScopes apiScopes,
                                                                                  CommonConstants.DefaultScopes defaultScopes)
        {
            var disco = await GetDiscoveryResponse();

            var client   = new HttpClient();
            var response = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
            {
                GrantType = OidcConstants.GrantTypes.ClientCredentials,

                Address = disco.TokenEndpoint,

                ClientId     = Options.ClientId,
                ClientSecret = Options.ClientSecret,

                Scope = CommonConstants.GetScopes(apiScopes, defaultScopes)
            });

            ValidateResponse(response);

            return(new AuthenticationData
            {
                AccessToken = response.AccessToken,
                RefreshToken = response.RefreshToken,
                ExpiresAt = DateTime.Now.AddSeconds(response.ExpiresIn)
            });
        }
        public async Task <DeviceAuthenticationRequestData> RequestDeviceAuthorizationAsync(
            CommonConstants.ApiScopes apiScopes)
        {
            var disco = await GetDiscoveryResponse();

            var client   = new HttpClient();
            var response = await client.RequestDeviceAuthorizationAsync(new DeviceAuthorizationRequest
            {
                Address = disco.DeviceAuthorizationEndpoint,

                ClientId     = Options.ClientId,
                ClientSecret = Options.ClientSecret,

                Scope = CommonConstants.GetScopes(apiScopes,
                                                  CommonConstants.DefaultScopes.UserDefault | CommonConstants.DefaultScopes.OfflineAccess)
            });

            ValidateResponse(response);

            return(new DeviceAuthenticationRequestData
            {
                UserCode = response.UserCode,
                DeviceCode = response.DeviceCode,
                VerificationUri = response.VerificationUri,
                VerificationUriComplete = response.VerificationUriComplete,
                PollingInterval = response.Interval,
                ExpiresAt = response.ExpiresIn.HasValue
                    ? DateTime.Now.AddSeconds(response.ExpiresIn.Value)
                    : default(DateTime?)
            });
        }