| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | using Microsoft.AspNetCore.DataProtection; |
| | | 4 | | |
| | | 5 | | namespace Elsa.SasTokens.Contracts; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A service that can create and decrypt SAS (Shared Access Signature) tokens using the <see cref="Microsoft.AspNetCore |
| | | 9 | | /// </summary> |
| | | 10 | | public class DataProtectorTokenService : ITokenService |
| | | 11 | | { |
| | | 12 | | private const string TimeLimitedTokenPrefix = "v1.tl."; |
| | | 13 | | private const string NonExpiringTokenPrefix = "v1.ne."; |
| | | 14 | | private readonly IDataProtector _dataProtector; |
| | | 15 | | private readonly ITimeLimitedDataProtector _timeLimitedDataProtector; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="DataProtectorTokenService"/> class. |
| | | 19 | | /// </summary> |
| | 9 | 20 | | public DataProtectorTokenService(IDataProtectionProvider dataProtector) |
| | | 21 | | { |
| | 9 | 22 | | _dataProtector = dataProtector.CreateProtector("Elsa Tokens"); |
| | 9 | 23 | | _timeLimitedDataProtector = _dataProtector.ToTimeLimitedDataProtector(); |
| | 9 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public string CreateToken<T>(T payload, TimeSpan lifetime) |
| | | 28 | | { |
| | 1 | 29 | | var json = JsonSerializer.Serialize(payload); |
| | 1 | 30 | | return TimeLimitedTokenPrefix + _timeLimitedDataProtector.Protect(json, lifetime); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <inheritdoc /> |
| | | 34 | | public string CreateToken<T>(T payload, DateTimeOffset expiresAt) |
| | | 35 | | { |
| | 2 | 36 | | var json = JsonSerializer.Serialize(payload); |
| | 2 | 37 | | return TimeLimitedTokenPrefix + _timeLimitedDataProtector.Protect(json, expiresAt); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public string CreateToken<T>(T payload) |
| | | 42 | | { |
| | 1 | 43 | | var json = JsonSerializer.Serialize(payload); |
| | 1 | 44 | | return NonExpiringTokenPrefix + _dataProtector.Protect(json); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public bool TryDecryptToken<T>(string token, out T payload) |
| | | 49 | | { |
| | 5 | 50 | | payload = default!; |
| | | 51 | | |
| | | 52 | | try |
| | | 53 | | { |
| | 5 | 54 | | payload = DecryptToken<T>(token); |
| | 3 | 55 | | return true; |
| | | 56 | | } |
| | 2 | 57 | | catch |
| | | 58 | | { |
| | | 59 | | // ignored. |
| | 2 | 60 | | } |
| | | 61 | | |
| | 2 | 62 | | return false; |
| | 3 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <inheritdoc /> |
| | | 66 | | public T DecryptToken<T>(string token) |
| | | 67 | | { |
| | 6 | 68 | | var json = Unprotect(token); |
| | 4 | 69 | | return JsonSerializer.Deserialize<T>(json)!; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | private string Unprotect(string token) |
| | | 73 | | { |
| | 6 | 74 | | if (token.StartsWith(TimeLimitedTokenPrefix, StringComparison.Ordinal)) |
| | 3 | 75 | | return _timeLimitedDataProtector.Unprotect(token[TimeLimitedTokenPrefix.Length..]); |
| | | 76 | | |
| | 3 | 77 | | if (token.StartsWith(NonExpiringTokenPrefix, StringComparison.Ordinal)) |
| | 1 | 78 | | return _dataProtector.Unprotect(token[NonExpiringTokenPrefix.Length..]); |
| | | 79 | | |
| | | 80 | | try |
| | | 81 | | { |
| | 2 | 82 | | return _timeLimitedDataProtector.Unprotect(token); |
| | | 83 | | } |
| | 1 | 84 | | catch (CryptographicException) |
| | | 85 | | { |
| | 1 | 86 | | var json = _dataProtector.Unprotect(token); |
| | | 87 | | |
| | | 88 | | // Expired time-limited tokens can be decrypted by the base protector, |
| | | 89 | | // but the result includes the expiration header. |
| | | 90 | | // Only accept fallback payloads that are standalone JSON produced by CreateToken(payload). |
| | | 91 | | try |
| | | 92 | | { |
| | 1 | 93 | | using var _ = JsonDocument.Parse(json); |
| | 1 | 94 | | return json; |
| | | 95 | | } |
| | 0 | 96 | | catch (JsonException e) |
| | | 97 | | { |
| | 0 | 98 | | throw new CryptographicException("Token payload is not a valid non-expiring token.", e); |
| | | 99 | | } |
| | | 100 | | } |
| | 2 | 101 | | } |
| | | 102 | | } |