-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserRepository.cs
140 lines (118 loc) · 3.58 KB
/
UserRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
using Domain.Entities;
using Domain.Repositories;
using Domain.Services;
using MongoDB.Driver;
using System;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace DataAccess
{
public class UserRepository : BaseRepository<User>, IUserRepository
{
private readonly ISecurityService _securityService;
public UserRepository(IMongoDbContext mongoDbContext, ISecurityService securityService) : base(mongoDbContext)
{
_securityService = securityService;
}
public async Task<User> FindUserAsync(Expression<Func<User, bool>> filter)
{
try
{
return await collection.Find(filter).SingleOrDefaultAsync();
}
catch
{
throw;
}
}
public async Task<bool> DeleteUserTokensByUserIdAsync(string userId)
{
try
{
FilterDefinition<User> filter = new FilterDefinitionBuilder<User>().Eq(x => x.Id, userId);
UpdateDefinition<User> update = new UpdateDefinitionBuilder<User>().Unset(x => x.Tokens);
await collection.FindOneAndUpdateAsync(filter, update);
return true;
}
catch
{
throw;
}
}
public async Task<bool> AddUserTokenByUserIdAsync(string userId, Token token)
{
try
{
FilterDefinition<User> filter = new FilterDefinitionBuilder<User>().Eq(x => x.Id, userId);
UpdateDefinition<User> update = new UpdateDefinitionBuilder<User>().AddToSet(x => x.Tokens, token);
await collection.UpdateOneAsync(filter, update);
return true;
}
catch
{
throw;
}
}
public async Task<bool> DeleteExpiredTokensAsync(string userId)
{
try
{
FilterDefinition<User> filter = new FilterDefinitionBuilder<User>().Eq(x => x.Id, userId);
UpdateDefinition<User> update = new UpdateDefinitionBuilder<User>().PullFilter(x => x.Tokens, i => i.RefreshTokenExpiresDateTime < DateTimeOffset.UtcNow);
await collection.UpdateManyAsync(filter, update);
return true;
}
catch
{
throw;
}
}
public async Task<bool> DeleteTokensWithSameRefreshTokenSourceAsync(string refreshTokenIdHashSource, string userId)
{
if (string.IsNullOrWhiteSpace(refreshTokenIdHashSource))
{
return true;
}
try
{
FilterDefinition<User> filter = new FilterDefinitionBuilder<User>().Eq(x => x.Id, userId);
UpdateDefinition<User> update = new UpdateDefinitionBuilder<User>().PullFilter(x => x.Tokens, i => i.RefreshTokenIdHashSource == refreshTokenIdHashSource || (i.RefreshTokenIdHash == refreshTokenIdHashSource && i.RefreshTokenIdHashSource == null));
await collection.UpdateManyAsync(filter, update);
return true;
}
catch
{
throw;
}
}
public async Task<(Token token, User user)> FindUserAndTokenByRefreshTokenAsync(string refreshToken)
{
try
{
string refreshTokenHash = _securityService.GetSha256Hash(refreshToken);
FilterDefinition<User> filter = new FilterDefinitionBuilder<User>().Eq($"{nameof(User.Tokens)}.{nameof(Token.RefreshTokenIdHash)}", refreshTokenHash);
User user = await collection.Find(filter).FirstOrDefaultAsync() ?? throw new Exception("Invalid refresh token");
return (user.Tokens.Where(x => x.RefreshTokenIdHash == refreshTokenHash).FirstOrDefault(), user);
}
catch
{
throw;
}
}
public async Task<bool> ChangePassword(string userId, string newPasswordHash, string newSerialNumber)
{
UpdateDefinition<User> update = new UpdateDefinitionBuilder<User>().Set(i => i.ProviderKey, newPasswordHash).Set(x => x.SerialNumber, newSerialNumber);
try
{
await collection.UpdateOneAsync(i => i.Id == userId, update);
return true;
}
catch
{
throw;
}
}
}
}