[Placeholder]: Implement interactive user credential login in Az
Description
Work with cli team to design interactive experience for user login with device auth with fallback to current device auth browser experience
- [ ] Write up / prototype design of new functionality
- [ ] Fil;e issues for implementation
Cost: 8
It would also be great if the cli and powershell could share the login context so I only need to log one in and I could use both.
@chrisrpatterson Sounds great, but I would be worried about security implications. It would need to be done very carefully.
General Steps
- Create a new AD application with reply URI pointing at localhost
- Scan for open port (between 8400 and 9000)
- Start a web server that listens for http://localhost:{{PORT}}
- Launch browser that points at the authentication URI
- If we are unable to launch browser (text browser, no browser, etc.), default back to device code authentication
- Once the user is authenticated, get the authorization code
- Use the authorization code to get the token
- Send the user text in the browser to notify them about authentication result
- Shutdown the web server
Azure CLI Steps
- Register the
logincommand that is mapped tologinfunction - Register the
logincommand parameters loginfunction implementation- Getting subscriptions in the
loginfunction by calling thefind_subscription_on_loginfunction find_subscription_on_loginfunction implementation- Getting subscriptions when device code login is not presented by calling the
find_through_authorization_code_flowfunction find_through_authorization_code_flowfunction implementation- Launching the browser and getting the code by calling the
_get_authorization_codefunction _get_authorization_codefunction implementation- Call to the
_get_authorization_code_workerfunction _get_authorization_code_workerfunction implementation- Find an open port for the
reply_urlbetween 8400 and 9000 - Create a web server at http://localhost:{{PORT}}
- Construct the browser url
- Open the browser by calling the
open_page_in_browserfunction open_page_in_browserfunction implementation- Once the code has been received, get the token for authentication by calling the
acquire_token_with_authorization_codefunction
So, if i understand this correctly, if i create an AAD App/Service Principal, I should be able to use this command?
$cred = Get-credentials
Connect-AzAccount - Credential $cred -TenantID
Where do I put the ServicePrincipal ID? How will the cmdlet know which servicePrincipal it needs to use? I get an error when I use the above two commands saying my user id (that i typed in the first line) does not have a serviceprincipal associated to it which is correct since i created the serviceprincipal for an app NOT my user id? How can i make this work?
@afaruqi for service principal authentication, it can be called one of two ways:
(1) If the service principal was authenticated with a secret:
$ServicePrincipal = Get-AzADServicePrincipal -DisplayName {{sp_display_name}}
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ( $ServicePrincipal.ApplicationId, {{sp_secret}} )
Connect-AzAccount -TenantId {{tenant_id}} -Credential $Credential -ServicePrincipal
(2) If the service principal was authenticated with a certificate:
$ServicePrincipal = Get-AzADServicePrincipal -DisplayName {{sp_display_name}}
$Cert = Get-ChildItem -Path {{certificate_path}} | Where-Object { $_.Subject -eq {{certificate_subject}} }
Connect-AzAccount -TenantId {{tenant_id}} -CertificateThumbprint $Cert.Thumbprint -ApplicationId $ServicePrincipal.ApplicationId -ServicePrincipal
Please let me know if you need more information around these scenarios, or if they don't work for you.
Thank you for the response! I've "Imported" the Az module, however, when I go to run the Get-AzADServicePrincipal cmdlet, I get an error saying it's not a recognized name of a cmdlet. any thoughts?
@afaruqi would you mind running Get-Module to see if the Az.Resources module is loaded in your current session? If not, would you mind running Get-Module -ListAvailable -Name Az* to see if you have the Az.Resources module installed?
Yes i do. So here's my situation. I'm working on two different environments as I don't want the conversion to effect our current scripts using the AzureRm modules. I created the app/serviceprincipal on the computer that has the AzureRm module installed. I am in a catch-22 situation because when i go to the second computer to retrieve the serviceprincipal, it doesn't return anything because I haven't logged in on that computer yet. The reason I haven't logged in is because I'm trying to use Connect-AzAccount but I can't use U/P combo with that anymore. The lack of the backward compatibility with the Connect-AzAccount is really making us go around in circles.
@afaruqi correct, U/P is not currently supported in the Az module (more information), but is there a reason you aren't able to use the (default) device code flow authentication that Connect-AzAccount provides? This will tell you to visit a site (more than likely https://microsoft.com/devicelogin, based on your environment), enter the provided code, and then you can sign in to your account from there. Once authenticated, you should be able to perform the above operations to create a service principal in the tenant that your default context is in (this can be found by running (Get-AzContext).Tenant.Id).
Using the device code is NOT feasible for us. We have an automated script that runs to create resources in azure and upload our data to storage, Having to visit the website and type in the code everytime we (at least 5 different people on 5 different machines) run our scripts is very cumbersome and inefficient. We also do not want to use Certificate authentication as it would require us to install the private key of the cert on each individual's machine who is running these scripts. And we do not want to get into the practice of using a secret to authenticate as it is not a very safe approach either.
I did, however, try your approach of doing Connect-AzAccount and then trying to get the ServicePrincipal. It works up to the point where I use the PSCredential cmdlet. When i run
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ( $ServicePrincipal.ApplicationId, {{sp_secret}} )
I get the error "New-Object: Cannot find an overload for "PSCredential" and the argument count:2"
Are there any talks of incorporating U/P back into this process?
@afaruqi I believe the issue is that the secret you're passing into the PSCredential is a string and not a SecureString. You can convert this to a SecureString by running ConvertTo-SecureString -String {{sp_secret}} -AsPlainText -Force.
As for U/P support, we are talking internally in our team, as well as with the authentication team (who owns the underlying library where U/P support is found) about ways to expose this scenario in the Az module. I believe we should have a timeline available for the public when we GA the Az module on December 18th.
any update on this issue? Last we talked you were going to fix the Connect-AzAccount cmdlet so it takes in the user credentials. Has that been done?
@afaruqi the -Credentials parameter is now supported in the Az module for desktop only (more information here)
We have an initial prototype with this interactive login functionality working on PowerShell Core, so this will be released when the Az module fully migrates to the Microsoft.Identity.Client authentication library (MSAL), which we expect to happen in within the next few months. Any additional updates will be posted in this issue.
A preview for this functionality can now be found in 2.0.0-preview of Az.Accounts. Please see the following GitHub release for more information on this preview version of the module.
reopening to track GA release
Does this solve the issue of the login window with lost focus?
I just came across this issue after searching for Connect-Azaccount VSCode having run in to a "hung" session when I executed connect-azaccount. Although I arrived here via two other issues:
- https://github.com/Azure/azure-powershell/issues/8476
- https://github.com/Azure/azure-powershell/issues/8649
Both of which describe the focus lost login window. I'm using powershell 5.1.18632 and vscode 1.45.1
Is there a fix for the lost focus login window?