|
| 1 | +using System; |
| 2 | +using System.IdentityModel; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Azure; |
| 6 | +using Azure.Core; |
| 7 | +using Azure.Core.Pipeline; |
| 8 | +using Azure.Identity; |
| 9 | +using Azure.Identity.Broker; |
| 10 | +using Microsoft.Identity.Client.NativeInterop; |
| 11 | + |
| 12 | +namespace Microsoft.Graph.PowerShell.Authentication.Core.Utilities |
| 13 | +{ |
| 14 | + public class PopClient |
| 15 | + { |
| 16 | + private readonly HttpPipeline _pipeline; |
| 17 | + private AuthenticationRecord _authenticationRecord; |
| 18 | + private readonly InteractiveBrowserCredential _interactiveBrowserCredential; |
| 19 | + |
| 20 | + public PopClient(TokenCredential credential, IAuthContext authContext, ClientOptions options = null) |
| 21 | + { |
| 22 | + //_interactiveBrowserCredential = (InteractiveBrowserCredential)credential; |
| 23 | + _interactiveBrowserCredential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialBrokerOptions(WindowHandleUtlities.GetConsoleOrTerminalWindow())); |
| 24 | + |
| 25 | + if (!(credential is ISupportsProofOfPossession)) |
| 26 | + { |
| 27 | + throw new ArgumentException("The provided TokenCredential does not support proof of possession.", nameof(credential)); |
| 28 | + } |
| 29 | + |
| 30 | + var pipelineOptions = new HttpPipelineOptions(options); |
| 31 | + pipelineOptions.PerRetryPolicies.Add(new InteractivePopTokenAuthenticationPolicy(_interactiveBrowserCredential, "https://graph.microsoft.com/.default", () => _authenticationRecord)); |
| 32 | + |
| 33 | + _pipeline = HttpPipelineBuilder.Build(pipelineOptions); |
| 34 | + } |
| 35 | + |
| 36 | + public async ValueTask<Response> GetAsync(Uri uri, CancellationToken cancellationToken = default) |
| 37 | + { |
| 38 | + using var request = _pipeline.CreateRequest(); |
| 39 | + request.Method = RequestMethod.Get; |
| 40 | + request.Uri.Reset(uri); |
| 41 | + return await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); |
| 42 | + } |
| 43 | + |
| 44 | + public Response Get(Uri uri, CancellationToken cancellationToken = default) |
| 45 | + { |
| 46 | + using var request = _pipeline.CreateRequest(); |
| 47 | + request.Method = RequestMethod.Get; |
| 48 | + request.Uri.Reset(uri); |
| 49 | + return _pipeline.SendRequest(request, cancellationToken); |
| 50 | + } |
| 51 | + |
| 52 | + public async ValueTask<AuthenticationRecord> GetAuthRecordAsync() |
| 53 | + { |
| 54 | + _authenticationRecord ??= await _interactiveBrowserCredential.AuthenticateAsync(); |
| 55 | + return _authenticationRecord; |
| 56 | + } |
| 57 | + |
| 58 | + public AuthenticationRecord GetAuthRecord() |
| 59 | + { |
| 60 | + _authenticationRecord ??= _interactiveBrowserCredential.Authenticate(); |
| 61 | + return _authenticationRecord; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public class InteractivePopTokenAuthenticationPolicy : PopTokenAuthenticationPolicy |
| 66 | + { |
| 67 | + private readonly InteractiveBrowserCredential _interactiveBrowserCredential; |
| 68 | + private readonly Func<AuthenticationRecord> _getAuthRecord; |
| 69 | + |
| 70 | + public InteractivePopTokenAuthenticationPolicy(InteractiveBrowserCredential credential, string scope, Func<AuthenticationRecord> getAuthRecord) |
| 71 | + : base(credential, scope) |
| 72 | + { |
| 73 | + _interactiveBrowserCredential = credential; |
| 74 | + _getAuthRecord = getAuthRecord; |
| 75 | + } |
| 76 | + |
| 77 | + protected override ValueTask AuthorizeRequestAsync(HttpMessage message) |
| 78 | + { |
| 79 | + var authRecord = _getAuthRecord(); |
| 80 | + if (authRecord != null) |
| 81 | + { |
| 82 | + _interactiveBrowserCredential.AuthenticateAsync(new TokenRequestContext(new[] { "https://graph.microsoft.com/.default" })).ConfigureAwait(false); |
| 83 | + } |
| 84 | + |
| 85 | + return base.AuthorizeRequestAsync(message); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments