Ejemplo n.º 1
0
 public EquationStartState(TokenVerifier tokenVerifier)
 {
     this.tokenVerifier = tokenVerifier;
 }
Ejemplo n.º 2
0
 public OperandTokenState(TokenVerifier tokenVerifier)
 {
     this.tokenVerifier = tokenVerifier;
 }
        public async Task Handle_GivenRequestForNewToken_ReturnsAnonymousTokenResponse()
        {
            //Arrange
            var automocker = new AutoMocker();

            var curveName    = "P-256";
            var ecParameters = CustomNamedCurves.GetByName(curveName);

            var initiator = new Initiator();
            var init      = initiator.Initiate(ecParameters.Curve);
            var t         = init.t;
            var r         = init.r;
            var P         = init.P;

            var privateKey     = await new InMemoryPrivateKeyStore().GetAsync();
            var publicKey      = (await new InMemoryPublicKeyStore().GetAsync()).Q;
            var signingKeyPair = new AnonymousTokenSigningKeypair("some-kid-123", curveName, privateKey, publicKey);

            var tokenGenerator = new TokenGenerator();

            var(expectedSignedPoint, expectedProofChallenge, expectedProofResponse) = tokenGenerator.GenerateToken(privateKey, publicKey, ecParameters, P);

            var tokenVerifier = new TokenVerifier(new InMemorySeedStore());

            var tokenRequest = new IssueAnonymousToken.Command
            {
                JwtTokenId     = "token-a",
                JwtTokenExpiry = DateTime.Now.AddMinutes(10),
                RequestData    = new AnonymousTokenRequest
                {
                    MaskedPoint = Convert.ToBase64String(P.GetEncoded())
                }
            };

            automocker
            .SetupOptions(new AnonymousTokensConfig
            {
                Enabled = true
            });

            automocker
            .Setup <IAnonymousTokenIssueRecordRepository, Task <IEnumerable <AnonymousTokenIssueRecord> > >(x => x.RetrieveRecordsJwtToken(It.IsAny <string>()))
            .Returns <string>(x => Task.FromResult(Enumerable.Empty <AnonymousTokenIssueRecord>()));

            automocker
            .Setup <IAnonymousTokensKeyStore, Task <AnonymousTokenSigningKeypair> >(x => x.GetActiveSigningKeyPair())
            .ReturnsAsync(signingKeyPair);

            automocker
            .Setup <ITokenGenerator, (ECPoint, BigInteger, BigInteger)>(x => x.GenerateToken(privateKey, publicKey, ecParameters, It.Is <ECPoint>(y => y.Equals(P))))
            .Returns((expectedSignedPoint, expectedProofChallenge, expectedProofResponse));

            var target = automocker.CreateInstance <IssueAnonymousToken.Handler>();

            //Act
            var result = await target.Handle(tokenRequest, new CancellationToken());

            //Assert
            using (new AssertionScope())
            {
                result.HasValue.Should().BeTrue();
                var anonymousTokenResponse = result.ValueOrFailure();

                anonymousTokenResponse.Kid.Should().Be("some-kid-123");

                var Q = ecParameters.Curve.DecodePoint(Convert.FromBase64String(anonymousTokenResponse.SignedPoint));
                var c = new BigInteger(Convert.FromBase64String(anonymousTokenResponse.ProofChallenge));
                var z = new BigInteger(Convert.FromBase64String(anonymousTokenResponse.ProofResponse));

                Q.Should().Be(expectedSignedPoint);
                c.Should().Be(expectedProofChallenge);
                z.Should().Be(expectedProofResponse);

                var W          = initiator.RandomiseToken(ecParameters, publicKey, P, Q, c, z, r);
                var isVerified = await tokenVerifier.VerifyTokenAsync(privateKey, ecParameters.Curve, t, W);

                isVerified.Should().BeTrue();
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> UpdateProperty([FromRoute] int id, [FromBody] AddProperty addProperty)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var    userCp   = HttpContext.User;
            string landlord = TokenVerifier.GetLandlord(userCp);

            Property currentProperty = await _context.Property.FindAsync(id);

            if (currentProperty == null)
            {
                return(NotFound());
            }

            if (landlord == null || landlord != currentProperty.AppUserRef)
            {
                return(Unauthorized());
            }

            //Generate property
            Property newproperty = _mapper.Map <AddProperty, Property>(addProperty);

            newproperty.PropertyStatus = Property.VerificationStatus.Pending;
            newproperty.AppUserRef     = landlord;

            //Validate new property
            if (!TryValidateModel(newproperty))
            {
                return(BadRequest());
            }

            List <BasicImage> images = new List <BasicImage>();

            int imgCount = 0;

            foreach (string imageStr in addProperty.Images)
            {
                BasicImage bi = WriteImage(imageStr, imgCount);

                if (bi != null)
                {
                    images.Add(bi);
                    imgCount++;
                }
                else
                {
                    CleanImages(images);
                    return(BadRequest());
                }
            }

            List <Image>      oldImages      = _context.Image.Where(i => i.PropertyRef == id).ToList();
            List <BasicImage> oldBasicImages = _mapper.Map <List <Image>, List <BasicImage> >(oldImages);

            //From DB
            foreach (Image i in oldImages)
            {
                _context.Image.Remove(i);
            }

            //From disk
            CleanImages(oldBasicImages);

            currentProperty.AddressLine1        = addProperty.AddressLine1;
            currentProperty.AddressLine2        = addProperty.AddressLine2;
            currentProperty.City                = addProperty.City;
            currentProperty.County              = addProperty.County;
            currentProperty.Postcode            = addProperty.Postcode;
            currentProperty.Longitude           = addProperty.Longitude;
            currentProperty.Latitude            = addProperty.Latitude;
            currentProperty.PropertyDescription = addProperty.PropertyDescription;
            currentProperty.PropertyStatus      = Property.VerificationStatus.Pending;

            foreach (BasicImage bi in images)
            {
                bi.PropertyRef = currentProperty.ID;
                Image im = _mapper.Map <BasicImage, Image>(bi);
                _context.Image.Add(im);
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }