示例#1
0
        public static async Task ResetFailedCountForAsync(string sessionToken, string email)
        {
            await Authorization.CheckAuthorizationAsync(sessionToken, MethodBase.GetCurrentMethod(), AccessType.Update).ConfigureAwait(false);

            var login = await QueryAliveSessionAsync(sessionToken).ConfigureAwait(false)
                        ?? throw new AuthorizationException(ErrorType.InvalidToken);

            using var identityCtrl = new Controllers.Persistence.Account.IdentityController(Factory.CreateContext())
                  {
                      SessionToken = sessionToken
                  };
            var identity = await identityCtrl.ExecuteFirstOrDefaultAsync(e => e.State == Contracts.Modules.Common.State.Active &&
                                                                         e.Email.ToLower() == email.ToLower())
                           .ConfigureAwait(false);

            if (identity == null)
            {
                throw new AuthorizationException(ErrorType.InvalidAccount);
            }

            identity.AccessFailedCount = 0;
            await identityCtrl.UpdateAsync(identity).ConfigureAwait(false);

            await identityCtrl.SaveChangesAsync().ConfigureAwait(false);
        }
示例#2
0
        public static async Task ChangePasswordAsync(string sessionToken, string oldPassword, string newPassword)
        {
            await Authorization.CheckAuthorizationAsync(sessionToken, MethodBase.GetCurrentMethod(), AccessType.Update).ConfigureAwait(false);

            var login = await QueryAliveSessionAsync(sessionToken).ConfigureAwait(false)
                        ?? throw new AuthorizationException(ErrorType.InvalidToken);

            using var identityCtrl = new Controllers.Persistence.Account.IdentityController(Factory.CreateContext())
                  {
                      SessionToken = Authorization.SystemAuthorizationToken
                  };
            var identity = await identityCtrl.ExecuteFirstOrDefaultAsync(e => e.Id == login.IdentityId)
                           .ConfigureAwait(false);

            if (identity != null)
            {
                if (VerifyPasswordHash(oldPassword, identity.PasswordHash, identity.PasswordSalt) == false)
                {
                    throw new AuthorizationException(ErrorType.InvalidPassword);
                }

                identity.Password = newPassword;
                await identityCtrl.UpdateAsync(identity).ConfigureAwait(false);

                await identityCtrl.SaveChangesAsync().ConfigureAwait(false);

                if (login.Identity != null)
                {
                    var(Hash, Salt) = CreatePasswordHash(newPassword);

                    login.Identity.PasswordHash = Hash;
                    login.Identity.PasswordSalt = Salt;
                }
            }
        }
示例#3
0
        public async static Task <ILoginSession> LogonAsync(string jsonWebToken)
        {
            jsonWebToken.CheckArgument(nameof(jsonWebToken));

            var result = default(LoginSession);

            if (JsonWebToken.CheckToken(jsonWebToken, out SecurityToken validatedToken))
            {
                if (validatedToken.ValidTo < DateTime.UtcNow)
                {
                    throw new AuthorizationException(ErrorType.AuthorizationTimeOut);
                }

                if (validatedToken is JwtSecurityToken jwtValidatedToken)
                {
                    var email = jwtValidatedToken.Claims.FirstOrDefault(e => e.Type == ClaimTypes.Email);

                    if (email != null && email.Value != null)
                    {
                        using var identityCtrl = new Controllers.Persistence.Account.IdentityController(Factory.CreateContext())
                              {
                                  SessionToken = Authorization.SystemAuthorizationToken
                              };
                        var identity = await identityCtrl.ExecuteFirstOrDefaultAsync(e => e.State == Contracts.Modules.Common.State.Active &&
                                                                                     e.EnableJwtAuth == true &&
                                                                                     e.Email.ToLower() == email.Value.ToString().ToLower())
                                       .ConfigureAwait(false);

                        if (identity != null)
                        {
                            var login = await QueryLoginByEmailAsync(identity.Email, identity.Password, string.Empty).ConfigureAwait(false);

                            if (login != null)
                            {
                                result = new LoginSession();
                                result.CopyProperties(login);
                                result.IsRemoteAuth = true;
                            }
                        }
                    }
                }
            }
            else
            {
                throw new AuthorizationException(ErrorType.InvalidJsonWebToken);
            }
            return(result ?? throw new AuthorizationException(ErrorType.InvalidAccount));
        }
示例#4
0
        internal static async Task <LoginSession> QueryAliveSessionAsync(string email, string password)
        {
            email.CheckArgument(nameof(email));
            password.CheckArgument(nameof(password));

            var result = LoginSessions.FirstOrDefault(e => e.IsActive &&
                                                      e.Email.Equals(email, StringComparison.CurrentCultureIgnoreCase));

            if (result == null)
            {
                using var identityCtrl = new Controllers.Persistence.Account.IdentityController(Factory.CreateContext())
                      {
                          SessionToken = Authorization.SystemAuthorizationToken,
                      };
                var identity = await identityCtrl.ExecuteFirstOrDefaultAsync(e => e.State == Contracts.Modules.Common.State.Active &&
                                                                             e.AccessFailedCount < 4 &&
                                                                             e.Email.ToLower() == email.ToLower())
                               .ConfigureAwait(false);

                if (identity != null &&
                    VerifyPasswordHash(password, identity.PasswordHash, identity.PasswordSalt))
                {
                    using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(identityCtrl);
                    var session = await sessionCtrl.ExecuteFirstOrDefaultAsync(e => e.LogoutTime == null &&
                                                                               e.IdentityId == identity.Id)
                                  .ConfigureAwait(false);

                    if (session != null &&
                        session.IsActive)
                    {
                        session.Identity = identity;
                        session.Roles.AddRange(await QueryIdentityRolesAsync(sessionCtrl, identity.Id).ConfigureAwait(false));
                        session.JsonWebToken = JsonWebToken.GenerateToken(new Claim[]
                        {
                            new Claim(ClaimTypes.Email, identity.Email),
                            new Claim(ClaimTypes.System, nameof(QnSProjectAward)),
                        }.Union(session.Roles.Select(e => new Claim(ClaimTypes.Role, e.Designation))));

                        result = new LoginSession();
                        result.CopyProperties(session);
                        LoginSessions.Add(session);
                    }
                }
            }
            return(result);
        }
示例#5
0
        internal static async Task <LoginSession> QueryAliveSessionAsync(string sessionToken)
        {
            LoginSession result = LoginSessions.FirstOrDefault(ls => ls.IsActive &&
                                                               ls.SessionToken.Equals(sessionToken));

            if (result == null)
            {
                using var sessionCtrl = new Controllers.Persistence.Account.LoginSessionController(Factory.CreateContext())
                      {
                          SessionToken = Authorization.SystemAuthorizationToken
                      };
                var session = await sessionCtrl.ExecuteFirstOrDefaultAsync(e => e.SessionToken.Equals(sessionToken))
                              .ConfigureAwait(false);

                if (session != null && session.IsActive)
                {
                    using var identityCtrl = new Controllers.Persistence.Account.IdentityController(sessionCtrl);
                    var identity = await identityCtrl.ExecuteFirstOrDefaultAsync(e => e.Id == session.IdentityId)
                                   .ConfigureAwait(false);

                    if (identity != null)
                    {
                        session.Name  = identity.Name;
                        session.Email = identity.Email;
                        session.Roles.AddRange(await QueryIdentityRolesAsync(sessionCtrl, identity.Id).ConfigureAwait(false));
                        session.JsonWebToken = JsonWebToken.GenerateToken(new Claim[]
                        {
                            new Claim(ClaimTypes.Email, identity.Email),
                            new Claim(ClaimTypes.System, nameof(QnSProjectAward)),
                        }.Union(session.Roles.Select(e => new Claim(ClaimTypes.Role, e.Designation))));

                        result = new LoginSession();
                        result.CopyProperties(session);
                        LoginSessions.Add(session);
                    }
                }
            }
            return(result);
        }