-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDownstreamTokenProvider.cs
More file actions
39 lines (33 loc) · 1.14 KB
/
Copy pathDownstreamTokenProvider.cs
File metadata and controls
39 lines (33 loc) · 1.14 KB
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
38
39
using BlazorDemo.Models;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.Extensions.Configuration;
namespace BlazorDemo.Shared.Downstream;
public class DownstreamTokenProvider : IDownstreamTokenProvider
{
private readonly IAccessTokenProvider _tokenProvider;
private readonly DownstreamApiConfiguration _downstreamApiConfiguration;
public DownstreamTokenProvider(IConfiguration configuration,
IAccessTokenProvider tokenProvider)
{
_tokenProvider = tokenProvider;
_downstreamApiConfiguration = configuration.GetRequiredSection("DownstreamApi")
.Get<DownstreamApiConfiguration>()!;
}
public async Task<string?> TryGetToken()
{
var accessTokenResult = await _tokenProvider.RequestAccessToken(new AccessTokenRequestOptions
{
Scopes = _downstreamApiConfiguration.Scopes
});
if (!accessTokenResult.TryGetToken(out var token))
{
return null;
}
var accessToken = token.Value;
return accessToken;
}
}
public interface IDownstreamTokenProvider
{
Task<string?> TryGetToken();
}