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

Commit e146f0d

Browse files
committed
Feature: Support to define an OnSuccessTransformation
1 parent fa2bc0f commit e146f0d

File tree

5 files changed

+37
-11
lines changed

5 files changed

+37
-11
lines changed

src/Unosquare.Labs.EmbedIO.BearerToken/BearerToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public class BearerToken
2929
/// Client username.
3030
/// </summary>
3131
[JsonProperty("userName")]
32-
public string Username { get; set; }
32+
public string? Username { get; set; }
3333
}
3434
}

src/Unosquare.Labs.EmbedIO.BearerToken/BearerTokenModule.cs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
namespace EmbedIO.BearerToken
1+
using Swan.Formatters;
2+
3+
namespace EmbedIO.BearerToken
24
{
35
using Microsoft.IdentityModel.Tokens;
6+
using System.Collections.Generic;
47
using System;
58
using System.Text;
69
using System.Threading.Tasks;
@@ -67,6 +70,14 @@ public BearerTokenModule(
6770
/// </value>
6871
public SymmetricSecurityKey SecretKey { get; }
6972

73+
/// <summary>
74+
/// Gets or sets the on success transformation method.
75+
/// </summary>
76+
/// <value>
77+
/// The on success.
78+
/// </value>
79+
public Action<IDictionary<string, object>>? OnSuccessTransformation { get; set; }
80+
7081
/// <inheritdoc />
7182
public override bool IsFinalHandler => false;
7283

@@ -107,14 +118,20 @@ await _authorizationServerProvider.ValidateClientAuthentication(validationContex
107118
DateTime.FromBinary(_authorizationServerProvider.GetExpirationDate()),
108119
DateTimeKind.Utc);
109120

121+
var token = new BearerToken
122+
{
123+
Token = validationContext.GetToken(SecretKey, expiryDate),
124+
TokenType = "bearer",
125+
ExpirationDate = _authorizationServerProvider.GetExpirationDate(),
126+
Username = validationContext.IdentityName,
127+
};
128+
129+
var dictToken = Json.Deserialize<Dictionary<string, object>>(Json.Serialize(token));
130+
131+
OnSuccessTransformation?.Invoke(dictToken);
132+
110133
await context
111-
.SendDataAsync(new BearerToken
112-
{
113-
Token = validationContext.GetToken(SecretKey, expiryDate),
114-
TokenType = "bearer",
115-
ExpirationDate = _authorizationServerProvider.GetExpirationDate(),
116-
Username = validationContext.IdentityName,
117-
})
134+
.SendDataAsync(dictToken)
118135
.ConfigureAwait(false);
119136
}
120137
}

src/Unosquare.Labs.EmbedIO.BearerToken/Unosquare.Labs.EmbedIO.BearerToken.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<LangVersion>8.0</LangVersion>
1111
<Nullable>enable</Nullable>
1212
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
13-
<Version>3.2.0</Version>
13+
<Version>3.3.0</Version>
1414
<RootNamespace>EmbedIO.BearerToken</RootNamespace>
1515
</PropertyGroup>
1616

src/Unosquare.Labs.EmbedIO.ExtraSample/Program.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,17 @@ private static async Task Main(string[] args)
4545
tokenSource.Cancel();
4646
}, tokenSource.Token);
4747

48+
var bearerTokenModule = new BearerTokenModule(
49+
"/api",
50+
authServer,
51+
"0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9eyJjbGF")
52+
{
53+
OnSuccessTransformation = dict => { dict.Add("logged", true); },
54+
};
55+
4856
// Our web server is disposable.
4957
using var server = new WebServer(url)
50-
.WithBearerToken("/api", "0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9eyJjbGF", authServer)
58+
.WithModule(bearerTokenModule)
5159
.WithModule(new JsonServerModule(jsonPath: Path.Combine(WebRootPath, "database.json")))
5260
.WithModule(new MarkdownStaticModule("/", WebRootPath));
5361

src/Unosquare.Labs.EmbedIO.ExtraSample/Unosquare.Labs.EmbedIO.ExtraSample.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<OutputType>Exe</OutputType>
88
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
99
<LangVersion>8.0</LangVersion>
10+
<Nullable>enable</Nullable>
1011
<RootNamespace>EmbedIO.ExtraSample</RootNamespace>
1112
</PropertyGroup>
1213

0 commit comments

Comments
 (0)