public string Get()
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var hashingKey    = new byte[32];
            var encryptionKey = new byte[32];

            using (var provider = new RNGCryptoServiceProvider()) {
                provider.GetBytes(hashingKey);
                provider.GetBytes(encryptionKey);
            }

            var nonceGenerator = new NonceGenerator();

            nonceGenerator.Execute();

            var armorToken = new ArmorToken("*****@*****.**", "HMH", nonceGenerator.Nonce);

            var hash      = new HashArmorTokenGenerationStep(new HMACSHA256HashingMechanismFactory(hashingKey), new EmptyArmorTokenGenerationStep());
            var encrypt   = new EncryptArmorTokenGenerationStep(new RijndaelEncryptionMechanismFactory(encryptionKey), hash);
            var serialise = new SerialiseArmorTokenGenerationStep(new ArmorTokenSerialisor(), encrypt);

            var armorTokenGenerator = new ArmorTokenGenerator(armorToken, serialise);

            armorTokenGenerator.Execute();

            stopwatch.Stop();
            Trace.WriteLine(string.Concat("Time: ", stopwatch.ElapsedMilliseconds));

            return(armorTokenGenerator.ArmorToken);
        }
        public bool TryFortify() {
            var identityReader = identityReaderFactory.Create();
            IEnumerable<Claim> identity;

            var isAuthenticated = identityReader.TryRead(out identity);
            if (!isAuthenticated) return false;

            var claims = identity.ToList();

            var userId = claims.Single(c => c.Type.Equals("UserId")).Value;
            var platform = claims.Single(c => c.Type.Equals("Platform")).Value;

            var encryptionKey = ArmorSettings.EncryptionKey;
            var hashingKey = ArmorSettings.HashingKey;

            var nonceGenerator = new NonceGenerator();
            nonceGenerator.Execute();

            var armorToken = new ArmorToken(userId, platform,
                nonceGenerator.Nonce);

            var armorTokenConstructor = new ArmorTokenConstructor();
            var standardSecureArmorTokenBuilder =
                new StandardSecureArmorTokenBuilder(armorToken, encryptionKey,
                    hashingKey);
            var generateSecureArmorToken =
                new GenerateSecureArmorToken(armorTokenConstructor,
                    standardSecureArmorTokenBuilder);

            generateSecureArmorToken.Execute();

            httpContext.Response.AppendHeader("ARMOR",
                generateSecureArmorToken.SecureArmorToken);
            return true;
        }
Esempio n. 3
0
        public void GivenIHaveGeneratedRandomNumbers(int p0)
        {
            for (var i = 0; i < p0; i++)
            {
                var nonceGenerator = new NonceGenerator();
                nonceGenerator.Execute();

                nonces.Add(nonceGenerator.Nonce);
            }
        }
        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            #region Read logged-in user claims

            var principal = (ClaimsIdentity)Thread.CurrentPrincipal.Identity;
            var userId    = principal.Claims.Single(c => c.Type.Equals("UserId")).Value;
            var platform  = principal.Claims.Single(c => c.Type.Equals("Platform")).Value;

            #endregion

            #region Ensure existence of ArmorToken in HTTP header

            var armorHeaderParser = new ArmorHeaderParser(actionContext.Request.Headers);
            armorHeaderParser.Execute();

            if (!armorHeaderParser.ArmorTokenHeader.IsValid)
            {
                return(false);
            }

            #endregion

            #region Validate ArmorToken

            var encryptionKey = Convert.FromBase64String("0nA6gWIoNXeeFjJFo1qi1ZlL7NI/4a6YbL8RnqMTC1A=");
            var hashingKey    = Convert.FromBase64String("0nA6gWIoNXeeFjJFo1qi1ZlL7NI/4a6YbL8RnqMTC1A=");

            var secureArmorTokenValidator = new SecureArmorTokenValidator(armorHeaderParser.ArmorTokenHeader.ArmorToken, encryptionKey, hashingKey, userId, 10000000000);
            secureArmorTokenValidator.Execute();

            if (!secureArmorTokenValidator.ArmorTokenValidationStepResult.IsValid)
            {
                return(false);
            }

            #endregion

            #region Refresh ArmorToken and re-issue

            var nonceGenerator = new NonceGenerator();
            nonceGenerator.Execute();

            var armorToken = new ArmorToken(userId, platform, nonceGenerator.Nonce, new[] { new Claim("Another", "Claim") });

            var armorTokenConstructor           = new ArmorTokenConstructor();
            var standardSecureArmorTokenBuilder = new StandardSecureArmorTokenBuilder(armorToken, encryptionKey, hashingKey);
            var generateSecureArmorToken        = new GenerateSecureArmorToken(armorTokenConstructor, standardSecureArmorTokenBuilder);

            generateSecureArmorToken.Execute();

            #endregion

            HttpContext.Current.Response.AppendHeader("ARMOR", generateSecureArmorToken.SecureArmorToken);
            return(true);
        }
Esempio n. 5
0
        public bool TryFortify()
        {
            var identityReader = identityReaderFactory.Create();
            IEnumerable <Claim> identity;

            var isAuthenticated = identityReader.TryRead(out identity);

            if (!isAuthenticated)
            {
                return(false);
            }

            var claims = identity.ToList();

            var userId   = claims.Single(c => c.Type.Equals("UserId")).Value;
            var platform = claims.SingleOrDefault(c => c.Type.Equals("Platform"));

            var encryptionKey = ArmorSettings.EncryptionKey;
            var hashingKey    = ArmorSettings.HashingKey;

            var nonceGenerator = new NonceGenerator();

            nonceGenerator.Execute();

            var armorToken = new ArmorToken(userId,
                                            platform == null ? "ARMOR" : platform.Value,
                                            nonceGenerator.Nonce);

            var armorTokenConstructor           = new ArmorTokenConstructor();
            var standardSecureArmorTokenBuilder =
                new StandardSecureArmorTokenBuilder(armorToken, encryptionKey,
                                                    hashingKey);
            var generateSecureArmorToken =
                new GenerateSecureArmorToken(armorTokenConstructor,
                                             standardSecureArmorTokenBuilder);

            generateSecureArmorToken.Execute();

            httpContext.Response.AppendHeader("ARMOR",
                                              generateSecureArmorToken.SecureArmorToken);
            return(true);
        }