private static Dictionary <string, string> CreateParameters(PartiesRequestArgs args)
        {
            var dictionary = new Dictionary <string, string>();

            void AddParameterIfExists(string name, string value)
            {
                if (value != null)
                {
                    dictionary.Add(name, value);
                }
            }

            void AddParameter(string name, bool?value)
            {
                if (value.HasValue)
                {
                    dictionary.Add(name, value.ToString().ToLower());
                }
            }

            AddParameterIfExists("name", args.Name);
            AddParameterIfExists("eori", args.Eori);
            AddParameter("certified_only", args.CertifiedOnly);
            AddParameter("active_only", args.ActiveOnly);
            AddParameterIfExists("certificate_subject_name", args.CertificateSubjectName);
            AddParameterIfExists("page", args.Page?.ToString());
            AddParameterIfExists("date_time", args.DateTime == null ? null : args.DateTime.Value.ToString("s") + "Z");

            return(dictionary);
        }
        public async Task GetAsync_AllArgumentsExist_MapsAndInvokesClient()
        {
            var args = new PartiesRequestArgs(
                "accessToken",
                "name",
                "eori",
                certifiedOnly: true,
                activeOnly: true,
                "certificateSubjectName",
                page: 10,
                new DateTime(2020, 12, 11, 10, 9, 8));

            await _sut.GetAsync(args);

            _clientMock.Verify(
                x => x.SendRequestAsync(
                    It.Is <TokenSendRequestArgs>(x =>
                                                 x.AccessToken == "accessToken" &&
                                                 x.RequestUri == "https://scheme.isharetest.net/parties" &&
                                                 x.Parameters["name"] == "name" &&
                                                 x.Parameters["eori"] == "eori" &&
                                                 x.Parameters["certified_only"] == "true" &&
                                                 x.Parameters["active_only"] == "true" &&
                                                 x.Parameters["certificate_subject_name"] == "certificateSubjectName" &&
                                                 x.Parameters["page"] == "10" &&
                                                 x.Parameters["date_time"] == "2020-12-11T10:09:08Z"),
                    It.IsAny <CancellationToken>()));
        }
Example #3
0
        private async Task <bool> DoesCertificateBelongToParty(
            CertificateValidationArgs validationArgs,
            string accessToken,
            CancellationToken cancellationToken)
        {
            var subjectName = validationArgs.PartyCertificate.SubjectName.Name;
            var args        = new PartiesRequestArgs(
                accessToken,
                eori: validationArgs.PartyEori,
                certificateSubjectName: subjectName);

            var response = await _partiesQueryService.GetAsync(args, cancellationToken);

            if (response.Count != 1)
            {
                _logger.LogWarning(
                    "Parties response count for eori: {eori} and subject: {subjectName} was not 1. Count: {count}.",
                    validationArgs.PartyEori,
                    subjectName,
                    response.Count);

                return(false);
            }

            var adherence = response.Parties.First().Adherence;

            return(adherence.Status == "Active" &&
                   adherence.StartDate < DateTime.UtcNow &&
                   (!adherence.EndDate.HasValue || adherence.EndDate > DateTime.UtcNow));
        }
        public void GetAsync_RetrievedTokenIsInvalid_Throws()
        {
            var args = new PartiesRequestArgs("accessToken", "*");

            _tokenResponseValidatorMock.Setup(x => x.IsValid(It.IsAny <AssertionModel>())).Returns(false);

            Func <Task> act = () => _sut.GetAsync(args);

            act.Should().Throw <UnsuccessfulResponseException>();
        }
        public void GetAsync_SomethingThrows_Throws()
        {
            var args = new PartiesRequestArgs("accessToken", "*");

            _clientMock
            .Setup(x => x.SendRequestAsync(It.IsAny <TokenSendRequestArgs>(), It.IsAny <CancellationToken>()))
            .Throws(new Exception());

            Func <Task> act = () => _sut.GetAsync(args);

            act.Should().Throw <UnsuccessfulResponseException>();
        }
        public async Task GetAsync_ArgumentsDoNotExist_MapsAndInvokesClient()
        {
            var args = new PartiesRequestArgs("accessToken", "*");

            await _sut.GetAsync(args);

            _clientMock.Verify(
                x => x.SendRequestAsync(
                    It.Is <TokenSendRequestArgs>(x =>
                                                 x.AccessToken == "accessToken" &&
                                                 x.RequestUri == "https://scheme.isharetest.net/parties" &&
                                                 x.Parameters.Count == 1),
                    It.IsAny <CancellationToken>()));
        }
        public async Task GetAsync_SuccessfulRequest_ReturnsResponse()
        {
            var args = new PartiesRequestArgs("accessToken", "*");

            var result = await _sut.GetAsync(args);

            result.Count.Should().Be(1);
            var party = result.Parties.First();

            party.PartyId.Should().Be("EU.EORI.NL000000004");
            party.PartyName.Should().Be("AskMeAnything Authorization Registry");
            party.CapabilityUrl.Should().Be("https://ar.isharetest.net/capabilities");
            party.Adherence.Status.Should().Be("Active");
            party.Adherence.StartDate.Should().Be(new DateTime(2018, 04, 26));
            party.Adherence.EndDate.Should().Be(new DateTime(2020, 07, 25));
            party.Certifications.Should().HaveCount(1);
            var certification = party.Certifications.First();

            certification.StartDate.Should().Be(new DateTime(2018, 01, 04));
            certification.EndDate.Should().Be(new DateTime(2020, 02, 02));
            certification.Role.Should().Be("AuthorisationRegistry");
            certification.LoA.Should().Be(LevelOfAssurance.High);
        }
        public async Task <PartiesResponse> GetAsync(PartiesRequestArgs args, CancellationToken token)
        {
            try
            {
                var requestArgs = MapIntoTokenSendRequestArgs(args);
                var response    = await _client.SendRequestAsync(requestArgs, token);

                var clientAssertion = _jwtTokenParser.Parse(response);
                if (!_tokenResponseValidator.IsValid(clientAssertion))
                {
                    throw new UnsuccessfulResponseException("Token which was retrieved from SO is corrupted.");
                }

                return(TokenConvert.DeserializeClaim <PartiesResponse>(clientAssertion.JwtSecurityToken, "parties_info"));
            }
            catch (UnsuccessfulResponseException)
            {
                throw;
            }
            catch (Exception e)
            {
                throw new UnsuccessfulResponseException("Parties request was unsuccessful.", e);
            }
        }
 private TokenSendRequestArgs MapIntoTokenSendRequestArgs(PartiesRequestArgs args)
 {
     return(new TokenSendRequestArgs(_requestUri, CreateParameters(args), args.AccessToken));
 }
Example #10
0
        public void Constructor_ValidArgs_CreatesObject()
        {
            var result = new PartiesRequestArgs("accessToken", "*");

            result.Should().NotBeNull();
        }