/
CredentialsService.cs
241 lines (216 loc) · 10.6 KB
/
CredentialsService.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using Witsml;
using Witsml.Data;
using Witsml.Extensions;
using WitsmlExplorer.Api.Configuration;
using WitsmlExplorer.Api.Extensions;
using WitsmlExplorer.Api.HttpHandlers;
using WitsmlExplorer.Api.Middleware;
using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Repositories;
namespace WitsmlExplorer.Api.Services
{
// ReSharper disable once UnusedMember.Global
public class CredentialsService : ICredentialsService
{
private readonly ITimeLimitedDataProtector _dataProtector;
private readonly ILogger<CredentialsService> _logger;
private readonly WitsmlClientCapabilities _clientCapabilities;
private readonly IWitsmlSystemCredentials _witsmlServerCredentials;
private readonly IDocumentRepository<Server, Guid> _witsmlServerRepository;
private readonly ICredentialsCache _credentialsCache;
private static readonly string SUBJECT = "sub";
private readonly bool _useOAuth2;
private readonly bool _isDesktopApp;
public CredentialsService(
IDataProtectionProvider dataProtectionProvider,
IOptions<WitsmlClientCapabilities> clientCapabilities,
IWitsmlSystemCredentials witsmlServerCredentials,
IDocumentRepository<Server, Guid> witsmlServerRepository,
ICredentialsCache credentialsCache,
ILogger<CredentialsService> logger,
IConfiguration configuration)
{
_dataProtector = dataProtectionProvider.CreateProtector("WitsmlServerPassword").ToTimeLimitedDataProtector();
_logger = logger ?? throw new ArgumentException("Missing ILogger");
_clientCapabilities = clientCapabilities?.Value ?? throw new ArgumentException("Missing WitsmlClientCapabilities");
_witsmlServerCredentials = witsmlServerCredentials ?? throw new ArgumentException("Missing WitsmlServerCredentials");
_witsmlServerRepository = witsmlServerRepository ?? throw new ArgumentException("Missing WitsmlServerRepository");
_credentialsCache = credentialsCache ?? throw new ArgumentException("CredentialsService missing");
_useOAuth2 = StringHelpers.ToBoolean(configuration[ConfigConstants.OAuth2Enabled]);
_isDesktopApp = StringHelpers.ToBoolean(configuration[ConfigConstants.IsDesktopApp]);
}
public async Task VerifyCredentials(ServerCredentials creds)
{
var witsmlClient = new WitsmlClient(options =>
{
options.Hostname = creds.Host.ToString();
options.Credentials = new WitsmlCredentials(creds.UserId, creds.Password);
options.ClientCapabilities = _clientCapabilities;
});
await witsmlClient.TestConnectionAsync();
}
public async Task<bool> VerifyAndCacheCredentials(IEssentialHeaders eh, bool keep, HttpContext httpContext)
{
ServerCredentials creds = HttpRequestExtensions.ParseServerHttpHeader(eh.WitsmlAuth, Decrypt);
if (creds.IsNullOrEmpty())
{
return false;
}
string cacheId = GetCacheId(eh);
if (!_useOAuth2 && (string.IsNullOrEmpty(cacheId) || _credentialsCache.GetItem(cacheId) == null))
{
cacheId = httpContext.CreateWitsmlExplorerCookie(_isDesktopApp);
}
await VerifyCredentials(creds);
double ttl = keep ? 24.0 : 1.0; // hours
CacheCredentials(cacheId, creds, ttl);
return true;
}
private async Task<bool> UserHasRoleForHost(string[] roles, Uri host)
{
ICollection<Server> allServers = await _witsmlServerRepository.GetDocumentsAsync();
bool validRole = allServers.Where(n => n.Url.EqualsIgnoreCase(host)).Any(n => n.Roles != null && n.Roles.Intersect(roles).Any());
return validRole;
}
private string Encrypt(string inputString)
{
return _dataProtector.Protect(inputString);
}
private string Decrypt(string inputString)
{
try
{
return _dataProtector.Unprotect(inputString);
}
catch
{
return inputString?.Length > 0 ? inputString : null;
}
}
public void RemoveCachedCredentials(string cacheId)
{
_credentialsCache.RemoveAllClientCredentials(cacheId);
}
public void RemoveAllCachedCredentials()
{
_credentialsCache.Clear();
}
public void CacheCredentials(string cacheId, ServerCredentials credentials, double ttl, Func<string, string> delEncrypt = null)
{
delEncrypt ??= Encrypt;
string encryptedPassword = delEncrypt(credentials.Password);
_credentialsCache.SetItem(cacheId, credentials.Host, encryptedPassword, ttl, credentials.UserId);
}
private async Task<List<ServerCredentials>> GetSystemCredentialsByToken(string token, Uri server)
{
List<ServerCredentials> results = new List<ServerCredentials>();
JwtSecurityTokenHandler handler = new();
JwtSecurityToken jwt = handler.ReadJwtToken(token);
string[] userRoles = jwt.Claims.Where(n => n.Type == "roles").Select(n => n.Value).ToArray();
_logger.LogDebug("User roles in JWT: {roles}", string.Join(",", userRoles));
if (await UserHasRoleForHost(userRoles, server))
{
ICollection<Server> allServers = await _witsmlServerRepository.GetDocumentsAsync();
List<string> credentialIds = allServers
.Where(n => n.Url.EqualsIgnoreCase(server) && !n.CredentialIds.IsNullOrEmpty())
?.SelectMany(n => n.CredentialIds)
?.ToList()
?? new List<string>();
_logger.LogDebug("Matching on {credentialIdOrHost} for server {server}", credentialIds.Count == 0 ? "host" : $"credentialIds {string.Join(", ", credentialIds)}", server);
var matchingCredentials = credentialIds.IsNullOrEmpty()
? _witsmlServerCredentials.WitsmlCreds.Where(n => n.Host.EqualsIgnoreCase(server))
: _witsmlServerCredentials.WitsmlCreds.Where(n => credentialIds.Contains(n.CredentialId, StringComparer.InvariantCultureIgnoreCase));
foreach (var credential in matchingCredentials)
{
if (!credential.IsNullOrEmpty())
{
CacheCredentials(GetClaimFromToken(token, SUBJECT), credential, 1.0);
results.Add(credential);
}
}
}
return results;
}
public string GetClaimFromToken(string token, string claim)
{
JwtSecurityTokenHandler handler = new();
JwtSecurityToken jwt = handler.ReadJwtToken(token);
return jwt.Claims.Where(n => n.Type == claim).Select(n => n.Value).FirstOrDefault();
}
public void VerifyUserIsLoggedIn(IEssentialHeaders eh, ServerType serverType)
{
string server = serverType == ServerType.Target ? eh.TargetServer : eh.SourceServer;
string username = serverType == ServerType.Target ? eh.TargetUsername : eh.SourceUsername;
ServerCredentials creds = GetCredentials(eh, server, username);
if (creds == null || creds.IsNullOrEmpty())
{
string serverTypeName = serverType == ServerType.Target ? "target" : "source";
throw new WitsmlClientProviderException($"Missing {serverTypeName} server credentials", (int)HttpStatusCode.Unauthorized, serverType);
}
}
public async Task<string[]> GetLoggedInUsernames(IEssentialHeaders eh, Uri serverUrl)
{
Dictionary<string, string> credentials = _credentialsCache.GetItem(GetCacheId(eh), serverUrl);
List<string> usernames = credentials == null ? new() : credentials.Keys.ToList();
if (_useOAuth2)
{
List<ServerCredentials> systemCredentials = await GetSystemCredentialsByToken(eh.GetBearerToken(), serverUrl);
foreach (var systemCredential in systemCredentials)
{
if (!systemCredential.IsNullOrEmpty() && !usernames.Contains(systemCredential.UserId))
{
usernames.Add(systemCredential.UserId);
}
}
}
_logger.LogDebug("Logged in usernames for server {server}: {usernames}", serverUrl, string.Join(",", usernames));
return usernames.ToArray();
}
private ServerCredentials GetCredentialsFromCache(IEssentialHeaders eh, string serverUrl, string username, Func<string, string> delDecrypt = null)
{
delDecrypt ??= Decrypt;
Dictionary<string, string> credentials = _credentialsCache.GetItem(GetCacheId(eh), new Uri(serverUrl));
if (credentials == null || !credentials.ContainsKey(username))
{
return null;
}
string password = delDecrypt(credentials[username]);
return new ServerCredentials()
{
Host = new Uri(serverUrl),
UserId = username,
Password = password
};
}
public ServerCredentials GetCredentials(IEssentialHeaders eh, string server, string username)
{
ServerCredentials creds = GetCredentialsFromCache(eh, server, username);
if (creds == null && _useOAuth2)
{
List<ServerCredentials> credsList = GetSystemCredentialsByToken(eh.GetBearerToken(), new Uri(server)).Result;
creds = credsList.FirstOrDefault(c => string.Equals(c.UserId, username, StringComparison.Ordinal));
if (creds == null || creds.IsNullOrEmpty())
{
return null;
}
}
return creds;
}
public string GetCacheId(IEssentialHeaders eh)
{
return _useOAuth2 ? GetClaimFromToken(eh.GetBearerToken(), SUBJECT) : eh.GetCookieValue();
}
}
}