-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented Security middleware (issue #8) and JWT token validation (…
…issue #21)
- Loading branch information
1 parent
4c895d0
commit 992e8ed
Showing
21 changed files
with
687 additions
and
12 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Onyx.Ianvs.Common | ||
{ | ||
/// <summary> | ||
/// Represents a Security Requirements Open Api object | ||
/// </summary> | ||
public class SecurityRequirement | ||
{ | ||
/// <summary> | ||
/// The required security scheme name | ||
/// </summary> | ||
public string SchemeName { get; set;} | ||
|
||
/// <summary> | ||
/// The authorizations scopes that must be granted to the caller | ||
/// </summary> | ||
public string[] Scopes { get; set; } | ||
} | ||
} |
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,53 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Onyx.Ianvs.Common | ||
{ | ||
/// <summary> | ||
/// Represents an Open API Security Scheme object | ||
/// </summary> | ||
public class SecurityScheme | ||
{ | ||
/// <summary> | ||
/// The security scheme name | ||
/// </summary> | ||
public string Name { get; set; } | ||
|
||
/// <summary> | ||
/// The security scheme type | ||
/// </summary> | ||
public string Type { get; set; } | ||
|
||
/// <summary> | ||
/// Where the authentication type is sent in the request; supported values: header, cookie | ||
/// </summary> | ||
public string In { get; set; } | ||
|
||
/// <summary> | ||
/// The security scheme; bearer, oauth, key, etc. | ||
/// </summary> | ||
public string Scheme { get; set; } | ||
|
||
/// <summary> | ||
/// Used as a hint for the value name: bearer, cookie name | ||
/// </summary> | ||
public string BearerFormat { get; set; } | ||
|
||
/// <summary> | ||
/// The Url of the auth authority openId configuration discovery Url | ||
/// </summary> | ||
public string OpenIdConnectUrl { get; set; } | ||
|
||
/// <summary> | ||
/// The Url of the auth credentials issuer/authority | ||
/// </summary> | ||
public string Issuer { get; set; } | ||
|
||
/// <summary> | ||
/// The list of valid audiences for the security scheme | ||
/// </summary> | ||
public string[] Audiences { get; set; } | ||
} | ||
} |
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
47 changes: 47 additions & 0 deletions
47
src/Ianvs/Configuration/Json/JsonSecurityRequirementParser.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,47 @@ | ||
using Onyx.Ianvs.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace Onyx.Ianvs.Configuration.Json | ||
{ | ||
public class JsonSecurityRequirementParser | ||
{ | ||
/// <summary> | ||
/// Parses a JSON representation of an Open API Security Requirement object | ||
/// </summary> | ||
/// <param name="serversData">JSON representation of an Open API Security Requirement object</param> | ||
/// <returns>An Open API Security Requirement object</returns> | ||
public static List<SecurityRequirement> Parse(JsonElement securityData) | ||
{ | ||
List<SecurityRequirement> security = new List<SecurityRequirement>(); | ||
foreach (JsonElement scheme in securityData.EnumerateArray()) | ||
{ | ||
security.Add(ParseSecurityRequirement(scheme)); | ||
} | ||
return security; | ||
} | ||
|
||
private static SecurityRequirement ParseSecurityRequirement(JsonElement definition) | ||
{ | ||
foreach (JsonProperty item in definition.EnumerateObject()) | ||
{ | ||
SecurityRequirement scheme = new SecurityRequirement() | ||
{ | ||
SchemeName = item.Name, | ||
Scopes = new string[item.Value.GetArrayLength()] | ||
}; | ||
|
||
int i = 0; | ||
foreach (JsonElement element in item.Value.EnumerateArray()) | ||
{ | ||
scheme.Scopes[i++] = element.GetString(); | ||
} | ||
return scheme; | ||
} | ||
return null; | ||
} | ||
} | ||
} |
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,51 @@ | ||
using Onyx.Ianvs.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
|
||
namespace Onyx.Ianvs.Configuration.Json | ||
{ | ||
public class JsonSecuritySchemeParser | ||
{ | ||
/// <summary> | ||
/// Parses a JSON representation of an Open API Security Schemes object | ||
/// </summary> | ||
/// <param name="serversData">JSON representation of an Open API Security Schemes object</param> | ||
/// <returns>An Open API Security Schemes object</returns> | ||
public static List<SecurityScheme> Parse(JsonElement securitySchemesData) | ||
{ | ||
List<SecurityScheme> securitySchemes = new List<SecurityScheme>(); | ||
foreach (JsonProperty scheme in securitySchemesData.EnumerateObject()) | ||
{ | ||
securitySchemes.Add(ParseSecurityScheme(scheme)); | ||
} | ||
return securitySchemes; | ||
} | ||
|
||
private static SecurityScheme ParseSecurityScheme(JsonProperty definition) | ||
{ | ||
SecurityScheme scheme = new SecurityScheme() | ||
{ | ||
Name = definition.Name | ||
}; | ||
foreach (JsonProperty property in definition.Value.EnumerateObject()) | ||
{ | ||
if (property.Name == IanvsMeta.E_CONFIG_SEC_SCHEME_TYPE) scheme.Type = property.Value.GetString(); | ||
if (property.Name == IanvsMeta.E_CONFIG_SEC_SCHEME_IN) scheme.In = property.Value.GetString(); | ||
if (property.Name == IanvsMeta.E_CONFIG_SEC_SCHEME_OPEN_ID_CONNECT_URL) scheme.OpenIdConnectUrl = property.Value.GetString(); | ||
if (property.Name == IanvsMeta.E_CONFIG_SEC_SCHEME_ISSUER) scheme.Issuer = property.Value.GetString(); | ||
if (property.Name == IanvsMeta.E_CONFIG_SEC_SCHEME_AUDIENCES) | ||
{ | ||
scheme.Audiences = new string[property.Value.GetArrayLength()]; | ||
for (int i = 0; i < scheme.Audiences.Length; i++) | ||
{ | ||
scheme.Audiences[i] = property.Value[i].GetString(); | ||
} | ||
} | ||
} | ||
return scheme; | ||
} | ||
} | ||
} |
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.