/
Client.cs
344 lines (251 loc) · 10.1 KB
/
Client.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
using System;
using System.Collections.Generic;
using System.Linq;
using IdentityServer4;
using IdentityServer4.Models;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities.Auditing;
namespace Volo.Abp.IdentityServer.Clients;
public class Client : FullAuditedAggregateRoot<Guid>
{
public virtual string ClientId { get; set; }
public virtual string ClientName { get; set; }
public virtual string Description { get; set; }
public virtual string ClientUri { get; set; }
public virtual string LogoUri { get; set; }
public virtual bool Enabled { get; set; } = true;
public virtual string ProtocolType { get; set; }
public virtual bool RequireClientSecret { get; set; }
public virtual bool RequireConsent { get; set; }
public virtual bool AllowRememberConsent { get; set; }
public virtual bool AlwaysIncludeUserClaimsInIdToken { get; set; }
public virtual bool RequirePkce { get; set; }
public virtual bool AllowPlainTextPkce { get; set; }
public virtual bool RequireRequestObject { get; set; }
public virtual bool AllowAccessTokensViaBrowser { get; set; }
public virtual string FrontChannelLogoutUri { get; set; }
public virtual bool FrontChannelLogoutSessionRequired { get; set; }
public virtual string BackChannelLogoutUri { get; set; }
public virtual bool BackChannelLogoutSessionRequired { get; set; }
public virtual bool AllowOfflineAccess { get; set; }
public virtual int IdentityTokenLifetime { get; set; }
public virtual string AllowedIdentityTokenSigningAlgorithms { get; set; }
public virtual int AccessTokenLifetime { get; set; }
public virtual int AuthorizationCodeLifetime { get; set; }
public virtual int? ConsentLifetime { get; set; }
public virtual int AbsoluteRefreshTokenLifetime { get; set; }
public virtual int SlidingRefreshTokenLifetime { get; set; }
public virtual int RefreshTokenUsage { get; set; }
public virtual bool UpdateAccessTokenClaimsOnRefresh { get; set; }
public virtual int RefreshTokenExpiration { get; set; }
public virtual int AccessTokenType { get; set; }
public virtual bool EnableLocalLogin { get; set; }
public virtual bool IncludeJwtId { get; set; }
public virtual bool AlwaysSendClientClaims { get; set; }
public virtual string ClientClaimsPrefix { get; set; }
public virtual string PairWiseSubjectSalt { get; set; }
public virtual int? UserSsoLifetime { get; set; }
public virtual string UserCodeType { get; set; }
public virtual int DeviceCodeLifetime { get; set; } = 300;
public virtual List<ClientScope> AllowedScopes { get; set; }
public virtual List<ClientSecret> ClientSecrets { get; set; }
public virtual List<ClientGrantType> AllowedGrantTypes { get; set; }
public virtual List<ClientCorsOrigin> AllowedCorsOrigins { get; set; }
public virtual List<ClientRedirectUri> RedirectUris { get; set; }
public virtual List<ClientPostLogoutRedirectUri> PostLogoutRedirectUris { get; set; }
public virtual List<ClientIdPRestriction> IdentityProviderRestrictions { get; set; }
public virtual List<ClientClaim> Claims { get; set; }
public virtual List<ClientProperty> Properties { get; set; }
protected Client()
{
}
public Client(Guid id, [NotNull] string clientId)
: base(id)
{
Check.NotNull(clientId, nameof(clientId));
ClientId = clientId;
//TODO: Replace magics with constants?
ProtocolType = IdentityServerConstants.ProtocolTypes.OpenIdConnect;
RequireClientSecret = true;
RequireConsent = false;
AllowRememberConsent = true;
RequirePkce = true;
FrontChannelLogoutSessionRequired = true;
BackChannelLogoutSessionRequired = true;
IdentityTokenLifetime = 300;
AccessTokenLifetime = 3600;
AuthorizationCodeLifetime = 300;
AbsoluteRefreshTokenLifetime = 2592000;
SlidingRefreshTokenLifetime = 1296000;
RefreshTokenUsage = (int)TokenUsage.OneTimeOnly;
RefreshTokenExpiration = (int)TokenExpiration.Absolute;
AccessTokenType = (int)IdentityServer4.Models.AccessTokenType.Jwt;
EnableLocalLogin = true;
ClientClaimsPrefix = "client_";
AllowedScopes = new List<ClientScope>();
ClientSecrets = new List<ClientSecret>();
AllowedGrantTypes = new List<ClientGrantType>();
AllowedCorsOrigins = new List<ClientCorsOrigin>();
RedirectUris = new List<ClientRedirectUri>();
PostLogoutRedirectUris = new List<ClientPostLogoutRedirectUri>();
IdentityProviderRestrictions = new List<ClientIdPRestriction>();
Claims = new List<ClientClaim>();
Properties = new List<ClientProperty>();
}
public virtual void AddGrantType([NotNull] string grantType)
{
AllowedGrantTypes.Add(new ClientGrantType(Id, grantType));
}
public virtual void RemoveAllAllowedGrantTypes()
{
AllowedGrantTypes.Clear();
}
public virtual void RemoveGrantType(string grantType)
{
AllowedGrantTypes.RemoveAll(r => r.GrantType == grantType);
}
public virtual ClientGrantType FindGrantType(string grantType)
{
return AllowedGrantTypes.FirstOrDefault(r => r.GrantType == grantType);
}
public virtual void AddSecret([NotNull] string value, DateTime? expiration = null, string type = IdentityServerConstants.SecretTypes.SharedSecret, string description = null)
{
ClientSecrets.Add(new ClientSecret(Id, value, expiration, type, description));
}
public virtual void RemoveSecret([NotNull] string value, string type = IdentityServerConstants.SecretTypes.SharedSecret)
{
ClientSecrets.RemoveAll(s => s.Value == value && s.Type == type);
}
public virtual ClientSecret FindSecret([NotNull] string value, string type = IdentityServerConstants.SecretTypes.SharedSecret)
{
return ClientSecrets.FirstOrDefault(s => s.Type == type && s.Value == value);
}
public virtual void AddScope([NotNull] string scope)
{
AllowedScopes.Add(new ClientScope(Id, scope));
}
public virtual void RemoveAllScopes()
{
AllowedScopes.Clear();
}
public virtual void RemoveScope(string scope)
{
AllowedScopes.RemoveAll(r => r.Scope == scope);
}
public virtual ClientScope FindScope(string scope)
{
return AllowedScopes.FirstOrDefault(r => r.Scope == scope);
}
public virtual void AddCorsOrigin([NotNull] string origin)
{
AllowedCorsOrigins.Add(new ClientCorsOrigin(Id, origin));
}
public virtual void AddRedirectUri([NotNull] string redirectUri)
{
RedirectUris.Add(new ClientRedirectUri(Id, redirectUri));
}
public virtual void AddPostLogoutRedirectUri([NotNull] string postLogoutRedirectUri)
{
PostLogoutRedirectUris.Add(new ClientPostLogoutRedirectUri(Id, postLogoutRedirectUri));
}
public virtual void RemoveAllCorsOrigins()
{
AllowedCorsOrigins.Clear();
}
public virtual void RemoveCorsOrigin(string uri)
{
AllowedCorsOrigins.RemoveAll(c => c.Origin == uri);
}
public virtual void RemoveAllRedirectUris()
{
RedirectUris.Clear();
}
public virtual void RemoveRedirectUri(string uri)
{
RedirectUris.RemoveAll(r => r.RedirectUri == uri);
}
public virtual void RemoveAllPostLogoutRedirectUris()
{
PostLogoutRedirectUris.Clear();
}
public virtual void RemovePostLogoutRedirectUri(string uri)
{
PostLogoutRedirectUris.RemoveAll(p => p.PostLogoutRedirectUri == uri);
}
public virtual ClientCorsOrigin FindCorsOrigin(string uri)
{
return AllowedCorsOrigins.FirstOrDefault(c => c.Origin == uri);
}
public virtual ClientRedirectUri FindRedirectUri(string uri)
{
return RedirectUris.FirstOrDefault(r => r.RedirectUri == uri);
}
public virtual ClientPostLogoutRedirectUri FindPostLogoutRedirectUri(string uri)
{
return PostLogoutRedirectUris.FirstOrDefault(p => p.PostLogoutRedirectUri == uri);
}
public virtual void AddProperty([NotNull] string key, [NotNull] string value)
{
var property = FindProperty(key);
if (property == null)
{
Properties.Add(new ClientProperty(Id, key, value));
}
else
{
property.Value = value;
}
}
public virtual void RemoveAllProperties()
{
Properties.Clear();
}
public virtual void RemoveProperty(string key)
{
Properties.RemoveAll(c => c.Key == key);
}
public virtual ClientProperty FindProperty(string key)
{
return Properties.FirstOrDefault(c => c.Key == key);
}
public virtual void AddClaim([NotNull] string type, [NotNull] string value)
{
Claims.Add(new ClientClaim(Id, type, value));
}
public virtual void RemoveAllClaims()
{
Claims.Clear();
}
public virtual void RemoveClaim(string type)
{
Claims.RemoveAll(c => c.Type == type);
}
public virtual void RemoveClaim(string type, string value)
{
Claims.RemoveAll(c => c.Type == type && c.Value == value);
}
public virtual List<ClientClaim> FindClaims(string type)
{
return Claims.Where(c => c.Type == type).ToList();
}
public virtual ClientClaim FindClaim(string type, string value)
{
return Claims.FirstOrDefault(c => c.Type == type && c.Value == value);
}
public virtual void AddIdentityProviderRestriction([NotNull] string provider)
{
IdentityProviderRestrictions.Add(new ClientIdPRestriction(Id, provider));
}
public virtual void RemoveAllIdentityProviderRestrictions()
{
IdentityProviderRestrictions.Clear();
}
public virtual void RemoveIdentityProviderRestriction(string provider)
{
IdentityProviderRestrictions.RemoveAll(r => r.Provider == provider);
}
public virtual ClientIdPRestriction FindIdentityProviderRestriction(string provider)
{
return IdentityProviderRestrictions.FirstOrDefault(r => r.Provider == provider);
}
}