Merge pull request #10154 from abpframework/pr/10142

Improve IdentityServer module.
pull/10160/head
liangshiwei 4 years ago committed by GitHub
commit 52c725576c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -36,11 +36,10 @@ namespace Volo.Abp.IdentityServer.ApiResources
}
public ApiResource(Guid id, [NotNull] string name, string displayName = null, string description = null)
: base(id)
{
Check.NotNull(name, nameof(name));
Id = id;
Name = name;
DisplayName = displayName;
@ -124,7 +123,15 @@ namespace Volo.Abp.IdentityServer.ApiResources
public virtual void AddProperty([NotNull] string key, string value)
{
Properties.Add(new ApiResourceProperty(Id, key, value));
var property = FindProperty(key);
if (property == null)
{
Properties.Add(new ApiResourceProperty(Id, key, value));
}
else
{
property.Value = value;
}
}
public virtual void RemoveAllProperties()

@ -40,11 +40,11 @@ namespace Volo.Abp.IdentityServer.ApiScopes
bool required = false,
bool emphasize = false,
bool showInDiscoveryDocument = true,
bool enabled = true)
bool enabled = true
) : base(id)
{
Check.NotNull(name, nameof(name));
Id = id;
Name = name;
DisplayName = displayName ?? name;
Description = description;
@ -79,7 +79,15 @@ namespace Volo.Abp.IdentityServer.ApiScopes
public virtual void AddProperty([NotNull] string key, string value)
{
Properties.Add(new ApiScopeProperty(Id, key, value));
var property = FindProperty(key);
if (property == null)
{
Properties.Add(new ApiScopeProperty(Id, key, value));
}
else
{
property.Value = value;
}
}
public virtual void RemoveAllProperties()

@ -266,7 +266,15 @@ namespace Volo.Abp.IdentityServer.Clients
public virtual void AddProperty([NotNull] string key, [NotNull] string value)
{
Properties.Add(new ClientProperty(Id, key,value));
var property = FindProperty(key);
if (property == null)
{
Properties.Add(new ClientProperty(Id, key, value));
}
else
{
property.Value = value;
}
}
public virtual void RemoveAllProperties()
@ -274,17 +282,17 @@ namespace Volo.Abp.IdentityServer.Clients
Properties.Clear();
}
public virtual void RemoveProperty(string key, string value)
public virtual void RemoveProperty(string key)
{
Properties.RemoveAll(c => c.Value == value && c.Key == key);
Properties.RemoveAll(c => c.Key == key);
}
public virtual ClientProperty FindProperty(string key, string value)
public virtual ClientProperty FindProperty(string key)
{
return Properties.FirstOrDefault(c => c.Key == key && c.Value == value);
return Properties.FirstOrDefault(c => c.Key == key);
}
public virtual void AddClaim([NotNull] string value, string type)
public virtual void AddClaim([NotNull] string type, [NotNull] string value)
{
Claims.Add(new ClientClaim(Id, type, value));
}
@ -294,12 +302,22 @@ namespace Volo.Abp.IdentityServer.Clients
Claims.Clear();
}
public virtual void RemoveClaim(string value, string type)
public virtual void RemoveClaim(string type)
{
Claims.RemoveAll(c => c.Value == value && c.Type == type);
Claims.RemoveAll(c => c.Type == type);
}
public virtual ClientClaim FindClaim(string value, string 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);
}

@ -1,4 +1,4 @@
using System;
using System;
using JetBrains.Annotations;
using Volo.Abp.Domain.Entities;
@ -17,11 +17,6 @@ namespace Volo.Abp.IdentityServer.Clients
}
public virtual bool Equals(Guid clientId, string value, string type)
{
return ClientId == clientId && Type == type && Value == value;
}
protected internal ClientClaim(Guid clientId, [NotNull] string type, string value)
{
Check.NotNull(type, nameof(type));
@ -31,9 +26,14 @@ namespace Volo.Abp.IdentityServer.Clients
Value = value;
}
public virtual bool Equals(Guid clientId, string type, string value)
{
return ClientId == clientId && Type == type && Value == value;
}
public override object[] GetKeys()
{
return new object[] { ClientId, Type, Value };
}
}
}
}

@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using IdentityModel;
using IdentityServer4.Models;
@ -38,7 +38,7 @@ namespace Volo.Abp.IdentityServer.Devices
DeviceCode = deviceCode,
UserCode = userCode,
ClientId = data.ClientId,
SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value,
SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject)?.Value,
CreationTime = data.CreationTime,
Expiration = data.CreationTime.AddSeconds(data.Lifetime),
Data = Serialize(data)
@ -93,7 +93,7 @@ namespace Volo.Abp.IdentityServer.Devices
throw new InvalidOperationException($"Could not update device code by the given userCode: {userCode}");
}
deviceCodes.SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject).Value;
deviceCodes.SubjectId = data.Subject?.FindFirst(JwtClaimTypes.Subject)?.Value;
deviceCodes.Data = Serialize(data);
await DeviceFlowCodesRepository

@ -27,12 +27,11 @@ namespace Volo.Abp.IdentityServer.Grants
protected PersistedGrant()
{
}
public PersistedGrant(Guid id)
: base(id)
{
Id = id;
}
}
}

@ -39,11 +39,11 @@ namespace Volo.Abp.IdentityServer.IdentityResources
bool enabled = true,
bool required = false,
bool emphasize = false,
bool showInDiscoveryDocument = true)
bool showInDiscoveryDocument = true
) : base(id)
{
Check.NotNull(name, nameof(name));
Id = id;
Name = name;
DisplayName = displayName;
Description = description;
@ -57,8 +57,8 @@ namespace Volo.Abp.IdentityServer.IdentityResources
}
public IdentityResource(Guid id, IdentityServer4.Models.IdentityResource resource)
: base(id)
{
Id = id;
Name = resource.Name;
DisplayName = resource.DisplayName;
Description = resource.Description;
@ -92,7 +92,15 @@ namespace Volo.Abp.IdentityServer.IdentityResources
public virtual void AddProperty([NotNull] string key, string value)
{
Properties.Add(new IdentityResourceProperty(Id, key, value));
var property = FindProperty(key);
if (property == null)
{
Properties.Add(new IdentityResourceProperty(Id, key, value));
}
else
{
property.Value = value;
}
}
public virtual void RemoveAllProperties()

@ -20,23 +20,20 @@ namespace Volo.Abp.IdentityServer.ApiResources
public async Task<ApiResource> FindByNameAsync(string apiResourceName, bool includeDetails = true, CancellationToken cancellationToken = default)
{
var query = from apiResource in (await GetDbSetAsync()).IncludeDetails(includeDetails)
where apiResource.Name == apiResourceName
orderby apiResource.Id
select apiResource;
return await query.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.OrderBy(apiResource => apiResource.Id)
.FirstOrDefaultAsync(apiResource => apiResource.Name == apiResourceName, GetCancellationToken(cancellationToken));
}
public async Task<List<ApiResource>> FindByNameAsync(string[] apiResourceNames, bool includeDetails = true,
CancellationToken cancellationToken = default)
{
var query = from apiResource in (await GetDbSetAsync()).IncludeDetails(includeDetails)
where apiResourceNames.Contains(apiResource.Name)
orderby apiResource.Name
select apiResource;
return await query.ToListAsync(GetCancellationToken(cancellationToken));
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.Where(apiResource => apiResourceNames.Contains(apiResource.Name))
.OrderBy(apiResource => apiResource.Name)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task<List<ApiResource>> GetListByScopesAsync(
@ -44,11 +41,10 @@ namespace Volo.Abp.IdentityServer.ApiResources
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
var query = from api in (await GetDbSetAsync()).IncludeDetails(includeDetails)
where api.Scopes.Any(x => scopeNames.Contains(x.Scope))
select api;
return await query.ToListAsync(GetCancellationToken(cancellationToken));
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.Where(api => api.Scopes.Any(x => scopeNames.Contains(x.Scope)))
.ToListAsync(GetCancellationToken(cancellationToken));
}
public virtual async Task<List<ApiResource>> GetListAsync(

@ -21,6 +21,7 @@ namespace Volo.Abp.IdentityServer.ApiScopes
public async Task<ApiScope> FindByNameAsync(string scopeName, bool includeDetails = true, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.OrderBy(x=>x.Id)
.FirstOrDefaultAsync(x => x.Name == scopeName, GetCancellationToken(cancellationToken));
}
@ -28,12 +29,11 @@ namespace Volo.Abp.IdentityServer.ApiScopes
public async Task<List<ApiScope>> GetListByNameAsync(string[] scopeNames, bool includeDetails = false,
CancellationToken cancellationToken = default)
{
var query = from scope in (await GetDbSetAsync()).IncludeDetails(includeDetails)
where scopeNames.Contains(scope.Name)
orderby scope.Id
select scope;
return await query.ToListAsync(GetCancellationToken(cancellationToken));
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.Where(scope => scopeNames.Contains(scope.Name))
.OrderBy(scope => scope.Id)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<List<ApiScope>> GetListAsync(string sorting, int skipCount, int maxResultCount, string filter = null, bool includeDetails = false, CancellationToken cancellationToken = default)

@ -58,7 +58,7 @@ namespace Volo.Abp.IdentityServer.Clients
public virtual async Task<bool> CheckClientIdExistAsync(string clientId, Guid? expectedId = null, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).AnyAsync(c => c.Id != expectedId && c.ClientId == clientId, cancellationToken: cancellationToken);
return await (await GetDbSetAsync()).AnyAsync(c => c.Id != expectedId && c.ClientId == clientId, GetCancellationToken(cancellationToken));
}
public async override Task DeleteAsync(Guid id, bool autoSave = false, CancellationToken cancellationToken = default)

@ -24,9 +24,8 @@ namespace Volo.Abp.IdentityServer.Devices
CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.Where(d => d.UserCode == userCode)
.OrderBy(d => d.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
.FirstOrDefaultAsync(d => d.UserCode == userCode, GetCancellationToken(cancellationToken));
}
public virtual async Task<DeviceFlowCodes> FindByDeviceCodeAsync(
@ -34,9 +33,8 @@ namespace Volo.Abp.IdentityServer.Devices
CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.Where(d => d.DeviceCode == deviceCode)
.OrderBy(d => d.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
.FirstOrDefaultAsync(d => d.DeviceCode == deviceCode, GetCancellationToken(cancellationToken));
}
public virtual async Task<List<DeviceFlowCodes>> GetListByExpirationAsync(DateTime maxExpirationDate, int maxResultCount,

@ -30,9 +30,8 @@ namespace Volo.Abp.IdentityServer.Grants
CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.Where(x => x.Key == key)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
.FirstOrDefaultAsync(x => x.Key == key, GetCancellationToken(cancellationToken));
}
public virtual async Task<List<PersistedGrant>> GetListBySubjectIdAsync(

@ -24,11 +24,10 @@ namespace Volo.Abp.IdentityServer.IdentityResources
bool includeDetails = false,
CancellationToken cancellationToken = default)
{
var query = from identityResource in (await GetDbSetAsync()).IncludeDetails(includeDetails)
where scopeNames.Contains(identityResource.Name)
select identityResource;
return await query.ToListAsync(GetCancellationToken(cancellationToken));
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.Where(identityResource => scopeNames.Contains(identityResource.Name))
.ToListAsync(GetCancellationToken(cancellationToken));
}
[Obsolete("Use WithDetailsAsync method.")]
@ -72,14 +71,13 @@ namespace Volo.Abp.IdentityServer.IdentityResources
{
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.Where(x => x.Name == name)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
.FirstOrDefaultAsync(x => x.Name == name, GetCancellationToken(cancellationToken));
}
public virtual async Task<bool> CheckNameExistAsync(string name, Guid? expectedId = null, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync()).AnyAsync(ir => ir.Id != expectedId && ir.Name == name, cancellationToken: cancellationToken);
return await (await GetDbSetAsync()).AnyAsync(ir => ir.Id != expectedId && ir.Name == name, GetCancellationToken(cancellationToken));
}
}
}

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -21,9 +21,8 @@ namespace Volo.Abp.IdentityServer.MongoDB
public async Task<ApiResource> FindByNameAsync(string apiResourceName, bool includeDetails = true, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
.Where(ar => ar.Name == apiResourceName)
.OrderBy(ar => ar.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
.FirstOrDefaultAsync(ar => ar.Name == apiResourceName, GetCancellationToken(cancellationToken));
}
public async Task<List<ApiResource>> FindByNameAsync(string[] apiResourceNames, bool includeDetails = true,

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -23,20 +23,17 @@ namespace Volo.Abp.IdentityServer.MongoDB
public async Task<ApiScope> FindByNameAsync(string scopeName, bool includeDetails = true, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
.Where(x => x.Name == scopeName)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
.FirstOrDefaultAsync(x => x.Name == scopeName, GetCancellationToken(cancellationToken));
}
public async Task<List<ApiScope>> GetListByNameAsync(string[] scopeNames, bool includeDetails = false,
CancellationToken cancellationToken = default)
{
var query = from scope in (await GetMongoQueryableAsync(cancellationToken))
where scopeNames.Contains(scope.Name)
orderby scope.Id
select scope;
return await query.ToListAsync(GetCancellationToken(cancellationToken));
return await (await GetMongoQueryableAsync(cancellationToken))
.Where(scope => scopeNames.Contains(scope.Name))
.OrderBy(scope => scope.Id)
.ToListAsync(GetCancellationToken(cancellationToken));
}
public async Task<List<ApiScope>> GetListAsync(string sorting, int skipCount, int maxResultCount, string filter = null, bool includeDetails = false,

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
@ -27,9 +27,8 @@ namespace Volo.Abp.IdentityServer.MongoDB
CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
.Where(x => x.ClientId == clientId)
.OrderBy(x => x.Id)
.FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
.FirstOrDefaultAsync(x => x.ClientId == clientId, GetCancellationToken(cancellationToken));
}
public virtual async Task<List<Client>> GetListAsync(
@ -69,7 +68,7 @@ namespace Volo.Abp.IdentityServer.MongoDB
public virtual async Task<bool> CheckClientIdExistAsync(string clientId, Guid? expectedId = null, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
.AnyAsync(c => c.Id != expectedId && c.ClientId == clientId, cancellationToken: cancellationToken);
.AnyAsync(c => c.Id != expectedId && c.ClientId == clientId, GetCancellationToken(cancellationToken));
}
}
}

@ -62,7 +62,7 @@ namespace Volo.Abp.IdentityServer.MongoDB
public virtual async Task<bool> CheckNameExistAsync(string name, Guid? expectedId = null, CancellationToken cancellationToken = default)
{
return await (await GetMongoQueryableAsync(cancellationToken))
.AnyAsync(ir => ir.Id != expectedId && ir.Name == name, cancellationToken: cancellationToken);
.AnyAsync(ir => ir.Id != expectedId && ir.Name == name, GetCancellationToken(cancellationToken));
}
}
}

@ -1,4 +1,4 @@
using System;
using System;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
@ -149,7 +149,7 @@ namespace Volo.Abp.IdentityServer
client.AddCorsOrigin("https://client1-origin.com");
client.AddCorsOrigin("https://{0}.abp.io");
client.AddClaim(nameof(ClientClaim.Value), nameof(ClientClaim.Type));
client.AddClaim(nameof(ClientClaim.Type), nameof(ClientClaim.Value));
client.AddGrantType(nameof(ClientGrantType.GrantType));
client.AddIdentityProviderRestriction(nameof(ClientIdPRestriction.Provider));
client.AddPostLogoutRedirectUri(nameof(ClientPostLogoutRedirectUri.PostLogoutRedirectUri));

Loading…
Cancel
Save