-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce token caching when using AAD auth to connect to SQL
- Loading branch information
Showing
6 changed files
with
82 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/AzureIdentityLivestream.Web/Services/Sql/AzureIdentityAzureSqlTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using Azure.Identity; | ||
|
||
namespace AzureIdentityLivestream.Web.Services.Sql | ||
{ | ||
public class AzureIdentityAzureSqlTokenProvider : IAzureSqlTokenProvider | ||
{ | ||
private static readonly TokenCredential _credential = new ChainedTokenCredential( | ||
new ManagedIdentityCredential(), | ||
new VisualStudioCodeCredential()); | ||
|
||
private static readonly string[] _azureSqlScopes = new string[] { "https://database.windows.net//.default" }; | ||
|
||
public async Task<(string Token, DateTimeOffset ExpiresOn)> GetAccessTokenAsync(CancellationToken cancellationToken = default) | ||
{ | ||
var tokenRequest = new TokenRequestContext(_azureSqlScopes); | ||
var tokenResult = await _credential.GetTokenAsync(tokenRequest, cancellationToken); | ||
|
||
return (tokenResult.Token, tokenResult.ExpiresOn); | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/AzureIdentityLivestream.Web/Services/Sql/CacheAzureSqlTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Caching.Memory; | ||
|
||
namespace AzureIdentityLivestream.Web.Services.Sql | ||
{ | ||
public class CacheAzureSqlTokenProvider : IAzureSqlTokenProvider | ||
{ | ||
private static readonly string _cacheKey = $"{nameof(CacheAzureSqlTokenProvider)}.{nameof(GetAccessTokenAsync)}"; | ||
|
||
private readonly IAzureSqlTokenProvider _inner; | ||
private readonly IMemoryCache _cache; | ||
|
||
public CacheAzureSqlTokenProvider(IAzureSqlTokenProvider inner, IMemoryCache cache) | ||
{ | ||
_inner = inner; | ||
_cache = cache; | ||
} | ||
|
||
public async Task<(string Token, DateTimeOffset ExpiresOn)> GetAccessTokenAsync(CancellationToken cancellationToken) | ||
{ | ||
return await _cache.GetOrCreateAsync(_cacheKey, async entry => | ||
{ | ||
var (token, expiresOn) = await _inner.GetAccessTokenAsync(cancellationToken); | ||
|
||
entry.SetAbsoluteExpiration(expiresOn); | ||
|
||
return (token, expiresOn); | ||
}); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/AzureIdentityLivestream.Web/Services/Sql/IAzureSqlTokenProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace AzureIdentityLivestream.Web.Services.Sql | ||
{ | ||
public interface IAzureSqlTokenProvider | ||
{ | ||
Task<(string Token, DateTimeOffset ExpiresOn)> GetAccessTokenAsync(CancellationToken cancellationToken = default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters