public async Task <Response <Usuario> > UsuarioValidoAsync(Usuario user) { var resp = new Response <Usuario>(); resp.Return = null; using (var client = this._context.GetClientInstance()) { QueryRequest request = ObterUsuarioQueryRequest("email", new AttributeValue { S = user.Login }); QueryResponse response = null; try { response = await client.QueryAsync(request); List <Usuario> lstUser = ExtractUserFrom(response.Items); if (lstUser != null && lstUser.Count > 0) { var userDb = lstUser[0]; var hash = SecurityCrypt.GenerateHash(user.Senha + userDb.Salt); if (hash == userDb.HashedPassword) { resp.Return = userDb; } } return(resp); } catch (Exception e) { resp.ErrorMessages.Add(e.Message); _log.LogError(e.Message); return(resp); } } }
public async Task <Response <Usuario> > SalvarAsync(Usuario user) { var resp = new Response <Usuario>(); using (var client = this._context.GetClientInstance()) { try { StringBuilder updExp = new StringBuilder("SET "); var exprAttrValues = new Dictionary <string, AttributeValue>(); var exprAttrNames = new Dictionary <string, string>(); if (user.Id < 1) { user.Id = (Int32)DateTimeOffset.UtcNow.ToUnixTimeSeconds(); } user.DataAtualizacao = DateTime.Now; exprAttrValues.Add(":dtAt", new AttributeValue { S = user.DataAtualizacao.Value.ToString("dd/MM/yyyy hh:mm:ss") }); updExp.Append(" #dtAt = :dtAt,"); exprAttrNames.Add("#dtAt", "dt-atualizacao"); exprAttrValues.Add(":tpUsuario", new AttributeValue { S = user.TipoUsuario.ToString() }); updExp.Append(" #tpUsuario = :tpUsuario,"); exprAttrNames.Add("#tpUsuario", "tp-usuario"); if (!String.IsNullOrEmpty(user.Senha)) { var salt = SecurityCrypt.GenerateSalt(); exprAttrValues.Add(":salt", new AttributeValue { S = salt }); updExp.Append(" #salt = :salt,"); exprAttrNames.Add("#salt", "salt"); var hash = SecurityCrypt.GenerateHash(user.Senha + salt); exprAttrValues.Add(":hash", new AttributeValue { S = hash }); updExp.Append(" #hash = :hash,"); exprAttrNames.Add("#hash", "hashedPassword"); } if (!String.IsNullOrEmpty(user.Login)) { exprAttrValues.Add(":login", new AttributeValue { S = user.Login }); updExp.Append(" #login = :login,"); exprAttrNames.Add("#login", "login"); } if (!String.IsNullOrEmpty(user.Nome)) { exprAttrValues.Add(":nome", new AttributeValue { S = user.Nome }); updExp.Append(" #nome = :nome,"); exprAttrNames.Add("#nome", "nome"); } if (!String.IsNullOrEmpty(user.Email)) { exprAttrValues.Add(":email", new AttributeValue { S = user.Email }); updExp.Append(" #email = :email,"); exprAttrNames.Add("#email", "email"); } if (!String.IsNullOrEmpty(user.Celular)) { exprAttrValues.Add(":celular", new AttributeValue { S = user.Celular }); updExp.Append(" #celular = :celular,"); exprAttrNames.Add("#celular", "celular"); } if (String.IsNullOrEmpty(user.CodigoEmail)) { var hashEmail = SecurityCrypt.GenerateHash(user.Email); user.CodigoEmail = hashEmail; exprAttrValues.Add(":codEmail", new AttributeValue { S = hashEmail }); updExp.Append(" #codEmail = :codEmail,"); exprAttrNames.Add("#codEmail", "cod-email"); } if (!user.EmailConfirmado.HasValue) { user.EmailConfirmado = false; } exprAttrValues.Add(":emailConf", new AttributeValue { BOOL = user.EmailConfirmado.Value }); updExp.Append(" #emailConf = :emailConf,"); exprAttrNames.Add("#emailConf", "email-confirmado"); var request = new UpdateItemRequest { TableName = _context.TableName, Key = new Dictionary <string, AttributeValue> { { "tipo", new AttributeValue { S = "usuario" } }, { "id", new AttributeValue { N = user.Id.ToString() } } }, ExpressionAttributeNames = exprAttrNames, ExpressionAttributeValues = exprAttrValues, UpdateExpression = updExp.ToString().Substring(0, updExp.ToString().Length - 1) }; var updResp = await client.UpdateItemAsync(request); resp.Return = user; return(resp); } catch (Exception e) { resp.Return = user; resp.ErrorMessages.Add(e.Message); _log.LogError(e.Message); return(resp); } } }
public async Task <Response <Usuario> > ResetarSenhaAsync(Usuario usuario) { var resp = new Response <Usuario>(); if (usuario == null) { resp.ErrorMessages.Add("Usuário nulo"); return(resp); } var result = await this.ObterUsuarioAsync(usuario); usuario.Id = result.Return.Id; usuario.CodigoEmail = result.Return.CodigoEmail; usuario.DataAtualizacao = DateTime.Now; if (String.IsNullOrEmpty(usuario.CodigoResetSenha)) { var hashCodigoReset = SecurityCrypt.GenerateHash(usuario.Email + usuario.DataAtualizacao.Value.ToString("dd/MM/yyyy hh:mm:ss")); usuario.CodigoResetSenha = hashCodigoReset; } var salt = SecurityCrypt.GenerateSalt(); var hash = SecurityCrypt.GenerateHash(usuario.Senha + salt); using (var client = this._context.GetClientInstance()) { try { var request = new UpdateItemRequest { TableName = _context.TableName, Key = new Dictionary <string, AttributeValue> { { "tipo", new AttributeValue { S = "usuario" } }, { "id", new AttributeValue { N = usuario.Id.ToString() } } }, ExpressionAttributeNames = new Dictionary <string, string>() { { "#email", "email" }, { "#dtAt", "dt-atualizacao" }, { "#codEmail", "cod-email" }, { "#salt", "salt" }, { "#hash", "hashedPassword" } }, ExpressionAttributeValues = new Dictionary <string, AttributeValue>() { { ":dtAt", new AttributeValue { S = usuario.DataAtualizacao.Value.ToString("dd/MM/yyyy hh:mm:ss") } }, { ":email", new AttributeValue { S = usuario.Email } }, { ":codEmail", new AttributeValue { S = usuario.CodigoEmail } }, { ":salt", new AttributeValue { S = salt } }, { ":hash", new AttributeValue { S = hash } } }, ConditionExpression = "#email = :email AND #codEmail = :codEmail", UpdateExpression = "SET #dtAt = :dtAt, #salt = :salt, #hash = :hash" }; var updResp = await client.UpdateItemAsync(request); resp.Return = usuario; return(resp); } catch (Exception e) { resp.Return = usuario; resp.ErrorMessages.Add(e.Message); _log.LogError(e.Message); return(resp); } } }