Example #1
0
        public async void JwtTokenLogSuccess(IAuthenticable user, string message)
        {
            var      usr      = JsonConvert.SerializeObject(user);
            Employee employee = null;

            if (user is Employee)
            {
                employee = (Employee)user;
            }
        }
Example #2
0
        public bool Login(IAuthenticable employee, string password)
        {
            bool autenticatedUser = employee.Authenticate(password);

            if (autenticatedUser)
            {
                Console.WriteLine("Wellcome to the system");
                return(true);
            }
            Console.WriteLine("Access denied");
            return(false);
        }
Example #3
0
        private async Task <OkObjectResult> GetClaimsIdentity(IAuthenticable user)
        {
            var identity = new ClaimsIdentity(new GenericIdentity(user.UserName, "Token"),
                                              new[]
            {
                new Claim("RoleId", user.Profile.Role.Id.ToString())
            });
            var role = "administrador";

            if (user.Profile.Role.Id == 3)
            {
                role = "resident";
            }
            if (user.Profile.Role.Id == 2)
            {
                role = "employee";
            }
            var claims = new[]
            {
                new Claim("UserName", user.UserName),
                new Claim("UserType", user.ToString()),
                new Claim(JwtRegisteredClaimNames.Sub, user.UserName),
                new Claim(JwtRegisteredClaimNames.Jti, await _jwtOptions.JtiGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_jwtOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64),
                identity.FindFirst("RoleId"),
                new Claim("roles", role)
            };

            // 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 response = new Token
            {
                access_token = encodedJwt,
                expires_in   = (int)_jwtOptions.ValidFor.TotalSeconds
            };

            var json = JsonConvert.SerializeObject(response, _serializerSettings);

            _jwtFactory.JwtTokenLogSuccess(user, json);

            return(new OkObjectResult(json));
        }
Example #4
0
 public bool LogInto(IAuthenticable authenticablePerson, string passworkAttempt)
 {
     if (authenticablePerson.Authenticate(passworkAttempt))
     {
         Console.WriteLine("Correct password. Welcome to the system!");
         return(true);
     }
     else
     {
         Console.WriteLine("Incorrect password!");
         return(false);
     }
 }
Example #5
0
        public bool Login(IAuthenticable employee, string password)
        {
            bool authenticatedUser = employee.Authenticate(password);

            if (authenticatedUser)
            {
                Console.WriteLine("Welcome to the system!");
                return(true);
            }
            else
            {
                Console.WriteLine("Incorrect password!");
                return(false);
            }
        }
Example #6
0
        /// <summary>
        /// Performs authentication for a BoxMessage
        /// </summary>
        /// <param name="msg">The message to authenticate</param>
        /// <returns>The AuthenticationResult defininf the authentication process</returns>
        public static AuthenticationResult Authenticate(BoxMessage msg)
        {
            Account account = GetAccount(msg.Username);

            if (account == null)
            {
                // Account doesn't exist
                return(AuthenticationResult.WrongCredentials);
            }

            AuthenticationResult auth = AuthenticationResult.WrongCredentials;

            if (AccountHandler.ProtectPasswords != PasswordProtection.None)
            {
                auth = msg.Authenticate(account.CryptPassword, true);
                if (auth == AuthenticationResult.WrongCredentials)
                {
                    auth = msg.Authenticate(account.NewCryptPassword, true);
                }
            }
            else
            {
                auth = msg.Authenticate(account.PlainPassword, false);
            }

            if (auth == AuthenticationResult.Success)
            {
                IAuthenticable iAuth = msg as IAuthenticable;

                if (iAuth != null)
                {
                    // This message has further authentication properties
                    if (iAuth.RequireOnlineMobile)
                    {
                        // Check for online mobile
                        if (Authentication.VerifyMobileAccessLevel(account, iAuth.MinAccessLevel))
                        {
                            return(AuthenticationResult.Success);
                        }
                        else
                        {
                            return(AuthenticationResult.OnlineMobileRequired);
                        }
                    }
                    else
                    {
                        if (Authentication.VerifyAccountAccessLevel(account, iAuth.MinAccessLevel))
                        {
                            return(AuthenticationResult.Success);
                        }
                        else
                        {
                            return(AuthenticationResult.AccessLevelError);
                        }
                    }
                }
                else
                {
                    // No need for advanced authentication, check only access level
                    if (Authentication.VerifyAccountAccessLevel(account, BoxConfig.MinAccessLevel))
                    {
                        return(AuthenticationResult.Success);
                    }
                    else
                    {
                        return(AuthenticationResult.AccessLevelError);
                    }
                }
            }
            else
            {
                return(auth);
            }
        }
 /// <summary>
 /// Constructor of the <c>DBAuthenticator</c> class
 /// </summary>
 /// <param name="connection">Instance implementing the <c>IAuthenticable</c> interface</param>
 public DBAuthenticator(IAuthenticable connection)
 {
     this.Connection = connection;                       //Store a reference to the database
 }