Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.

Commit 5ef026b

Browse files
authored
Add client certificate login (#160)
* Add client certificate login
1 parent 92b9028 commit 5ef026b

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Added
11+
- Added certificate login (`mgc login --strategy ClientCertificate`)
1112

1213
### Changed
14+
- Implement `none` output formatter as no-op output
15+
1316

1417
## [0.1.0-preview.6] - 2022-08-18
1518

samples/1-Login.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@ mgc login --scopes User.ReadWrite Mail.ReadWrite --client-id <client id> --tenan
1818
mgc login --scopes User.ReadWrite Mail.ReadWrite --client-id <client id> --tenant-id <tenant id>
1919
```
2020

21+
### Login using interactive browser
22+
23+
```sh
24+
mgc login --strategy InteractiveBrowser
25+
```
26+
27+
### Login using client certificate
28+
29+
```sh
30+
mgc login --strategy ClientCertificate
31+
```
32+
2133
## Logout
2234

2335
Forget access tokens

src/Program.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ static async Task<int> Main(string[] args)
3636
var config = configBuilder.Build();
3737

3838
var authSettings = config.GetSection(nameof(AuthenticationOptions)).Get<AuthenticationOptions>();
39-
var authServiceFactory = new AuthenticationServiceFactory(new PathUtility());
39+
var pathUtil = new PathUtility();
40+
var authServiceFactory = new AuthenticationServiceFactory(pathUtil, authSettings);
4041
var authStrategy = AuthenticationStrategy.DeviceCode;
4142

42-
var credential = await authServiceFactory.GetTokenCredentialAsync(authStrategy, authSettings?.TenantId, authSettings?.ClientId);
43+
var credential = await authServiceFactory.GetTokenCredentialAsync(authStrategy, authSettings?.TenantId, authSettings?.ClientId, authSettings?.ClientCertificateName, authSettings?.ClientCertificateThumbPrint);
4344
var authProvider = new AzureIdentityAuthenticationProvider(credential, new string[] { "graph.microsoft.com" });
4445

4546
var assemblyVersion = Assembly.GetExecutingAssembly().GetName().Version;
4647
var options = new GraphClientOptions
4748
{
4849
GraphProductPrefix = "graph-cli",
49-
GraphServiceLibraryClientVersion = $"{assemblyVersion.Major}.{assemblyVersion.Minor}.{assemblyVersion.Build}",
50+
GraphServiceLibraryClientVersion = $"{assemblyVersion?.Major ?? 0}.{assemblyVersion?.Minor ?? 0}.{assemblyVersion?.Build ?? 0}",
5051
GraphServiceTargetVersion = "1.0"
5152
};
5253
using var httpClient = GraphCliClientFactory.GetDefaultClient(options);
@@ -57,7 +58,8 @@ static async Task<int> Main(string[] args)
5758
var loginCommand = new LoginCommand(authServiceFactory);
5859
commands.Add(loginCommand.Build());
5960

60-
var logoutCommand = new LogoutCommand(new LogoutService());
61+
var authCacheUtil = new AuthenticationCacheUtility(pathUtil);
62+
var logoutCommand = new LogoutCommand(new LogoutService(authCacheUtil));
6163
commands.Add(logoutCommand.Build());
6264

6365
var builder = BuildCommandLine(client, commands).UseDefaults().UseHost(CreateHostBuilder);
@@ -73,20 +75,23 @@ static async Task<int> Main(string[] args)
7375
});
7476
builder.UseExceptionHandler((ex, context) =>
7577
{
76-
if (ex is AuthenticationRequiredException)
77-
{
78-
Console.ResetColor();
79-
Console.ForegroundColor = ConsoleColor.Red;
80-
context.Console.Error.WriteLine("Token acquisition failed. Run mgc login command first to get an access token.");
81-
Console.ResetColor();
82-
}
83-
else
78+
switch (ex)
8479
{
85-
Console.ResetColor();
86-
Console.ForegroundColor = ConsoleColor.Red;
87-
context.Console.Error.WriteLine(ex.Message);
88-
context.Console.Error.WriteLine(ex.StackTrace);
89-
Console.ResetColor();
80+
case AuthenticationRequiredException:
81+
Console.ResetColor();
82+
Console.ForegroundColor = ConsoleColor.Red;
83+
context.Console.Error.WriteLine("Token acquisition failed. Run mgc login command first to get an access token.");
84+
Console.ResetColor();
85+
break;
86+
case TaskCanceledException:
87+
break;
88+
default:
89+
Console.ResetColor();
90+
Console.ForegroundColor = ConsoleColor.Red;
91+
context.Console.Error.WriteLine(ex.Message);
92+
context.Console.Error.WriteLine(ex.StackTrace);
93+
Console.ResetColor();
94+
break;
9095
}
9196
});
9297

src/msgraph-cli.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@
2323
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
2424
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
2525
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
26-
<PackageReference Include="Microsoft.Graph.Cli.Core" Version="0.1.3-preview.2" />
26+
<PackageReference Include="Microsoft.Graph.Cli.Core" Version="0.1.4-preview.1" />
2727
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.0.0-preview.10" />
2828
<PackageReference Include="Microsoft.Kiota.Authentication.Azure" Version="1.0.0-preview.3" />
29-
<PackageReference Include="Microsoft.Kiota.Cli.Commons" Version="0.1.9-preview.2" />
3029
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.0.0-preview.8" />
3130
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.0.0-preview.6" />
3231
<PackageReference Include="System.CommandLine.Hosting" Version="0.4.0-alpha.22272.1" />

0 commit comments

Comments
 (0)