Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets
using System.Net;
using System.Globalization;

[Cmdlet(VerbsCommunications.Connect, "Graph", DefaultParameterSetName = Constants.UserParameterSet)]
public class ConnectGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssemblyCleanup
[Cmdlet(VerbsCommunications.Connect, "MgGraph", DefaultParameterSetName = Constants.UserParameterSet)]
[Alias("Connect-Graph")]
public class ConnectMgGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssemblyCleanup
{
[Parameter(ParameterSetName = Constants.UserParameterSet,
Position = 1,
Expand All @@ -29,11 +30,13 @@ public class ConnectGraph : PSCmdlet, IModuleAssemblyInitializer, IModuleAssembl
Position = 1,
Mandatory = true,
HelpMessage = "The client id of your application.")]
[Alias("AppId")]
public string ClientId { get; set; }

[Parameter(ParameterSetName = Constants.AppParameterSet,
Position = 2,
HelpMessage = "The name of your certificate. The Certificate will be retrieved from the current user's certificate store.")]
[Alias("CertificateSubject")]
public string CertificateName { get; set; }

[Parameter(ParameterSetName = Constants.AppParameterSet,
Expand Down Expand Up @@ -85,7 +88,8 @@ protected override void ProcessRecord()
TimeSpan authTimeout = new TimeSpan(0, 0, Constants.MaxDeviceCodeTimeOut);
cancellationTokenSource = new CancellationTokenSource(authTimeout);
authContext.AuthType = AuthenticationType.Delegated;
authContext.Scopes = Scopes ?? new string[] { "User.Read" };
string[] processedScopes = ProcessScopes(Scopes);
authContext.Scopes = processedScopes.Length == 0 ? new string[] { "User.Read" } : processedScopes;
// Default to CurrentUser but allow the customer to change this via `ContextScope` param.
authContext.ContextScope = this.IsParameterBound(nameof(ContextScope)) ? ContextScope : ContextScope.CurrentUser;
}
Expand Down Expand Up @@ -190,6 +194,31 @@ protected override void StopProcessing()
base.StopProcessing();
}

/// <summary>
/// Processes user provided scopes by removing whitespace and splitting comma separated scopes.
/// </summary>
/// <param name="scopes">An array of scopes.</param>
/// <returns>A formated array of scopes.</returns>
private string[] ProcessScopes(string[] scopes)
{
if (scopes == null)
{
return new string[0];
}

List<string> formatedScopes = new List<string>();
foreach (string scope in scopes)
{
string[] cleanScopes = scope.Split(',')
.Select(s => s.Trim())
.Where(s => !string.IsNullOrWhiteSpace(s))
.ToArray();

formatedScopes.AddRange(cleanScopes);
}
return formatedScopes.ToArray();
}

private void ValidateParameters()
{
switch (ParameterSetName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets
using Microsoft.Graph.PowerShell.Authentication.Helpers;
using System;
using System.Management.Automation;
[Cmdlet(VerbsCommunications.Disconnect, "Graph")]
public class DisconnectGraph : PSCmdlet
[Cmdlet(VerbsCommunications.Disconnect, "MgGraph")]
[Alias("Disconnect-Graph")]
public class DisconnectMgGraph : PSCmdlet
{
protected override void BeginProcessing()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@

namespace Microsoft.Graph.PowerShell.Authentication.Cmdlets
{
[Cmdlet(VerbsLifecycle.Invoke, "GraphRequest", DefaultParameterSetName = Constants.UserParameterSet)]
public class InvokeGraphRequest : PSCmdlet
[Cmdlet(VerbsLifecycle.Invoke, "MgGraphRequest", DefaultParameterSetName = Constants.UserParameterSet)]
[Alias("Invoke-GraphRequest")]
public class InvokeMgGraphRequest : PSCmdlet
{
private readonly CancellationTokenSource _cancellationTokenSource;
private readonly InvokeGraphRequestUserAgent _graphRequestUserAgent;
private string _originalFilePath;

public InvokeGraphRequest()
public InvokeMgGraphRequest()
{
_cancellationTokenSource = new CancellationTokenSource();
_graphRequestUserAgent = new InvokeGraphRequestUserAgent(this);
Expand Down
2 changes: 1 addition & 1 deletion src/Authentication/Authentication/Helpers/HttpHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers
public static class HttpHelpers
{
/// The version for current assembly.
private static AssemblyName AssemblyInfo = typeof(ConnectGraph).GetTypeInfo().Assembly.GetName();
private static AssemblyName AssemblyInfo = typeof(ConnectMgGraph).GetTypeInfo().Assembly.GetName();

/// The value for the Auth module version header.
private static string AuthModuleVersionHeaderValue =
Expand Down
2 changes: 1 addition & 1 deletion src/Authentication/Authentication/Helpers/PathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Graph.PowerShell.Authentication.Helpers
/// </summary>
internal static class PathUtils
{
public static string ResolveFilePath(string filePath, InvokeGraphRequest command, bool isLiteralPath)
public static string ResolveFilePath(string filePath, InvokeMgGraphRequest command, bool isLiteralPath)
{
string path = null;
try
Expand Down
2 changes: 1 addition & 1 deletion src/Authentication/Authentication/Helpers/StreamHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal static string StreamToString(this Stream stream, Encoding encoding)
}

internal static void SaveStreamToFile(this Stream baseResponseStream, string filePath,
InvokeGraphRequest invokeGraphRequest, CancellationToken token)
InvokeMgGraphRequest invokeGraphRequest, CancellationToken token)
{
// If the web cmdlet should resume, append the file instead of overwriting.
const FileMode fileMode = FileMode.Create;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ FormatsToProcess = './Microsoft.Graph.Authentication.format.ps1xml'
FunctionsToExport = @()

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = 'Connect-Graph', 'Disconnect-Graph', 'Get-MgContext', 'Get-MgProfile',
'Select-MgProfile', 'Invoke-GraphRequest'
CmdletsToExport = 'Connect-MgGraph', 'Disconnect-MgGraph', 'Get-MgContext', 'Get-MgProfile',
'Select-MgProfile', 'Invoke-MgGraphRequest'

# Variables to export from this module
# VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = @()
AliasesToExport = @('Connect-Graph', 'Disconnect-Graph', 'Invoke-GraphRequest')

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down