Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CertificateThumbprint authentication #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Common/Utils/AuthUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace Microsoft.Intune.PowerShellGraphSDK
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography.X509Certificates;

internal static partial class AuthUtils
{
Expand Down Expand Up @@ -77,6 +78,35 @@ internal static SdkAuthResult AuthWithClientCredentials(string clientSecret)
return authResult;
}

internal static SdkAuthResult AuthWithCertificateThumbprint(string certificateThumbprint)
{
// Create auth context that we will use to connect to the AAD endpoint
AuthenticationContext authContext = new AuthenticationContext(CurrentEnvironmentParameters.AuthUrl);

//Find certificate
X509Certificate2 certificate = null;
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
{
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection source = store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, false).Find(X509FindType.FindByThumbprint, certificateThumbprint, false);
if (source == null)
{
throw new Exception(certificateThumbprint + " certificate was not found or has expired.");
}
certificate = source.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault<X509Certificate2>();
}
ClientAssertionCertificate clientCertificate = new ClientAssertionCertificate(CurrentEnvironmentParameters.AppId, certificate);

// Get the AuthenticationResult from AAD
SdkAuthResult authResult = authContext.AcquireTokenAsync(CurrentEnvironmentParameters.GraphBaseAddress, clientCertificate).GetAwaiter().GetResult().ToSdkAuthResult();

// Save the auth result
AuthUtils.LatestAdalAuthResult = authResult;

return authResult;
}


/// <summary>
/// Refreshes the access token using ADAL if required, otherwise returns the most recent still-valid refresh token.
/// </summary>
Expand Down
20 changes: 19 additions & 1 deletion PowerShellCmdlets/Utils/UtilCmdlets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public class Connect : PSCmdlet
/// </summary>
private const string ParameterSetAppOnly = "AppOnly";

/// <summary>
/// Parameter set for triggering app-only authentication.
/// </summary>
private const string ParameterSetCertificateThumbprint = "CertificateThumbprint";

#if NETFRAMEWORK

private const string ParameterSetForceInteractive = "ForceInteractive";
Expand Down Expand Up @@ -87,6 +92,14 @@ public class Connect : PSCmdlet
[Parameter(ParameterSetName = ParameterSetAppOnly)]
public string ClientSecret { get; set; }

/// <summary>
/// <para type="description">
/// If the certificate thumbprint is set, app-only authentication will be performed using the client ID specified by the AppId environment parameter.
/// </para>
/// </summary>
[Parameter(ParameterSetName = ParameterSetCertificateThumbprint)]
public string CertificateThumbprint { get; set; }

/// <summary>
/// <para type="description">
/// If the AdminConsent flag is set, admin consent can be granted for the currently selected AppId
Expand Down Expand Up @@ -122,9 +135,14 @@ protected override void ProcessRecord()
SdkAuthResult authResult;
if (this.ParameterSetName == ParameterSetAppOnly)
{
// App-only auth
// App-only auth - ClientSecret
authResult = AuthUtils.AuthWithClientCredentials(this.ClientSecret);
}
else if(this.ParameterSetName == ParameterSetCertificateThumbprint)
{
// App-only auth - CertificateThumbprint
authResult = AuthUtils.AuthWithCertificateThumbprint(this.CertificateThumbprint);
}
else
{
// User auth
Expand Down
2 changes: 1 addition & 1 deletion PowerShellGraphSDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Library</OutputType>
<Version>6.1907.1.0</Version>
<Version>6.1907.1.1</Version>
<Authors>Rajesh Soy, Rohit Ramu</Authors>
<Company>Microsoft Corporation</Company>
<Product>Microsoft Intune Graph PowerShell Client SDK</Product>
Expand Down