diff --git a/src/Unosquare.Labs.EmbedIO.BearerToken/BearerToken.cs b/src/Unosquare.Labs.EmbedIO.BearerToken/BearerToken.cs index e53cee6..ebd5beb 100644 --- a/src/Unosquare.Labs.EmbedIO.BearerToken/BearerToken.cs +++ b/src/Unosquare.Labs.EmbedIO.BearerToken/BearerToken.cs @@ -29,6 +29,6 @@ public class BearerToken /// Client username. /// [JsonProperty("userName")] - public string Username { get; set; } + public string? Username { get; set; } } } diff --git a/src/Unosquare.Labs.EmbedIO.BearerToken/BearerTokenModule.cs b/src/Unosquare.Labs.EmbedIO.BearerToken/BearerTokenModule.cs index 41f8f98..ddd72cb 100644 --- a/src/Unosquare.Labs.EmbedIO.BearerToken/BearerTokenModule.cs +++ b/src/Unosquare.Labs.EmbedIO.BearerToken/BearerTokenModule.cs @@ -1,6 +1,9 @@ -namespace EmbedIO.BearerToken +using Swan.Formatters; + +namespace EmbedIO.BearerToken { using Microsoft.IdentityModel.Tokens; + using System.Collections.Generic; using System; using System.Text; using System.Threading.Tasks; @@ -67,6 +70,14 @@ public BearerTokenModule( /// public SymmetricSecurityKey SecretKey { get; } + /// + /// Gets or sets the on success transformation method. + /// + /// + /// The on success. + /// + public Action>? OnSuccessTransformation { get; set; } + /// public override bool IsFinalHandler => false; @@ -107,14 +118,20 @@ await _authorizationServerProvider.ValidateClientAuthentication(validationContex DateTime.FromBinary(_authorizationServerProvider.GetExpirationDate()), DateTimeKind.Utc); + var token = new BearerToken + { + Token = validationContext.GetToken(SecretKey, expiryDate), + TokenType = "bearer", + ExpirationDate = _authorizationServerProvider.GetExpirationDate(), + Username = validationContext.IdentityName, + }; + + var dictToken = Json.Deserialize>(Json.Serialize(token)); + + OnSuccessTransformation?.Invoke(dictToken); + await context - .SendDataAsync(new BearerToken - { - Token = validationContext.GetToken(SecretKey, expiryDate), - TokenType = "bearer", - ExpirationDate = _authorizationServerProvider.GetExpirationDate(), - Username = validationContext.IdentityName, - }) + .SendDataAsync(dictToken) .ConfigureAwait(false); } } diff --git a/src/Unosquare.Labs.EmbedIO.BearerToken/Unosquare.Labs.EmbedIO.BearerToken.csproj b/src/Unosquare.Labs.EmbedIO.BearerToken/Unosquare.Labs.EmbedIO.BearerToken.csproj index 55a4b67..5a2d587 100644 --- a/src/Unosquare.Labs.EmbedIO.BearerToken/Unosquare.Labs.EmbedIO.BearerToken.csproj +++ b/src/Unosquare.Labs.EmbedIO.BearerToken/Unosquare.Labs.EmbedIO.BearerToken.csproj @@ -10,7 +10,7 @@ 8.0 enable ..\..\StyleCop.Analyzers.ruleset - 3.2.0 + 3.3.0 EmbedIO.BearerToken diff --git a/src/Unosquare.Labs.EmbedIO.ExtraSample/Program.cs b/src/Unosquare.Labs.EmbedIO.ExtraSample/Program.cs index a827f5c..95c286e 100644 --- a/src/Unosquare.Labs.EmbedIO.ExtraSample/Program.cs +++ b/src/Unosquare.Labs.EmbedIO.ExtraSample/Program.cs @@ -45,9 +45,17 @@ private static async Task Main(string[] args) tokenSource.Cancel(); }, tokenSource.Token); + var bearerTokenModule = new BearerTokenModule( + "/api", + authServer, + "0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9eyJjbGF") + { + OnSuccessTransformation = dict => { dict.Add("logged", true); }, + }; + // Our web server is disposable. using var server = new WebServer(url) - .WithBearerToken("/api", "0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9eyJjbGF", authServer) + .WithModule(bearerTokenModule) .WithModule(new JsonServerModule(jsonPath: Path.Combine(WebRootPath, "database.json"))) .WithModule(new MarkdownStaticModule("/", WebRootPath)); diff --git a/src/Unosquare.Labs.EmbedIO.ExtraSample/Unosquare.Labs.EmbedIO.ExtraSample.csproj b/src/Unosquare.Labs.EmbedIO.ExtraSample/Unosquare.Labs.EmbedIO.ExtraSample.csproj index ee6a63f..02e06dd 100644 --- a/src/Unosquare.Labs.EmbedIO.ExtraSample/Unosquare.Labs.EmbedIO.ExtraSample.csproj +++ b/src/Unosquare.Labs.EmbedIO.ExtraSample/Unosquare.Labs.EmbedIO.ExtraSample.csproj @@ -7,6 +7,7 @@ Exe ..\..\StyleCop.Analyzers.ruleset 8.0 + enable EmbedIO.ExtraSample