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

Commit d7c8452

Browse files
committed
Allow setting auth strategy in login command
1 parent fd561d8 commit d7c8452

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/Commands/Authentication/LoginCommand.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
using Microsoft.Graph.Cli.Authentication;
2+
using Microsoft.Graph.Cli.Utils;
23
using System.CommandLine;
34
using System.CommandLine.Invocation;
45

56
namespace Microsoft.Graph.Cli.Commands.Authentication;
67

78
class LoginCommand
89
{
9-
private IAuthenticationService authenticationService;
10+
private AuthenticationServiceFactory authenticationServiceFactory;
1011

11-
public LoginCommand(IAuthenticationService authenticationService) {
12-
this.authenticationService = authenticationService;
12+
public LoginCommand(AuthenticationServiceFactory authenticationServiceFactory) {
13+
this.authenticationServiceFactory = authenticationServiceFactory;
1314
}
1415

15-
public Command Build() {
16+
public Command Build(bool persistToken = false) {
1617
var loginCommand = new Command("login", "Login and store the session for use in subsequent commands");
1718
var scopes = new Option<string>("--scopes", "The login scopes e.g. User.Read");
1819
scopes.IsRequired = true;
1920
scopes.Arity = ArgumentArity.OneOrMore;
2021
loginCommand.AddOption(scopes);
21-
loginCommand.Handler = CommandHandler.Create<string[]>(async (scopes) =>
22+
23+
var strategy = new Option<AuthenticationStrategy>("--strategy", () => Constants.defaultAuthStrategy, "The authentication strategy to use.");
24+
loginCommand.AddOption(strategy);
25+
26+
loginCommand.Handler = CommandHandler.Create<string[], AuthenticationStrategy>(async (scopes, strategy) =>
2227
{
23-
await this.authenticationService.LoginAsync(scopes);
28+
var authService = await this.authenticationServiceFactory.GetAuthenticationServiceAsync(strategy, persistToken);
29+
await authService.LoginAsync(scopes);
2430
});
2531

2632
return loginCommand;

src/Program.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ static async Task<int> Main(string[] args) {
2424
var rootCommand = client.BuildCommand();
2525
rootCommand.Description = "Microsoft Graph CLI";
2626

27-
var authenticationService = await authServiceFactory.GetAuthenticationServiceAsync(authStrategy, persistToken);
28-
var loginCommand = new LoginCommand(authenticationService);
29-
rootCommand.AddCommand(loginCommand.Build());
27+
var loginCommand = new LoginCommand(authServiceFactory);
28+
rootCommand.AddCommand(loginCommand.Build(persistToken));
3029
return await rootCommand.InvokeAsync(args);
3130
}
3231
}

src/utils/Constants.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using Microsoft.Graph.Cli.Authentication;
2+
13
namespace Microsoft.Graph.Cli.Utils
24
{
35
class Constants
@@ -6,6 +8,8 @@ class Constants
68

79
public const string TokenCacheName = "MicrosoftGraph";
810

11+
public const AuthenticationStrategy defaultAuthStrategy = AuthenticationStrategy.DeviceCode;
12+
913
public const string ClientId = "f645f5f8-2332-496d-9d85-e714b1192f0c";
1014

1115
public const string TenantId = "39aafea4-2975-4790-9c07-e616c1c35d99";

0 commit comments

Comments
 (0)