-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidateAndStripSignatureTokenExchangeHandlerPreProcessor.cs
38 lines (32 loc) · 1.45 KB
/
ValidateAndStripSignatureTokenExchangeHandlerPreProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Threading.Tasks;
namespace TokenExchange.Contracts.Services
{
public class ValidateAndStripSignatureTokenExchangeHandlerPreProcessor : ITokenExchangeHandlerPreProcessor
{
private ITokenValidator _tokenValidator;
public ValidateAndStripSignatureTokenExchangeHandlerPreProcessor(ITokenValidator tokenValidator)
{
_tokenValidator = tokenValidator;
}
public string Name => "validate-strip-signature";
public Task<List<KeyValuePair<string, string>>> ProcessAsync(ref TokenExchangeRequest tokenExchangeRequest)
{
var header = new JwtHeader();
var handler = new JwtSecurityTokenHandler();
foreach (var tokenWithScheme in tokenExchangeRequest.Tokens)
{
var principal = _tokenValidator.ValidateTokenAsync(new TokenDescriptor
{
TokenScheme = tokenWithScheme.TokenScheme,
Token = tokenWithScheme.Token
}).GetAwaiter().GetResult();
var token = handler.ReadJwtToken(tokenWithScheme.Token);
var unsignedSecurityToken = new JwtSecurityToken(header, token.Payload);
tokenWithScheme.Token = handler.WriteToken(unsignedSecurityToken);
}
return Task.FromResult(new List<KeyValuePair<string, string>>());
}
}
}