{"id":4569,"date":"2020-05-11T00:59:56","date_gmt":"2020-05-11T07:59:56","guid":{"rendered":"https:\/\/officedevblogs.wpengine.com\/?p=4569"},"modified":"2020-05-11T00:59:56","modified_gmt":"2020-05-11T07:59:56","slug":"using-your-own-azure-ad-identity-with-office365-cli","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/using-your-own-azure-ad-identity-with-office365-cli\/","title":{"rendered":"Using your own Azure AD identity with Office 365 CLI"},"content":{"rendered":"<p>The Office 365 CLI provides a quick and easy way to manage your Office 365 tenant from any operating system and any shell.<\/p>\n<p>When you use the Office 365 CLI to connect to your tenant for the first time, you are presented with a <code class=\"language-plaintext highlighter-rouge\">Permissions requested<\/code> prompt from Azure, by accepting this prompt you are consenting to using the <code class=\"language-plaintext highlighter-rouge\">PnP Office 365 Management Shell<\/code> Azure AD application with your tenant as well as the permissions that it requires.<\/p>\n<p>We ask for a wide range to permissions up front, including permissions that require administrative level consent, so that it is easy for to get started with the CLI and try out the commands across many Office 365 CLI workloads in your tenant without having to handle the complexity of managing the permissions for the different commands manually in Azure.<\/p>\n<p>Whilst this is fine for working against development and test environments, using these levels of permissions against production environments is inconvenient and administrators are not comfortable with granting such permissions to a multi-tenant application within their environment.<\/p>\n<p>In this scenario, administrators will want to provide their own Azure AD app registration to use with the CLI to enable greater control over the permissions that are granted.<\/p>\n<p>This tutorial will walk you through how to create your own Azure AD application with permissions restricted to only read information about SharePoint Online Site Collections and how to use this custom application with the Office 365 CLI.<\/p>\n<h2 id=\"register-azure-ad-application-in-your-tenant\">Register Azure AD application in your tenant<\/h2>\n<p>We first need to register a new Azure AD application in your tenant, to do this we will need to navigate to the <a href=\"https:\/\/portal.azure.com\">Azure Portal<\/a>.<\/p>\n<p>Select <code class=\"language-plaintext highlighter-rouge\">Azure Active Directory<\/code> from the global menu, select <code class=\"language-plaintext highlighter-rouge\">App registrations<\/code> in the Azure Active Directory blade and then select the <code class=\"language-plaintext highlighter-rouge\">New registration<\/code> action button to open the <code class=\"language-plaintext highlighter-rouge\">Register an application<\/code> form.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_18.png\" alt=\"\" \/><\/p>\n<p>In the form, enter a name for your new application, for the purposes of this tutorial lets use <code class=\"language-plaintext highlighter-rouge\">Custom PnP Office 365 CLI<\/code>, you can always change this later if you want. Leave the <code class=\"language-plaintext highlighter-rouge\">Supported account types<\/code> and <code class=\"language-plaintext highlighter-rouge\">Redirect URI<\/code> as they are and select the <code class=\"language-plaintext highlighter-rouge\">Register<\/code> button at the foot of the form to create your custom application.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_17.png\" alt=\"\" \/><\/p>\n<p>After the application has been created, you will be presented with the blade for your application displaying an overview of some properties of the application. At this point it is a good idea to take note of two key pieces of information that we will need later.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_16.png\" alt=\"\" \/><\/p>\n<p>Take a copy of both the <code class=\"language-plaintext highlighter-rouge\">Application (client) ID<\/code> and <code class=\"language-plaintext highlighter-rouge\">Directory (tenant) ID<\/code> values and save them to a place for you to refer back to them later.<\/p>\n<h3 id=\"confgure-authentication-settings\">Configure Authentication settings<\/h3>\n<p>We next need to configure our new application so that it can be used with the Office 365 CLI, to do this we need select <code class=\"language-plaintext highlighter-rouge\">Authentication<\/code> in the <code class=\"language-plaintext highlighter-rouge\">Custom PnP Office 365 CLI<\/code> blade menu.<\/p>\n<p>This will present you with three sections, <code class=\"language-plaintext highlighter-rouge\">Platform configuration<\/code>, <code class=\"language-plaintext highlighter-rouge\">Supported account type<\/code> and <code class=\"language-plaintext highlighter-rouge\">Advanced settings<\/code>.<\/p>\n<p>Select the <code class=\"language-plaintext highlighter-rouge\">Add a platform<\/code> button to open up the <code class=\"language-plaintext highlighter-rouge\">Configure platforms<\/code> menu and select <code class=\"language-plaintext highlighter-rouge\">Mobile and desktop applications<\/code> under the <code class=\"language-plaintext highlighter-rouge\">Mobile and desktop applications<\/code> heading, this will open another menu called <code class=\"language-plaintext highlighter-rouge\">Configure Desktop + Devices<\/code> displaying a section called <code class=\"language-plaintext highlighter-rouge\">Redirect URIs<\/code> and a list of checkboxes with some pre-defined URIs.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_14.png\" alt=\"\" \/><\/p>\n<p>Select the first option in the list, <code class=\"language-plaintext highlighter-rouge\">https:\/\/login.microsoftonline.com\/common\/oauth2\/nativeclient<\/code> and select the <code class=\"language-plaintext highlighter-rouge\">Configure<\/code> button at the foot of the menu.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_13.png\" alt=\"\" \/><\/p>\n<p>This will refresh the <code class=\"language-plaintext highlighter-rouge\">Authentication<\/code> blade and will display the Redirect URI we just chose from the menu.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_11.png\" alt=\"\" \/><\/p>\n<blockquote><p>This Redirect URI is specific to the use of authentication methods that do not use a web interface for authenticating users and are therefore called <code class=\"language-plaintext highlighter-rouge\">Native Clients<\/code>, this is the category that the Office 365 falls into.<\/p><\/blockquote>\n<p>Moving on, we can skip over the <code class=\"language-plaintext highlighter-rouge\">Supported account type<\/code> section, as this is defaulted to <code class=\"language-plaintext highlighter-rouge\">Accounts in this organizational directory only (&lt;tenant&gt; only - Single tenant)<\/code> it means that only users within the current tenant directory can use this identity and onto the <code class=\"language-plaintext highlighter-rouge\">Advanced settings<\/code> section, in here we need to toggle the <code class=\"language-plaintext highlighter-rouge\">Default client type<\/code> toggle so that is enabled, as we are using the <code class=\"language-plaintext highlighter-rouge\">Device code flow<\/code> method to authenticate to our tenant using the Office 365 CLI.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_12.png\" alt=\"\" \/><\/p>\n<p>To make sure all these changes are applied, select the <code class=\"language-plaintext highlighter-rouge\">Save<\/code> button before moving on.<\/p>\n<h3 id=\"configure-api-permissions\">Configure API Permissions<\/h3>\n<p>Now that we have configured the application to work with the Office 365 CLI, we next need to grant what permissions the CLI will have against our tenant. Select the <code class=\"language-plaintext highlighter-rouge\">API permissions<\/code> in the <code class=\"language-plaintext highlighter-rouge\">Custom PnP Office 365 CLI<\/code> blade menu.<\/p>\n<p>You will see a section called <code class=\"language-plaintext highlighter-rouge\">Configured permissions<\/code> with one permission already granted, this is the default permission which allows the application to sign the user account used when authenticating to the Microsoft Graph.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_10.png\" alt=\"\" \/><\/p>\n<p>Select the <code class=\"language-plaintext highlighter-rouge\">Add a permission<\/code> button to open the <code class=\"language-plaintext highlighter-rouge\">Request API permissions<\/code> menu, as we are only interested in granting our application access to SharePoint Online for the purpose of this tutorial, select <code class=\"language-plaintext highlighter-rouge\">SharePoint<\/code> in the list of APIs that are available.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_9.png\" alt=\"\" \/><\/p>\n<p>This opens the menu with two options <code class=\"language-plaintext highlighter-rouge\">Delegated permissions<\/code> or <code class=\"language-plaintext highlighter-rouge\">Application permissions<\/code>, as we are going to be communicating with the SharePoint Online APIs as the authenticated user, select <code class=\"language-plaintext highlighter-rouge\">Delegated permissions<\/code>, this will display a list of available permissions that we can choose from.<\/p>\n<p>For the purpose of this tutorial we only want to grant read access to SharePoint Online, so expand the <code class=\"language-plaintext highlighter-rouge\">AllSites<\/code> grouping and select the <code class=\"language-plaintext highlighter-rouge\">AllSites.Read<\/code> permissions and select the <code class=\"language-plaintext highlighter-rouge\">Add permissions<\/code> button to apply these permissions.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_8.png\" alt=\"\" \/><\/p>\n<blockquote><p>Note that the <code class=\"language-plaintext highlighter-rouge\">AllSites.Read<\/code> permission does not directly grant the signed-in user access to all sites in SharePoint Online. As we authenticate as the signed-in user, that user must still have access to the SharePoint sites that we want to return information about, otherwise the SharePoint Online API calls will fail with <code class=\"language-plaintext highlighter-rouge\">401 Unauthorized<\/code><\/p><\/blockquote>\n<p>You will be presented with the <code class=\"language-plaintext highlighter-rouge\">Configured permissions<\/code> section again but this time the <code class=\"language-plaintext highlighter-rouge\">AllSites.Read<\/code> permission will be shown under the <code class=\"language-plaintext highlighter-rouge\">SharePoint<\/code> grouping.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_7.png\" alt=\"\" \/><\/p>\n<p>This completes the configuration required in the Azure portal, we can now move onto configuring the Office 365 CLI to use our custom application to login to Office 365.<\/p>\n<h2 id=\"create-office-365-cli-environment-variables\">Create Office 365 CLI environment variables<\/h2>\n<p>To configure the Office 365 CLI to use our newly created custom application, we need to tell it the Client ID of our custom application and the Tenant ID of where the custom application has been created.<\/p>\n<p>To do that, we need to create two environment variables, called <code class=\"language-plaintext highlighter-rouge\">OFFICE365CLI_AADAPPID<\/code> and <code class=\"language-plaintext highlighter-rouge\">OFFICE365CLI_TENANT<\/code>, giving them the values that you saved earlier.<\/p>\n<p>How you set the environment variables is dependant on the operating system and shell you are using.<\/p>\n<p>If you are on Windows, you can set the environment variables using the <code class=\"language-plaintext highlighter-rouge\">$env:&lt;variable-name&gt;<\/code> approach in a PowerShell session.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>$env:OFFICE365CLI_AADAPPID=\"506af689-32aa-46c8-afb5-972ebf9d218a\"\n$env:OFFICE365CLI_TENANT=\"e8954f17-a373-4b61-b54d-45c038fe3188\"\n<\/code><\/pre>\n<\/div>\n<\/div>\n<blockquote><p>Execute <code class=\"language-plaintext highlighter-rouge\">$env:OFFICE365CLI_AADAPPID<\/code> and <code class=\"language-plaintext highlighter-rouge\">$env:OFFICE365CLI_TENANT<\/code> to verify that the environment variables have been created correctly<\/p><\/blockquote>\n<p>If you using Linux or MacOS, you can set the environment variables using the <code class=\"language-plaintext highlighter-rouge\">export<\/code> command from your terminal prompt.<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>export OFFICE365CLI_AADAPPID=506af689-32aa-46c8-afb5-972ebf9d218a\nexport OFFICE365CLI_TENANT=e8954f17-a373-4b61-b54d-45c038fe3188\n<\/code><\/pre>\n<\/div>\n<\/div>\n<blockquote><p>Execute <code class=\"language-plaintext highlighter-rouge\">printenv<\/code> to verify that the environment variables have been created correctly<\/p><\/blockquote>\n<p>Now that we have set our environment variables, we are now ready to use our custom application to log in with using the Office 365 CLI.<\/p>\n<h3 id=\"login-and-consent\">Login and consent<\/h3>\n<p>For the purpose of this tutorial, we will be using the <code class=\"language-plaintext highlighter-rouge\">Device code flow<\/code> to interactively authenticate with an Office 365 CLI tenant. As this is the first time that we will have used the custom application to authenticate, we will also be required to give our consent.<\/p>\n<p>At your terminal session, execute <code class=\"language-plaintext highlighter-rouge\">o365 login<\/code> to start the authentication process, a login device code will be displayed along with a link to a web page where it needs to be entered. Navigate to <a href=\"https:\/\/microsoft.com\/devicelogin\">https:\/\/microsoft.com\/devicelogin<\/a>, enter the code into the input field and select <code class=\"language-plaintext highlighter-rouge\">Next<\/code>. You will then be presented with either a login screen or accounts that you have already logged in to Office 365 with. Login with or choose the account from the list that you want to use with Office 365 CLI.<\/p>\n<p>You will now be prompted to consent that the custom application, <code class=\"language-plaintext highlighter-rouge\">Custom PnP Office 365 CLI<\/code>, can use the two permissions that we configure earlier, <code class=\"language-plaintext highlighter-rouge\">Read items in all site collections<\/code> and <code class=\"language-plaintext highlighter-rouge\">Sign you in and read your profile<\/code> on your behalf. Select <code class=\"language-plaintext highlighter-rouge\">Accept<\/code> to consent and complete the sign-in process.<\/p>\n<p>Returning back to your command line, you can now verify that the sign in has been succesful by executing the <code class=\"language-plaintext highlighter-rouge\">o365 status<\/code> command.<\/p>\n<p>Finally, to test that we can indeed read SharePoint Online site collections, lets invoke the following command<\/p>\n<div class=\"language-plaintext highlighter-rouge\">\n<div class=\"highlight\">\n<pre class=\"highlight\"><code>o365 spo site get --url https:\/\/trinder365dev.sharepoint.com -o json --pretty\n<\/code><\/pre>\n<\/div>\n<\/div>\n<p>The JSON representation of the SharePoint Online site will be returned to the console.<\/p>\n<p>Congratulations! You have just configured the Office 365 CLI to use your own custom application with custom permissions from your own Azure Active Directory.<\/p>\n<p><img decoding=\"async\" class=\"center-image\" src=\"https:\/\/garrytrinder.github.io\/public\/img\/o365cli\/o365cli-custom-app-registration_1.png\" alt=\"\" \/><\/p>\n<h3 id=\"find-out-more\">Find out more<\/h3>\n<p>If you would like to know more about Office 365 CLI, visit the web site at <a href=\"aka.ms\/o365cli\">https:\/\/aka.ms\/o365cli<\/a>.<\/p>\n<p>If you see any room for improvement or suggestions, please don\u2019t hesitate to reach out to us either on <a href=\"https:\/\/github.com\/pnp\/office365-cli\/discussions\">GitHub<\/a> or <a href=\"https:\/\/twitter.com\/office365cli\">@office365cli<\/a> on Twitter.<\/p>\n<h3 id=\"appendix-persisting-environment-variables\">Appendix: Persisting environment variables<\/h3>\n<p>As mentioned earlier, the way in which we set the environment variables meant that they are only set for the lifetime of the session, if the terminal session is closed, you will need to repeat those steps, which maybe undesirable.<\/p>\n<p>How you permanently set the environment variable is dependant on the operating system and shell you are using.<\/p>\n<p>If you are on Windows, you can set the environment variables using the <code class=\"language-plaintext highlighter-rouge\">Edit the system environment variables<\/code> approach in the Windows UI.<\/p>\n<p>Search for <code class=\"language-plaintext highlighter-rouge\">Edit the system environment variables<\/code> in Start Menu and launch it. Select <code class=\"language-plaintext highlighter-rouge\">Environment Variables<\/code>, under the <code class=\"language-plaintext highlighter-rouge\">User variables for &lt;user-name&gt;<\/code> section, select <code class=\"language-plaintext highlighter-rouge\">New...<\/code> to open a dialog. In the dialog, enter <code class=\"language-plaintext highlighter-rouge\">OFFICE365CLI_AADAPPID<\/code> in the variable name field and set the value using the Client ID (quotes should be ommitted), select <code class=\"language-plaintext highlighter-rouge\">OK<\/code> to save the value and repeat the process for the <code class=\"language-plaintext highlighter-rouge\">OFFICE365CLI_TENANT<\/code> variable. Select <code class=\"language-plaintext highlighter-rouge\">OK<\/code> until all windows are closed to persist the changes.<\/p>\n<p>Open a new PowerShell session and execute <code class=\"language-plaintext highlighter-rouge\">$env:OFFICE365CLI_AADAPPID<\/code> and <code class=\"language-plaintext highlighter-rouge\">$env:OFFICE365CLI_TENANT<\/code> to verify that the environment variables have been created correctly.<\/p>\n<p>If you are on Linux or MacOS, depending on your terminal, add the <code class=\"language-plaintext highlighter-rouge\">export<\/code> lines to <code class=\"language-plaintext highlighter-rouge\">.bashrc<\/code> or <code class=\"language-plaintext highlighter-rouge\">.zshrc<\/code> file in your home directory.<\/p>\n<p>If you are using PowerShell Core, it is worth noting that environment variables set in <code class=\"language-plaintext highlighter-rouge\">bash<\/code> or <code class=\"language-plaintext highlighter-rouge\">zsh<\/code> will persist to the <code class=\"language-plaintext highlighter-rouge\">pwsh<\/code> session and the same applies to Windows.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create your own Azure AD application with permissions restricted to only read information about SharePoint Online Site Collections and use this custom application with the Office 365 CLI.<\/p>\n","protected":false},"author":69079,"featured_media":4317,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[5,128,11],"tags":[61,22,53,140],"class_list":["post-4569","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-identity-platform","category-microsoft-teams","category-office-add-ins","tag-azure","tag-azure-ad","tag-office-365","tag-office-365-cli"],"acf":[],"blog_post_summary":"<p>Create your own Azure AD application with permissions restricted to only read information about SharePoint Online Site Collections and use this custom application with the Office 365 CLI.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/4569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/users\/69079"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/comments?post=4569"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/posts\/4569\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media\/4317"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/media?parent=4569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/categories?post=4569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/microsoft365dev\/wp-json\/wp\/v2\/tags?post=4569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}