Skip to content
This repository has been archived by the owner on Aug 10, 2022. It is now read-only.

Commit

Permalink
Feature: Support to define an OnSuccessTransformation
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Dec 26, 2019
1 parent fa2bc0f commit e146f0d
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Unosquare.Labs.EmbedIO.BearerToken/BearerToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public class BearerToken
/// Client username.
/// </summary>
[JsonProperty("userName")]
public string Username { get; set; }
public string? Username { get; set; }
}
}
33 changes: 25 additions & 8 deletions src/Unosquare.Labs.EmbedIO.BearerToken/BearerTokenModule.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -67,6 +70,14 @@ public BearerTokenModule(
/// </value>
public SymmetricSecurityKey SecretKey { get; }

/// <summary>
/// Gets or sets the on success transformation method.
/// </summary>
/// <value>
/// The on success.
/// </value>
public Action<IDictionary<string, object>>? OnSuccessTransformation { get; set; }

/// <inheritdoc />
public override bool IsFinalHandler => false;

Expand Down Expand Up @@ -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<Dictionary<string, object>>(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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
<Version>3.2.0</Version>
<Version>3.3.0</Version>
<RootNamespace>EmbedIO.BearerToken</RootNamespace>
</PropertyGroup>

Expand Down
10 changes: 9 additions & 1 deletion src/Unosquare.Labs.EmbedIO.ExtraSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<OutputType>Exe</OutputType>
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>
<RootNamespace>EmbedIO.ExtraSample</RootNamespace>
</PropertyGroup>

Expand Down

0 comments on commit e146f0d

Please sign in to comment.