-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adding "-AT PoP" option to "Set-MgGraphOptions" * Adding AT PoP skeleton --------- Co-authored-by: Tim <[email protected]> Co-authored-by: Peter Ombwa <[email protected]> Co-authored-by: Peter Ombwa <[email protected]> Co-authored-by: Mustafa Zengin <[email protected]> Co-authored-by: Clément Notin <[email protected]> Co-authored-by: Microsoft Graph DevX Tooling <[email protected]> Co-authored-by: Vincent Biret <[email protected]> Co-authored-by: Vincent Biret <[email protected]> Co-authored-by: Subhajit Ray (from Dev Box) <[email protected]>
- Loading branch information
1 parent
007369d
commit d228848
Showing
5 changed files
with
129 additions
and
12 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
16 changes: 8 additions & 8 deletions
16
src/Authentication/Authentication.Core/Microsoft.Graph.Authentication.Core.csproj
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
88 changes: 88 additions & 0 deletions
88
src/Authentication/Authentication.Core/Utilities/PopClient.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,88 @@ | ||
using System; | ||
using System.IdentityModel; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure; | ||
using Azure.Core; | ||
using Azure.Core.Pipeline; | ||
using Azure.Identity; | ||
using Azure.Identity.Broker; | ||
using Microsoft.Identity.Client.NativeInterop; | ||
|
||
namespace Microsoft.Graph.PowerShell.Authentication.Core.Utilities | ||
{ | ||
public class PopClient | ||
{ | ||
private readonly HttpPipeline _pipeline; | ||
private AuthenticationRecord _authenticationRecord; | ||
private readonly InteractiveBrowserCredential _interactiveBrowserCredential; | ||
|
||
public PopClient(TokenCredential credential, IAuthContext authContext, ClientOptions options = null) | ||
{ | ||
//_interactiveBrowserCredential = (InteractiveBrowserCredential)credential; | ||
_interactiveBrowserCredential = new InteractiveBrowserCredential(new InteractiveBrowserCredentialBrokerOptions(WindowHandleUtlities.GetConsoleOrTerminalWindow())); | ||
|
||
if (!(credential is ISupportsProofOfPossession)) | ||
{ | ||
throw new ArgumentException("The provided TokenCredential does not support proof of possession.", nameof(credential)); | ||
} | ||
|
||
var pipelineOptions = new HttpPipelineOptions(options); | ||
pipelineOptions.PerRetryPolicies.Add(new InteractivePopTokenAuthenticationPolicy(_interactiveBrowserCredential, "https://graph.microsoft.com/.default", () => _authenticationRecord)); | ||
|
||
_pipeline = HttpPipelineBuilder.Build(pipelineOptions); | ||
} | ||
|
||
public async ValueTask<Response> GetAsync(Uri uri, CancellationToken cancellationToken = default) | ||
{ | ||
using var request = _pipeline.CreateRequest(); | ||
request.Method = RequestMethod.Get; | ||
request.Uri.Reset(uri); | ||
return await _pipeline.SendRequestAsync(request, cancellationToken).ConfigureAwait(false); | ||
} | ||
|
||
public Response Get(Uri uri, CancellationToken cancellationToken = default) | ||
{ | ||
using var request = _pipeline.CreateRequest(); | ||
request.Method = RequestMethod.Get; | ||
request.Uri.Reset(uri); | ||
return _pipeline.SendRequest(request, cancellationToken); | ||
} | ||
|
||
public async ValueTask<AuthenticationRecord> GetAuthRecordAsync() | ||
{ | ||
_authenticationRecord ??= await _interactiveBrowserCredential.AuthenticateAsync(); | ||
return _authenticationRecord; | ||
} | ||
|
||
public AuthenticationRecord GetAuthRecord() | ||
{ | ||
_authenticationRecord ??= _interactiveBrowserCredential.Authenticate(); | ||
return _authenticationRecord; | ||
} | ||
} | ||
|
||
public class InteractivePopTokenAuthenticationPolicy : PopTokenAuthenticationPolicy | ||
{ | ||
private readonly InteractiveBrowserCredential _interactiveBrowserCredential; | ||
private readonly Func<AuthenticationRecord> _getAuthRecord; | ||
|
||
public InteractivePopTokenAuthenticationPolicy(InteractiveBrowserCredential credential, string scope, Func<AuthenticationRecord> getAuthRecord) | ||
: base(credential, scope) | ||
{ | ||
_interactiveBrowserCredential = credential; | ||
_getAuthRecord = getAuthRecord; | ||
} | ||
|
||
protected override ValueTask AuthorizeRequestAsync(HttpMessage message) | ||
{ | ||
var authRecord = _getAuthRecord(); | ||
if (authRecord != null) | ||
{ | ||
_interactiveBrowserCredential.AuthenticateAsync(new TokenRequestContext(new[] { "https://graph.microsoft.com/.default" })).ConfigureAwait(false); | ||
} | ||
|
||
return base.AuthorizeRequestAsync(message); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/Authentication/Authentication.Core/Utilities/PopClientOptions.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,2 @@ | ||
using Azure.Core; | ||
public class PopClientOptions : ClientOptions { } |