azureazure-active-directoryazure-cli2azure-ad-powershell-v2

How to create scope using Azure CLI (az ad app)


Using the Azure CLI 2.x, I cannot find a way to "add a Scope" under the expose an API section in Azure AD Portal.

enter image description here

What I do see is if I pass the --identifier-uris when the app is created, the APP ID URI and a Scope get automatically set:

    `az ad app create --display-name "$appName" --identifier-uris "https://$tenantDomain/$appName" --reply-urls "$replyUrl" --oauth2-allow-implicit-flow true`

enter image description here

Not what I expected nor what I want

So, I removed --identifier-urls from the create command and added the scope I wanted in manually. then I see via manifest what I'm looking for under OAuth2Permissions as shown below. Can I put this in manifest.json with a new guid and insert it somehow?

enter image description here

What CLI command supports the explicit support to define a Scope? Then Adding a Client application I would need to select the defined Scope, how is this referenced?

Documentation is very sparse, IMO. This reference is very helpful but nothing in here talks about adding scopes and clients. https://learn.microsoft.com/en-us/cli/azure/ad?view=azure-cli-latest. Any help towards samples or documentation much appreciated.


Solution

  • With help from the thread above, and a ton of trial-n-error plus a pretty useful link, I was able to work out the CLI script to add scope using a windows environment. PowerShell is not happy with 'jq' on windows and use of the back-tick had to be removed to get things working. Now I need to solve adding preAuthorizedApplication with the CLI.

    $userAccessScopeApi = '{
        "lang": null,
        "origin": "Application",        
        "adminConsentDescription": "Access CP Debug desc",
        "adminConsentDisplayName": "Access CP Debug",
        "id": "--- replaced in scripts ---",
        "isEnabled": true,
        "type": "Admin",
        "userConsentDescription": null,
        "userConsentDisplayName": null,
        "value": "Access"
    }' | ConvertTo-Json | ConvertFrom-Json
    `
    
    Write-Host " -  1 read oauth2permissions"
    #(az ad app show  --id $appid)
    $appjson = (az ad app list --display-name $appName)         
    $app = $appjson | ConvertFrom-Json
    $oauth2Permissions = $app.oauth2Permissions
    $oauth2Permissions[0].isEnabled = 'false'
    
    $oauth2Permissionsjson = ConvertTo-Json -InputObject @($oauth2Permissions) 
    
    Write-Host " -  2 disable oauth2Permission in Azure App Registration"
    $oauth2Permissionsjson | Out-File -FilePath .\oauth2Permissionsold.json
    az ad app update --id $appId --set oauth2Permissions=@oauth2Permissionsold.json
    
    Write-Host " -  3 delete the default oauth2Permission"
    az ad app update --id $appId --set oauth2Permissions='[]'
    
    Write-Host " -  4 add the new scope required add the new oauth2Permissions values"
    $oauth2PermissionsApiNew = $userAccessScopeApi | ConvertFrom-Json
    $oauth2PermissionsApiNew[0].id = New-Guid
    $oauth2PermissionsApiNew = ConvertTo-Json -InputObject @($oauth2PermissionsApiNew) 
    
    # Write-Host "new oauth2permissions : " + $oauth2PermissionsApiNew" 
    $oauth2PermissionsApiNew | Out-File -FilePath .\oauth2Permissionsnew.json
    az ad app update --id $appId --set oauth2Permissions=@oauth2Permissionsnew.json
    
    Write-Host " - Updated scopes (oauth2Permissions) for App Registration: $appId"`