Skip to content

Commit 9a13536

Browse files
authored
Merge pull request #374 from microsoftgraph/po/BYOT
Add -AccessToken parameter to Connect-Graph
2 parents ebbe1b5 + 7fb8c48 commit 9a13536

File tree

11 files changed

+372
-87
lines changed

11 files changed

+372
-87
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
namespace Microsoft.Graph.Authentication.Test.Helpers
2+
{
3+
using Microsoft.Graph.Auth;
4+
using Microsoft.Graph.PowerShell.Authentication;
5+
using Microsoft.Graph.PowerShell.Authentication.Helpers;
6+
using System;
7+
using System.Linq;
8+
using System.Net;
9+
using System.Net.Http;
10+
using System.Security.Cryptography;
11+
using System.Security.Cryptography.X509Certificates;
12+
using System.Threading.Tasks;
13+
using Xunit;
14+
public class AuthenticationHelpersTests
15+
{
16+
public AuthenticationHelpersTests()
17+
{
18+
GraphSession.Initialize(() => new GraphSession());
19+
}
20+
21+
[Fact]
22+
public async Task ShouldUseDelegateAuthProviderWhenUserAccessTokenIsProvidedAsync()
23+
{
24+
// Arrange
25+
string accessToken = "ACCESS_TOKEN_VIA_DELEGATE_PROVIDER";
26+
GraphSession.Instance.UserProvidedToken = new NetworkCredential(string.Empty, accessToken).SecurePassword;
27+
AuthContext userProvidedAuthContext = new AuthContext
28+
{
29+
AuthType = AuthenticationType.UserProvidedAccessToken,
30+
ContextScope = ContextScope.Process
31+
};
32+
33+
IAuthenticationProvider authProvider = AuthenticationHelpers.GetAuthProvider(userProvidedAuthContext);
34+
HttpRequestMessage requestMessage = new HttpRequestMessage();
35+
36+
// Act
37+
await authProvider.AuthenticateRequestAsync(requestMessage);
38+
39+
// Assert
40+
Assert.IsType<DelegateAuthenticationProvider>(authProvider);
41+
Assert.Equal("Bearer", requestMessage.Headers.Authorization.Scheme);
42+
Assert.Equal(accessToken, requestMessage.Headers.Authorization.Parameter);
43+
44+
// reset static instance.
45+
GraphSession.Reset();
46+
}
47+
48+
[Fact]
49+
public void ShouldUseDeviceCodeProviderWhenDelegatedContextIsProvided()
50+
{
51+
// Arrange
52+
AuthContext delegatedAuthContext = new AuthContext
53+
{
54+
AuthType = AuthenticationType.Delegated,
55+
Scopes = new string[] { "User.Read" },
56+
ContextScope = ContextScope.Process
57+
};
58+
59+
// Act
60+
IAuthenticationProvider authProvider = AuthenticationHelpers.GetAuthProvider(delegatedAuthContext);
61+
62+
// Assert
63+
Assert.IsType<DeviceCodeProvider>(authProvider);
64+
65+
// reset static instance.
66+
GraphSession.Reset();
67+
}
68+
69+
[Fact]
70+
public void ShouldUseClientCredentialProviderWhenAppOnlyContextIsProvided()
71+
{
72+
// Arrange
73+
AuthContext appOnlyAuthContext = new AuthContext
74+
{
75+
AuthType = AuthenticationType.AppOnly,
76+
ClientId = Guid.NewGuid().ToString(),
77+
CertificateName = "cn=dummyCert",
78+
ContextScope = ContextScope.Process
79+
};
80+
CreateSelfSignedCert(appOnlyAuthContext.CertificateName);
81+
82+
// Act
83+
IAuthenticationProvider authProvider = AuthenticationHelpers.GetAuthProvider(appOnlyAuthContext);
84+
85+
// Assert
86+
Assert.IsType<ClientCredentialProvider>(authProvider);
87+
88+
// reset
89+
DeleteSelfSignedCert(appOnlyAuthContext.CertificateName);
90+
GraphSession.Reset();
91+
92+
}
93+
94+
private void CreateSelfSignedCert(string certName)
95+
{
96+
ECDsa ecdsaKey = ECDsa.Create();
97+
CertificateRequest certificateRequest = new CertificateRequest(certName, ecdsaKey, HashAlgorithmName.SHA256);
98+
// We have to export cert to dummy cert since `CreateSelfSigned` creates a cert without a private key.
99+
X509Certificate2 cert = certificateRequest.CreateSelfSigned(DateTimeOffset.Now, DateTimeOffset.Now.AddYears(5));
100+
101+
X509Certificate2 dummyCert = null;
102+
if (PowerShell.Authentication.Helpers.OperatingSystem.IsMacOS())
103+
{
104+
dummyCert = new X509Certificate2(cert.Export(X509ContentType.Pfx, "P@55w0rd"), "P@55w0rd", X509KeyStorageFlags.Exportable);
105+
}
106+
else
107+
{
108+
dummyCert = new X509Certificate2(cert.Export(X509ContentType.Pfx, "P@55w0rd"), "P@55w0rd", X509KeyStorageFlags.PersistKeySet);
109+
}
110+
using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
111+
{
112+
store.Open(OpenFlags.ReadWrite);
113+
store.Add(dummyCert);
114+
}
115+
}
116+
117+
private void DeleteSelfSignedCert(string certificateName)
118+
{
119+
using (X509Store xStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
120+
{
121+
xStore.Open(OpenFlags.ReadWrite);
122+
123+
X509Certificate2Collection unexpiredCerts = xStore.Certificates
124+
.Find(X509FindType.FindByTimeValid, DateTime.Now, false)
125+
.Find(X509FindType.FindBySubjectDistinguishedName, certificateName, false);
126+
127+
// Only return current cert.
128+
var xCertificate = unexpiredCerts
129+
.OfType<X509Certificate2>()
130+
.OrderByDescending(c => c.NotBefore)
131+
.FirstOrDefault();
132+
133+
xStore.Remove(xCertificate);
134+
}
135+
}
136+
137+
}
138+
}

0 commit comments

Comments
 (0)