Skip to content

Commit 6e6c863

Browse files
committed
Made DataProtectionSecretEncryptor the default implementation of ISecretEncryptor.
1 parent 3f07b15 commit 6e6c863

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,11 @@ This value will be retrieved from the registered service app.
161161
This value will be retrieved from the registered service app. As the name suggests, it should be kept secret and so is probably best not added directly to `appSettings.json` and checked into source control.
162162

163163
###### UseProofKeyForCodeExchange *
164+
164165
This flag will extend the OAuth flow with an additional security layer called [PKCE - Proof Key for Code Exchange](https://auth0.com/docs/get-started/authentication-and-authorization-flow/authorization-code-flow-with-proof-key-for-code-exchange-pkce).
166+
165167
In the OAuth with PKCE flow, a random code will be generated on the client and stored under the name `code_verifier`, and then using the `SHA-256` algorithm it will be hashed under the name `code_challenge`.
168+
166169
When the authorization URL is generated, the `code_challenge` will be sent to the OAuth Server, which will store it. The next request for access token will pass the `code_verifier` as a header key, and the OAuth Server will
167170
compare it with the previously sent `code_challenge`.
168171

@@ -301,14 +304,14 @@ Responsible for encrypting and decrypting stored tokens (or other values).
301304

302305
It has three implementations:
303306

304-
- `AesSecretEncryptor` - default implementation that is using a standard `AES` cryptographic algorithm for encrypting/decrypting values based on the provided `TokenEncryptionKey`.
307+
- `DataProtectionSecretEncryptor` - default implementation that uses the `IDataProtectionProvider` interface for providing data protection services.
308+
- `AesSecretEncryptor` - additional implementation that is using a standard `AES` cryptographic algorithm for encrypting/decrypting values based on the provided `TokenEncryptionKey`.
305309
- `NoopSecretEncryptor` - provides no encryption saving the provided token as is.
306-
- `DataProtectionSecretEncryptor` - additional implementation that uses the `IDataProtectionProvider` interface for providing data protection services.
307310

308-
Switching the encryption engine to for example `DataProtectionSecretEncryptor` can be done in code, adding these two lines:
311+
Switching the encryption engine to for example `AesSecretEncryptor` can be done in code, via:
309312

310313
```
311-
builder.Services.AddUnique<ISecretEncryptor, DataProtectionSecretEncrytor>();
314+
builder.Services.AddUnique<ISecretEncryptor, AesSecretEncryptor>();
312315
```
313316

314317
#### ITokenFactory

src/Umbraco.AuthorizedServices/AuthorizedServicesComposer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static void RegisterServices(IUmbracoBuilder builder)
5353
builder.Services.AddUnique<IAuthorizationPayloadCache, AuthorizationPayloadCache>();
5454
builder.Services.AddUnique<IAuthorizationPayloadBuilder, AuthorizationPayloadBuilder>();
5555

56-
builder.Services.AddUnique<ISecretEncryptor, AesSecretEncryptor>();
56+
builder.Services.AddUnique<ISecretEncryptor, DataProtectionSecretEncryptor>();
5757

5858
builder.Services.AddUnique<ITokenFactory, TokenFactory>();
5959
builder.Services.AddUnique<ITokenStorage, DatabaseTokenStorage>();

src/Umbraco.AuthorizedServices/Services/Implement/DataProtectionSecretEncryptor.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Security.Cryptography;
12
using Microsoft.AspNetCore.DataProtection;
23

34
namespace Umbraco.AuthorizedServices.Services.Implement;
@@ -15,7 +16,15 @@ public DataProtectionSecretEncryptor(IDataProtectionProvider dataProtectionProvi
1516

1617
public bool TryDecrypt(string encryptedValue, out string decryptedValue)
1718
{
18-
decryptedValue = _protector.Unprotect(encryptedValue);
19-
return true;
19+
try
20+
{
21+
decryptedValue = _protector.Unprotect(encryptedValue);
22+
return true;
23+
}
24+
catch (CryptographicException)
25+
{
26+
decryptedValue = string.Empty;
27+
return false;
28+
}
2029
}
2130
}

0 commit comments

Comments
 (0)