Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromForm] BitIdCredentials request)
        {
            string jsonResponseToken = string.Empty;

            BitIdResponse response = request.VerifyMessage();

            jsonResponseToken = JsonConvert.SerializeObject(response, _serializerSettings);
            if (response.Success)
            {
                // use attribute >> [Authorize(Policy = Constants.AuthorizationPolicy)]
                var identity = GetClaimsIdentity(request.Address);

                var claims = new[]
                {
                    new Claim(JwtRegisteredClaimNames.Sub, request.Address),
                    new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()),
                    new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
                    identity.FindFirst(Constants.ClaimType)
                };
                // Create the JWT security token and encode it.
                var jwt = new JwtSecurityToken(
                    issuer: _jwtOptions.Issuer,
                    audience: _jwtOptions.Audience,
                    claims: claims,
                    notBefore: _jwtOptions.NotBefore,
                    expires: _jwtOptions.Expiration,
                    signingCredentials: _jwtOptions.SigningCredentials);
                var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

                // Serialize and return the response
                var responseToken = new
                {
                    access_token = encodedJwt,
                    expires_in   = (int)_jwtOptions.ValidFor.TotalSeconds
                };

                jsonResponseToken = JsonConvert.SerializeObject(responseToken, _serializerSettings);
            }

            return(new ObjectResult(jsonResponseToken));
        }
Exemplo n.º 2
0
        public void Login_BitidRequestMessageIsValid_ReturnsTrue()
        {
            // Arrange
            Key            privateKey = new Key(); //Create private key
            BitcoinSecret  secret     = privateKey.GetBitcoinSecret(Network.Main);
            BitcoinAddress pubAddress = secret.GetAddress();
            Guid           guid       = Guid.NewGuid();
            string         guidString = guid.ToString().Replace("-", "");

            long   ticks     = DateTime.UtcNow.Ticks;
            string nonce     = guidString + ticks.ToString("x");
            string bitIdUri  = "bitid://localhost/api/identity?x=" + nonce + "&u=1";
            string signature = privateKey.SignMessage(bitIdUri);

            var request = new BitIdCredentials(pubAddress.ToString(), bitIdUri, signature);

            // Act
            BitIdResponse response = request.VerifyMessage();

            // Assert
            Assert.True(response.Success);
        }