-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added swagger authentication for identity server (#217)
* Added swagger authentication for identity server * Make things less hardcoded Co-authored-by: Dmytro M <[email protected]>
- Loading branch information
1 parent
15c16c2
commit 691815d
Showing
14 changed files
with
294 additions
and
110 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
OutOfSchool/OutOfSchool.DataAccess/Extensions/DbSetExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.ChangeTracking; | ||
|
||
#nullable enable | ||
|
||
namespace OutOfSchool.Services.Extensions | ||
{ | ||
public static class DbSetExtensions | ||
{ | ||
/// <summary> | ||
/// Add entry if it doesn't exist based on a predicate. | ||
/// Not thread safe! There's a window between Any and Add where another thread can add an entry. | ||
/// Performance issues is need to add multiple entities. | ||
/// </summary> | ||
/// <param name="dbSet">Extension target.</param> | ||
/// <param name="entity">Entry to add.</param> | ||
/// <param name="predicate">Optional predicate.</param> | ||
/// <typeparam name="T">Entity type.</typeparam> | ||
/// <returns>Return an entity that was just added or null.</returns> | ||
public static EntityEntry<T>? AddIfNotExists<T>(this DbSet<T> dbSet, T entity, Expression<Func<T, bool>>? predicate = null) | ||
where T : class, new() | ||
{ | ||
var exists = predicate != null ? dbSet.Any(predicate) : dbSet.Any(); | ||
return !exists ? dbSet.Add(entity) : null; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
OutOfSchool/OutOfSchool.IdentityServer/Config/AdditionalIdentityClients.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace OutOfSchool.IdentityServer.Config | ||
{ | ||
public class AdditionalIdentityClients | ||
{ | ||
public string ClientId { get; set; } | ||
|
||
public string[] RedirectUris { get; set; } | ||
|
||
public string[] PostLogoutRedirectUris { get; set; } | ||
|
||
public string[] AllowedCorsOrigins { get; set; } | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
OutOfSchool/OutOfSchool.IdentityServer/Config/IdentityAccessOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace OutOfSchool.IdentityServer.Config | ||
{ | ||
public class IdentityAccessOptions | ||
{ | ||
public readonly string Name = "IdentityAccessConfig"; | ||
|
||
public AdditionalIdentityClients[] AdditionalIdentityClients { get; set; } | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
OutOfSchool/OutOfSchool.IdentityServer/Config/StaticConfig.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using IdentityServer4; | ||
using IdentityServer4.Models; | ||
|
||
namespace OutOfSchool.IdentityServer.Config | ||
{ | ||
public static class StaticConfig | ||
{ | ||
public static IEnumerable<IdentityResource> IdentityResources => | ||
new[] | ||
{ | ||
new IdentityResources.OpenId(), | ||
new IdentityResources.Profile(), | ||
new IdentityResource | ||
{ | ||
Name = "role", | ||
UserClaims = new List<string> {"role"}, | ||
}, | ||
}; | ||
|
||
public static IEnumerable<ApiScope> ApiScopes => | ||
new[] | ||
{ | ||
new ApiScope("outofschoolapi.read"), | ||
new ApiScope("outofschoolapi.write"), | ||
}; | ||
|
||
public static IEnumerable<ApiResource> ApiResources(string apiSecret) => new[] | ||
{ | ||
new ApiResource("outofschoolapi") | ||
{ | ||
Scopes = new List<string> {"outofschoolapi.read", "outofschoolapi.write"}, | ||
ApiSecrets = new List<Secret> { new Secret(apiSecret.Sha256()) }, | ||
UserClaims = new List<string> {"role"}, | ||
}, | ||
}; | ||
|
||
public static IEnumerable<Client> Clients(string clientSecret, IEnumerable<AdditionalIdentityClients> additionalClients) | ||
{ | ||
// m2m client credentials flow client | ||
var clients = new List<Client> | ||
{ | ||
new Client | ||
{ | ||
ClientId = "m2m.client", | ||
ClientName = "Client Credentials Client", | ||
AllowedGrantTypes = GrantTypes.ClientCredentials, | ||
ClientSecrets = {new Secret(clientSecret.Sha256()) }, | ||
AllowedScopes = {"outofschoolapi.read", "outofschoolapi.write"}, | ||
}, | ||
}; | ||
|
||
clients.AddRange(additionalClients.Select(c => new Client | ||
{ | ||
ClientId = c.ClientId, | ||
AllowedGrantTypes = GrantTypes.Code, | ||
RequirePkce = true, | ||
RequireClientSecret = false, | ||
AllowOfflineAccess = true, | ||
|
||
RedirectUris = c.RedirectUris, | ||
PostLogoutRedirectUris = c.PostLogoutRedirectUris, | ||
AllowedCorsOrigins = c.AllowedCorsOrigins, | ||
|
||
AllowedScopes = | ||
{ | ||
IdentityServerConstants.StandardScopes.OpenId, | ||
"outofschoolapi.read", "outofschoolapi.write", | ||
}, | ||
|
||
AllowAccessTokensViaBrowser = true, | ||
RequireConsent = false, | ||
})); | ||
|
||
return clients; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.