{"id":3701,"date":"2021-09-21T14:08:09","date_gmt":"2021-09-21T13:08:09","guid":{"rendered":"https:\/\/www.lieben.nu\/liebensraum\/?p=3701"},"modified":"2021-09-21T14:08:09","modified_gmt":"2021-09-21T13:08:09","slug":"expanding-microsoft-first-party-application-permissions-in-azuread","status":"publish","type":"post","link":"https:\/\/lieben.nu\/liebensraum\/2021\/09\/expanding-microsoft-first-party-application-permissions-in-azuread\/","title":{"rendered":"Expanding Microsoft First Party Application Permissions in AzureAD"},"content":{"rendered":"\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth-role-added-to-microsoft-azure-powershell-client-1950a258-227b-4e31-a9cf-717495945fc2.png\"><img loading=\"lazy\" decoding=\"async\" width=\"264\" height=\"46\" src=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth-role-added-to-microsoft-azure-powershell-client-1950a258-227b-4e31-a9cf-717495945fc2.png\" alt=\"\" class=\"wp-image-3703\"\/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"> Connect-AzAccount and my own <a rel=\"noreferrer noopener\" href=\"https:\/\/gitlab.com\/Lieben\/assortedFunctions\/-\/blob\/master\/get-azResourceTokenSilentlyWithoutModuleDependencies.ps1\" target=\"_blank\">silent token function<\/a> use the Microsoft built in client ID of &#8220;1950a258-227b-4e31-a9cf-717495945fc2&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The resulting token has some openID scopes and most backend calls use RBAC, but I wanted to experiment by adding OAuth2 permissions and app roles to it so I can use the context\/cached refresh token to also call other Microsoft API&#8217;s.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I discovered that this can be done by adding the client to your AzureAD as an SPN (Enterprise Application):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$spn = New-AzureADServicePrincipal -AppId \"1950a258-227b-4e31-a9cf-717495945fc2\" -DisplayName \"Microsoft Azure PowerShell\"\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Since this is Microsoft owned app, you&#8217;ll actually see that show up in AzureAD:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/first-party-app-azure-ad.png\"><img loading=\"lazy\" decoding=\"async\" width=\"862\" height=\"186\" src=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/first-party-app-azure-ad.png\" alt=\"\" class=\"wp-image-3706\" srcset=\"https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/first-party-app-azure-ad.png 862w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/first-party-app-azure-ad-300x65.png 300w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/first-party-app-azure-ad-768x166.png 768w\" sizes=\"auto, (max-width: 862px) 100vw, 862px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we can add the  AuditLog.Read.All permission to the local instance of this app ($spn), this is a Graph permission, so we first need to get the resourceId of graph in our tenant:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$GraphServicePrincipal = Get-AzureADServicePrincipal -Filter \"appId eq '00000003-0000-0000-c000-000000000000'\"\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Then we prepare our post body for the Graph API to add the AuditLog.Read.All permission to Microsoft Azure PowerShell:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n        $patchBody = @{\n            \"clientId\"= $spn.ObjectId\n            \"consentType\"= \"AllPrincipals\"\n            \"principalId\"= $Null\n            \"resourceId\"= $GraphServicePrincipal.ObjectId\n            \"scope\"= \"AuditLog.Read.All\"\n            \"expiryTime\" = \"2022-05-05T09:00:00Z\"\n        }\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Then we&#8217;ll grab a token for the Graph API:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\n$token =  get-azResourceTokenSilentlyWithoutModuleDependencies -userUPN myupn@lieben.nu\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">And call Graph to add the permission to our local instance of Microsoft Azure PowerShell:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nInvoke-RestMethod -Method POST -body ($patchBody | convertto-json) -Uri \"https:\/\/graph.microsoft.com\/beta\/oauth2PermissionGrants\" -Headers @{\"Authorization\"=\"Bearer $token\"} -ContentType \"application\/json\"\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Any future tokens you grab for graph.microsoft.com using 1950a258-227b-4e31-a9cf-717495945fc2 as clientId will now contain the AuditLog.Read.All scope as well;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth2-consented-permissions.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1828\" height=\"498\" src=\"https:\/\/www.lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth2-consented-permissions.png\" alt=\"\" class=\"wp-image-3707\" srcset=\"https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth2-consented-permissions.png 1828w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth2-consented-permissions-300x82.png 300w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth2-consented-permissions-1024x279.png 1024w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth2-consented-permissions-768x209.png 768w, https:\/\/lieben.nu\/liebensraum\/wp-content\/uploads\/2021\/09\/oauth2-consented-permissions-1536x418.png 1536w\" sizes=\"auto, (max-width: 1828px) 100vw, 1828px\" \/><\/a><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You should also be able to <a rel=\"noreferrer noopener\" href=\"https:\/\/gitlab.com\/Lieben\/assortedFunctions\/-\/blob\/master\/add-roleToManagedIdentity.ps1\" target=\"_blank\">add approles<\/a>, but since (hopefully) only Microsoft has the client credentials, they won&#8217;t do much.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inspired by a question <a rel=\"noreferrer noopener\" href=\"https:\/\/twitter.com\/PrzemyslawKlys\" data-type=\"URL\" data-id=\"https:\/\/twitter.com\/PrzemyslawKlys\" target=\"_blank\">PrzemyslawKlys <\/a>asked me \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Connect-AzAccount and my own silent token function use the Microsoft built in client ID of &#8220;1950a258-227b-4e31-a9cf-717495945fc2&#8221;. The resulting token has some openID scopes and most backend calls use RBAC, but I wanted to experiment by adding OAuth2 permissions and app roles to it so I can use the context\/cached refresh token to also call other &hellip; <a href=\"https:\/\/lieben.nu\/liebensraum\/2021\/09\/expanding-microsoft-first-party-application-permissions-in-azuread\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Expanding Microsoft First Party Application Permissions in AzureAD<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[7,39],"tags":[],"class_list":["post-3701","post","type-post","status-publish","format-standard","hentry","category-azuread","category-powershell"],"_links":{"self":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/3701","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/comments?post=3701"}],"version-history":[{"count":0,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/posts\/3701\/revisions"}],"wp:attachment":[{"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/media?parent=3701"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/categories?post=3701"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lieben.nu\/liebensraum\/wp-json\/wp\/v2\/tags?post=3701"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}